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