1//===-------- llvm/GlobalAlias.h - GlobalAlias class ------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declaration of the GlobalAlias class, which
11// represents a single function or variable alias in the IR.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_GLOBALALIAS_H
16#define LLVM_IR_GLOBALALIAS_H
17
18#include "llvm/ADT/Twine.h"
19#include "llvm/ADT/ilist_node.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/IR/OperandTraits.h"
22
23namespace llvm {
24
25class Module;
26template<typename ValueSubClass, typename ItemParentClass>
27  class SymbolTableListTraits;
28
29class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
30  friend class SymbolTableListTraits<GlobalAlias, Module>;
31  void operator=(const GlobalAlias &) LLVM_DELETED_FUNCTION;
32  GlobalAlias(const GlobalAlias &) LLVM_DELETED_FUNCTION;
33
34  void setParent(Module *parent);
35
36  GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage,
37              const Twine &Name, Constant *Aliasee, Module *Parent);
38
39public:
40  // allocate space for exactly one operand
41  void *operator new(size_t s) {
42    return User::operator new(s, 1);
43  }
44
45  /// If a parent module is specified, the alias is automatically inserted into
46  /// the end of the specified module's alias list.
47  static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
48                             LinkageTypes Linkage, const Twine &Name,
49                             Constant *Aliasee, Module *Parent);
50
51  // Without the Aliasee.
52  static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
53                             LinkageTypes Linkage, const Twine &Name,
54                             Module *Parent);
55
56  // The module is taken from the Aliasee.
57  static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
58                             LinkageTypes Linkage, const Twine &Name,
59                             GlobalValue *Aliasee);
60
61  // Type, Parent and AddressSpace taken from the Aliasee.
62  static GlobalAlias *create(LinkageTypes Linkage, const Twine &Name,
63                             GlobalValue *Aliasee);
64
65  // Linkage, Type, Parent and AddressSpace taken from the Aliasee.
66  static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
67
68  /// Provide fast operand accessors
69  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
70
71  /// removeFromParent - This method unlinks 'this' from the containing module,
72  /// but does not delete it.
73  ///
74  void removeFromParent() override;
75
76  /// eraseFromParent - This method unlinks 'this' from the containing module
77  /// and deletes it.
78  ///
79  void eraseFromParent() override;
80
81  /// These methods retrive and set alias target.
82  void setAliasee(Constant *Aliasee);
83  const Constant *getAliasee() const {
84    return const_cast<GlobalAlias *>(this)->getAliasee();
85  }
86  Constant *getAliasee() {
87    return getOperand(0);
88  }
89
90  const GlobalObject *getBaseObject() const {
91    return const_cast<GlobalAlias *>(this)->getBaseObject();
92  }
93  GlobalObject *getBaseObject() {
94    return dyn_cast<GlobalObject>(getAliasee()->stripInBoundsOffsets());
95  }
96
97  const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const {
98    return const_cast<GlobalAlias *>(this)->getBaseObject(DL, Offset);
99  }
100  GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) {
101    return dyn_cast<GlobalObject>(
102        getAliasee()->stripAndAccumulateInBoundsConstantOffsets(DL, Offset));
103  }
104
105  static bool isValidLinkage(LinkageTypes L) {
106    return isExternalLinkage(L) || isLocalLinkage(L) ||
107      isWeakLinkage(L) || isLinkOnceLinkage(L);
108  }
109
110  // Methods for support type inquiry through isa, cast, and dyn_cast:
111  static inline bool classof(const Value *V) {
112    return V->getValueID() == Value::GlobalAliasVal;
113  }
114};
115
116template <>
117struct OperandTraits<GlobalAlias> :
118  public FixedNumOperandTraits<GlobalAlias, 1> {
119};
120
121DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Constant)
122
123} // End llvm namespace
124
125#endif
126