Attr.h revision 17f194f4393a67fd28ad822c06d32b8cb99bad3f
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 <cassert>
18#include <cstring>
19#include <string>
20#include <algorithm>
21
22namespace clang {
23
24/// Attr - This represents one attribute.
25class Attr {
26public:
27  enum Kind {
28    Alias,
29    Aligned,
30    AlwaysInline,
31    Annotate,
32    AsmLabel, // Represent GCC asm label extension.
33    Constructor,
34    Deprecated,
35    Destructor,
36    DLLImport,
37    DLLExport,
38    FastCall,
39    Format,
40    IBOutletKind, // Clang-specific.  Use "Kind" suffix to not conflict with
41    NonNull,
42    NoReturn,
43    NoThrow,
44    ObjCGC,
45    ObjCNSObject,
46    Overloadable, // Clang-specific
47    Packed,
48    Section,
49    StdCall,
50    TransparentUnion,
51    Unavailable,
52    Unused,
53    Visibility,
54    Weak,
55    Blocks,
56    Const,
57    Pure,
58    Cleanup
59  };
60
61private:
62  Attr *Next;
63  Kind AttrKind;
64  bool Inherited : 1;
65
66protected:
67  Attr(Kind AK) : Next(0), AttrKind(AK), Inherited(false) {}
68public:
69  virtual ~Attr() {
70    delete Next;
71  }
72
73  Kind getKind() const { return AttrKind; }
74
75  Attr *getNext() { return Next; }
76  const Attr *getNext() const { return Next; }
77  void setNext(Attr *next) { Next = next; }
78
79  bool isInherited() const { return Inherited; }
80  void setInherited(bool value) { Inherited = value; }
81
82  void addAttr(Attr *attr) {
83    assert((attr != 0) && "addAttr(): attr is null");
84
85    // FIXME: This doesn't preserve the order in any way.
86    attr->Next = Next;
87    Next = attr;
88  }
89
90  // Implement isa/cast/dyncast/etc.
91  static bool classof(const Attr *) { return true; }
92};
93
94class PackedAttr : public Attr {
95  unsigned Alignment;
96
97public:
98  PackedAttr(unsigned alignment) : Attr(Packed), Alignment(alignment) {}
99
100  /// getAlignment - The specified alignment in bits.
101  unsigned getAlignment() const { return Alignment; }
102
103  // Implement isa/cast/dyncast/etc.
104  static bool classof(const Attr *A) {
105    return A->getKind() == Packed;
106  }
107  static bool classof(const PackedAttr *A) { return true; }
108};
109
110class AlignedAttr : public Attr {
111  unsigned Alignment;
112public:
113  AlignedAttr(unsigned alignment) : Attr(Aligned), Alignment(alignment) {}
114
115  /// getAlignment - The specified alignment in bits.
116  unsigned getAlignment() const { return Alignment; }
117
118  // Implement isa/cast/dyncast/etc.
119  static bool classof(const Attr *A) {
120    return A->getKind() == Aligned;
121  }
122  static bool classof(const AlignedAttr *A) { return true; }
123};
124
125class AnnotateAttr : public Attr {
126  std::string Annotation;
127public:
128  AnnotateAttr(const std::string &ann) : Attr(Annotate), Annotation(ann) {}
129
130  const std::string& getAnnotation() const { return Annotation; }
131
132  // Implement isa/cast/dyncast/etc.
133  static bool classof(const Attr *A) {
134    return A->getKind() == Annotate;
135  }
136  static bool classof(const AnnotateAttr *A) { return true; }
137};
138
139class AsmLabelAttr : public Attr {
140  std::string Label;
141public:
142  AsmLabelAttr(const std::string &L) : Attr(AsmLabel), Label(L) {}
143
144  const std::string& getLabel() const { return Label; }
145
146  // Implement isa/cast/dyncast/etc.
147  static bool classof(const Attr *A) {
148    return A->getKind() == AsmLabel;
149  }
150  static bool classof(const AsmLabelAttr *A) { return true; }
151};
152
153class AlwaysInlineAttr : public Attr {
154public:
155  AlwaysInlineAttr() : Attr(AlwaysInline) {}
156
157  // Implement isa/cast/dyncast/etc.
158
159  static bool classof(const Attr *A) { return A->getKind() == AlwaysInline; }
160  static bool classof(const AlwaysInlineAttr *A) { return true; }
161};
162
163class AliasAttr : public Attr {
164  std::string Aliasee;
165public:
166  AliasAttr(const std::string &aliasee) : Attr(Alias), Aliasee(aliasee) {}
167
168  const std::string& getAliasee() const { return Aliasee; }
169
170  // Implement isa/cast/dyncast/etc.
171
172  static bool classof(const Attr *A) { return A->getKind() == Alias; }
173  static bool classof(const AliasAttr *A) { return true; }
174};
175
176class ConstructorAttr : public Attr {
177  int priority;
178public:
179  ConstructorAttr(int p) : Attr(Constructor), priority(p) {}
180
181  int getPriority() const { return priority; }
182
183  // Implement isa/cast/dyncast/etc.
184  static bool classof(const Attr *A) { return A->getKind() == Constructor; }
185  static bool classof(const ConstructorAttr *A) { return true; }
186};
187
188class DestructorAttr : public Attr {
189  int priority;
190public:
191  DestructorAttr(int p) : Attr(Destructor), priority(p) {}
192
193  int getPriority() const { return priority; }
194
195  // Implement isa/cast/dyncast/etc.
196  static bool classof(const Attr *A) { return A->getKind() == Destructor; }
197  static bool classof(const DestructorAttr *A) { return true; }
198};
199
200class IBOutletAttr : public Attr {
201public:
202  IBOutletAttr() : Attr(IBOutletKind) {}
203
204  // Implement isa/cast/dyncast/etc.
205  static bool classof(const Attr *A) {
206    return A->getKind() == IBOutletKind;
207  }
208  static bool classof(const IBOutletAttr *A) { return true; }
209};
210
211class NoReturnAttr : public Attr {
212public:
213  NoReturnAttr() : Attr(NoReturn) {}
214
215  // Implement isa/cast/dyncast/etc.
216
217  static bool classof(const Attr *A) { return A->getKind() == NoReturn; }
218  static bool classof(const NoReturnAttr *A) { return true; }
219};
220
221class DeprecatedAttr : public Attr {
222public:
223  DeprecatedAttr() : Attr(Deprecated) {}
224
225  // Implement isa/cast/dyncast/etc.
226
227  static bool classof(const Attr *A) { return A->getKind() == Deprecated; }
228  static bool classof(const DeprecatedAttr *A) { return true; }
229};
230
231class SectionAttr : public Attr {
232  std::string Name;
233public:
234  SectionAttr(const std::string &N) : Attr(Section), Name(N) {}
235
236  const std::string& getName() const { return Name; }
237
238  // Implement isa/cast/dyncast/etc.
239  static bool classof(const Attr *A) {
240    return A->getKind() == Section;
241  }
242  static bool classof(const SectionAttr *A) { return true; }
243};
244
245class UnavailableAttr : public Attr {
246public:
247  UnavailableAttr() : Attr(Unavailable) {}
248
249  // Implement isa/cast/dyncast/etc.
250
251  static bool classof(const Attr *A) { return A->getKind() == Unavailable; }
252  static bool classof(const UnavailableAttr *A) { return true; }
253};
254
255class UnusedAttr : public Attr {
256public:
257  UnusedAttr() : Attr(Unused) {}
258
259  // Implement isa/cast/dyncast/etc.
260  static bool classof(const Attr *A) { return A->getKind() == Unused; }
261  static bool classof(const UnusedAttr *A) { return true; }
262};
263
264class WeakAttr : public Attr {
265public:
266  WeakAttr() : Attr(Weak) {}
267
268  // Implement isa/cast/dyncast/etc.
269
270  static bool classof(const Attr *A) { return A->getKind() == Weak; }
271  static bool classof(const WeakAttr *A) { return true; }
272};
273
274class NoThrowAttr : public Attr {
275public:
276  NoThrowAttr() : Attr(NoThrow) {}
277
278  // Implement isa/cast/dyncast/etc.
279
280  static bool classof(const Attr *A) { return A->getKind() == NoThrow; }
281  static bool classof(const NoThrowAttr *A) { return true; }
282};
283
284class ConstAttr : public Attr {
285public:
286  ConstAttr() : Attr(Const) {}
287
288  // Implement isa/cast/dyncast/etc.
289
290  static bool classof(const Attr *A) { return A->getKind() == Const; }
291  static bool classof(const ConstAttr *A) { return true; }
292};
293
294class PureAttr : public Attr {
295public:
296  PureAttr() : Attr(Pure) {}
297
298  // Implement isa/cast/dyncast/etc.
299
300  static bool classof(const Attr *A) { return A->getKind() == Pure; }
301  static bool classof(const PureAttr *A) { return true; }
302};
303
304class NonNullAttr : public Attr {
305  unsigned* ArgNums;
306  unsigned Size;
307public:
308  NonNullAttr(unsigned* arg_nums = 0, unsigned size = 0) : Attr(NonNull),
309    ArgNums(0), Size(0) {
310
311    if (size) {
312      assert (arg_nums);
313      ArgNums = new unsigned[size];
314      Size = size;
315      memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size);
316    }
317  }
318
319  virtual ~NonNullAttr() {
320    delete [] ArgNums;
321  }
322
323  bool isNonNull(unsigned arg) const {
324    return ArgNums ? std::binary_search(ArgNums, ArgNums+Size, arg) : true;
325  }
326
327  static bool classof(const Attr *A) { return A->getKind() == NonNull; }
328  static bool classof(const NonNullAttr *A) { return true; }
329};
330
331class FormatAttr : public Attr {
332  std::string Type;
333  int formatIdx, firstArg;
334public:
335  FormatAttr(const std::string &type, int idx, int first) : Attr(Format),
336             Type(type), formatIdx(idx), firstArg(first) {}
337
338  const std::string& getType() const { return Type; }
339  int getFormatIdx() const { return formatIdx; }
340  int getFirstArg() const { return firstArg; }
341
342  // Implement isa/cast/dyncast/etc.
343
344  static bool classof(const Attr *A) { return A->getKind() == Format; }
345  static bool classof(const FormatAttr *A) { return true; }
346};
347
348class VisibilityAttr : public Attr {
349public:
350  /// @brief An enumeration for the kinds of visibility of symbols.
351  enum VisibilityTypes {
352    DefaultVisibility = 0,
353    HiddenVisibility,
354    ProtectedVisibility
355  };
356private:
357  VisibilityTypes VisibilityType;
358public:
359  VisibilityAttr(VisibilityTypes v) : Attr(Visibility),
360                 VisibilityType(v) {}
361
362  VisibilityTypes getVisibility() const { return VisibilityType; }
363
364  // Implement isa/cast/dyncast/etc.
365
366  static bool classof(const Attr *A) { return A->getKind() == Visibility; }
367  static bool classof(const VisibilityAttr *A) { return true; }
368};
369
370class DLLImportAttr : public Attr {
371public:
372  DLLImportAttr() : Attr(DLLImport) {}
373
374  // Implement isa/cast/dyncast/etc.
375
376  static bool classof(const Attr *A) { return A->getKind() == DLLImport; }
377  static bool classof(const DLLImportAttr *A) { return true; }
378};
379
380class DLLExportAttr : public Attr {
381public:
382  DLLExportAttr() : Attr(DLLExport) {}
383
384  // Implement isa/cast/dyncast/etc.
385
386  static bool classof(const Attr *A) { return A->getKind() == DLLExport; }
387  static bool classof(const DLLExportAttr *A) { return true; }
388};
389
390class FastCallAttr : public Attr {
391public:
392  FastCallAttr() : Attr(FastCall) {}
393
394  // Implement isa/cast/dyncast/etc.
395
396  static bool classof(const Attr *A) { return A->getKind() == FastCall; }
397  static bool classof(const FastCallAttr *A) { return true; }
398};
399
400class StdCallAttr : public Attr {
401public:
402  StdCallAttr() : Attr(StdCall) {}
403
404  // Implement isa/cast/dyncast/etc.
405
406  static bool classof(const Attr *A) { return A->getKind() == StdCall; }
407  static bool classof(const StdCallAttr *A) { return true; }
408};
409
410class TransparentUnionAttr : public Attr {
411public:
412  TransparentUnionAttr() : Attr(TransparentUnion) {}
413
414  // Implement isa/cast/dyncast/etc.
415
416  static bool classof(const Attr *A) { return A->getKind() == TransparentUnion; }
417  static bool classof(const TransparentUnionAttr *A) { return true; }
418};
419
420class ObjCGCAttr : public Attr {
421public:
422  enum GCAttrTypes {
423    Weak = 0,
424    Strong
425  };
426private:
427  GCAttrTypes GCAttrType;
428public:
429  ObjCGCAttr(GCAttrTypes t) : Attr(ObjCGC), GCAttrType(t) {}
430
431  GCAttrTypes getType() const { return GCAttrType; }
432
433  // Implement isa/cast/dyncast/etc.
434
435  static bool classof(const Attr *A) { return A->getKind() == ObjCGC; }
436  static bool classof(const ObjCGCAttr *A) { return true; }
437};
438
439class ObjCNSObjectAttr : public Attr {
440// Implement isa/cast/dyncast/etc.
441public:
442  ObjCNSObjectAttr() : Attr(ObjCNSObject) {}
443
444static bool classof(const Attr *A) { return A->getKind() == ObjCNSObject; }
445static bool classof(const ObjCNSObjectAttr *A) { return true; }
446};
447
448class OverloadableAttr : public Attr {
449public:
450  OverloadableAttr() : Attr(Overloadable) { }
451
452  static bool classof(const Attr *A) { return A->getKind() == Overloadable; }
453  static bool classof(const OverloadableAttr *) { return true; }
454};
455
456class BlocksAttr : public Attr {
457public:
458  enum BlocksAttrTypes {
459    ByRef = 0
460  };
461private:
462  BlocksAttrTypes BlocksAttrType;
463public:
464  BlocksAttr(BlocksAttrTypes t) : Attr(Blocks), BlocksAttrType(t) {}
465
466  BlocksAttrTypes getType() const { return BlocksAttrType; }
467
468  // Implement isa/cast/dyncast/etc.
469
470  static bool classof(const Attr *A) { return A->getKind() == Blocks; }
471  static bool classof(const BlocksAttr *A) { return true; }
472};
473
474class FunctionDecl;
475
476class CleanupAttr : public Attr {
477  FunctionDecl *FD;
478
479public:
480  CleanupAttr(FunctionDecl *fd) : Attr(Cleanup), FD(fd) {}
481
482  const FunctionDecl *getFunctionDecl() const { return FD; }
483
484  // Implement isa/cast/dyncast/etc.
485
486  static bool classof(const Attr *A) { return A->getKind() == Cleanup; }
487  static bool classof(const CleanupAttr *A) { return true; }
488};
489
490}  // end namespace clang
491
492#endif
493