clang  9.0.0svn
GlobalDecl.h
Go to the documentation of this file.
1 //===- GlobalDecl.h - Global declaration holder -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A GlobalDecl can hold either a regular variable/function or a C++ ctor/dtor
10 // together with its type.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_GLOBALDECL_H
15 #define LLVM_CLANG_AST_GLOBALDECL_H
16 
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/Basic/ABI.h"
21 #include "clang/Basic/LLVM.h"
22 #include "llvm/ADT/DenseMapInfo.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/type_traits.h"
26 #include <cassert>
27 
28 namespace clang {
29 
30 /// GlobalDecl - represents a global declaration. This can either be a
31 /// CXXConstructorDecl and the constructor type (Base, Complete).
32 /// a CXXDestructorDecl and the destructor type (Base, Complete) or
33 /// a VarDecl, a FunctionDecl or a BlockDecl.
34 class GlobalDecl {
35  llvm::PointerIntPair<const Decl *, 2> Value;
36  unsigned MultiVersionIndex = 0;
37 
38  void Init(const Decl *D) {
39  assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
40  assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
41 
42  Value.setPointer(D);
43  }
44 
45 public:
46  GlobalDecl() = default;
47  GlobalDecl(const VarDecl *D) { Init(D);}
48  GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
49  : MultiVersionIndex(MVIndex) {
50  Init(D);
51  }
52  GlobalDecl(const BlockDecl *D) { Init(D); }
53  GlobalDecl(const CapturedDecl *D) { Init(D); }
54  GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
55  GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
56  GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) : Value(D, Type) {}
57  GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
58 
60  GlobalDecl CanonGD;
61  CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
62  CanonGD.Value.setInt(Value.getInt());
63  CanonGD.MultiVersionIndex = MultiVersionIndex;
64 
65  return CanonGD;
66  }
67 
68  const Decl *getDecl() const { return Value.getPointer(); }
69 
71  assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
72  return static_cast<CXXCtorType>(Value.getInt());
73  }
74 
76  assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
77  return static_cast<CXXDtorType>(Value.getInt());
78  }
79 
80  unsigned getMultiVersionIndex() const {
81  assert(isa<FunctionDecl>(getDecl()) &&
82  !isa<CXXConstructorDecl>(getDecl()) &&
83  !isa<CXXDestructorDecl>(getDecl()) &&
84  "Decl is not a plain FunctionDecl!");
85  return MultiVersionIndex;
86  }
87 
88  friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
89  return LHS.Value == RHS.Value &&
90  LHS.MultiVersionIndex == RHS.MultiVersionIndex;
91  }
92 
93  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
94 
95  static GlobalDecl getFromOpaquePtr(void *P) {
96  GlobalDecl GD;
97  GD.Value.setFromOpaqueValue(P);
98  return GD;
99  }
100 
102  GlobalDecl Result(*this);
103  Result.Value.setPointer(D);
104  return Result;
105  }
106 
108  assert(isa<FunctionDecl>(getDecl()) &&
109  !isa<CXXConstructorDecl>(getDecl()) &&
110  !isa<CXXDestructorDecl>(getDecl()) &&
111  "Decl is not a plain FunctionDecl!");
112  GlobalDecl Result(*this);
113  Result.MultiVersionIndex = Index;
114  return Result;
115  }
116 };
117 
118 } // namespace clang
119 
120 namespace llvm {
121 
122  template<> struct DenseMapInfo<clang::GlobalDecl> {
123  static inline clang::GlobalDecl getEmptyKey() {
124  return clang::GlobalDecl();
125  }
126 
128  return clang::GlobalDecl::
129  getFromOpaquePtr(reinterpret_cast<void*>(-1));
130  }
131 
132  static unsigned getHashValue(clang::GlobalDecl GD) {
134  }
135 
136  static bool isEqual(clang::GlobalDecl LHS,
137  clang::GlobalDecl RHS) {
138  return LHS == RHS;
139  }
140  };
141 
142 } // namespace llvm
143 
144 #endif // LLVM_CLANG_AST_GLOBALDECL_H
Represents a function declaration or definition.
Definition: Decl.h:1737
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:101
GlobalDecl(const VarDecl *D)
Definition: GlobalDecl.h:47
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition: GlobalDecl.h:107
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:75
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
static clang::GlobalDecl getEmptyKey()
Definition: GlobalDecl.h:123
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
StringRef P
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:59
The base class of the type hierarchy.
Definition: Type.h:1409
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2491
static GlobalDecl getFromOpaquePtr(void *P)
Definition: GlobalDecl.h:95
Represents a variable declaration or definition.
Definition: Decl.h:812
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
GlobalDecl()=default
static bool isEqual(clang::GlobalDecl LHS, clang::GlobalDecl RHS)
Definition: GlobalDecl.h:136
void * getAsOpaquePtr() const
Definition: GlobalDecl.h:93
static unsigned getHashValue(clang::GlobalDecl GD)
Definition: GlobalDecl.h:132
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type)
Definition: GlobalDecl.h:57
GlobalDecl(const CapturedDecl *D)
Definition: GlobalDecl.h:53
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type)
Definition: GlobalDecl.h:56
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:70
Enums/classes describing ABI related information about constructors, destructors and thunks...
GlobalDecl(const OMPDeclareReductionDecl *D)
Definition: GlobalDecl.h:55
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:4043
GlobalDecl(const FunctionDecl *D, unsigned MVIndex=0)
Definition: GlobalDecl.h:48
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3857
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2713
The result type of a method or function.
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:34
static clang::GlobalDecl getTombstoneKey()
Definition: GlobalDecl.h:127
This represents &#39;#pragma omp declare reduction ...&#39; directive.
Definition: DeclOpenMP.h:102
const Decl * getDecl() const
Definition: GlobalDecl.h:68
GlobalDecl(const ObjCMethodDecl *D)
Definition: GlobalDecl.h:54
This file defines OpenMP nodes for declarative directives.
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
Dataflow Directional Tag Classes.
unsigned getMultiVersionIndex() const
Definition: GlobalDecl.h:80
GlobalDecl(const BlockDecl *D)
Definition: GlobalDecl.h:52
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS)
Definition: GlobalDecl.h:88