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