1 //===--- Attr.h - Classes for representing attributes ----------*- 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 defines the Attr interface and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_ATTR_H
15 #define LLVM_CLANG_AST_ATTR_H
16 
17 #include "clang/AST/AttrIterator.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/AttrKinds.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Basic/SourceLocation.h"
24 #include "clang/Basic/VersionTuple.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cassert>
32 
33 namespace clang {
34   class ASTContext;
35   class IdentifierInfo;
36   class ObjCInterfaceDecl;
37   class Expr;
38   class QualType;
39   class FunctionDecl;
40   class TypeSourceInfo;
41 
42 /// Attr - This represents one attribute.
43 class Attr {
44 private:
45   SourceRange Range;
46   unsigned AttrKind : 16;
47 
48 protected:
49   /// An index into the spelling list of an
50   /// attribute defined in Attr.td file.
51   unsigned SpellingListIndex : 4;
52   bool Inherited : 1;
53   bool IsPackExpansion : 1;
54   bool Implicit : 1;
55 
56   virtual ~Attr();
57 
new(size_t bytes)58   void* operator new(size_t bytes) throw() {
59     llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
60   }
delete(void * data)61   void operator delete(void* data) throw() {
62     llvm_unreachable("Attrs cannot be released with regular 'delete'.");
63   }
64 
65 public:
66   // Forward so that the regular new and delete do not hide global ones.
67   void* operator new(size_t Bytes, ASTContext &C,
throw()68                      size_t Alignment = 16) throw() {
69     return ::operator new(Bytes, C, Alignment);
70   }
delete(void * Ptr,ASTContext & C,size_t Alignment)71   void operator delete(void *Ptr, ASTContext &C,
72                        size_t Alignment) throw() {
73     return ::operator delete(Ptr, C, Alignment);
74   }
75 
76 protected:
77   Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex = 0)
Range(R)78     : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
79       Inherited(false), IsPackExpansion(false), Implicit(false) {}
80 
81 public:
82 
getKind()83   attr::Kind getKind() const {
84     return static_cast<attr::Kind>(AttrKind);
85   }
86 
getSpellingListIndex()87   unsigned getSpellingListIndex() const { return SpellingListIndex; }
88   virtual const char *getSpelling() const = 0;
89 
getLocation()90   SourceLocation getLocation() const { return Range.getBegin(); }
getRange()91   SourceRange getRange() const { return Range; }
setRange(SourceRange R)92   void setRange(SourceRange R) { Range = R; }
93 
isInherited()94   bool isInherited() const { return Inherited; }
95 
96   /// \brief Returns true if the attribute has been implicitly created instead
97   /// of explicitly written by the user.
isImplicit()98   bool isImplicit() const { return Implicit; }
setImplicit(bool I)99   void setImplicit(bool I) { Implicit = I; }
100 
setPackExpansion(bool PE)101   void setPackExpansion(bool PE) { IsPackExpansion = PE; }
isPackExpansion()102   bool isPackExpansion() const { return IsPackExpansion; }
103 
104   // Clone this attribute.
105   virtual Attr *clone(ASTContext &C) const = 0;
106 
isLateParsed()107   virtual bool isLateParsed() const { return false; }
108 
109   // Pretty print this attribute.
110   virtual void printPretty(raw_ostream &OS,
111                            const PrintingPolicy &Policy) const = 0;
112 
113   /// \brief By default, attributes cannot be duplicated when being merged;
114   /// however, an attribute can override this. Returns true if the attribute
115   /// can be duplicated when merging.
duplicatesAllowed()116   virtual bool duplicatesAllowed() const { return false; }
117 };
118 
119 class InheritableAttr : public Attr {
120   virtual void anchor();
121 protected:
122   InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex = 0)
Attr(AK,R,SpellingListIndex)123     : Attr(AK, R, SpellingListIndex) {}
124 
125 public:
setInherited(bool I)126   void setInherited(bool I) { Inherited = I; }
127 
128   // Implement isa/cast/dyncast/etc.
classof(const Attr * A)129   static bool classof(const Attr *A) {
130     return A->getKind() <= attr::LAST_INHERITABLE;
131   }
132 };
133 
134 class InheritableParamAttr : public InheritableAttr {
135   void anchor() override;
136 protected:
137   InheritableParamAttr(attr::Kind AK, SourceRange R,
138                        unsigned SpellingListIndex = 0)
InheritableAttr(AK,R,SpellingListIndex)139     : InheritableAttr(AK, R, SpellingListIndex) {}
140 
141 public:
142   // Implement isa/cast/dyncast/etc.
classof(const Attr * A)143   static bool classof(const Attr *A) {
144     // Relies on relative order of enum emission with respect to MS inheritance
145     // attrs.
146     return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
147   }
148 };
149 
150 #include "clang/AST/Attrs.inc"
151 
152 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
153                                            const Attr *At) {
154   DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
155                   DiagnosticsEngine::ak_attr);
156   return DB;
157 }
158 
159 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
160                                            const Attr *At) {
161   PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
162                   DiagnosticsEngine::ak_attr);
163   return PD;
164 }
165 }  // end namespace clang
166 
167 #endif
168