]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/Attr.h
Upgrade our Clang in base to r108428.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / Attr.h
1 //===--- Attr.h - Classes for representing expressions ----------*- 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 "llvm/Support/Casting.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "clang/Basic/AttrKinds.h"
20 #include <cassert>
21 #include <cstring>
22 #include <algorithm>
23 using llvm::dyn_cast;
24
25 namespace clang {
26   class ASTContext;
27   class IdentifierInfo;
28   class ObjCInterfaceDecl;
29   class Expr;
30 }
31
32 // Defined in ASTContext.h
33 void *operator new(size_t Bytes, clang::ASTContext &C,
34                    size_t Alignment = 16) throw ();
35
36 // It is good practice to pair new/delete operators.  Also, MSVC gives many
37 // warnings if a matching delete overload is not declared, even though the
38 // throw() spec guarantees it will not be implicitly called.
39 void operator delete(void *Ptr, clang::ASTContext &C, size_t)
40               throw ();
41
42 namespace clang {
43
44 /// Attr - This represents one attribute.
45 class Attr {
46 private:
47   Attr *Next;
48   attr::Kind AttrKind;
49   bool Inherited : 1;
50
51 protected:
52   void* operator new(size_t bytes) throw() {
53     assert(0 && "Attrs cannot be allocated with regular 'new'.");
54     return 0;
55   }
56   void operator delete(void* data) throw() {
57     assert(0 && "Attrs cannot be released with regular 'delete'.");
58   }
59
60 protected:
61   Attr(attr::Kind AK) : Next(0), AttrKind(AK), Inherited(false) {}
62   virtual ~Attr() {
63     assert(Next == 0 && "Destroy didn't work");
64   }
65 public:
66   virtual void Destroy(ASTContext &C);
67
68   /// \brief Whether this attribute should be merged to new
69   /// declarations.
70   virtual bool isMerged() const { return true; }
71
72   attr::Kind getKind() const { return AttrKind; }
73
74   Attr *getNext() { return Next; }
75   const Attr *getNext() const { return Next; }
76   void setNext(Attr *next) { Next = next; }
77
78   template<typename T> const T *getNext() const {
79     for (const Attr *attr = getNext(); attr; attr = attr->getNext())
80       if (const T *V = dyn_cast<T>(attr))
81         return V;
82     return 0;
83   }
84
85   bool isInherited() const { return Inherited; }
86   void setInherited(bool value) { Inherited = value; }
87
88   void addAttr(Attr *attr) {
89     assert((attr != 0) && "addAttr(): attr is null");
90
91     // FIXME: This doesn't preserve the order in any way.
92     attr->Next = Next;
93     Next = attr;
94   }
95
96   // Clone this attribute.
97   virtual Attr* clone(ASTContext &C) const = 0;
98
99   // Implement isa/cast/dyncast/etc.
100   static bool classof(const Attr *) { return true; }
101 };
102
103 #include "clang/AST/Attrs.inc"
104   
105 class AttrWithString : public Attr {
106 private:
107   const char *Str;
108   unsigned StrLen;
109 protected:
110   AttrWithString(attr::Kind AK, ASTContext &C, llvm::StringRef s);
111   llvm::StringRef getString() const { return llvm::StringRef(Str, StrLen); }
112   void ReplaceString(ASTContext &C, llvm::StringRef newS);
113 public:
114   virtual void Destroy(ASTContext &C);
115 };
116
117 #define DEF_SIMPLE_ATTR(ATTR)                                           \
118 class ATTR##Attr : public Attr {                                        \
119 public:                                                                 \
120   ATTR##Attr() : Attr(attr::ATTR) {}                                          \
121   virtual Attr *clone(ASTContext &C) const;                             \
122   static bool classof(const Attr *A) { return A->getKind() == attr::ATTR; }   \
123   static bool classof(const ATTR##Attr *A) { return true; }             \
124 }
125
126 DEF_SIMPLE_ATTR(Packed);
127
128 /// \brief Attribute for specifying a maximum field alignment; this is only
129 /// valid on record decls.
130 class MaxFieldAlignmentAttr : public Attr {
131   unsigned Alignment;
132
133 public:
134   MaxFieldAlignmentAttr(unsigned alignment)
135     : Attr(attr::MaxFieldAlignment), Alignment(alignment) {}
136
137   /// getAlignment - The specified alignment in bits.
138   unsigned getAlignment() const { return Alignment; }
139
140   virtual Attr* clone(ASTContext &C) const;
141
142   // Implement isa/cast/dyncast/etc.
143   static bool classof(const Attr *A) {
144     return A->getKind() == attr::MaxFieldAlignment;
145   }
146   static bool classof(const MaxFieldAlignmentAttr *A) { return true; }
147 };
148
149 DEF_SIMPLE_ATTR(AlignMac68k);
150
151 /// \brief Atribute for specifying the alignment of a variable or type.
152 ///
153 /// This node will either contain the precise Alignment (in bits, not bytes!)
154 /// or will contain the expression for the alignment attribute in the case of
155 /// a dependent expression within a class or function template. At template
156 /// instantiation time these are transformed into concrete attributes.
157 class AlignedAttr : public Attr {
158   unsigned Alignment;
159   Expr *AlignmentExpr;
160 public:
161   AlignedAttr(unsigned alignment)
162     : Attr(attr::Aligned), Alignment(alignment), AlignmentExpr(0) {}
163   AlignedAttr(Expr *E)
164     : Attr(attr::Aligned), Alignment(0), AlignmentExpr(E) {}
165
166   /// getAlignmentExpr - Get a dependent alignment expression if one is present.
167   Expr *getAlignmentExpr() const {
168     return AlignmentExpr;
169   }
170
171   /// isDependent - Is the alignment a dependent expression
172   bool isDependent() const {
173     return getAlignmentExpr();
174   }
175
176   /// getAlignment - The specified alignment in bits. Requires !isDependent().
177   unsigned getAlignment() const {
178     assert(!isDependent() && "Cannot get a value dependent alignment");
179     return Alignment;
180   }
181
182   /// getMaxAlignment - Get the maximum alignment of attributes on this list.
183   unsigned getMaxAlignment() const {
184     const AlignedAttr *Next = getNext<AlignedAttr>();
185     if (Next)
186       return std::max(Next->getMaxAlignment(), getAlignment());
187     else
188       return getAlignment();
189   }
190
191   virtual Attr* clone(ASTContext &C) const;
192
193   // Implement isa/cast/dyncast/etc.
194   static bool classof(const Attr *A) {
195     return A->getKind() == attr::Aligned;
196   }
197   static bool classof(const AlignedAttr *A) { return true; }
198 };
199
200 class AnnotateAttr : public AttrWithString {
201 public:
202   AnnotateAttr(ASTContext &C, llvm::StringRef ann)
203     : AttrWithString(attr::Annotate, C, ann) {}
204
205   llvm::StringRef getAnnotation() const { return getString(); }
206
207   virtual Attr* clone(ASTContext &C) const;
208
209   // Implement isa/cast/dyncast/etc.
210   static bool classof(const Attr *A) {
211     return A->getKind() == attr::Annotate;
212   }
213   static bool classof(const AnnotateAttr *A) { return true; }
214 };
215
216 class AsmLabelAttr : public AttrWithString {
217 public:
218   AsmLabelAttr(ASTContext &C, llvm::StringRef L)
219     : AttrWithString(attr::AsmLabel, C, L) {}
220
221   llvm::StringRef getLabel() const { return getString(); }
222
223   virtual Attr* clone(ASTContext &C) const;
224
225   // Implement isa/cast/dyncast/etc.
226   static bool classof(const Attr *A) {
227     return A->getKind() == attr::AsmLabel;
228   }
229   static bool classof(const AsmLabelAttr *A) { return true; }
230 };
231
232 DEF_SIMPLE_ATTR(AlwaysInline);
233
234 class AliasAttr : public AttrWithString {
235 public:
236   AliasAttr(ASTContext &C, llvm::StringRef aliasee)
237     : AttrWithString(attr::Alias, C, aliasee) {}
238
239   llvm::StringRef getAliasee() const { return getString(); }
240
241   virtual Attr *clone(ASTContext &C) const;
242
243   // Implement isa/cast/dyncast/etc.
244   static bool classof(const Attr *A) { return A->getKind() == attr::Alias; }
245   static bool classof(const AliasAttr *A) { return true; }
246 };
247
248 class ConstructorAttr : public Attr {
249   int priority;
250 public:
251   ConstructorAttr(int p) : Attr(attr::Constructor), priority(p) {}
252
253   int getPriority() const { return priority; }
254
255   virtual Attr *clone(ASTContext &C) const;
256
257   // Implement isa/cast/dyncast/etc.
258   static bool classof(const Attr *A)
259     { return A->getKind() == attr::Constructor; }
260   static bool classof(const ConstructorAttr *A) { return true; }
261 };
262
263 class DestructorAttr : public Attr {
264   int priority;
265 public:
266   DestructorAttr(int p) : Attr(attr::Destructor), priority(p) {}
267
268   int getPriority() const { return priority; }
269
270   virtual Attr *clone(ASTContext &C) const;
271
272   // Implement isa/cast/dyncast/etc.
273   static bool classof(const Attr *A)
274     { return A->getKind() == attr::Destructor; }
275   static bool classof(const DestructorAttr *A) { return true; }
276 };
277
278 class IBOutletAttr : public Attr {
279 public:
280   IBOutletAttr() : Attr(attr::IBOutlet) {}
281
282   virtual Attr *clone(ASTContext &C) const;
283
284   // Implement isa/cast/dyncast/etc.
285   static bool classof(const Attr *A) {
286     return A->getKind() == attr::IBOutlet;
287   }
288   static bool classof(const IBOutletAttr *A) { return true; }
289 };
290
291 class IBOutletCollectionAttr : public Attr {
292   const ObjCInterfaceDecl *D;
293 public:
294   IBOutletCollectionAttr(const ObjCInterfaceDecl *d = 0)
295     : Attr(attr::IBOutletCollection), D(d) {}
296
297   const ObjCInterfaceDecl *getClass() const { return D; }
298
299   virtual Attr *clone(ASTContext &C) const;
300
301   // Implement isa/cast/dyncast/etc.
302   static bool classof(const Attr *A) {
303     return A->getKind() == attr::IBOutletCollection;
304   }
305   static bool classof(const IBOutletCollectionAttr *A) { return true; }
306 };
307
308 class IBActionAttr : public Attr {
309 public:
310   IBActionAttr() : Attr(attr::IBAction) {}
311
312   virtual Attr *clone(ASTContext &C) const;
313
314     // Implement isa/cast/dyncast/etc.
315   static bool classof(const Attr *A) {
316     return A->getKind() == attr::IBAction;
317   }
318   static bool classof(const IBActionAttr *A) { return true; }
319 };
320
321 DEF_SIMPLE_ATTR(AnalyzerNoReturn);
322 DEF_SIMPLE_ATTR(Deprecated);
323 DEF_SIMPLE_ATTR(GNUInline);
324 DEF_SIMPLE_ATTR(Malloc);
325 DEF_SIMPLE_ATTR(NoReturn);
326 DEF_SIMPLE_ATTR(NoInstrumentFunction);
327
328 class SectionAttr : public AttrWithString {
329 public:
330   SectionAttr(ASTContext &C, llvm::StringRef N)
331     : AttrWithString(attr::Section, C, N) {}
332
333   llvm::StringRef getName() const { return getString(); }
334
335   virtual Attr *clone(ASTContext &C) const;
336
337   // Implement isa/cast/dyncast/etc.
338   static bool classof(const Attr *A) {
339     return A->getKind() == attr::Section;
340   }
341   static bool classof(const SectionAttr *A) { return true; }
342 };
343
344 DEF_SIMPLE_ATTR(Unavailable);
345 DEF_SIMPLE_ATTR(Unused);
346 DEF_SIMPLE_ATTR(Used);
347 DEF_SIMPLE_ATTR(Weak);
348 DEF_SIMPLE_ATTR(WeakImport);
349 DEF_SIMPLE_ATTR(WeakRef);
350 DEF_SIMPLE_ATTR(NoThrow);
351 DEF_SIMPLE_ATTR(Const);
352 DEF_SIMPLE_ATTR(Pure);
353
354 class NonNullAttr : public Attr {
355   unsigned* ArgNums;
356   unsigned Size;
357 public:
358   NonNullAttr(ASTContext &C, unsigned* arg_nums = 0, unsigned size = 0);
359
360   virtual void Destroy(ASTContext &C);
361
362   typedef const unsigned *iterator;
363   iterator begin() const { return ArgNums; }
364   iterator end() const { return ArgNums + Size; }
365   unsigned size() const { return Size; }
366
367   bool isNonNull(unsigned arg) const {
368     return ArgNums ? std::binary_search(ArgNums, ArgNums+Size, arg) : true;
369   }
370
371   virtual Attr *clone(ASTContext &C) const;
372
373   static bool classof(const Attr *A) { return A->getKind() == attr::NonNull; }
374   static bool classof(const NonNullAttr *A) { return true; }
375 };
376
377 class FormatAttr : public AttrWithString {
378   int formatIdx, firstArg;
379 public:
380   FormatAttr(ASTContext &C, llvm::StringRef type, int idx, int first)
381     : AttrWithString(attr::Format, C, type), formatIdx(idx), firstArg(first) {}
382
383   llvm::StringRef getType() const { return getString(); }
384   void setType(ASTContext &C, llvm::StringRef type);
385   int getFormatIdx() const { return formatIdx; }
386   int getFirstArg() const { return firstArg; }
387
388   virtual Attr *clone(ASTContext &C) const;
389
390   // Implement isa/cast/dyncast/etc.
391   static bool classof(const Attr *A) { return A->getKind() == attr::Format; }
392   static bool classof(const FormatAttr *A) { return true; }
393 };
394
395 class FormatArgAttr : public Attr {
396   int formatIdx;
397 public:
398   FormatArgAttr(int idx) : Attr(attr::FormatArg), formatIdx(idx) {}
399   int getFormatIdx() const { return formatIdx; }
400
401   virtual Attr *clone(ASTContext &C) const;
402
403   // Implement isa/cast/dyncast/etc.
404   static bool classof(const Attr *A) { return A->getKind() == attr::FormatArg; }
405   static bool classof(const FormatArgAttr *A) { return true; }
406 };
407
408 class SentinelAttr : public Attr {
409   int sentinel, NullPos;
410 public:
411   SentinelAttr(int sentinel_val, int nullPos) : Attr(attr::Sentinel),
412                sentinel(sentinel_val), NullPos(nullPos) {}
413   int getSentinel() const { return sentinel; }
414   int getNullPos() const { return NullPos; }
415
416   virtual Attr *clone(ASTContext &C) const;
417
418   // Implement isa/cast/dyncast/etc.
419   static bool classof(const Attr *A) { return A->getKind() == attr::Sentinel; }
420   static bool classof(const SentinelAttr *A) { return true; }
421 };
422
423 class VisibilityAttr : public Attr {
424 public:
425   /// @brief An enumeration for the kinds of visibility of symbols.
426   enum VisibilityTypes {
427     DefaultVisibility = 0,
428     HiddenVisibility,
429     ProtectedVisibility
430   };
431 private:
432   VisibilityTypes VisibilityType;
433 public:
434   VisibilityAttr(VisibilityTypes v) : Attr(attr::Visibility),
435                  VisibilityType(v) {}
436
437   VisibilityTypes getVisibility() const { return VisibilityType; }
438
439   virtual Attr *clone(ASTContext &C) const;
440
441   // Implement isa/cast/dyncast/etc.
442   static bool classof(const Attr *A)
443     { return A->getKind() == attr::Visibility; }
444   static bool classof(const VisibilityAttr *A) { return true; }
445 };
446
447 DEF_SIMPLE_ATTR(FastCall);
448 DEF_SIMPLE_ATTR(StdCall);
449 DEF_SIMPLE_ATTR(ThisCall);
450 DEF_SIMPLE_ATTR(CDecl);
451 DEF_SIMPLE_ATTR(TransparentUnion);
452 DEF_SIMPLE_ATTR(ObjCNSObject);
453 DEF_SIMPLE_ATTR(ObjCException);
454
455 class OverloadableAttr : public Attr {
456 public:
457   OverloadableAttr() : Attr(attr::Overloadable) { }
458
459   virtual bool isMerged() const { return false; }
460
461   virtual Attr *clone(ASTContext &C) const;
462
463   static bool classof(const Attr *A)
464     { return A->getKind() == attr::Overloadable; }
465   static bool classof(const OverloadableAttr *) { return true; }
466 };
467
468 class BlocksAttr : public Attr {
469 public:
470   enum BlocksAttrTypes {
471     ByRef = 0
472   };
473 private:
474   BlocksAttrTypes BlocksAttrType;
475 public:
476   BlocksAttr(BlocksAttrTypes t) : Attr(attr::Blocks), BlocksAttrType(t) {}
477
478   BlocksAttrTypes getType() const { return BlocksAttrType; }
479
480   virtual Attr *clone(ASTContext &C) const;
481
482   // Implement isa/cast/dyncast/etc.
483   static bool classof(const Attr *A) { return A->getKind() == attr::Blocks; }
484   static bool classof(const BlocksAttr *A) { return true; }
485 };
486
487 class FunctionDecl;
488
489 class CleanupAttr : public Attr {
490   FunctionDecl *FD;
491
492 public:
493   CleanupAttr(FunctionDecl *fd) : Attr(attr::Cleanup), FD(fd) {}
494
495   const FunctionDecl *getFunctionDecl() const { return FD; }
496
497   virtual Attr *clone(ASTContext &C) const;
498
499   // Implement isa/cast/dyncast/etc.
500   static bool classof(const Attr *A) { return A->getKind() == attr::Cleanup; }
501   static bool classof(const CleanupAttr *A) { return true; }
502 };
503
504 DEF_SIMPLE_ATTR(NoDebug);
505 DEF_SIMPLE_ATTR(WarnUnusedResult);
506 DEF_SIMPLE_ATTR(NoInline);
507
508 class RegparmAttr : public Attr {
509   unsigned NumParams;
510
511 public:
512   RegparmAttr(unsigned np) : Attr(attr::Regparm), NumParams(np) {}
513
514   unsigned getNumParams() const { return NumParams; }
515
516   virtual Attr *clone(ASTContext &C) const;
517
518   // Implement isa/cast/dyncast/etc.
519   static bool classof(const Attr *A) { return A->getKind() == attr::Regparm; }
520   static bool classof(const RegparmAttr *A) { return true; }
521 };
522
523 class ReqdWorkGroupSizeAttr : public Attr {
524   unsigned X, Y, Z;
525 public:
526   ReqdWorkGroupSizeAttr(unsigned X, unsigned Y, unsigned Z)
527   : Attr(attr::ReqdWorkGroupSize), X(X), Y(Y), Z(Z) {}
528
529   unsigned getXDim() const { return X; }
530   unsigned getYDim() const { return Y; }
531   unsigned getZDim() const { return Z; }
532
533   virtual Attr *clone(ASTContext &C) const;
534
535   // Implement isa/cast/dyncast/etc.
536   static bool classof(const Attr *A) {
537     return A->getKind() == attr::ReqdWorkGroupSize;
538   }
539   static bool classof(const ReqdWorkGroupSizeAttr *A) { return true; }
540 };
541
542 class InitPriorityAttr : public Attr {
543   unsigned Priority;
544 public:
545   InitPriorityAttr(unsigned priority) 
546     : Attr(attr::InitPriority),  Priority(priority) {}
547     
548   virtual void Destroy(ASTContext &C) { Attr::Destroy(C); }
549     
550   unsigned getPriority() const { return Priority; }
551     
552   virtual Attr *clone(ASTContext &C) const;
553     
554   static bool classof(const Attr *A) 
555     { return A->getKind() == attr::InitPriority; }
556   static bool classof(const InitPriorityAttr *A) { return true; }
557 };
558   
559 // Checker-specific attributes.
560 DEF_SIMPLE_ATTR(CFReturnsNotRetained);
561 DEF_SIMPLE_ATTR(CFReturnsRetained);
562 DEF_SIMPLE_ATTR(NSReturnsNotRetained);
563 DEF_SIMPLE_ATTR(NSReturnsRetained);
564
565 // Target-specific attributes
566 DEF_SIMPLE_ATTR(DLLImport);
567 DEF_SIMPLE_ATTR(DLLExport);
568
569 class MSP430InterruptAttr : public Attr {
570   unsigned Number;
571
572 public:
573   MSP430InterruptAttr(unsigned n) : Attr(attr::MSP430Interrupt), Number(n) {}
574
575   unsigned getNumber() const { return Number; }
576
577   virtual Attr *clone(ASTContext &C) const;
578
579   // Implement isa/cast/dyncast/etc.
580   static bool classof(const Attr *A)
581     { return A->getKind() == attr::MSP430Interrupt; }
582   static bool classof(const MSP430InterruptAttr *A) { return true; }
583 };
584
585 DEF_SIMPLE_ATTR(X86ForceAlignArgPointer);
586
587 #undef DEF_SIMPLE_ATTR
588
589 }  // end namespace clang
590
591 #endif