]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/Attr.h
Merge clang trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / Attr.h
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/ASTContextAllocate.h"  // For Attrs.inc
18 #include "clang/AST/AttrIterator.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/AttrKinds.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/Basic/OpenMPKinds.h"
25 #include "clang/Basic/Sanitizers.h"
26 #include "clang/Basic/SourceLocation.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/VersionTuple.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <cassert>
33
34 namespace clang {
35   class ASTContext;
36   class IdentifierInfo;
37   class ObjCInterfaceDecl;
38   class Expr;
39   class QualType;
40   class FunctionDecl;
41   class TypeSourceInfo;
42
43 /// Attr - This represents one attribute.
44 class Attr {
45 private:
46   SourceRange Range;
47   unsigned AttrKind : 16;
48
49 protected:
50   /// An index into the spelling list of an
51   /// attribute defined in Attr.td file.
52   unsigned SpellingListIndex : 4;
53   unsigned Inherited : 1;
54   unsigned IsPackExpansion : 1;
55   unsigned Implicit : 1;
56   // FIXME: These are properties of the attribute kind, not state for this
57   // instance of the attribute.
58   unsigned IsLateParsed : 1;
59   unsigned InheritEvenIfAlreadyPresent : 1;
60
61   void *operator new(size_t bytes) noexcept {
62     llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
63   }
64   void operator delete(void *data) noexcept {
65     llvm_unreachable("Attrs cannot be released with regular 'delete'.");
66   }
67
68 public:
69   // Forward so that the regular new and delete do not hide global ones.
70   void *operator new(size_t Bytes, ASTContext &C,
71                      size_t Alignment = 8) noexcept {
72     return ::operator new(Bytes, C, Alignment);
73   }
74   void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
75     return ::operator delete(Ptr, C, Alignment);
76   }
77
78 protected:
79   Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
80        bool IsLateParsed)
81     : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
82       Inherited(false), IsPackExpansion(false), Implicit(false),
83       IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
84
85 public:
86
87   attr::Kind getKind() const {
88     return static_cast<attr::Kind>(AttrKind);
89   }
90
91   unsigned getSpellingListIndex() const { return SpellingListIndex; }
92   const char *getSpelling() const;
93
94   SourceLocation getLocation() const { return Range.getBegin(); }
95   SourceRange getRange() const { return Range; }
96   void setRange(SourceRange R) { Range = R; }
97
98   bool isInherited() const { return Inherited; }
99
100   /// Returns true if the attribute has been implicitly created instead
101   /// of explicitly written by the user.
102   bool isImplicit() const { return Implicit; }
103   void setImplicit(bool I) { Implicit = I; }
104
105   void setPackExpansion(bool PE) { IsPackExpansion = PE; }
106   bool isPackExpansion() const { return IsPackExpansion; }
107
108   // Clone this attribute.
109   Attr *clone(ASTContext &C) const;
110
111   bool isLateParsed() const { return IsLateParsed; }
112
113   // Pretty print this attribute.
114   void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
115 };
116
117 class TypeAttr : public Attr {
118 protected:
119   TypeAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
120            bool IsLateParsed)
121       : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
122
123 public:
124   static bool classof(const Attr *A) {
125     return A->getKind() >= attr::FirstTypeAttr &&
126            A->getKind() <= attr::LastTypeAttr;
127   }
128 };
129
130 class StmtAttr : public Attr {
131 protected:
132   StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
133                   bool IsLateParsed)
134       : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
135
136 public:
137   static bool classof(const Attr *A) {
138     return A->getKind() >= attr::FirstStmtAttr &&
139            A->getKind() <= attr::LastStmtAttr;
140   }
141 };
142
143 class InheritableAttr : public Attr {
144 protected:
145   InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
146                   bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
147       : Attr(AK, R, SpellingListIndex, IsLateParsed) {
148     this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
149   }
150
151 public:
152   void setInherited(bool I) { Inherited = I; }
153
154   /// Should this attribute be inherited from a prior declaration even if it's
155   /// explicitly provided in the current declaration?
156   bool shouldInheritEvenIfAlreadyPresent() const {
157     return InheritEvenIfAlreadyPresent;
158   }
159
160   // Implement isa/cast/dyncast/etc.
161   static bool classof(const Attr *A) {
162     return A->getKind() >= attr::FirstInheritableAttr &&
163            A->getKind() <= attr::LastInheritableAttr;
164   }
165 };
166
167 class InheritableParamAttr : public InheritableAttr {
168 protected:
169   InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
170                        bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
171       : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
172                         InheritEvenIfAlreadyPresent) {}
173
174 public:
175   // Implement isa/cast/dyncast/etc.
176   static bool classof(const Attr *A) {
177     return A->getKind() >= attr::FirstInheritableParamAttr &&
178            A->getKind() <= attr::LastInheritableParamAttr;
179   }
180 };
181
182 /// A parameter attribute which changes the argument-passing ABI rule
183 /// for the parameter.
184 class ParameterABIAttr : public InheritableParamAttr {
185 protected:
186   ParameterABIAttr(attr::Kind AK, SourceRange R,
187                    unsigned SpellingListIndex, bool IsLateParsed,
188                    bool InheritEvenIfAlreadyPresent)
189     : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
190                            InheritEvenIfAlreadyPresent) {}
191
192 public:
193   ParameterABI getABI() const {
194     switch (getKind()) {
195     case attr::SwiftContext:
196       return ParameterABI::SwiftContext;
197     case attr::SwiftErrorResult:
198       return ParameterABI::SwiftErrorResult;
199     case attr::SwiftIndirectResult:
200       return ParameterABI::SwiftIndirectResult;
201     default:
202       llvm_unreachable("bad parameter ABI attribute kind");
203     }
204   }
205
206   static bool classof(const Attr *A) {
207     return A->getKind() >= attr::FirstParameterABIAttr &&
208            A->getKind() <= attr::LastParameterABIAttr;
209    }
210 };
211
212 /// A single parameter index whose accessors require each use to make explicit
213 /// the parameter index encoding needed.
214 class ParamIdx {
215   // Idx is exposed only via accessors that specify specific encodings.
216   unsigned Idx : 30;
217   unsigned HasThis : 1;
218   unsigned IsValid : 1;
219
220   void assertComparable(const ParamIdx &I) const {
221     assert(isValid() && I.isValid() &&
222            "ParamIdx must be valid to be compared");
223     // It's possible to compare indices from separate functions, but so far
224     // it's not proven useful.  Moreover, it might be confusing because a
225     // comparison on the results of getASTIndex might be inconsistent with a
226     // comparison on the ParamIdx objects themselves.
227     assert(HasThis == I.HasThis &&
228            "ParamIdx must be for the same function to be compared");
229   }
230
231 public:
232   /// Construct an invalid parameter index (\c isValid returns false and
233   /// accessors fail an assert).
234   ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
235
236   /// \param Idx is the parameter index as it is normally specified in
237   /// attributes in the source: one-origin including any C++ implicit this
238   /// parameter.
239   ///
240   /// \param D is the declaration containing the parameters.  It is used to
241   /// determine if there is a C++ implicit this parameter.
242   ParamIdx(unsigned Idx, const Decl *D)
243       : Idx(Idx), HasThis(false), IsValid(true) {
244     assert(Idx >= 1 && "Idx must be one-origin");
245     if (const auto *FD = dyn_cast<FunctionDecl>(D))
246       HasThis = FD->isCXXInstanceMember();
247   }
248
249   /// A type into which \c ParamIdx can be serialized.
250   ///
251   /// A static assertion that it's of the correct size follows the \c ParamIdx
252   /// class definition.
253   typedef uint32_t SerialType;
254
255   /// Produce a representation that can later be passed to \c deserialize to
256   /// construct an equivalent \c ParamIdx.
257   SerialType serialize() const {
258     return *reinterpret_cast<const SerialType *>(this);
259   }
260
261   /// Construct from a result from \c serialize.
262   static ParamIdx deserialize(SerialType S) {
263     ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
264     assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
265     return P;
266   }
267
268   /// Is this parameter index valid?
269   bool isValid() const { return IsValid; }
270
271   /// Get the parameter index as it would normally be encoded for attributes at
272   /// the source level of representation: one-origin including any C++ implicit
273   /// this parameter.
274   ///
275   /// This encoding thus makes sense for diagnostics, pretty printing, and
276   /// constructing new attributes from a source-like specification.
277   unsigned getSourceIndex() const {
278     assert(isValid() && "ParamIdx must be valid");
279     return Idx;
280   }
281
282   /// Get the parameter index as it would normally be encoded at the AST level
283   /// of representation: zero-origin not including any C++ implicit this
284   /// parameter.
285   ///
286   /// This is the encoding primarily used in Sema.  However, in diagnostics,
287   /// Sema uses \c getSourceIndex instead.
288   unsigned getASTIndex() const {
289     assert(isValid() && "ParamIdx must be valid");
290     assert(Idx >= 1 + HasThis &&
291            "stored index must be base-1 and not specify C++ implicit this");
292     return Idx - 1 - HasThis;
293   }
294
295   /// Get the parameter index as it would normally be encoded at the LLVM level
296   /// of representation: zero-origin including any C++ implicit this parameter.
297   ///
298   /// This is the encoding primarily used in CodeGen.
299   unsigned getLLVMIndex() const {
300     assert(isValid() && "ParamIdx must be valid");
301     assert(Idx >= 1 && "stored index must be base-1");
302     return Idx - 1;
303   }
304
305   bool operator==(const ParamIdx &I) const {
306     assertComparable(I);
307     return Idx == I.Idx;
308   }
309   bool operator!=(const ParamIdx &I) const {
310     assertComparable(I);
311     return Idx != I.Idx;
312   }
313   bool operator<(const ParamIdx &I) const {
314     assertComparable(I);
315     return Idx < I.Idx;
316   }
317   bool operator>(const ParamIdx &I) const {
318     assertComparable(I);
319     return Idx > I.Idx;
320   }
321   bool operator<=(const ParamIdx &I) const {
322     assertComparable(I);
323     return Idx <= I.Idx;
324   }
325   bool operator>=(const ParamIdx &I) const {
326     assertComparable(I);
327     return Idx >= I.Idx;
328   }
329 };
330
331 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
332               "ParamIdx does not fit its serialization type");
333
334 #include "clang/AST/Attrs.inc"
335
336 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
337                                            const Attr *At) {
338   DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
339                   DiagnosticsEngine::ak_attr);
340   return DB;
341 }
342
343 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
344                                            const Attr *At) {
345   PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
346                   DiagnosticsEngine::ak_attr);
347   return PD;
348 }
349 }  // end namespace clang
350
351 #endif