AttributeList.h revision f312b1ea179f1c44371f9ee0cd0bc006f612de11
1//===--- AttributeList.h - Parsed attribute sets ----------------*- 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 AttributeList class, which is used to collect
11// parsed attributes.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SEMA_ATTRLIST_H
16#define LLVM_CLANG_SEMA_ATTRLIST_H
17
18#include "clang/Sema/Ownership.h"
19#include "clang/Basic/SourceLocation.h"
20#include <cassert>
21
22namespace clang {
23  class IdentifierInfo;
24  class Expr;
25
26/// AttributeList - Represents GCC's __attribute__ declaration. There are
27/// 4 forms of this construct...they are:
28///
29/// 1: __attribute__(( const )). ParmName/Args/NumArgs will all be unused.
30/// 2: __attribute__(( mode(byte) )). ParmName used, Args/NumArgs unused.
31/// 3: __attribute__(( format(printf, 1, 2) )). ParmName/Args/NumArgs all used.
32/// 4: __attribute__(( aligned(16) )). ParmName is unused, Args/Num used.
33///
34class AttributeList {
35  IdentifierInfo *AttrName;
36  SourceLocation AttrLoc;
37  IdentifierInfo *ScopeName;
38  SourceLocation ScopeLoc;
39  IdentifierInfo *ParmName;
40  SourceLocation ParmLoc;
41  Expr **Args;
42  unsigned NumArgs;
43  AttributeList *Next;
44  bool DeclspecAttribute, CXX0XAttribute;
45  mutable bool Invalid; /// True if already diagnosed as invalid.
46  AttributeList(const AttributeList &); // DO NOT IMPLEMENT
47  void operator=(const AttributeList &); // DO NOT IMPLEMENT
48public:
49  AttributeList(IdentifierInfo *AttrName, SourceLocation AttrLoc,
50                IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
51                IdentifierInfo *ParmName, SourceLocation ParmLoc,
52                Expr **args, unsigned numargs,
53                AttributeList *Next, bool declspec = false, bool cxx0x = false);
54  ~AttributeList();
55
56  enum Kind {             // Please keep this list alphabetized.
57    AT_IBAction,          // Clang-specific.
58    AT_IBOutlet,          // Clang-specific.
59    AT_IBOutletCollection, // Clang-specific.
60    AT_address_space,
61    AT_alias,
62    AT_aligned,
63    AT_always_inline,
64    AT_analyzer_noreturn,
65    AT_annotate,
66    AT_base_check,
67    AT_blocks,
68    AT_carries_dependency,
69    AT_cdecl,
70    AT_cleanup,
71    AT_const,
72    AT_constructor,
73    AT_deprecated,
74    AT_destructor,
75    AT_dllexport,
76    AT_dllimport,
77    AT_ext_vector_type,
78    AT_fastcall,
79    AT_final,
80    AT_format,
81    AT_format_arg,
82    AT_gnu_inline,
83    AT_hiding,
84    AT_malloc,
85    AT_mode,
86    AT_nodebug,
87    AT_noinline,
88    AT_no_instrument_function,
89    AT_nonnull,
90    AT_noreturn,
91    AT_nothrow,
92    AT_nsobject,
93    AT_objc_exception,
94    AT_override,
95    AT_cf_returns_not_retained, // Clang-specific.
96    AT_cf_returns_retained,     // Clang-specific.
97    AT_ns_returns_not_retained, // Clang-specific.
98    AT_ns_returns_retained,     // Clang-specific.
99    AT_objc_gc,
100    AT_overloadable,       // Clang-specific.
101    AT_ownership_holds,    // Clang-specific.
102    AT_ownership_returns,  // Clang-specific.
103    AT_ownership_takes,    // Clang-specific.
104    AT_packed,
105    AT_pure,
106    AT_regparm,
107    AT_section,
108    AT_sentinel,
109    AT_stdcall,
110    AT_thiscall,
111    AT_transparent_union,
112    AT_unavailable,
113    AT_unused,
114    AT_used,
115    AT_vecreturn,     // PS3 PPU-specific.
116    AT_vector_size,
117    AT_visibility,
118    AT_warn_unused_result,
119    AT_weak,
120    AT_weakref,
121    AT_weak_import,
122    AT_reqd_wg_size,
123    AT_init_priority,
124    IgnoredAttribute,
125    UnknownAttribute
126  };
127
128  IdentifierInfo *getName() const { return AttrName; }
129  SourceLocation getLoc() const { return AttrLoc; }
130
131  bool hasScope() const { return ScopeName; }
132  IdentifierInfo *getScopeName() const { return ScopeName; }
133  SourceLocation getScopeLoc() const { return ScopeLoc; }
134
135  IdentifierInfo *getParameterName() const { return ParmName; }
136
137  bool isDeclspecAttribute() const { return DeclspecAttribute; }
138  bool isCXX0XAttribute() const { return CXX0XAttribute; }
139
140  bool isInvalid() const { return Invalid; }
141  void setInvalid(bool b = true) const { Invalid = b; }
142
143  Kind getKind() const { return getKind(getName()); }
144  static Kind getKind(const IdentifierInfo *Name);
145
146  AttributeList *getNext() const { return Next; }
147  void setNext(AttributeList *N) { Next = N; }
148
149  /// getNumArgs - Return the number of actual arguments to this attribute.
150  unsigned getNumArgs() const { return NumArgs; }
151
152  /// getArg - Return the specified argument.
153  Expr *getArg(unsigned Arg) const {
154    assert(Arg < NumArgs && "Arg access out of range!");
155    return Args[Arg];
156  }
157
158  class arg_iterator {
159    Expr** X;
160    unsigned Idx;
161  public:
162    arg_iterator(Expr** x, unsigned idx) : X(x), Idx(idx) {}
163
164    arg_iterator& operator++() {
165      ++Idx;
166      return *this;
167    }
168
169    bool operator==(const arg_iterator& I) const {
170      assert (X == I.X &&
171              "compared arg_iterators are for different argument lists");
172      return Idx == I.Idx;
173    }
174
175    bool operator!=(const arg_iterator& I) const {
176      return !operator==(I);
177    }
178
179    Expr* operator*() const {
180      return X[Idx];
181    }
182
183    unsigned getArgNum() const {
184      return Idx+1;
185    }
186  };
187
188  arg_iterator arg_begin() const {
189    return arg_iterator(Args, 0);
190  }
191
192  arg_iterator arg_end() const {
193    return arg_iterator(Args, NumArgs);
194  }
195};
196
197/// addAttributeLists - Add two AttributeLists together
198/// The right-hand list is appended to the left-hand list, if any
199/// A pointer to the joined list is returned.
200/// Note: the lists are not left unmodified.
201inline AttributeList* addAttributeLists (AttributeList *Left,
202                                         AttributeList *Right) {
203  if (!Left)
204    return Right;
205
206  AttributeList *next = Left, *prev;
207  do {
208    prev = next;
209    next = next->getNext();
210  } while (next);
211  prev->setNext(Right);
212  return Left;
213}
214
215/// CXX0XAttributeList - A wrapper around a C++0x attribute list.
216/// Stores, in addition to the list proper, whether or not an actual list was
217/// (as opposed to an empty list, which may be ill-formed in some places) and
218/// the source range of the list.
219struct CXX0XAttributeList {
220  AttributeList *AttrList;
221  SourceRange Range;
222  bool HasAttr;
223  CXX0XAttributeList (AttributeList *attrList, SourceRange range, bool hasAttr)
224    : AttrList(attrList), Range(range), HasAttr (hasAttr) {
225  }
226  CXX0XAttributeList ()
227    : AttrList(0), Range(), HasAttr(false) {
228  }
229};
230
231}  // end namespace clang
232
233#endif
234