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