llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
extern.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <string>
8 #include <llvm/ADT/ilist_node.h>
9 
10 #include "core/global.h"
11 #include "core/symbol_table.h"
12 
13 class Prog;
14 
15 
16 
20 class Extern final : public llvm::ilist_node_with_parent<Extern, Prog>, public Global {
21 public:
23  static constexpr Global::Kind kGlobalKind = Global::Kind::EXTERN;
24 
25 public:
29  Extern(
30  const std::string_view name,
31  Visibility visibility = Visibility::GLOBAL_DEFAULT
32  );
33 
37  Extern(
38  const std::string_view name,
39  const std::string_view section,
40  Visibility visibility = Visibility::GLOBAL_DEFAULT
41  );
42 
46  ~Extern() override;
47 
49  Prog *getParent() { return parent_; }
50 
52  void removeFromParent() override;
54  void eraseFromParent() override;
55 
57  std::optional<llvm::Align> GetAlignment() const override
58  {
59  return std::nullopt;
60  }
61 
63  void SetValue(Ref<Value> g);
65  Ref<Value> GetValue() { return Get<0>(); }
67  ConstRef<Value> GetValue() const { return Get<0>(); }
68 
70  bool HasValue() const { return GetValue(); }
71 
73  Prog *getProg() override { return parent_; }
74 
76  void SetSection(const std::string_view section) { section_ = section; }
78  std::optional<const std::string_view> GetSection() const { return section_; }
79 
81  void dump(llvm::raw_ostream &os = llvm::errs()) const override;
82 
83 private:
84  friend struct SymbolTableListTraits<Extern>;
86  void setParent(Prog *parent) { parent_ = parent; }
87 
88 private:
90  Prog *parent_;
92  std::optional<std::string> section_;
93 };
Extern::GetValue
Ref< Value > GetValue()
Returns the alias, if it exists.
Definition: extern.h:65
ConstRef
Definition: ref.h:83
Extern::removeFromParent
void removeFromParent() override
Removes an extern from the parent.
Definition: extern.cpp:39
Global::Kind
Kind
Enumeration of global kinds.
Definition: global.h:30
SymbolTableListTraits
Definition: symbol_table.h:33
Extern::eraseFromParent
void eraseFromParent() override
Erases the extern from the parent, deleting it.
Definition: extern.cpp:45
Extern::GetAlignment
std::optional< llvm::Align > GetAlignment() const override
Externs have no known alignment.
Definition: extern.h:57
Extern::kGlobalKind
static constexpr Global::Kind kGlobalKind
Kind of the global.
Definition: extern.h:23
Extern::getParent
Prog * getParent()
Returns the parent node.
Definition: extern.h:49
Extern::~Extern
~Extern() override
Definition: extern.cpp:34
Extern::GetValue
ConstRef< Value > GetValue() const
Returns the alias, if it exists.
Definition: extern.h:67
Prog
Definition: prog.h:33
Extern::SetValue
void SetValue(Ref< Value > g)
Maps the extern to an alias.
Definition: extern.cpp:51
Ref< Value >
Extern::Extern
Extern(const std::string_view name, Visibility visibility=Visibility::GLOBAL_DEFAULT)
Definition: extern.cpp:14
Extern
Definition: extern.h:20
Extern::getProg
Prog * getProg() override
Returns the program to which the extern belongs.
Definition: extern.h:73
Extern::GetSection
std::optional< const std::string_view > GetSection() const
Returns the section.
Definition: extern.h:78
Global
Definition: global.h:23
Extern::HasValue
bool HasValue() const
Checks if the extern is a weak alias to another symbol.
Definition: extern.h:70
Extern::dump
void dump(llvm::raw_ostream &os=llvm::errs()) const override
Dumps the representation of the function.
Definition: extern.cpp:57
Extern::SetSection
void SetSection(const std::string_view section)
Sets the section of the extern.
Definition: extern.h:76