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