DeclSpec.h revision a5fc472b353b88be3b4981da946fb01f5a5cc0c6
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- DeclSpec.h - Parsed declaration specifiers -------------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines the classes used to store parsed information about
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// declaration-specifiers and declarators.
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)//
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//   static const int volatile x, *y, *(*(*z)[10])(const void *x);
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ------------------------- -  --  ---------------------------
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     declaration-specifiers  \  |   /
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                            declarators
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_CLANG_SEMA_DECLSPEC_H
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_SEMA_DECLSPEC_H
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Sema/AttributeList.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Sema/Ownership.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/NestedNameSpecifier.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/Token.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/ExceptionSpecificationType.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/OperatorKinds.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/Specifiers.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/SmallVector.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace clang {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ASTContext;
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class TypeLoc;
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class LangOptions;
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class Diagnostic;
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class IdentifierInfo;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NamespaceAliasDecl;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NamespaceDecl;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NestedNameSpecifier;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NestedNameSpecifierLoc;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Preprocessor;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Declarator;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct TemplateIdAnnotation;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// CXXScopeSpec - Represents a C++ nested-name-specifier or a global scope
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// specifier.  These can be in 3 states:
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///   1) Not present, identified by isEmpty()
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///   2) Present, identified by isNotEmpty()
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///      2.a) Valid, idenified by isValid()
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///      2.b) Invalid, identified by isInvalid().
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// isSet() is deprecated because it mostly corresponded to "valid" but was
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// often used as if it meant "present".
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The actual scope is described by getScopeRep().
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CXXScopeSpec {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceRange Range;
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NestedNameSpecifierLocBuilder Builder;
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccipublic:
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const SourceRange &getRange() const { return Range; }
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void setRange(const SourceRange &R) { Range = R; }
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SourceLocation getBeginLoc() const { return Range.getBegin(); }
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation getEndLoc() const { return Range.getEnd(); }
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the representation of the nested-name-specifier.
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NestedNameSpecifier *getScopeRep() const {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Builder.getRepresentation();
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Extend the current nested-name-specifier by another
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// nested-name-specifier component of the form 'type::'.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Context The AST context in which this nested-name-specifier
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// resides.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param TemplateKWLoc The location of the 'template' keyword, if present.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param TL The TypeLoc that describes the type preceding the '::'.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param ColonColonLoc The location of the trailing '::'.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              SourceLocation ColonColonLoc);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Extend the current nested-name-specifier by another
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// nested-name-specifier component of the form 'identifier::'.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Context The AST context in which this nested-name-specifier
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// resides.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Identifier The identifier.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param IdentifierLoc The location of the identifier.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param ColonColonLoc The location of the trailing '::'.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Extend(ASTContext &Context, IdentifierInfo *Identifier,
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Extend the current nested-name-specifier by another
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// nested-name-specifier component of the form 'namespace::'.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Context The AST context in which this nested-name-specifier
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// resides.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Namespace The namespace.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param NamespaceLoc The location of the namespace name.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param ColonColonLoc The location of the trailing '::'.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Extend(ASTContext &Context, NamespaceDecl *Namespace,
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Extend the current nested-name-specifier by another
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// nested-name-specifier component of the form 'namespace-alias::'.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Context The AST context in which this nested-name-specifier
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// resides.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Alias The namespace alias.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param AliasLoc The location of the namespace alias
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// name.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param ColonColonLoc The location of the trailing '::'.
129  void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
130              SourceLocation AliasLoc, SourceLocation ColonColonLoc);
131
132  /// \brief Turn this (empty) nested-name-specifier into the global
133  /// nested-name-specifier '::'.
134  void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
135
136  /// \brief Make a new nested-name-specifier from incomplete source-location
137  /// information.
138  ///
139  /// FIXME: This routine should be used very, very rarely, in cases where we
140  /// need to synthesize a nested-name-specifier. Most code should instead use
141  /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
142  void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
143                   SourceRange R);
144
145  /// \brief Adopt an existing nested-name-specifier (with source-range
146  /// information).
147  void Adopt(NestedNameSpecifierLoc Other);
148
149  /// \brief Retrieve a nested-name-specifier with location information, copied
150  /// into the given AST context.
151  ///
152  /// \param Context The context into which this nested-name-specifier will be
153  /// copied.
154  NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
155
156  /// No scope specifier.
157  bool isEmpty() const { return !Range.isValid(); }
158  /// A scope specifier is present, but may be valid or invalid.
159  bool isNotEmpty() const { return !isEmpty(); }
160
161  /// An error occured during parsing of the scope specifier.
162  bool isInvalid() const { return isNotEmpty() && getScopeRep() == 0; }
163  /// A scope specifier is present, and it refers to a real scope.
164  bool isValid() const { return isNotEmpty() && getScopeRep() != 0; }
165
166  /// \brief Indicate that this nested-name-specifier is invalid.
167  void SetInvalid(SourceRange R) {
168    assert(R.isValid() && "Must have a valid source range");
169    if (Range.getBegin().isInvalid())
170      Range.setBegin(R.getBegin());
171    Range.setEnd(R.getEnd());
172    Builder.Clear();
173  }
174
175  /// Deprecated.  Some call sites intend isNotEmpty() while others intend
176  /// isValid().
177  bool isSet() const { return getScopeRep() != 0; }
178
179  void clear() {
180    Range = SourceRange();
181    Builder.Clear();
182  }
183
184  /// \brief Retrieve the data associated with the source-location information.
185  char *location_data() const { return Builder.getBuffer().first; }
186
187  /// \brief Retrieve the size of the data associated with source-location
188  /// information.
189  unsigned location_size() const { return Builder.getBuffer().second; }
190};
191
192/// DeclSpec - This class captures information about "declaration specifiers",
193/// which encompasses storage-class-specifiers, type-specifiers,
194/// type-qualifiers, and function-specifiers.
195class DeclSpec {
196public:
197  // storage-class-specifier
198  // Note: The order of these enumerators is important for diagnostics.
199  enum SCS {
200    SCS_unspecified = 0,
201    SCS_typedef,
202    SCS_extern,
203    SCS_static,
204    SCS_auto,
205    SCS_register,
206    SCS_private_extern,
207    SCS_mutable
208  };
209
210  // Import type specifier width enumeration and constants.
211  typedef TypeSpecifierWidth TSW;
212  static const TSW TSW_unspecified = clang::TSW_unspecified;
213  static const TSW TSW_short = clang::TSW_short;
214  static const TSW TSW_long = clang::TSW_long;
215  static const TSW TSW_longlong = clang::TSW_longlong;
216
217  enum TSC {
218    TSC_unspecified,
219    TSC_imaginary,
220    TSC_complex
221  };
222
223  // Import type specifier sign enumeration and constants.
224  typedef TypeSpecifierSign TSS;
225  static const TSS TSS_unspecified = clang::TSS_unspecified;
226  static const TSS TSS_signed = clang::TSS_signed;
227  static const TSS TSS_unsigned = clang::TSS_unsigned;
228
229  // Import type specifier type enumeration and constants.
230  typedef TypeSpecifierType TST;
231  static const TST TST_unspecified = clang::TST_unspecified;
232  static const TST TST_void = clang::TST_void;
233  static const TST TST_char = clang::TST_char;
234  static const TST TST_wchar = clang::TST_wchar;
235  static const TST TST_char16 = clang::TST_char16;
236  static const TST TST_char32 = clang::TST_char32;
237  static const TST TST_int = clang::TST_int;
238  static const TST TST_float = clang::TST_float;
239  static const TST TST_double = clang::TST_double;
240  static const TST TST_bool = clang::TST_bool;
241  static const TST TST_decimal32 = clang::TST_decimal32;
242  static const TST TST_decimal64 = clang::TST_decimal64;
243  static const TST TST_decimal128 = clang::TST_decimal128;
244  static const TST TST_enum = clang::TST_enum;
245  static const TST TST_union = clang::TST_union;
246  static const TST TST_struct = clang::TST_struct;
247  static const TST TST_class = clang::TST_class;
248  static const TST TST_typename = clang::TST_typename;
249  static const TST TST_typeofType = clang::TST_typeofType;
250  static const TST TST_typeofExpr = clang::TST_typeofExpr;
251  static const TST TST_decltype = clang::TST_decltype;
252  static const TST TST_auto = clang::TST_auto;
253  static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
254  static const TST TST_error = clang::TST_error;
255
256  // type-qualifiers
257  enum TQ {   // NOTE: These flags must be kept in sync with Qualifiers::TQ.
258    TQ_unspecified = 0,
259    TQ_const       = 1,
260    TQ_restrict    = 2,
261    TQ_volatile    = 4
262  };
263
264  /// ParsedSpecifiers - Flags to query which specifiers were applied.  This is
265  /// returned by getParsedSpecifiers.
266  enum ParsedSpecifiers {
267    PQ_None                  = 0,
268    PQ_StorageClassSpecifier = 1,
269    PQ_TypeSpecifier         = 2,
270    PQ_TypeQualifier         = 4,
271    PQ_FunctionSpecifier     = 8
272  };
273
274private:
275  // storage-class-specifier
276  /*SCS*/unsigned StorageClassSpec : 3;
277  unsigned SCS_thread_specified : 1;
278  unsigned SCS_extern_in_linkage_spec : 1;
279
280  // type-specifier
281  /*TSW*/unsigned TypeSpecWidth : 2;
282  /*TSC*/unsigned TypeSpecComplex : 2;
283  /*TSS*/unsigned TypeSpecSign : 2;
284  /*TST*/unsigned TypeSpecType : 5;
285  unsigned TypeAltiVecVector : 1;
286  unsigned TypeAltiVecPixel : 1;
287  unsigned TypeAltiVecBool : 1;
288  unsigned TypeSpecOwned : 1;
289
290  // type-qualifiers
291  unsigned TypeQualifiers : 3;  // Bitwise OR of TQ.
292
293  // function-specifier
294  unsigned FS_inline_specified : 1;
295  unsigned FS_virtual_specified : 1;
296  unsigned FS_explicit_specified : 1;
297
298  // friend-specifier
299  unsigned Friend_specified : 1;
300
301  // constexpr-specifier
302  unsigned Constexpr_specified : 1;
303
304  /*SCS*/unsigned StorageClassSpecAsWritten : 3;
305
306  union {
307    UnionParsedType TypeRep;
308    Decl *DeclRep;
309    Expr *ExprRep;
310  };
311
312  // attributes.
313  ParsedAttributes Attrs;
314
315  // Scope specifier for the type spec, if applicable.
316  CXXScopeSpec TypeScope;
317
318  // List of protocol qualifiers for objective-c classes.  Used for
319  // protocol-qualified interfaces "NString<foo>" and protocol-qualified id
320  // "id<foo>".
321  Decl * const *ProtocolQualifiers;
322  unsigned NumProtocolQualifiers;
323  SourceLocation ProtocolLAngleLoc;
324  SourceLocation *ProtocolLocs;
325
326  // SourceLocation info.  These are null if the item wasn't specified or if
327  // the setting was synthesized.
328  SourceRange Range;
329
330  SourceLocation StorageClassSpecLoc, SCS_threadLoc;
331  SourceLocation TSWLoc, TSCLoc, TSSLoc, TSTLoc, AltiVecLoc;
332  /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
333  /// typename, then this is the location of the named type (if present);
334  /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
335  /// TSTNameLoc provides source range info for tag types.
336  SourceLocation TSTNameLoc;
337  SourceRange TypeofParensRange;
338  SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc;
339  SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc;
340  SourceLocation FriendLoc, ConstexprLoc;
341
342  WrittenBuiltinSpecs writtenBS;
343  void SaveWrittenBuiltinSpecs();
344  void SaveStorageSpecifierAsWritten();
345
346  static bool isTypeRep(TST T) {
347    return (T == TST_typename || T == TST_typeofType);
348  }
349  static bool isExprRep(TST T) {
350    return (T == TST_typeofExpr || T == TST_decltype);
351  }
352  static bool isDeclRep(TST T) {
353    return (T == TST_enum || T == TST_struct ||
354            T == TST_union || T == TST_class);
355  }
356
357  DeclSpec(const DeclSpec&);       // DO NOT IMPLEMENT
358  void operator=(const DeclSpec&); // DO NOT IMPLEMENT
359public:
360
361  DeclSpec(AttributeFactory &attrFactory)
362    : StorageClassSpec(SCS_unspecified),
363      SCS_thread_specified(false),
364      SCS_extern_in_linkage_spec(false),
365      TypeSpecWidth(TSW_unspecified),
366      TypeSpecComplex(TSC_unspecified),
367      TypeSpecSign(TSS_unspecified),
368      TypeSpecType(TST_unspecified),
369      TypeAltiVecVector(false),
370      TypeAltiVecPixel(false),
371      TypeAltiVecBool(false),
372      TypeSpecOwned(false),
373      TypeQualifiers(TSS_unspecified),
374      FS_inline_specified(false),
375      FS_virtual_specified(false),
376      FS_explicit_specified(false),
377      Friend_specified(false),
378      Constexpr_specified(false),
379      StorageClassSpecAsWritten(SCS_unspecified),
380      Attrs(attrFactory),
381      ProtocolQualifiers(0),
382      NumProtocolQualifiers(0),
383      ProtocolLocs(0),
384      writtenBS() {
385  }
386  ~DeclSpec() {
387    delete [] ProtocolQualifiers;
388    delete [] ProtocolLocs;
389  }
390  // storage-class-specifier
391  SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
392  bool isThreadSpecified() const { return SCS_thread_specified; }
393  bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
394  void setExternInLinkageSpec(bool Value) {
395    SCS_extern_in_linkage_spec = Value;
396  }
397
398  SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
399  SourceLocation getThreadSpecLoc() const { return SCS_threadLoc; }
400
401  void ClearStorageClassSpecs() {
402    StorageClassSpec     = DeclSpec::SCS_unspecified;
403    SCS_thread_specified = false;
404    SCS_extern_in_linkage_spec = false;
405    StorageClassSpecLoc  = SourceLocation();
406    SCS_threadLoc        = SourceLocation();
407  }
408
409  // type-specifier
410  TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
411  TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
412  TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
413  TST getTypeSpecType() const { return (TST)TypeSpecType; }
414  bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
415  bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
416  bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
417  bool isTypeSpecOwned() const { return TypeSpecOwned; }
418  ParsedType getRepAsType() const {
419    assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
420    return TypeRep;
421  }
422  Decl *getRepAsDecl() const {
423    assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
424    return DeclRep;
425  }
426  Expr *getRepAsExpr() const {
427    assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
428    return ExprRep;
429  }
430  CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
431  const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
432
433  const SourceRange &getSourceRange() const { return Range; }
434  SourceLocation getTypeSpecWidthLoc() const { return TSWLoc; }
435  SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
436  SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
437  SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
438  SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
439
440  SourceLocation getTypeSpecTypeNameLoc() const {
441    assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename);
442    return TSTNameLoc;
443  }
444
445  SourceRange getTypeofParensRange() const { return TypeofParensRange; }
446  void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
447
448  /// getSpecifierName - Turn a type-specifier-type into a string like "_Bool"
449  /// or "union".
450  static const char *getSpecifierName(DeclSpec::TST T);
451  static const char *getSpecifierName(DeclSpec::TQ Q);
452  static const char *getSpecifierName(DeclSpec::TSS S);
453  static const char *getSpecifierName(DeclSpec::TSC C);
454  static const char *getSpecifierName(DeclSpec::TSW W);
455  static const char *getSpecifierName(DeclSpec::SCS S);
456
457  // type-qualifiers
458
459  /// getTypeQualifiers - Return a set of TQs.
460  unsigned getTypeQualifiers() const { return TypeQualifiers; }
461  SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
462  SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
463  SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
464
465  // function-specifier
466  bool isInlineSpecified() const { return FS_inline_specified; }
467  SourceLocation getInlineSpecLoc() const { return FS_inlineLoc; }
468
469  bool isVirtualSpecified() const { return FS_virtual_specified; }
470  SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
471
472  bool isExplicitSpecified() const { return FS_explicit_specified; }
473  SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
474
475  void ClearFunctionSpecs() {
476    FS_inline_specified = false;
477    FS_inlineLoc = SourceLocation();
478    FS_virtual_specified = false;
479    FS_virtualLoc = SourceLocation();
480    FS_explicit_specified = false;
481    FS_explicitLoc = SourceLocation();
482  }
483
484  /// hasTypeSpecifier - Return true if any type-specifier has been found.
485  bool hasTypeSpecifier() const {
486    return getTypeSpecType() != DeclSpec::TST_unspecified ||
487           getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
488           getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
489           getTypeSpecSign() != DeclSpec::TSS_unspecified;
490  }
491
492  /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
493  /// DeclSpec includes.
494  ///
495  unsigned getParsedSpecifiers() const;
496
497  SCS getStorageClassSpecAsWritten() const {
498    return (SCS)StorageClassSpecAsWritten;
499  }
500
501  /// isEmpty - Return true if this declaration specifier is completely empty:
502  /// no tokens were parsed in the production of it.
503  bool isEmpty() const {
504    return getParsedSpecifiers() == DeclSpec::PQ_None;
505  }
506
507  void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
508  void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
509
510  /// These methods set the specified attribute of the DeclSpec and
511  /// return false if there was no error.  If an error occurs (for
512  /// example, if we tried to set "auto" on a spec with "extern"
513  /// already set), they return true and set PrevSpec and DiagID
514  /// such that
515  ///   Diag(Loc, DiagID) << PrevSpec;
516  /// will yield a useful result.
517  ///
518  /// TODO: use a more general approach that still allows these
519  /// diagnostics to be ignored when desired.
520  bool SetStorageClassSpec(SCS S, SourceLocation Loc, const char *&PrevSpec,
521                           unsigned &DiagID, const LangOptions &Lang);
522  bool SetStorageClassSpecThread(SourceLocation Loc, const char *&PrevSpec,
523                                 unsigned &DiagID);
524  bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
525                        unsigned &DiagID);
526  bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
527                          unsigned &DiagID);
528  bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
529                       unsigned &DiagID);
530  bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
531                       unsigned &DiagID);
532  bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
533                       unsigned &DiagID, ParsedType Rep);
534  bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
535                       unsigned &DiagID, Decl *Rep, bool Owned);
536  bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
537                       SourceLocation TagNameLoc, const char *&PrevSpec,
538                       unsigned &DiagID, ParsedType Rep);
539  bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
540                       SourceLocation TagNameLoc, const char *&PrevSpec,
541                       unsigned &DiagID, Decl *Rep, bool Owned);
542
543  bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
544                       unsigned &DiagID, Expr *Rep);
545  bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
546                       const char *&PrevSpec, unsigned &DiagID);
547  bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
548                       const char *&PrevSpec, unsigned &DiagID);
549  bool SetTypeSpecError();
550  void UpdateDeclRep(Decl *Rep) {
551    assert(isDeclRep((TST) TypeSpecType));
552    DeclRep = Rep;
553  }
554  void UpdateTypeRep(ParsedType Rep) {
555    assert(isTypeRep((TST) TypeSpecType));
556    TypeRep = Rep;
557  }
558  void UpdateExprRep(Expr *Rep) {
559    assert(isExprRep((TST) TypeSpecType));
560    ExprRep = Rep;
561  }
562
563  bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
564                   unsigned &DiagID, const LangOptions &Lang);
565
566  bool SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
567                             unsigned &DiagID);
568  bool SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
569                              unsigned &DiagID);
570  bool SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
571                               unsigned &DiagID);
572
573  bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
574                     unsigned &DiagID);
575
576  bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
577                        unsigned &DiagID);
578
579  bool isFriendSpecified() const { return Friend_specified; }
580  SourceLocation getFriendSpecLoc() const { return FriendLoc; }
581
582  bool isConstexprSpecified() const { return Constexpr_specified; }
583  SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
584
585  AttributePool &getAttributePool() const {
586    return Attrs.getPool();
587  }
588
589  /// AddAttributes - contatenates two attribute lists.
590  /// The GCC attribute syntax allows for the following:
591  ///
592  /// short __attribute__(( unused, deprecated ))
593  /// int __attribute__(( may_alias, aligned(16) )) var;
594  ///
595  /// This declares 4 attributes using 2 lists. The following syntax is
596  /// also allowed and equivalent to the previous declaration.
597  ///
598  /// short __attribute__((unused)) __attribute__((deprecated))
599  /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
600  ///
601  void addAttributes(AttributeList *AL) {
602    Attrs.addAll(AL);
603  }
604  void setAttributes(AttributeList *AL) {
605    Attrs.set(AL);
606  }
607
608  bool hasAttributes() const { return !Attrs.empty(); }
609
610  ParsedAttributes &getAttributes() { return Attrs; }
611  const ParsedAttributes &getAttributes() const { return Attrs; }
612
613  /// TakeAttributes - Return the current attribute list and remove them from
614  /// the DeclSpec so that it doesn't own them.
615  ParsedAttributes takeAttributes() {
616    // The non-const "copy" constructor clears the operand automatically.
617    return Attrs;
618  }
619
620  void takeAttributesFrom(ParsedAttributes &attrs) {
621    Attrs.takeAllFrom(attrs);
622  }
623
624  typedef Decl * const *ProtocolQualifierListTy;
625  ProtocolQualifierListTy getProtocolQualifiers() const {
626    return ProtocolQualifiers;
627  }
628  SourceLocation *getProtocolLocs() const { return ProtocolLocs; }
629  unsigned getNumProtocolQualifiers() const {
630    return NumProtocolQualifiers;
631  }
632  SourceLocation getProtocolLAngleLoc() const { return ProtocolLAngleLoc; }
633  void setProtocolQualifiers(Decl * const *Protos, unsigned NP,
634                             SourceLocation *ProtoLocs,
635                             SourceLocation LAngleLoc);
636
637  /// Finish - This does final analysis of the declspec, issuing diagnostics for
638  /// things like "_Imaginary" (lacking an FP type).  After calling this method,
639  /// DeclSpec is guaranteed self-consistent, even if an error occurred.
640  void Finish(Diagnostic &D, Preprocessor &PP);
641
642  const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
643    return writtenBS;
644  }
645
646  /// isMissingDeclaratorOk - This checks if this DeclSpec can stand alone,
647  /// without a Declarator. Only tag declspecs can stand alone.
648  bool isMissingDeclaratorOk();
649};
650
651/// ObjCDeclSpec - This class captures information about
652/// "declaration specifiers" specific to objective-c
653class ObjCDeclSpec {
654public:
655  /// ObjCDeclQualifier - Qualifier used on types in method declarations
656  enum ObjCDeclQualifier {
657    DQ_None = 0x0,
658    DQ_In = 0x1,
659    DQ_Inout = 0x2,
660    DQ_Out = 0x4,
661    DQ_Bycopy = 0x8,
662    DQ_Byref = 0x10,
663    DQ_Oneway = 0x20
664  };
665
666  /// PropertyAttributeKind - list of property attributes.
667  enum ObjCPropertyAttributeKind { DQ_PR_noattr = 0x0,
668    DQ_PR_readonly = 0x01,
669    DQ_PR_getter = 0x02,
670    DQ_PR_assign = 0x04,
671    DQ_PR_readwrite = 0x08,
672    DQ_PR_retain = 0x10,
673    DQ_PR_copy = 0x20,
674    DQ_PR_nonatomic = 0x40,
675    DQ_PR_setter = 0x80,
676    DQ_PR_atomic = 0x100
677  };
678
679
680  ObjCDeclSpec()
681    : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
682      GetterName(0), SetterName(0) { }
683  ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; }
684  void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
685    objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
686  }
687
688  ObjCPropertyAttributeKind getPropertyAttributes() const {
689    return ObjCPropertyAttributeKind(PropertyAttributes);
690  }
691  void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
692    PropertyAttributes =
693      (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
694  }
695
696  const IdentifierInfo *getGetterName() const { return GetterName; }
697  IdentifierInfo *getGetterName() { return GetterName; }
698  void setGetterName(IdentifierInfo *name) { GetterName = name; }
699
700  const IdentifierInfo *getSetterName() const { return SetterName; }
701  IdentifierInfo *getSetterName() { return SetterName; }
702  void setSetterName(IdentifierInfo *name) { SetterName = name; }
703private:
704  // FIXME: These two are unrelated and mutially exclusive. So perhaps
705  // we can put them in a union to reflect their mutual exclusiveness
706  // (space saving is negligible).
707  ObjCDeclQualifier objcDeclQualifier : 6;
708
709  // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
710  unsigned PropertyAttributes : 9;
711  IdentifierInfo *GetterName;    // getter name of NULL if no getter
712  IdentifierInfo *SetterName;    // setter name of NULL if no setter
713};
714
715/// \brief Represents a C++ unqualified-id that has been parsed.
716class UnqualifiedId {
717private:
718  const UnqualifiedId &operator=(const UnqualifiedId &); // DO NOT IMPLEMENT
719
720public:
721  /// \brief Describes the kind of unqualified-id parsed.
722  enum IdKind {
723    /// \brief An identifier.
724    IK_Identifier,
725    /// \brief An overloaded operator name, e.g., operator+.
726    IK_OperatorFunctionId,
727    /// \brief A conversion function name, e.g., operator int.
728    IK_ConversionFunctionId,
729    /// \brief A user-defined literal name, e.g., operator "" _i.
730    IK_LiteralOperatorId,
731    /// \brief A constructor name.
732    IK_ConstructorName,
733    /// \brief A constructor named via a template-id.
734    IK_ConstructorTemplateId,
735    /// \brief A destructor name.
736    IK_DestructorName,
737    /// \brief A template-id, e.g., f<int>.
738    IK_TemplateId
739  } Kind;
740
741  /// \brief Anonymous union that holds extra data associated with the
742  /// parsed unqualified-id.
743  union {
744    /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind
745    /// == IK_UserLiteralId, the identifier suffix.
746    IdentifierInfo *Identifier;
747
748    /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator
749    /// that we parsed.
750    struct {
751      /// \brief The kind of overloaded operator.
752      OverloadedOperatorKind Operator;
753
754      /// \brief The source locations of the individual tokens that name
755      /// the operator, e.g., the "new", "[", and "]" tokens in
756      /// operator new [].
757      ///
758      /// Different operators have different numbers of tokens in their name,
759      /// up to three. Any remaining source locations in this array will be
760      /// set to an invalid value for operators with fewer than three tokens.
761      unsigned SymbolLocations[3];
762    } OperatorFunctionId;
763
764    /// \brief When Kind == IK_ConversionFunctionId, the type that the
765    /// conversion function names.
766    UnionParsedType ConversionFunctionId;
767
768    /// \brief When Kind == IK_ConstructorName, the class-name of the type
769    /// whose constructor is being referenced.
770    UnionParsedType ConstructorName;
771
772    /// \brief When Kind == IK_DestructorName, the type referred to by the
773    /// class-name.
774    UnionParsedType DestructorName;
775
776    /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId,
777    /// the template-id annotation that contains the template name and
778    /// template arguments.
779    TemplateIdAnnotation *TemplateId;
780  };
781
782  /// \brief The location of the first token that describes this unqualified-id,
783  /// which will be the location of the identifier, "operator" keyword,
784  /// tilde (for a destructor), or the template name of a template-id.
785  SourceLocation StartLocation;
786
787  /// \brief The location of the last token that describes this unqualified-id.
788  SourceLocation EndLocation;
789
790  UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { }
791
792  /// \brief Do not use this copy constructor. It is temporary, and only
793  /// exists because we are holding FieldDeclarators in a SmallVector when we
794  /// don't actually need them.
795  ///
796  /// FIXME: Kill this copy constructor.
797  UnqualifiedId(const UnqualifiedId &Other)
798    : Kind(IK_Identifier), Identifier(Other.Identifier),
799      StartLocation(Other.StartLocation), EndLocation(Other.EndLocation) {
800    assert(Other.Kind == IK_Identifier && "Cannot copy non-identifiers");
801  }
802
803  /// \brief Destroy this unqualified-id.
804  ~UnqualifiedId() { clear(); }
805
806  /// \brief Clear out this unqualified-id, setting it to default (invalid)
807  /// state.
808  void clear();
809
810  /// \brief Determine whether this unqualified-id refers to a valid name.
811  bool isValid() const { return StartLocation.isValid(); }
812
813  /// \brief Determine whether this unqualified-id refers to an invalid name.
814  bool isInvalid() const { return !isValid(); }
815
816  /// \brief Determine what kind of name we have.
817  IdKind getKind() const { return Kind; }
818
819  /// \brief Specify that this unqualified-id was parsed as an identifier.
820  ///
821  /// \param Id the parsed identifier.
822  /// \param IdLoc the location of the parsed identifier.
823  void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
824    Kind = IK_Identifier;
825    Identifier = const_cast<IdentifierInfo *>(Id);
826    StartLocation = EndLocation = IdLoc;
827  }
828
829  /// \brief Specify that this unqualified-id was parsed as an
830  /// operator-function-id.
831  ///
832  /// \param OperatorLoc the location of the 'operator' keyword.
833  ///
834  /// \param Op the overloaded operator.
835  ///
836  /// \param SymbolLocations the locations of the individual operator symbols
837  /// in the operator.
838  void setOperatorFunctionId(SourceLocation OperatorLoc,
839                             OverloadedOperatorKind Op,
840                             SourceLocation SymbolLocations[3]);
841
842  /// \brief Specify that this unqualified-id was parsed as a
843  /// conversion-function-id.
844  ///
845  /// \param OperatorLoc the location of the 'operator' keyword.
846  ///
847  /// \param Ty the type to which this conversion function is converting.
848  ///
849  /// \param EndLoc the location of the last token that makes up the type name.
850  void setConversionFunctionId(SourceLocation OperatorLoc,
851                               ParsedType Ty,
852                               SourceLocation EndLoc) {
853    Kind = IK_ConversionFunctionId;
854    StartLocation = OperatorLoc;
855    EndLocation = EndLoc;
856    ConversionFunctionId = Ty;
857  }
858
859  /// \brief Specific that this unqualified-id was parsed as a
860  /// literal-operator-id.
861  ///
862  /// \param Id the parsed identifier.
863  ///
864  /// \param OpLoc the location of the 'operator' keyword.
865  ///
866  /// \param IdLoc the location of the identifier.
867  void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
868                              SourceLocation IdLoc) {
869    Kind = IK_LiteralOperatorId;
870    Identifier = const_cast<IdentifierInfo *>(Id);
871    StartLocation = OpLoc;
872    EndLocation = IdLoc;
873  }
874
875  /// \brief Specify that this unqualified-id was parsed as a constructor name.
876  ///
877  /// \param ClassType the class type referred to by the constructor name.
878  ///
879  /// \param ClassNameLoc the location of the class name.
880  ///
881  /// \param EndLoc the location of the last token that makes up the type name.
882  void setConstructorName(ParsedType ClassType,
883                          SourceLocation ClassNameLoc,
884                          SourceLocation EndLoc) {
885    Kind = IK_ConstructorName;
886    StartLocation = ClassNameLoc;
887    EndLocation = EndLoc;
888    ConstructorName = ClassType;
889  }
890
891  /// \brief Specify that this unqualified-id was parsed as a
892  /// template-id that names a constructor.
893  ///
894  /// \param TemplateId the template-id annotation that describes the parsed
895  /// template-id. This UnqualifiedId instance will take ownership of the
896  /// \p TemplateId and will free it on destruction.
897  void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
898
899  /// \brief Specify that this unqualified-id was parsed as a destructor name.
900  ///
901  /// \param TildeLoc the location of the '~' that introduces the destructor
902  /// name.
903  ///
904  /// \param ClassType the name of the class referred to by the destructor name.
905  void setDestructorName(SourceLocation TildeLoc,
906                         ParsedType ClassType,
907                         SourceLocation EndLoc) {
908    Kind = IK_DestructorName;
909    StartLocation = TildeLoc;
910    EndLocation = EndLoc;
911    DestructorName = ClassType;
912  }
913
914  /// \brief Specify that this unqualified-id was parsed as a template-id.
915  ///
916  /// \param TemplateId the template-id annotation that describes the parsed
917  /// template-id. This UnqualifiedId instance will take ownership of the
918  /// \p TemplateId and will free it on destruction.
919  void setTemplateId(TemplateIdAnnotation *TemplateId);
920
921  /// \brief Return the source range that covers this unqualified-id.
922  SourceRange getSourceRange() const {
923    return SourceRange(StartLocation, EndLocation);
924  }
925};
926
927/// CachedTokens - A set of tokens that has been cached for later
928/// parsing.
929typedef llvm::SmallVector<Token, 4> CachedTokens;
930
931/// DeclaratorChunk - One instance of this struct is used for each type in a
932/// declarator that is parsed.
933///
934/// This is intended to be a small value object.
935struct DeclaratorChunk {
936  enum {
937    Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren
938  } Kind;
939
940  /// Loc - The place where this type was defined.
941  SourceLocation Loc;
942  /// EndLoc - If valid, the place where this chunck ends.
943  SourceLocation EndLoc;
944
945  struct TypeInfoCommon {
946    AttributeList *AttrList;
947  };
948
949  struct PointerTypeInfo : TypeInfoCommon {
950    /// The type qualifiers: const/volatile/restrict.
951    unsigned TypeQuals : 3;
952
953    /// The location of the const-qualifier, if any.
954    unsigned ConstQualLoc;
955
956    /// The location of the volatile-qualifier, if any.
957    unsigned VolatileQualLoc;
958
959    /// The location of the restrict-qualifier, if any.
960    unsigned RestrictQualLoc;
961
962    void destroy() {
963    }
964  };
965
966  struct ReferenceTypeInfo : TypeInfoCommon {
967    /// The type qualifier: restrict. [GNU] C++ extension
968    bool HasRestrict : 1;
969    /// True if this is an lvalue reference, false if it's an rvalue reference.
970    bool LValueRef : 1;
971    void destroy() {
972    }
973  };
974
975  struct ArrayTypeInfo : TypeInfoCommon {
976    /// The type qualifiers for the array: const/volatile/restrict.
977    unsigned TypeQuals : 3;
978
979    /// True if this dimension included the 'static' keyword.
980    bool hasStatic : 1;
981
982    /// True if this dimension was [*].  In this case, NumElts is null.
983    bool isStar : 1;
984
985    /// This is the size of the array, or null if [] or [*] was specified.
986    /// Since the parser is multi-purpose, and we don't want to impose a root
987    /// expression class on all clients, NumElts is untyped.
988    Expr *NumElts;
989
990    void destroy() {}
991  };
992
993  /// ParamInfo - An array of paraminfo objects is allocated whenever a function
994  /// declarator is parsed.  There are two interesting styles of arguments here:
995  /// K&R-style identifier lists and parameter type lists.  K&R-style identifier
996  /// lists will have information about the identifier, but no type information.
997  /// Parameter type lists will have type info (if the actions module provides
998  /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
999  struct ParamInfo {
1000    IdentifierInfo *Ident;
1001    SourceLocation IdentLoc;
1002    Decl *Param;
1003
1004    /// DefaultArgTokens - When the parameter's default argument
1005    /// cannot be parsed immediately (because it occurs within the
1006    /// declaration of a member function), it will be stored here as a
1007    /// sequence of tokens to be parsed once the class definition is
1008    /// complete. Non-NULL indicates that there is a default argument.
1009    CachedTokens *DefaultArgTokens;
1010
1011    ParamInfo() {}
1012    ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
1013              Decl *param,
1014              CachedTokens *DefArgTokens = 0)
1015      : Ident(ident), IdentLoc(iloc), Param(param),
1016        DefaultArgTokens(DefArgTokens) {}
1017  };
1018
1019  struct TypeAndRange {
1020    ParsedType Ty;
1021    SourceRange Range;
1022  };
1023
1024  struct FunctionTypeInfo : TypeInfoCommon {
1025    /// hasPrototype - This is true if the function had at least one typed
1026    /// argument.  If the function is () or (a,b,c), then it has no prototype,
1027    /// and is treated as a K&R-style function.
1028    unsigned hasPrototype : 1;
1029
1030    /// isVariadic - If this function has a prototype, and if that
1031    /// proto ends with ',...)', this is true. When true, EllipsisLoc
1032    /// contains the location of the ellipsis.
1033    unsigned isVariadic : 1;
1034
1035    /// \brief Whether the ref-qualifier (if any) is an lvalue reference.
1036    /// Otherwise, it's an rvalue reference.
1037    unsigned RefQualifierIsLValueRef : 1;
1038
1039    /// The type qualifiers: const/volatile/restrict.
1040    /// The qualifier bitmask values are the same as in QualType.
1041    unsigned TypeQuals : 3;
1042
1043    /// ExceptionSpecType - An ExceptionSpecificationType value.
1044    unsigned ExceptionSpecType : 3;
1045
1046    /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo.
1047    unsigned DeleteArgInfo : 1;
1048
1049    /// When isVariadic is true, the location of the ellipsis in the source.
1050    unsigned EllipsisLoc;
1051
1052    /// NumArgs - This is the number of formal arguments provided for the
1053    /// declarator.
1054    unsigned NumArgs;
1055
1056    /// NumExceptions - This is the number of types in the dynamic-exception-
1057    /// decl, if the function has one.
1058    unsigned NumExceptions;
1059
1060    /// \brief The location of the ref-qualifier, if any.
1061    ///
1062    /// If this is an invalid location, there is no ref-qualifier.
1063    unsigned RefQualifierLoc;
1064
1065    /// \brief When ExceptionSpecType isn't EST_None, the location of the
1066    /// keyword introducing the spec.
1067    unsigned ExceptionSpecLoc;
1068
1069    /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that
1070    /// describe the arguments for this function declarator.  This is null if
1071    /// there are no arguments specified.
1072    ParamInfo *ArgInfo;
1073
1074    union {
1075      /// \brief Pointer to a new[]'d array of TypeAndRange objects that
1076      /// contain the types in the function's dynamic exception specification
1077      /// and their locations, if there is one.
1078      TypeAndRange *Exceptions;
1079
1080      /// \brief Pointer to the expression in the noexcept-specifier of this
1081      /// function, if it has one.
1082      Expr *NoexceptExpr;
1083    };
1084
1085    /// TrailingReturnType - If this isn't null, it's the trailing return type
1086    /// specified. This is actually a ParsedType, but stored as void* to
1087    /// allow union storage.
1088    void *TrailingReturnType;
1089
1090    /// freeArgs - reset the argument list to having zero arguments.  This is
1091    /// used in various places for error recovery.
1092    void freeArgs() {
1093      if (DeleteArgInfo) {
1094        delete[] ArgInfo;
1095        DeleteArgInfo = false;
1096      }
1097      NumArgs = 0;
1098    }
1099
1100    void destroy() {
1101      if (DeleteArgInfo)
1102        delete[] ArgInfo;
1103      if (getExceptionSpecType() == EST_Dynamic)
1104        delete[] Exceptions;
1105    }
1106
1107    /// isKNRPrototype - Return true if this is a K&R style identifier list,
1108    /// like "void foo(a,b,c)".  In a function definition, this will be followed
1109    /// by the argument type definitions.
1110    bool isKNRPrototype() const {
1111      return !hasPrototype && NumArgs != 0;
1112    }
1113
1114    SourceLocation getEllipsisLoc() const {
1115      return SourceLocation::getFromRawEncoding(EllipsisLoc);
1116    }
1117    SourceLocation getExceptionSpecLoc() const {
1118      return SourceLocation::getFromRawEncoding(ExceptionSpecLoc);
1119    }
1120
1121    /// \brief Retrieve the location of the ref-qualifier, if any.
1122    SourceLocation getRefQualifierLoc() const {
1123      return SourceLocation::getFromRawEncoding(RefQualifierLoc);
1124    }
1125
1126    /// \brief Determine whether this function declaration contains a
1127    /// ref-qualifier.
1128    bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1129
1130    /// \brief Get the type of exception specification this function has.
1131    ExceptionSpecificationType getExceptionSpecType() const {
1132      return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1133    }
1134  };
1135
1136  struct BlockPointerTypeInfo : TypeInfoCommon {
1137    /// For now, sema will catch these as invalid.
1138    /// The type qualifiers: const/volatile/restrict.
1139    unsigned TypeQuals : 3;
1140
1141    void destroy() {
1142    }
1143  };
1144
1145  struct MemberPointerTypeInfo : TypeInfoCommon {
1146    /// The type qualifiers: const/volatile/restrict.
1147    unsigned TypeQuals : 3;
1148    // CXXScopeSpec has a constructor, so it can't be a direct member.
1149    // So we need some pointer-aligned storage and a bit of trickery.
1150    union {
1151      void *Aligner;
1152      char Mem[sizeof(CXXScopeSpec)];
1153    } ScopeMem;
1154    CXXScopeSpec &Scope() {
1155      return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem);
1156    }
1157    const CXXScopeSpec &Scope() const {
1158      return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem);
1159    }
1160    void destroy() {
1161      Scope().~CXXScopeSpec();
1162    }
1163  };
1164
1165  union {
1166    TypeInfoCommon        Common;
1167    PointerTypeInfo       Ptr;
1168    ReferenceTypeInfo     Ref;
1169    ArrayTypeInfo         Arr;
1170    FunctionTypeInfo      Fun;
1171    BlockPointerTypeInfo  Cls;
1172    MemberPointerTypeInfo Mem;
1173  };
1174
1175  void destroy() {
1176    switch (Kind) {
1177    default: assert(0 && "Unknown decl type!");
1178    case DeclaratorChunk::Function:      return Fun.destroy();
1179    case DeclaratorChunk::Pointer:       return Ptr.destroy();
1180    case DeclaratorChunk::BlockPointer:  return Cls.destroy();
1181    case DeclaratorChunk::Reference:     return Ref.destroy();
1182    case DeclaratorChunk::Array:         return Arr.destroy();
1183    case DeclaratorChunk::MemberPointer: return Mem.destroy();
1184    case DeclaratorChunk::Paren:         return;
1185    }
1186  }
1187
1188  /// getAttrs - If there are attributes applied to this declaratorchunk, return
1189  /// them.
1190  const AttributeList *getAttrs() const {
1191    return Common.AttrList;
1192  }
1193
1194  AttributeList *&getAttrListRef() {
1195    return Common.AttrList;
1196  }
1197
1198  /// getPointer - Return a DeclaratorChunk for a pointer.
1199  ///
1200  static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1201                                    SourceLocation ConstQualLoc,
1202                                    SourceLocation VolatileQualLoc,
1203                                    SourceLocation RestrictQualLoc) {
1204    DeclaratorChunk I;
1205    I.Kind                = Pointer;
1206    I.Loc                 = Loc;
1207    I.Ptr.TypeQuals       = TypeQuals;
1208    I.Ptr.ConstQualLoc    = ConstQualLoc.getRawEncoding();
1209    I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
1210    I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
1211    I.Ptr.AttrList        = 0;
1212    return I;
1213  }
1214
1215  /// getReference - Return a DeclaratorChunk for a reference.
1216  ///
1217  static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1218                                      bool lvalue) {
1219    DeclaratorChunk I;
1220    I.Kind            = Reference;
1221    I.Loc             = Loc;
1222    I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1223    I.Ref.LValueRef   = lvalue;
1224    I.Ref.AttrList    = 0;
1225    return I;
1226  }
1227
1228  /// getArray - Return a DeclaratorChunk for an array.
1229  ///
1230  static DeclaratorChunk getArray(unsigned TypeQuals,
1231                                  bool isStatic, bool isStar, Expr *NumElts,
1232                                  SourceLocation LBLoc, SourceLocation RBLoc) {
1233    DeclaratorChunk I;
1234    I.Kind          = Array;
1235    I.Loc           = LBLoc;
1236    I.EndLoc        = RBLoc;
1237    I.Arr.AttrList  = 0;
1238    I.Arr.TypeQuals = TypeQuals;
1239    I.Arr.hasStatic = isStatic;
1240    I.Arr.isStar    = isStar;
1241    I.Arr.NumElts   = NumElts;
1242    return I;
1243  }
1244
1245  /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1246  /// "TheDeclarator" is the declarator that this will be added to.
1247  static DeclaratorChunk getFunction(bool hasProto, bool isVariadic,
1248                                     SourceLocation EllipsisLoc,
1249                                     ParamInfo *ArgInfo, unsigned NumArgs,
1250                                     unsigned TypeQuals,
1251                                     bool RefQualifierIsLvalueRef,
1252                                     SourceLocation RefQualifierLoc,
1253                                     ExceptionSpecificationType ESpecType,
1254                                     SourceLocation ESpecLoc,
1255                                     ParsedType *Exceptions,
1256                                     SourceRange *ExceptionRanges,
1257                                     unsigned NumExceptions,
1258                                     Expr *NoexceptExpr,
1259                                     SourceLocation LocalRangeBegin,
1260                                     SourceLocation LocalRangeEnd,
1261                                     Declarator &TheDeclarator,
1262                                     ParsedType TrailingReturnType =
1263                                                    ParsedType());
1264
1265  /// getBlockPointer - Return a DeclaratorChunk for a block.
1266  ///
1267  static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1268                                         SourceLocation Loc) {
1269    DeclaratorChunk I;
1270    I.Kind          = BlockPointer;
1271    I.Loc           = Loc;
1272    I.Cls.TypeQuals = TypeQuals;
1273    I.Cls.AttrList  = 0;
1274    return I;
1275  }
1276
1277  static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1278                                          unsigned TypeQuals,
1279                                          SourceLocation Loc) {
1280    DeclaratorChunk I;
1281    I.Kind          = MemberPointer;
1282    I.Loc           = Loc;
1283    I.Mem.TypeQuals = TypeQuals;
1284    I.Mem.AttrList  = 0;
1285    new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS);
1286    return I;
1287  }
1288
1289  /// getParen - Return a DeclaratorChunk for a paren.
1290  ///
1291  static DeclaratorChunk getParen(SourceLocation LParenLoc,
1292                                  SourceLocation RParenLoc) {
1293    DeclaratorChunk I;
1294    I.Kind          = Paren;
1295    I.Loc           = LParenLoc;
1296    I.EndLoc        = RParenLoc;
1297    I.Common.AttrList = 0;
1298    return I;
1299  }
1300
1301};
1302
1303/// Declarator - Information about one declarator, including the parsed type
1304/// information and the identifier.  When the declarator is fully formed, this
1305/// is turned into the appropriate Decl object.
1306///
1307/// Declarators come in two types: normal declarators and abstract declarators.
1308/// Abstract declarators are used when parsing types, and don't have an
1309/// identifier.  Normal declarators do have ID's.
1310///
1311/// Instances of this class should be a transient object that lives on the
1312/// stack, not objects that are allocated in large quantities on the heap.
1313class Declarator {
1314public:
1315  enum TheContext {
1316    FileContext,         // File scope declaration.
1317    PrototypeContext,    // Within a function prototype.
1318    ObjCPrototypeContext,// Within a method prototype.
1319    KNRTypeListContext,  // K&R type definition list for formals.
1320    TypeNameContext,     // Abstract declarator for types.
1321    MemberContext,       // Struct/Union field.
1322    BlockContext,        // Declaration within a block in a function.
1323    ForContext,          // Declaration within first part of a for loop.
1324    ConditionContext,    // Condition declaration in a C++ if/switch/while/for.
1325    TemplateParamContext,// Within a template parameter list.
1326    CXXCatchContext,     // C++ catch exception-declaration
1327    BlockLiteralContext,  // Block literal declarator.
1328    TemplateTypeArgContext // Template type argument.
1329  };
1330
1331private:
1332  const DeclSpec &DS;
1333  CXXScopeSpec SS;
1334  UnqualifiedId Name;
1335  SourceRange Range;
1336
1337  /// Context - Where we are parsing this declarator.
1338  ///
1339  TheContext Context;
1340
1341  /// DeclTypeInfo - This holds each type that the declarator includes as it is
1342  /// parsed.  This is pushed from the identifier out, which means that element
1343  /// #0 will be the most closely bound to the identifier, and
1344  /// DeclTypeInfo.back() will be the least closely bound.
1345  llvm::SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1346
1347  /// InvalidType - Set by Sema::GetTypeForDeclarator().
1348  bool InvalidType : 1;
1349
1350  /// GroupingParens - Set by Parser::ParseParenDeclarator().
1351  bool GroupingParens : 1;
1352
1353  /// Attrs - Attributes.
1354  ParsedAttributes Attrs;
1355
1356  /// AsmLabel - The asm label, if specified.
1357  Expr *AsmLabel;
1358
1359  /// InlineParams - This is a local array used for the first function decl
1360  /// chunk to avoid going to the heap for the common case when we have one
1361  /// function chunk in the declarator.
1362  DeclaratorChunk::ParamInfo InlineParams[16];
1363  bool InlineParamsUsed;
1364
1365  /// Extension - true if the declaration is preceded by __extension__.
1366  bool Extension : 1;
1367
1368  /// \brief If provided, the source location of the ellipsis used to describe
1369  /// this declarator as a parameter pack.
1370  SourceLocation EllipsisLoc;
1371
1372  friend struct DeclaratorChunk;
1373
1374public:
1375  Declarator(const DeclSpec &ds, TheContext C)
1376    : DS(ds), Range(ds.getSourceRange()), Context(C),
1377      InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1378      GroupingParens(false), Attrs(ds.getAttributePool().getFactory()),
1379      AsmLabel(0), InlineParamsUsed(false), Extension(false) {
1380  }
1381
1382  ~Declarator() {
1383    clear();
1384  }
1385
1386  /// getDeclSpec - Return the declaration-specifier that this declarator was
1387  /// declared with.
1388  const DeclSpec &getDeclSpec() const { return DS; }
1389
1390  /// getMutableDeclSpec - Return a non-const version of the DeclSpec.  This
1391  /// should be used with extreme care: declspecs can often be shared between
1392  /// multiple declarators, so mutating the DeclSpec affects all of the
1393  /// Declarators.  This should only be done when the declspec is known to not
1394  /// be shared or when in error recovery etc.
1395  DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1396
1397  AttributePool &getAttributePool() const {
1398    return Attrs.getPool();
1399  }
1400
1401  /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1402  /// nested-name-specifier) that is part of the declarator-id.
1403  const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1404  CXXScopeSpec &getCXXScopeSpec() { return SS; }
1405
1406  /// \brief Retrieve the name specified by this declarator.
1407  UnqualifiedId &getName() { return Name; }
1408
1409  TheContext getContext() const { return Context; }
1410
1411  bool isPrototypeContext() const {
1412    return (Context == PrototypeContext || Context == ObjCPrototypeContext);
1413  }
1414
1415  /// getSourceRange - Get the source range that spans this declarator.
1416  const SourceRange &getSourceRange() const { return Range; }
1417
1418  void SetSourceRange(SourceRange R) { Range = R; }
1419  /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1420  /// invalid.
1421  void SetRangeBegin(SourceLocation Loc) {
1422    if (!Loc.isInvalid())
1423      Range.setBegin(Loc);
1424  }
1425  /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1426  void SetRangeEnd(SourceLocation Loc) {
1427    if (!Loc.isInvalid())
1428      Range.setEnd(Loc);
1429  }
1430  /// ExtendWithDeclSpec - Extend the declarator source range to include the
1431  /// given declspec, unless its location is invalid. Adopts the range start if
1432  /// the current range start is invalid.
1433  void ExtendWithDeclSpec(const DeclSpec &DS) {
1434    const SourceRange &SR = DS.getSourceRange();
1435    if (Range.getBegin().isInvalid())
1436      Range.setBegin(SR.getBegin());
1437    if (!SR.getEnd().isInvalid())
1438      Range.setEnd(SR.getEnd());
1439  }
1440
1441  /// clear - Reset the contents of this Declarator.
1442  void clear() {
1443    SS.clear();
1444    Name.clear();
1445    Range = DS.getSourceRange();
1446
1447    for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1448      DeclTypeInfo[i].destroy();
1449    DeclTypeInfo.clear();
1450    Attrs.clear();
1451    AsmLabel = 0;
1452    InlineParamsUsed = false;
1453  }
1454
1455  /// mayOmitIdentifier - Return true if the identifier is either optional or
1456  /// not allowed.  This is true for typenames, prototypes, and template
1457  /// parameter lists.
1458  bool mayOmitIdentifier() const {
1459    switch (Context) {
1460    case FileContext:
1461    case KNRTypeListContext:
1462    case MemberContext:
1463    case BlockContext:
1464    case ForContext:
1465    case ConditionContext:
1466      return false;
1467
1468    case TypeNameContext:
1469    case PrototypeContext:
1470    case ObjCPrototypeContext:
1471    case TemplateParamContext:
1472    case CXXCatchContext:
1473    case BlockLiteralContext:
1474    case TemplateTypeArgContext:
1475      return true;
1476    }
1477    llvm_unreachable("unknown context kind!");
1478  }
1479
1480  /// mayHaveIdentifier - Return true if the identifier is either optional or
1481  /// required.  This is true for normal declarators and prototypes, but not
1482  /// typenames.
1483  bool mayHaveIdentifier() const {
1484    switch (Context) {
1485    case FileContext:
1486    case KNRTypeListContext:
1487    case MemberContext:
1488    case BlockContext:
1489    case ForContext:
1490    case ConditionContext:
1491    case PrototypeContext:
1492    case TemplateParamContext:
1493    case CXXCatchContext:
1494      return true;
1495
1496    case TypeNameContext:
1497    case ObjCPrototypeContext:
1498    case BlockLiteralContext:
1499    case TemplateTypeArgContext:
1500      return false;
1501    }
1502    llvm_unreachable("unknown context kind!");
1503  }
1504
1505  /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
1506  /// followed by a C++ direct initializer, e.g. "int x(1);".
1507  bool mayBeFollowedByCXXDirectInit() const {
1508    if (hasGroupingParens()) return false;
1509
1510    switch (Context) {
1511    case FileContext:
1512    case BlockContext:
1513    case ForContext:
1514      return true;
1515
1516    case KNRTypeListContext:
1517    case MemberContext:
1518    case ConditionContext:
1519    case PrototypeContext:
1520    case ObjCPrototypeContext:
1521    case TemplateParamContext:
1522    case CXXCatchContext:
1523    case TypeNameContext:
1524    case BlockLiteralContext:
1525    case TemplateTypeArgContext:
1526      return false;
1527    }
1528    llvm_unreachable("unknown context kind!");
1529  }
1530
1531  /// isPastIdentifier - Return true if we have parsed beyond the point where
1532  /// the
1533  bool isPastIdentifier() const { return Name.isValid(); }
1534
1535  /// hasName - Whether this declarator has a name, which might be an
1536  /// identifier (accessible via getIdentifier()) or some kind of
1537  /// special C++ name (constructor, destructor, etc.).
1538  bool hasName() const {
1539    return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier;
1540  }
1541
1542  IdentifierInfo *getIdentifier() const {
1543    if (Name.getKind() == UnqualifiedId::IK_Identifier)
1544      return Name.Identifier;
1545
1546    return 0;
1547  }
1548  SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
1549
1550  /// \brief Set the name of this declarator to be the given identifier.
1551  void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
1552    Name.setIdentifier(Id, IdLoc);
1553  }
1554
1555  /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
1556  /// EndLoc, which should be the last token of the chunk.
1557  void AddTypeInfo(const DeclaratorChunk &TI,
1558                   ParsedAttributes &attrs,
1559                   SourceLocation EndLoc) {
1560    DeclTypeInfo.push_back(TI);
1561    DeclTypeInfo.back().getAttrListRef() = attrs.getList();
1562    getAttributePool().takeAllFrom(attrs.getPool());
1563
1564    if (!EndLoc.isInvalid())
1565      SetRangeEnd(EndLoc);
1566  }
1567
1568  /// AddInnermostTypeInfo - Add a new innermost chunk to this declarator.
1569  void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
1570    DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
1571  }
1572
1573  /// getNumTypeObjects() - Return the number of types applied to this
1574  /// declarator.
1575  unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
1576
1577  /// Return the specified TypeInfo from this declarator.  TypeInfo #0 is
1578  /// closest to the identifier.
1579  const DeclaratorChunk &getTypeObject(unsigned i) const {
1580    assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1581    return DeclTypeInfo[i];
1582  }
1583  DeclaratorChunk &getTypeObject(unsigned i) {
1584    assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1585    return DeclTypeInfo[i];
1586  }
1587
1588  void DropFirstTypeObject()
1589  {
1590    assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
1591    DeclTypeInfo.front().destroy();
1592    DeclTypeInfo.erase(DeclTypeInfo.begin());
1593  }
1594
1595  /// isFunctionDeclarator - This method returns true if the declarator
1596  /// is a function declarator (looking through parentheses).
1597  /// If true is returned, then the reference type parameter idx is
1598  /// assigned with the index of the declaration chunk.
1599  bool isFunctionDeclarator(unsigned& idx) const {
1600    for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
1601      switch (DeclTypeInfo[i].Kind) {
1602      case DeclaratorChunk::Function:
1603        idx = i;
1604        return true;
1605      case DeclaratorChunk::Paren:
1606        continue;
1607      case DeclaratorChunk::Pointer:
1608      case DeclaratorChunk::Reference:
1609      case DeclaratorChunk::Array:
1610      case DeclaratorChunk::BlockPointer:
1611      case DeclaratorChunk::MemberPointer:
1612        return false;
1613      }
1614      llvm_unreachable("Invalid type chunk");
1615      return false;
1616    }
1617    return false;
1618  }
1619
1620  /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
1621  /// this method returns true if the identifier is a function declarator
1622  /// (looking through parentheses).
1623  bool isFunctionDeclarator() const {
1624    unsigned index;
1625    return isFunctionDeclarator(index);
1626  }
1627
1628  /// getFunctionTypeInfo - Retrieves the function type info object
1629  /// (looking through parentheses).
1630  DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
1631    assert(isFunctionDeclarator() && "Not a function declarator!");
1632    unsigned index = 0;
1633    isFunctionDeclarator(index);
1634    return DeclTypeInfo[index].Fun;
1635  }
1636
1637  /// getFunctionTypeInfo - Retrieves the function type info object
1638  /// (looking through parentheses).
1639  const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
1640    return const_cast<Declarator*>(this)->getFunctionTypeInfo();
1641  }
1642
1643  /// takeAttributes - Takes attributes from the given parsed-attributes
1644  /// set and add them to this declarator.
1645  ///
1646  /// These examples both add 3 attributes to "var":
1647  ///  short int var __attribute__((aligned(16),common,deprecated));
1648  ///  short int x, __attribute__((aligned(16)) var
1649  ///                                 __attribute__((common,deprecated));
1650  ///
1651  /// Also extends the range of the declarator.
1652  void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
1653    Attrs.takeAllFrom(attrs);
1654
1655    if (!lastLoc.isInvalid())
1656      SetRangeEnd(lastLoc);
1657  }
1658
1659  const AttributeList *getAttributes() const { return Attrs.getList(); }
1660  AttributeList *getAttributes() { return Attrs.getList(); }
1661
1662  AttributeList *&getAttrListRef() { return Attrs.getListRef(); }
1663
1664  /// hasAttributes - do we contain any attributes?
1665  bool hasAttributes() const {
1666    if (getAttributes() || getDeclSpec().hasAttributes()) return true;
1667    for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
1668      if (getTypeObject(i).getAttrs())
1669        return true;
1670    return false;
1671  }
1672
1673  void setAsmLabel(Expr *E) { AsmLabel = E; }
1674  Expr *getAsmLabel() const { return AsmLabel; }
1675
1676  void setExtension(bool Val = true) { Extension = Val; }
1677  bool getExtension() const { return Extension; }
1678
1679  void setInvalidType(bool Val = true) { InvalidType = Val; }
1680  bool isInvalidType() const {
1681    return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
1682  }
1683
1684  void setGroupingParens(bool flag) { GroupingParens = flag; }
1685  bool hasGroupingParens() const { return GroupingParens; }
1686
1687  bool hasEllipsis() const { return EllipsisLoc.isValid(); }
1688  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
1689  void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
1690};
1691
1692/// FieldDeclarator - This little struct is used to capture information about
1693/// structure field declarators, which is basically just a bitfield size.
1694struct FieldDeclarator {
1695  Declarator D;
1696  Expr *BitfieldSize;
1697  explicit FieldDeclarator(DeclSpec &DS) : D(DS, Declarator::MemberContext) {
1698    BitfieldSize = 0;
1699  }
1700};
1701
1702/// VirtSpecifiers - Represents a C++0x virt-specifier-seq.
1703class VirtSpecifiers {
1704public:
1705  enum Specifier {
1706    VS_None = 0,
1707    VS_Override = 1,
1708    VS_Final = 2
1709  };
1710
1711  VirtSpecifiers() : Specifiers(0) { }
1712
1713  bool SetSpecifier(Specifier VS, SourceLocation Loc,
1714                    const char *&PrevSpec);
1715
1716  bool isOverrideSpecified() const { return Specifiers & VS_Override; }
1717  SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
1718
1719  bool isFinalSpecified() const { return Specifiers & VS_Final; }
1720  SourceLocation getFinalLoc() const { return VS_finalLoc; }
1721
1722  void clear() { Specifiers = 0; }
1723
1724  static const char *getSpecifierName(Specifier VS);
1725
1726  SourceLocation getLastLocation() const { return LastLocation; }
1727
1728private:
1729  unsigned Specifiers;
1730
1731  SourceLocation VS_overrideLoc, VS_finalLoc;
1732  SourceLocation LastLocation;
1733};
1734
1735} // end namespace clang
1736
1737#endif
1738