Warning: That file was not part of the compilation database. It may have many parsing errors.

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/// 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/AST/NestedNameSpecifier.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 "clang/Lex/Token.h"
32#include "clang/Sema/Ownership.h"
33#include "clang/Sema/ParsedAttr.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"
37
38namespace clang {
39 class ASTContext;
40 class CXXRecordDecl;
41 class TypeLoc;
42 class LangOptions;
43 class IdentifierInfo;
44 class NamespaceAliasDecl;
45 class NamespaceDecl;
46 class ObjCDeclSpec;
47 class Sema;
48 class Declarator;
49 struct TemplateIdAnnotation;
50
51/// Represents a C++ nested-name-specifier or a global scope specifier.
52///
53/// These can be in 3 states:
54/// 1) Not present, identified by isEmpty()
55/// 2) Present, identified by isNotEmpty()
56/// 2.a) Valid, identified by isValid()
57/// 2.b) Invalid, identified by isInvalid().
58///
59/// isSet() is deprecated because it mostly corresponded to "valid" but was
60/// often used as if it meant "present".
61///
62/// The actual scope is described by getScopeRep().
63class CXXScopeSpec {
64 SourceRange Range;
65 NestedNameSpecifierLocBuilder Builder;
66
67public:
68 SourceRange getRange() const { return Range; }
69 void setRange(SourceRange R) { Range = R; }
70 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
71 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
72 SourceLocation getBeginLoc() const { return Range.getBegin(); }
73 SourceLocation getEndLoc() const { return Range.getEnd(); }
74
75 /// Retrieve the representation of the nested-name-specifier.
76 NestedNameSpecifier *getScopeRep() const {
77 return Builder.getRepresentation();
78 }
79
80 /// Extend the current nested-name-specifier by another
81 /// nested-name-specifier component of the form 'type::'.
82 ///
83 /// \param Context The AST context in which this nested-name-specifier
84 /// resides.
85 ///
86 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
87 ///
88 /// \param TL The TypeLoc that describes the type preceding the '::'.
89 ///
90 /// \param ColonColonLoc The location of the trailing '::'.
91 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
92 SourceLocation ColonColonLoc);
93
94 /// Extend the current nested-name-specifier by another
95 /// nested-name-specifier component of the form 'identifier::'.
96 ///
97 /// \param Context The AST context in which this nested-name-specifier
98 /// resides.
99 ///
100 /// \param Identifier The identifier.
101 ///
102 /// \param IdentifierLoc The location of the identifier.
103 ///
104 /// \param ColonColonLoc The location of the trailing '::'.
105 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
106 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
107
108 /// Extend the current nested-name-specifier by another
109 /// nested-name-specifier component of the form 'namespace::'.
110 ///
111 /// \param Context The AST context in which this nested-name-specifier
112 /// resides.
113 ///
114 /// \param Namespace The namespace.
115 ///
116 /// \param NamespaceLoc The location of the namespace name.
117 ///
118 /// \param ColonColonLoc The location of the trailing '::'.
119 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
120 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
121
122 /// Extend the current nested-name-specifier by another
123 /// nested-name-specifier component of the form 'namespace-alias::'.
124 ///
125 /// \param Context The AST context in which this nested-name-specifier
126 /// resides.
127 ///
128 /// \param Alias The namespace alias.
129 ///
130 /// \param AliasLoc The location of the namespace alias
131 /// name.
132 ///
133 /// \param ColonColonLoc The location of the trailing '::'.
134 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
135 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
136
137 /// Turn this (empty) nested-name-specifier into the global
138 /// nested-name-specifier '::'.
139 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
140
141 /// Turns this (empty) nested-name-specifier into '__super'
142 /// nested-name-specifier.
143 ///
144 /// \param Context The AST context in which this nested-name-specifier
145 /// resides.
146 ///
147 /// \param RD The declaration of the class in which nested-name-specifier
148 /// appeared.
149 ///
150 /// \param SuperLoc The location of the '__super' keyword.
151 /// name.
152 ///
153 /// \param ColonColonLoc The location of the trailing '::'.
154 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
155 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
156
157 /// Make a new nested-name-specifier from incomplete source-location
158 /// information.
159 ///
160 /// FIXME: This routine should be used very, very rarely, in cases where we
161 /// need to synthesize a nested-name-specifier. Most code should instead use
162 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
163 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
164 SourceRange R);
165
166 /// Adopt an existing nested-name-specifier (with source-range
167 /// information).
168 void Adopt(NestedNameSpecifierLoc Other);
169
170 /// Retrieve a nested-name-specifier with location information, copied
171 /// into the given AST context.
172 ///
173 /// \param Context The context into which this nested-name-specifier will be
174 /// copied.
175 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
176
177 /// Retrieve the location of the name in the last qualifier
178 /// in this nested name specifier.
179 ///
180 /// For example, the location of \c bar
181 /// in
182 /// \verbatim
183 /// \::foo::bar<0>::
184 /// ^~~
185 /// \endverbatim
186 SourceLocation getLastQualifierNameLoc() const;
187
188 /// No scope specifier.
189 bool isEmpty() const { return !Range.isValid(); }
190 /// A scope specifier is present, but may be valid or invalid.
191 bool isNotEmpty() const { return !isEmpty(); }
192
193 /// An error occurred during parsing of the scope specifier.
194 bool isInvalid() const { return isNotEmpty() && getScopeRep() == nullptr; }
195 /// A scope specifier is present, and it refers to a real scope.
196 bool isValid() const { return isNotEmpty() && getScopeRep() != nullptr; }
197
198 /// Indicate that this nested-name-specifier is invalid.
199 void SetInvalid(SourceRange R) {
200 assert(R.isValid() && "Must have a valid source range");
201 if (Range.getBegin().isInvalid())
202 Range.setBegin(R.getBegin());
203 Range.setEnd(R.getEnd());
204 Builder.Clear();
205 }
206
207 /// Deprecated. Some call sites intend isNotEmpty() while others intend
208 /// isValid().
209 bool isSet() const { return getScopeRep() != nullptr; }
210
211 void clear() {
212 Range = SourceRange();
213 Builder.Clear();
214 }
215
216 /// Retrieve the data associated with the source-location information.
217 char *location_data() const { return Builder.getBuffer().first; }
218
219 /// Retrieve the size of the data associated with source-location
220 /// information.
221 unsigned location_size() const { return Builder.getBuffer().second; }
222};
223
224/// Captures information about "declaration specifiers".
225///
226/// "Declaration specifiers" encompasses storage-class-specifiers,
227/// type-specifiers, type-qualifiers, and function-specifiers.
228class DeclSpec {
229public:
230 /// storage-class-specifier
231 /// \note The order of these enumerators is important for diagnostics.
232 enum SCS {
233 SCS_unspecified = 0,
234 SCS_typedef,
235 SCS_extern,
236 SCS_static,
237 SCS_auto,
238 SCS_register,
239 SCS_private_extern,
240 SCS_mutable
241 };
242
243 // Import thread storage class specifier enumeration and constants.
244 // These can be combined with SCS_extern and SCS_static.
245 typedef ThreadStorageClassSpecifier TSCS;
246 static const TSCS TSCS_unspecified = clang::TSCS_unspecified;
247 static const TSCS TSCS___thread = clang::TSCS___thread;
248 static const TSCS TSCS_thread_local = clang::TSCS_thread_local;
249 static const TSCS TSCS__Thread_local = clang::TSCS__Thread_local;
250
251 // Import type specifier width enumeration and constants.
252 typedef TypeSpecifierWidth TSW;
253 static const TSW TSW_unspecified = clang::TSW_unspecified;
254 static const TSW TSW_short = clang::TSW_short;
255 static const TSW TSW_long = clang::TSW_long;
256 static const TSW TSW_longlong = clang::TSW_longlong;
257
258 enum TSC {
259 TSC_unspecified,
260 TSC_imaginary,
261 TSC_complex
262 };
263
264 // Import type specifier sign enumeration and constants.
265 typedef TypeSpecifierSign TSS;
266 static const TSS TSS_unspecified = clang::TSS_unspecified;
267 static const TSS TSS_signed = clang::TSS_signed;
268 static const TSS TSS_unsigned = clang::TSS_unsigned;
269
270 // Import type specifier type enumeration and constants.
271 typedef TypeSpecifierType TST;
272 static const TST TST_unspecified = clang::TST_unspecified;
273 static const TST TST_void = clang::TST_void;
274 static const TST TST_char = clang::TST_char;
275 static const TST TST_wchar = clang::TST_wchar;
276 static const TST TST_char8 = clang::TST_char8;
277 static const TST TST_char16 = clang::TST_char16;
278 static const TST TST_char32 = clang::TST_char32;
279 static const TST TST_int = clang::TST_int;
280 static const TST TST_int128 = clang::TST_int128;
281 static const TST TST_half = clang::TST_half;
282 static const TST TST_float = clang::TST_float;
283 static const TST TST_double = clang::TST_double;
284 static const TST TST_float16 = clang::TST_Float16;
285 static const TST TST_accum = clang::TST_Accum;
286 static const TST TST_fract = clang::TST_Fract;
287 static const TST TST_float128 = clang::TST_float128;
288 static const TST TST_bool = clang::TST_bool;
289 static const TST TST_decimal32 = clang::TST_decimal32;
290 static const TST TST_decimal64 = clang::TST_decimal64;
291 static const TST TST_decimal128 = clang::TST_decimal128;
292 static const TST TST_enum = clang::TST_enum;
293 static const TST TST_union = clang::TST_union;
294 static const TST TST_struct = clang::TST_struct;
295 static const TST TST_interface = clang::TST_interface;
296 static const TST TST_class = clang::TST_class;
297 static const TST TST_typename = clang::TST_typename;
298 static const TST TST_typeofType = clang::TST_typeofType;
299 static const TST TST_typeofExpr = clang::TST_typeofExpr;
300 static const TST TST_decltype = clang::TST_decltype;
301 static const TST TST_decltype_auto = clang::TST_decltype_auto;
302 static const TST TST_underlyingType = clang::TST_underlyingType;
303 static const TST TST_auto = clang::TST_auto;
304 static const TST TST_auto_type = clang::TST_auto_type;
305 static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
306 static const TST TST_atomic = clang::TST_atomic;
307#define GENERIC_IMAGE_TYPE(ImgType, Id) \
308 static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t;
309#include "clang/Basic/OpenCLImageTypes.def"
310 static const TST TST_error = clang::TST_error;
311
312 // type-qualifiers
313 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ.
314 TQ_unspecified = 0,
315 TQ_const = 1,
316 TQ_restrict = 2,
317 TQ_volatile = 4,
318 TQ_unaligned = 8,
319 // This has no corresponding Qualifiers::TQ value, because it's not treated
320 // as a qualifier in our type system.
321 TQ_atomic = 16
322 };
323
324 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
325 /// returned by getParsedSpecifiers.
326 enum ParsedSpecifiers {
327 PQ_None = 0,
328 PQ_StorageClassSpecifier = 1,
329 PQ_TypeSpecifier = 2,
330 PQ_TypeQualifier = 4,
331 PQ_FunctionSpecifier = 8
332 // FIXME: Attributes should be included here.
333 };
334
335private:
336 // storage-class-specifier
337 /*SCS*/unsigned StorageClassSpec : 3;
338 /*TSCS*/unsigned ThreadStorageClassSpec : 2;
339 unsigned SCS_extern_in_linkage_spec : 1;
340
341 // type-specifier
342 /*TSW*/unsigned TypeSpecWidth : 2;
343 /*TSC*/unsigned TypeSpecComplex : 2;
344 /*TSS*/unsigned TypeSpecSign : 2;
345 /*TST*/unsigned TypeSpecType : 6;
346 unsigned TypeAltiVecVector : 1;
347 unsigned TypeAltiVecPixel : 1;
348 unsigned TypeAltiVecBool : 1;
349 unsigned TypeSpecOwned : 1;
350 unsigned TypeSpecPipe : 1;
351 unsigned TypeSpecSat : 1;
352
353 // type-qualifiers
354 unsigned TypeQualifiers : 5; // Bitwise OR of TQ.
355
356 // function-specifier
357 unsigned FS_inline_specified : 1;
358 unsigned FS_forceinline_specified: 1;
359 unsigned FS_virtual_specified : 1;
360 unsigned FS_explicit_specified : 1;
361 unsigned FS_noreturn_specified : 1;
362
363 // friend-specifier
364 unsigned Friend_specified : 1;
365
366 // constexpr-specifier
367 unsigned Constexpr_specified : 1;
368
369 union {
370 UnionParsedType TypeRep;
371 Decl *DeclRep;
372 Expr *ExprRep;
373 };
374
375 // attributes.
376 ParsedAttributes Attrs;
377
378 // Scope specifier for the type spec, if applicable.
379 CXXScopeSpec TypeScope;
380
381 // SourceLocation info. These are null if the item wasn't specified or if
382 // the setting was synthesized.
383 SourceRange Range;
384
385 SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc;
386 SourceRange TSWRange;
387 SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc, TSSatLoc;
388 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
389 /// typename, then this is the location of the named type (if present);
390 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
391 /// TSTNameLoc provides source range info for tag types.
392 SourceLocation TSTNameLoc;
393 SourceRange TypeofParensRange;
394 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc,
395 TQ_unalignedLoc;
396 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc;
397 SourceLocation FS_forceinlineLoc;
398 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc;
399 SourceLocation TQ_pipeLoc;
400
401 WrittenBuiltinSpecs writtenBS;
402 void SaveWrittenBuiltinSpecs();
403
404 ObjCDeclSpec *ObjCQualifiers;
405
406 static bool isTypeRep(TST T) {
407 return (T == TST_typename || T == TST_typeofType ||
408 T == TST_underlyingType || T == TST_atomic);
409 }
410 static bool isExprRep(TST T) {
411 return (T == TST_typeofExpr || T == TST_decltype);
412 }
413
414 DeclSpec(const DeclSpec &) = delete;
415 void operator=(const DeclSpec &) = delete;
416public:
417 static bool isDeclRep(TST T) {
418 return (T == TST_enum || T == TST_struct ||
419 T == TST_interface || T == TST_union ||
420 T == TST_class);
421 }
422
423 DeclSpec(AttributeFactory &attrFactory)
424 : StorageClassSpec(SCS_unspecified),
425 ThreadStorageClassSpec(TSCS_unspecified),
426 SCS_extern_in_linkage_spec(false),
427 TypeSpecWidth(TSW_unspecified),
428 TypeSpecComplex(TSC_unspecified),
429 TypeSpecSign(TSS_unspecified),
430 TypeSpecType(TST_unspecified),
431 TypeAltiVecVector(false),
432 TypeAltiVecPixel(false),
433 TypeAltiVecBool(false),
434 TypeSpecOwned(false),
435 TypeSpecPipe(false),
436 TypeSpecSat(false),
437 TypeQualifiers(TQ_unspecified),
438 FS_inline_specified(false),
439 FS_forceinline_specified(false),
440 FS_virtual_specified(false),
441 FS_explicit_specified(false),
442 FS_noreturn_specified(false),
443 Friend_specified(false),
444 Constexpr_specified(false),
445 Attrs(attrFactory),
446 writtenBS(),
447 ObjCQualifiers(nullptr) {
448 }
449
450 // storage-class-specifier
451 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
452 TSCS getThreadStorageClassSpec() const {
453 return (TSCS)ThreadStorageClassSpec;
454 }
455 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
456 void setExternInLinkageSpec(bool Value) {
457 SCS_extern_in_linkage_spec = Value;
458 }
459
460 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
461 SourceLocation getThreadStorageClassSpecLoc() const {
462 return ThreadStorageClassSpecLoc;
463 }
464
465 void ClearStorageClassSpecs() {
466 StorageClassSpec = DeclSpec::SCS_unspecified;
467 ThreadStorageClassSpec = DeclSpec::TSCS_unspecified;
468 SCS_extern_in_linkage_spec = false;
469 StorageClassSpecLoc = SourceLocation();
470 ThreadStorageClassSpecLoc = SourceLocation();
471 }
472
473 void ClearTypeSpecType() {
474 TypeSpecType = DeclSpec::TST_unspecified;
475 TypeSpecOwned = false;
476 TSTLoc = SourceLocation();
477 }
478
479 // type-specifier
480 TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
481 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
482 TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
483 TST getTypeSpecType() const { return (TST)TypeSpecType; }
484 bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
485 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
486 bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
487 bool isTypeSpecOwned() const { return TypeSpecOwned; }
488 bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); }
489 bool isTypeSpecPipe() const { return TypeSpecPipe; }
490 bool isTypeSpecSat() const { return TypeSpecSat; }
491
492 ParsedType getRepAsType() const {
493 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
494 return TypeRep;
495 }
496 Decl *getRepAsDecl() const {
497 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
498 return DeclRep;
499 }
500 Expr *getRepAsExpr() const {
501 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
502 return ExprRep;
503 }
504 CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
505 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
506
507 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
508 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
509 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
510 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
511 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
512
513 SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); }
514 SourceRange getTypeSpecWidthRange() const { return TSWRange; }
515 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
516 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
517 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
518 SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
519 SourceLocation getTypeSpecSatLoc() const { return TSSatLoc; }
520
521 SourceLocation getTypeSpecTypeNameLoc() const {
522 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename);
523 return TSTNameLoc;
524 }
525
526 SourceRange getTypeofParensRange() const { return TypeofParensRange; }
527 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
528
529 bool hasAutoTypeSpec() const {
530 return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type ||
531 TypeSpecType == TST_decltype_auto);
532 }
533
534 bool hasTagDefinition() const;
535
536 /// Turn a type-specifier-type into a string like "_Bool" or "union".
537 static const char *getSpecifierName(DeclSpec::TST T,
538 const PrintingPolicy &Policy);
539 static const char *getSpecifierName(DeclSpec::TQ Q);
540 static const char *getSpecifierName(DeclSpec::TSS S);
541 static const char *getSpecifierName(DeclSpec::TSC C);
542 static const char *getSpecifierName(DeclSpec::TSW W);
543 static const char *getSpecifierName(DeclSpec::SCS S);
544 static const char *getSpecifierName(DeclSpec::TSCS S);
545
546 // type-qualifiers
547
548 /// getTypeQualifiers - Return a set of TQs.
549 unsigned getTypeQualifiers() const { return TypeQualifiers; }
550 SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
551 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
552 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
553 SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; }
554 SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; }
555 SourceLocation getPipeLoc() const { return TQ_pipeLoc; }
556
557 /// Clear out all of the type qualifiers.
558 void ClearTypeQualifiers() {
559 TypeQualifiers = 0;
560 TQ_constLoc = SourceLocation();
561 TQ_restrictLoc = SourceLocation();
562 TQ_volatileLoc = SourceLocation();
563 TQ_atomicLoc = SourceLocation();
564 TQ_unalignedLoc = SourceLocation();
565 TQ_pipeLoc = SourceLocation();
566 }
567
568 // function-specifier
569 bool isInlineSpecified() const {
570 return FS_inline_specified | FS_forceinline_specified;
571 }
572 SourceLocation getInlineSpecLoc() const {
573 return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc;
574 }
575
576 bool isVirtualSpecified() const { return FS_virtual_specified; }
577 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
578
579 bool isExplicitSpecified() const { return FS_explicit_specified; }
580 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
581
582 bool isNoreturnSpecified() const { return FS_noreturn_specified; }
583 SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; }
584
585 void ClearFunctionSpecs() {
586 FS_inline_specified = false;
587 FS_inlineLoc = SourceLocation();
588 FS_forceinline_specified = false;
589 FS_forceinlineLoc = SourceLocation();
590 FS_virtual_specified = false;
591 FS_virtualLoc = SourceLocation();
592 FS_explicit_specified = false;
593 FS_explicitLoc = SourceLocation();
594 FS_noreturn_specified = false;
595 FS_noreturnLoc = SourceLocation();
596 }
597
598 /// Return true if any type-specifier has been found.
599 bool hasTypeSpecifier() const {
600 return getTypeSpecType() != DeclSpec::TST_unspecified ||
601 getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
602 getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
603 getTypeSpecSign() != DeclSpec::TSS_unspecified;
604 }
605
606 /// Return a bitmask of which flavors of specifiers this
607 /// DeclSpec includes.
608 unsigned getParsedSpecifiers() const;
609
610 /// isEmpty - Return true if this declaration specifier is completely empty:
611 /// no tokens were parsed in the production of it.
612 bool isEmpty() const {
613 return getParsedSpecifiers() == DeclSpec::PQ_None;
614 }
615
616 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
617 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
618
619 /// These methods set the specified attribute of the DeclSpec and
620 /// return false if there was no error. If an error occurs (for
621 /// example, if we tried to set "auto" on a spec with "extern"
622 /// already set), they return true and set PrevSpec and DiagID
623 /// such that
624 /// Diag(Loc, DiagID) << PrevSpec;
625 /// will yield a useful result.
626 ///
627 /// TODO: use a more general approach that still allows these
628 /// diagnostics to be ignored when desired.
629 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
630 const char *&PrevSpec, unsigned &DiagID,
631 const PrintingPolicy &Policy);
632 bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
633 const char *&PrevSpec, unsigned &DiagID);
634 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
635 unsigned &DiagID, const PrintingPolicy &Policy);
636 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
637 unsigned &DiagID);
638 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
639 unsigned &DiagID);
640 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
641 unsigned &DiagID, const PrintingPolicy &Policy);
642 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
643 unsigned &DiagID, ParsedType Rep,
644 const PrintingPolicy &Policy);
645 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
646 unsigned &DiagID, Decl *Rep, bool Owned,
647 const PrintingPolicy &Policy);
648 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
649 SourceLocation TagNameLoc, const char *&PrevSpec,
650 unsigned &DiagID, ParsedType Rep,
651 const PrintingPolicy &Policy);
652 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
653 SourceLocation TagNameLoc, const char *&PrevSpec,
654 unsigned &DiagID, Decl *Rep, bool Owned,
655 const PrintingPolicy &Policy);
656
657 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
658 unsigned &DiagID, Expr *Rep,
659 const PrintingPolicy &policy);
660 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
661 const char *&PrevSpec, unsigned &DiagID,
662 const PrintingPolicy &Policy);
663 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
664 const char *&PrevSpec, unsigned &DiagID,
665 const PrintingPolicy &Policy);
666 bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
667 const char *&PrevSpec, unsigned &DiagID,
668 const PrintingPolicy &Policy);
669 bool SetTypePipe(bool isPipe, SourceLocation Loc,
670 const char *&PrevSpec, unsigned &DiagID,
671 const PrintingPolicy &Policy);
672 bool SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
673 unsigned &DiagID);
674 bool SetTypeSpecError();
675 void UpdateDeclRep(Decl *Rep) {
676 assert(isDeclRep((TST) TypeSpecType));
677 DeclRep = Rep;
678 }
679 void UpdateTypeRep(ParsedType Rep) {
680 assert(isTypeRep((TST) TypeSpecType));
681 TypeRep = Rep;
682 }
683 void UpdateExprRep(Expr *Rep) {
684 assert(isExprRep((TST) TypeSpecType));
685 ExprRep = Rep;
686 }
687
688 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
689 unsigned &DiagID, const LangOptions &Lang);
690
691 bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
692 unsigned &DiagID);
693 bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
694 unsigned &DiagID);
695 bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
696 unsigned &DiagID);
697 bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
698 unsigned &DiagID);
699 bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec,
700 unsigned &DiagID);
701
702 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
703 unsigned &DiagID);
704 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
705 unsigned &DiagID);
706 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
707 unsigned &DiagID);
708
709 bool isFriendSpecified() const { return Friend_specified; }
710 SourceLocation getFriendSpecLoc() const { return FriendLoc; }
711
712 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); }
713 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; }
714
715 bool isConstexprSpecified() const { return Constexpr_specified; }
716 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
717
718 void ClearConstexprSpec() {
719 Constexpr_specified = false;
720 ConstexprLoc = SourceLocation();
721 }
722
723 AttributePool &getAttributePool() const {
724 return Attrs.getPool();
725 }
726
727 /// Concatenates two attribute lists.
728 ///
729 /// The GCC attribute syntax allows for the following:
730 ///
731 /// \code
732 /// short __attribute__(( unused, deprecated ))
733 /// int __attribute__(( may_alias, aligned(16) )) var;
734 /// \endcode
735 ///
736 /// This declares 4 attributes using 2 lists. The following syntax is
737 /// also allowed and equivalent to the previous declaration.
738 ///
739 /// \code
740 /// short __attribute__((unused)) __attribute__((deprecated))
741 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
742 /// \endcode
743 ///
744 void addAttributes(ParsedAttributesView &AL) {
745 Attrs.addAll(AL.begin(), AL.end());
746 }
747
748 bool hasAttributes() const { return !Attrs.empty(); }
749
750 ParsedAttributes &getAttributes() { return Attrs; }
751 const ParsedAttributes &getAttributes() const { return Attrs; }
752
753 void takeAttributesFrom(ParsedAttributes &attrs) {
754 Attrs.takeAllFrom(attrs);
755 }
756
757 /// Finish - This does final analysis of the declspec, issuing diagnostics for
758 /// things like "_Imaginary" (lacking an FP type). After calling this method,
759 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
760 void Finish(Sema &S, const PrintingPolicy &Policy);
761
762 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
763 return writtenBS;
764 }
765
766 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; }
767 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; }
768
769 /// Checks if this DeclSpec can stand alone, without a Declarator.
770 ///
771 /// Only tag declspecs can stand alone.
772 bool isMissingDeclaratorOk();
773};
774
775/// Captures information about "declaration specifiers" specific to
776/// Objective-C.
777class ObjCDeclSpec {
778public:
779 /// ObjCDeclQualifier - Qualifier used on types in method
780 /// declarations. Not all combinations are sensible. Parameters
781 /// can be one of { in, out, inout } with one of { bycopy, byref }.
782 /// Returns can either be { oneway } or not.
783 ///
784 /// This should be kept in sync with Decl::ObjCDeclQualifier.
785 enum ObjCDeclQualifier {
786 DQ_None = 0x0,
787 DQ_In = 0x1,
788 DQ_Inout = 0x2,
789 DQ_Out = 0x4,
790 DQ_Bycopy = 0x8,
791 DQ_Byref = 0x10,
792 DQ_Oneway = 0x20,
793 DQ_CSNullability = 0x40
794 };
795
796 /// PropertyAttributeKind - list of property attributes.
797 /// Keep this list in sync with LLVM's Dwarf.h ApplePropertyAttributes.
798 enum ObjCPropertyAttributeKind {
799 DQ_PR_noattr = 0x0,
800 DQ_PR_readonly = 0x01,
801 DQ_PR_getter = 0x02,
802 DQ_PR_assign = 0x04,
803 DQ_PR_readwrite = 0x08,
804 DQ_PR_retain = 0x10,
805 DQ_PR_copy = 0x20,
806 DQ_PR_nonatomic = 0x40,
807 DQ_PR_setter = 0x80,
808 DQ_PR_atomic = 0x100,
809 DQ_PR_weak = 0x200,
810 DQ_PR_strong = 0x400,
811 DQ_PR_unsafe_unretained = 0x800,
812 DQ_PR_nullability = 0x1000,
813 DQ_PR_null_resettable = 0x2000,
814 DQ_PR_class = 0x4000
815 };
816
817 ObjCDeclSpec()
818 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
819 Nullability(0), GetterName(nullptr), SetterName(nullptr) { }
820
821 ObjCDeclQualifier getObjCDeclQualifier() const {
822 return (ObjCDeclQualifier)objcDeclQualifier;
823 }
824 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
825 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
826 }
827 void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) {
828 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal);
829 }
830
831 ObjCPropertyAttributeKind getPropertyAttributes() const {
832 return ObjCPropertyAttributeKind(PropertyAttributes);
833 }
834 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
835 PropertyAttributes =
836 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
837 }
838
839 NullabilityKind getNullability() const {
840 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
841 (getPropertyAttributes() & DQ_PR_nullability)) &&
842 "Objective-C declspec doesn't have nullability");
843 return static_cast<NullabilityKind>(Nullability);
844 }
845
846 SourceLocation getNullabilityLoc() const {
847 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
848 (getPropertyAttributes() & DQ_PR_nullability)) &&
849 "Objective-C declspec doesn't have nullability");
850 return NullabilityLoc;
851 }
852
853 void setNullability(SourceLocation loc, NullabilityKind kind) {
854 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
855 (getPropertyAttributes() & DQ_PR_nullability)) &&
856 "Set the nullability declspec or property attribute first");
857 Nullability = static_cast<unsigned>(kind);
858 NullabilityLoc = loc;
859 }
860
861 const IdentifierInfo *getGetterName() const { return GetterName; }
862 IdentifierInfo *getGetterName() { return GetterName; }
863 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
864 void setGetterName(IdentifierInfo *name, SourceLocation loc) {
865 GetterName = name;
866 GetterNameLoc = loc;
867 }
868
869 const IdentifierInfo *getSetterName() const { return SetterName; }
870 IdentifierInfo *getSetterName() { return SetterName; }
871 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
872 void setSetterName(IdentifierInfo *name, SourceLocation loc) {
873 SetterName = name;
874 SetterNameLoc = loc;
875 }
876
877private:
878 // FIXME: These two are unrelated and mutually exclusive. So perhaps
879 // we can put them in a union to reflect their mutual exclusivity
880 // (space saving is negligible).
881 unsigned objcDeclQualifier : 7;
882
883 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
884 unsigned PropertyAttributes : 15;
885
886 unsigned Nullability : 2;
887
888 SourceLocation NullabilityLoc;
889
890 IdentifierInfo *GetterName; // getter name or NULL if no getter
891 IdentifierInfo *SetterName; // setter name or NULL if no setter
892 SourceLocation GetterNameLoc; // location of the getter attribute's value
893 SourceLocation SetterNameLoc; // location of the setter attribute's value
894
895};
896
897/// Describes the kind of unqualified-id parsed.
898enum class UnqualifiedIdKind {
899 /// An identifier.
900 IK_Identifier,
901 /// An overloaded operator name, e.g., operator+.
902 IK_OperatorFunctionId,
903 /// A conversion function name, e.g., operator int.
904 IK_ConversionFunctionId,
905 /// A user-defined literal name, e.g., operator "" _i.
906 IK_LiteralOperatorId,
907 /// A constructor name.
908 IK_ConstructorName,
909 /// A constructor named via a template-id.
910 IK_ConstructorTemplateId,
911 /// A destructor name.
912 IK_DestructorName,
913 /// A template-id, e.g., f<int>.
914 IK_TemplateId,
915 /// An implicit 'self' parameter
916 IK_ImplicitSelfParam,
917 /// A deduction-guide name (a template-name)
918 IK_DeductionGuideName
919};
920
921/// Represents a C++ unqualified-id that has been parsed.
922class UnqualifiedId {
923private:
924 UnqualifiedId(const UnqualifiedId &Other) = delete;
925 const UnqualifiedId &operator=(const UnqualifiedId &) = delete;
926
927public:
928 /// Describes the kind of unqualified-id parsed.
929 UnqualifiedIdKind Kind;
930
931 struct OFI {
932 /// The kind of overloaded operator.
933 OverloadedOperatorKind Operator;
934
935 /// The source locations of the individual tokens that name
936 /// the operator, e.g., the "new", "[", and "]" tokens in
937 /// operator new [].
938 ///
939 /// Different operators have different numbers of tokens in their name,
940 /// up to three. Any remaining source locations in this array will be
941 /// set to an invalid value for operators with fewer than three tokens.
942 unsigned SymbolLocations[3];
943 };
944
945 /// Anonymous union that holds extra data associated with the
946 /// parsed unqualified-id.
947 union {
948 /// When Kind == IK_Identifier, the parsed identifier, or when
949 /// Kind == IK_UserLiteralId, the identifier suffix.
950 IdentifierInfo *Identifier;
951
952 /// When Kind == IK_OperatorFunctionId, the overloaded operator
953 /// that we parsed.
954 struct OFI OperatorFunctionId;
955
956 /// When Kind == IK_ConversionFunctionId, the type that the
957 /// conversion function names.
958 UnionParsedType ConversionFunctionId;
959
960 /// When Kind == IK_ConstructorName, the class-name of the type
961 /// whose constructor is being referenced.
962 UnionParsedType ConstructorName;
963
964 /// When Kind == IK_DestructorName, the type referred to by the
965 /// class-name.
966 UnionParsedType DestructorName;
967
968 /// When Kind == IK_DeductionGuideName, the parsed template-name.
969 UnionParsedTemplateTy TemplateName;
970
971 /// When Kind == IK_TemplateId or IK_ConstructorTemplateId,
972 /// the template-id annotation that contains the template name and
973 /// template arguments.
974 TemplateIdAnnotation *TemplateId;
975 };
976
977 /// The location of the first token that describes this unqualified-id,
978 /// which will be the location of the identifier, "operator" keyword,
979 /// tilde (for a destructor), or the template name of a template-id.
980 SourceLocation StartLocation;
981
982 /// The location of the last token that describes this unqualified-id.
983 SourceLocation EndLocation;
984
985 UnqualifiedId()
986 : Kind(UnqualifiedIdKind::IK_Identifier), Identifier(nullptr) {}
987
988 /// Clear out this unqualified-id, setting it to default (invalid)
989 /// state.
990 void clear() {
991 Kind = UnqualifiedIdKind::IK_Identifier;
992 Identifier = nullptr;
993 StartLocation = SourceLocation();
994 EndLocation = SourceLocation();
995 }
996
997 /// Determine whether this unqualified-id refers to a valid name.
998 bool isValid() const { return StartLocation.isValid(); }
999
1000 /// Determine whether this unqualified-id refers to an invalid name.
1001 bool isInvalid() const { return !isValid(); }
1002
1003 /// Determine what kind of name we have.
1004 UnqualifiedIdKind getKind() const { return Kind; }
1005 void setKind(UnqualifiedIdKind kind) { Kind = kind; }
1006
1007 /// Specify that this unqualified-id was parsed as an identifier.
1008 ///
1009 /// \param Id the parsed identifier.
1010 /// \param IdLoc the location of the parsed identifier.
1011 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
1012 Kind = UnqualifiedIdKind::IK_Identifier;
1013 Identifier = const_cast<IdentifierInfo *>(Id);
1014 StartLocation = EndLocation = IdLoc;
1015 }
1016
1017 /// Specify that this unqualified-id was parsed as an
1018 /// operator-function-id.
1019 ///
1020 /// \param OperatorLoc the location of the 'operator' keyword.
1021 ///
1022 /// \param Op the overloaded operator.
1023 ///
1024 /// \param SymbolLocations the locations of the individual operator symbols
1025 /// in the operator.
1026 void setOperatorFunctionId(SourceLocation OperatorLoc,
1027 OverloadedOperatorKind Op,
1028 SourceLocation SymbolLocations[3]);
1029
1030 /// Specify that this unqualified-id was parsed as a
1031 /// conversion-function-id.
1032 ///
1033 /// \param OperatorLoc the location of the 'operator' keyword.
1034 ///
1035 /// \param Ty the type to which this conversion function is converting.
1036 ///
1037 /// \param EndLoc the location of the last token that makes up the type name.
1038 void setConversionFunctionId(SourceLocation OperatorLoc,
1039 ParsedType Ty,
1040 SourceLocation EndLoc) {
1041 Kind = UnqualifiedIdKind::IK_ConversionFunctionId;
1042 StartLocation = OperatorLoc;
1043 EndLocation = EndLoc;
1044 ConversionFunctionId = Ty;
1045 }
1046
1047 /// Specific that this unqualified-id was parsed as a
1048 /// literal-operator-id.
1049 ///
1050 /// \param Id the parsed identifier.
1051 ///
1052 /// \param OpLoc the location of the 'operator' keyword.
1053 ///
1054 /// \param IdLoc the location of the identifier.
1055 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
1056 SourceLocation IdLoc) {
1057 Kind = UnqualifiedIdKind::IK_LiteralOperatorId;
1058 Identifier = const_cast<IdentifierInfo *>(Id);
1059 StartLocation = OpLoc;
1060 EndLocation = IdLoc;
1061 }
1062
1063 /// Specify that this unqualified-id was parsed as a constructor name.
1064 ///
1065 /// \param ClassType the class type referred to by the constructor name.
1066 ///
1067 /// \param ClassNameLoc the location of the class name.
1068 ///
1069 /// \param EndLoc the location of the last token that makes up the type name.
1070 void setConstructorName(ParsedType ClassType,
1071 SourceLocation ClassNameLoc,
1072 SourceLocation EndLoc) {
1073 Kind = UnqualifiedIdKind::IK_ConstructorName;
1074 StartLocation = ClassNameLoc;
1075 EndLocation = EndLoc;
1076 ConstructorName = ClassType;
1077 }
1078
1079 /// Specify that this unqualified-id was parsed as a
1080 /// template-id that names a constructor.
1081 ///
1082 /// \param TemplateId the template-id annotation that describes the parsed
1083 /// template-id. This UnqualifiedId instance will take ownership of the
1084 /// \p TemplateId and will free it on destruction.
1085 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
1086
1087 /// Specify that this unqualified-id was parsed as a destructor name.
1088 ///
1089 /// \param TildeLoc the location of the '~' that introduces the destructor
1090 /// name.
1091 ///
1092 /// \param ClassType the name of the class referred to by the destructor name.
1093 void setDestructorName(SourceLocation TildeLoc,
1094 ParsedType ClassType,
1095 SourceLocation EndLoc) {
1096 Kind = UnqualifiedIdKind::IK_DestructorName;
1097 StartLocation = TildeLoc;
1098 EndLocation = EndLoc;
1099 DestructorName = ClassType;
1100 }
1101
1102 /// Specify that this unqualified-id was parsed as a template-id.
1103 ///
1104 /// \param TemplateId the template-id annotation that describes the parsed
1105 /// template-id. This UnqualifiedId instance will take ownership of the
1106 /// \p TemplateId and will free it on destruction.
1107 void setTemplateId(TemplateIdAnnotation *TemplateId);
1108
1109 /// Specify that this unqualified-id was parsed as a template-name for
1110 /// a deduction-guide.
1111 ///
1112 /// \param Template The parsed template-name.
1113 /// \param TemplateLoc The location of the parsed template-name.
1114 void setDeductionGuideName(ParsedTemplateTy Template,
1115 SourceLocation TemplateLoc) {
1116 Kind = UnqualifiedIdKind::IK_DeductionGuideName;
1117 TemplateName = Template;
1118 StartLocation = EndLocation = TemplateLoc;
1119 }
1120
1121 /// Return the source range that covers this unqualified-id.
1122 SourceRange getSourceRange() const LLVM_READONLY {
1123 return SourceRange(StartLocation, EndLocation);
1124 }
1125 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1126 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLocation; }
1127 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1128 SourceLocation getEndLoc() const LLVM_READONLY { return EndLocation; }
1129};
1130
1131/// A set of tokens that has been cached for later parsing.
1132typedef SmallVector<Token, 4> CachedTokens;
1133
1134/// One instance of this struct is used for each type in a
1135/// declarator that is parsed.
1136///
1137/// This is intended to be a small value object.
1138struct DeclaratorChunk {
1139 enum {
1140 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe
1141 } Kind;
1142
1143 /// Loc - The place where this type was defined.
1144 SourceLocation Loc;
1145 /// EndLoc - If valid, the place where this chunck ends.
1146 SourceLocation EndLoc;
1147
1148 SourceRange getSourceRange() const {
1149 if (EndLoc.isInvalid())
1150 return SourceRange(Loc, Loc);
1151 return SourceRange(Loc, EndLoc);
1152 }
1153
1154 ParsedAttributesView AttrList;
1155
1156 struct PointerTypeInfo {
1157 /// The type qualifiers: const/volatile/restrict/unaligned/atomic.
1158 unsigned TypeQuals : 5;
1159
1160 /// The location of the const-qualifier, if any.
1161 unsigned ConstQualLoc;
1162
1163 /// The location of the volatile-qualifier, if any.
1164 unsigned VolatileQualLoc;
1165
1166 /// The location of the restrict-qualifier, if any.
1167 unsigned RestrictQualLoc;
1168
1169 /// The location of the _Atomic-qualifier, if any.
1170 unsigned AtomicQualLoc;
1171
1172 /// The location of the __unaligned-qualifier, if any.
1173 unsigned UnalignedQualLoc;
1174
1175 void destroy() {
1176 }
1177 };
1178
1179 struct ReferenceTypeInfo {
1180 /// The type qualifier: restrict. [GNU] C++ extension
1181 bool HasRestrict : 1;
1182 /// True if this is an lvalue reference, false if it's an rvalue reference.
1183 bool LValueRef : 1;
1184 void destroy() {
1185 }
1186 };
1187
1188 struct ArrayTypeInfo {
1189 /// The type qualifiers for the array:
1190 /// const/volatile/restrict/__unaligned/_Atomic.
1191 unsigned TypeQuals : 5;
1192
1193 /// True if this dimension included the 'static' keyword.
1194 unsigned hasStatic : 1;
1195
1196 /// True if this dimension was [*]. In this case, NumElts is null.
1197 unsigned isStar : 1;
1198
1199 /// This is the size of the array, or null if [] or [*] was specified.
1200 /// Since the parser is multi-purpose, and we don't want to impose a root
1201 /// expression class on all clients, NumElts is untyped.
1202 Expr *NumElts;
1203
1204 void destroy() {}
1205 };
1206
1207 /// ParamInfo - An array of paraminfo objects is allocated whenever a function
1208 /// declarator is parsed. There are two interesting styles of parameters
1209 /// here:
1210 /// K&R-style identifier lists and parameter type lists. K&R-style identifier
1211 /// lists will have information about the identifier, but no type information.
1212 /// Parameter type lists will have type info (if the actions module provides
1213 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
1214 struct ParamInfo {
1215 IdentifierInfo *Ident;
1216 SourceLocation IdentLoc;
1217 Decl *Param;
1218
1219 /// DefaultArgTokens - When the parameter's default argument
1220 /// cannot be parsed immediately (because it occurs within the
1221 /// declaration of a member function), it will be stored here as a
1222 /// sequence of tokens to be parsed once the class definition is
1223 /// complete. Non-NULL indicates that there is a default argument.
1224 std::unique_ptr<CachedTokens> DefaultArgTokens;
1225
1226 ParamInfo() = default;
1227 ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
1228 Decl *param,
1229 std::unique_ptr<CachedTokens> DefArgTokens = nullptr)
1230 : Ident(ident), IdentLoc(iloc), Param(param),
1231 DefaultArgTokens(std::move(DefArgTokens)) {}
1232 };
1233
1234 struct TypeAndRange {
1235 ParsedType Ty;
1236 SourceRange Range;
1237 };
1238
1239 struct FunctionTypeInfo {
1240 /// hasPrototype - This is true if the function had at least one typed
1241 /// parameter. If the function is () or (a,b,c), then it has no prototype,
1242 /// and is treated as a K&R-style function.
1243 unsigned hasPrototype : 1;
1244
1245 /// isVariadic - If this function has a prototype, and if that
1246 /// proto ends with ',...)', this is true. When true, EllipsisLoc
1247 /// contains the location of the ellipsis.
1248 unsigned isVariadic : 1;
1249
1250 /// Can this declaration be a constructor-style initializer?
1251 unsigned isAmbiguous : 1;
1252
1253 /// Whether the ref-qualifier (if any) is an lvalue reference.
1254 /// Otherwise, it's an rvalue reference.
1255 unsigned RefQualifierIsLValueRef : 1;
1256
1257 /// The type qualifiers: const/volatile/restrict/__unaligned
1258 /// The qualifier bitmask values are the same as in QualType.
1259 unsigned TypeQuals : 4;
1260
1261 /// ExceptionSpecType - An ExceptionSpecificationType value.
1262 unsigned ExceptionSpecType : 4;
1263
1264 /// DeleteParams - If this is true, we need to delete[] Params.
1265 unsigned DeleteParams : 1;
1266
1267 /// HasTrailingReturnType - If this is true, a trailing return type was
1268 /// specified.
1269 unsigned HasTrailingReturnType : 1;
1270
1271 /// The location of the left parenthesis in the source.
1272 unsigned LParenLoc;
1273
1274 /// When isVariadic is true, the location of the ellipsis in the source.
1275 unsigned EllipsisLoc;
1276
1277 /// The location of the right parenthesis in the source.
1278 unsigned RParenLoc;
1279
1280 /// NumParams - This is the number of formal parameters specified by the
1281 /// declarator.
1282 unsigned NumParams;
1283
1284 /// NumExceptionsOrDecls - This is the number of types in the
1285 /// dynamic-exception-decl, if the function has one. In C, this is the
1286 /// number of declarations in the function prototype.
1287 unsigned NumExceptionsOrDecls;
1288
1289 /// The location of the ref-qualifier, if any.
1290 ///
1291 /// If this is an invalid location, there is no ref-qualifier.
1292 unsigned RefQualifierLoc;
1293
1294 /// The location of the const-qualifier, if any.
1295 ///
1296 /// If this is an invalid location, there is no const-qualifier.
1297 unsigned ConstQualifierLoc;
1298
1299 /// The location of the volatile-qualifier, if any.
1300 ///
1301 /// If this is an invalid location, there is no volatile-qualifier.
1302 unsigned VolatileQualifierLoc;
1303
1304 /// The location of the restrict-qualifier, if any.
1305 ///
1306 /// If this is an invalid location, there is no restrict-qualifier.
1307 unsigned RestrictQualifierLoc;
1308
1309 /// The location of the 'mutable' qualifer in a lambda-declarator, if
1310 /// any.
1311 unsigned MutableLoc;
1312
1313 /// The beginning location of the exception specification, if any.
1314 unsigned ExceptionSpecLocBeg;
1315
1316 /// The end location of the exception specification, if any.
1317 unsigned ExceptionSpecLocEnd;
1318
1319 /// Params - This is a pointer to a new[]'d array of ParamInfo objects that
1320 /// describe the parameters specified by this function declarator. null if
1321 /// there are no parameters specified.
1322 ParamInfo *Params;
1323
1324 union {
1325 /// Pointer to a new[]'d array of TypeAndRange objects that
1326 /// contain the types in the function's dynamic exception specification
1327 /// and their locations, if there is one.
1328 TypeAndRange *Exceptions;
1329
1330 /// Pointer to the expression in the noexcept-specifier of this
1331 /// function, if it has one.
1332 Expr *NoexceptExpr;
1333
1334 /// Pointer to the cached tokens for an exception-specification
1335 /// that has not yet been parsed.
1336 CachedTokens *ExceptionSpecTokens;
1337
1338 /// Pointer to a new[]'d array of declarations that need to be available
1339 /// for lookup inside the function body, if one exists. Does not exist in
1340 /// C++.
1341 NamedDecl **DeclsInPrototype;
1342 };
1343
1344 /// If HasTrailingReturnType is true, this is the trailing return
1345 /// type specified.
1346 UnionParsedType TrailingReturnType;
1347
1348 /// Reset the parameter list to having zero parameters.
1349 ///
1350 /// This is used in various places for error recovery.
1351 void freeParams() {
1352 for (unsigned I = 0; I < NumParams; ++I)
1353 Params[I].DefaultArgTokens.reset();
1354 if (DeleteParams) {
1355 delete[] Params;
1356 DeleteParams = false;
1357 }
1358 NumParams = 0;
1359 }
1360
1361 void destroy() {
1362 freeParams();
1363 switch (getExceptionSpecType()) {
1364 default:
1365 break;
1366 case EST_Dynamic:
1367 delete[] Exceptions;
1368 break;
1369 case EST_Unparsed:
1370 delete ExceptionSpecTokens;
1371 break;
1372 case EST_None:
1373 if (NumExceptionsOrDecls != 0)
1374 delete[] DeclsInPrototype;
1375 break;
1376 }
1377 }
1378
1379 /// isKNRPrototype - Return true if this is a K&R style identifier list,
1380 /// like "void foo(a,b,c)". In a function definition, this will be followed
1381 /// by the parameter type definitions.
1382 bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; }
1383
1384 SourceLocation getLParenLoc() const {
1385 return SourceLocation::getFromRawEncoding(LParenLoc);
1386 }
1387
1388 SourceLocation getEllipsisLoc() const {
1389 return SourceLocation::getFromRawEncoding(EllipsisLoc);
1390 }
1391
1392 SourceLocation getRParenLoc() const {
1393 return SourceLocation::getFromRawEncoding(RParenLoc);
1394 }
1395
1396 SourceLocation getExceptionSpecLocBeg() const {
1397 return SourceLocation::getFromRawEncoding(ExceptionSpecLocBeg);
1398 }
1399
1400 SourceLocation getExceptionSpecLocEnd() const {
1401 return SourceLocation::getFromRawEncoding(ExceptionSpecLocEnd);
1402 }
1403
1404 SourceRange getExceptionSpecRange() const {
1405 return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd());
1406 }
1407
1408 /// Retrieve the location of the ref-qualifier, if any.
1409 SourceLocation getRefQualifierLoc() const {
1410 return SourceLocation::getFromRawEncoding(RefQualifierLoc);
1411 }
1412
1413 /// Retrieve the location of the 'const' qualifier, if any.
1414 SourceLocation getConstQualifierLoc() const {
1415 return SourceLocation::getFromRawEncoding(ConstQualifierLoc);
1416 }
1417
1418 /// Retrieve the location of the 'volatile' qualifier, if any.
1419 SourceLocation getVolatileQualifierLoc() const {
1420 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc);
1421 }
1422
1423 /// Retrieve the location of the 'restrict' qualifier, if any.
1424 SourceLocation getRestrictQualifierLoc() const {
1425 return SourceLocation::getFromRawEncoding(RestrictQualifierLoc);
1426 }
1427
1428 /// Retrieve the location of the 'mutable' qualifier, if any.
1429 SourceLocation getMutableLoc() const {
1430 return SourceLocation::getFromRawEncoding(MutableLoc);
1431 }
1432
1433 /// Determine whether this function declaration contains a
1434 /// ref-qualifier.
1435 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1436
1437 /// Determine whether this lambda-declarator contains a 'mutable'
1438 /// qualifier.
1439 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1440
1441 /// Get the type of exception specification this function has.
1442 ExceptionSpecificationType getExceptionSpecType() const {
1443 return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1444 }
1445
1446 /// Get the number of dynamic exception specifications.
1447 unsigned getNumExceptions() const {
1448 assert(ExceptionSpecType != EST_None);
1449 return NumExceptionsOrDecls;
1450 }
1451
1452 /// Get the non-parameter decls defined within this function
1453 /// prototype. Typically these are tag declarations.
1454 ArrayRef<NamedDecl *> getDeclsInPrototype() const {
1455 assert(ExceptionSpecType == EST_None);
1456 return llvm::makeArrayRef(DeclsInPrototype, NumExceptionsOrDecls);
1457 }
1458
1459 /// Determine whether this function declarator had a
1460 /// trailing-return-type.
1461 bool hasTrailingReturnType() const { return HasTrailingReturnType; }
1462
1463 /// Get the trailing-return-type for this function declarator.
1464 ParsedType getTrailingReturnType() const { return TrailingReturnType; }
1465 };
1466
1467 struct BlockPointerTypeInfo {
1468 /// For now, sema will catch these as invalid.
1469 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1470 unsigned TypeQuals : 5;
1471
1472 void destroy() {
1473 }
1474 };
1475
1476 struct MemberPointerTypeInfo {
1477 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1478 unsigned TypeQuals : 5;
1479 // CXXScopeSpec has a constructor, so it can't be a direct member.
1480 // So we need some pointer-aligned storage and a bit of trickery.
1481 alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)];
1482 CXXScopeSpec &Scope() {
1483 return *reinterpret_cast<CXXScopeSpec *>(ScopeMem);
1484 }
1485 const CXXScopeSpec &Scope() const {
1486 return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem);
1487 }
1488 void destroy() {
1489 Scope().~CXXScopeSpec();
1490 }
1491 };
1492
1493 struct PipeTypeInfo {
1494 /// The access writes.
1495 unsigned AccessWrites : 3;
1496
1497 void destroy() {}
1498 };
1499
1500 union {
1501 PointerTypeInfo Ptr;
1502 ReferenceTypeInfo Ref;
1503 ArrayTypeInfo Arr;
1504 FunctionTypeInfo Fun;
1505 BlockPointerTypeInfo Cls;
1506 MemberPointerTypeInfo Mem;
1507 PipeTypeInfo PipeInfo;
1508 };
1509
1510 void destroy() {
1511 switch (Kind) {
1512 case DeclaratorChunk::Function: return Fun.destroy();
1513 case DeclaratorChunk::Pointer: return Ptr.destroy();
1514 case DeclaratorChunk::BlockPointer: return Cls.destroy();
1515 case DeclaratorChunk::Reference: return Ref.destroy();
1516 case DeclaratorChunk::Array: return Arr.destroy();
1517 case DeclaratorChunk::MemberPointer: return Mem.destroy();
1518 case DeclaratorChunk::Paren: return;
1519 case DeclaratorChunk::Pipe: return PipeInfo.destroy();
1520 }
1521 }
1522
1523 /// If there are attributes applied to this declaratorchunk, return
1524 /// them.
1525 const ParsedAttributesView &getAttrs() const { return AttrList; }
1526 ParsedAttributesView &getAttrs() { return AttrList; }
1527
1528 /// Return a DeclaratorChunk for a pointer.
1529 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1530 SourceLocation ConstQualLoc,
1531 SourceLocation VolatileQualLoc,
1532 SourceLocation RestrictQualLoc,
1533 SourceLocation AtomicQualLoc,
1534 SourceLocation UnalignedQualLoc) {
1535 DeclaratorChunk I;
1536 I.Kind = Pointer;
1537 I.Loc = Loc;
1538 I.Ptr.TypeQuals = TypeQuals;
1539 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding();
1540 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
1541 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
1542 I.Ptr.AtomicQualLoc = AtomicQualLoc.getRawEncoding();
1543 I.Ptr.UnalignedQualLoc = UnalignedQualLoc.getRawEncoding();
1544 return I;
1545 }
1546
1547 /// Return a DeclaratorChunk for a reference.
1548 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1549 bool lvalue) {
1550 DeclaratorChunk I;
1551 I.Kind = Reference;
1552 I.Loc = Loc;
1553 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1554 I.Ref.LValueRef = lvalue;
1555 return I;
1556 }
1557
1558 /// Return a DeclaratorChunk for an array.
1559 static DeclaratorChunk getArray(unsigned TypeQuals,
1560 bool isStatic, bool isStar, Expr *NumElts,
1561 SourceLocation LBLoc, SourceLocation RBLoc) {
1562 DeclaratorChunk I;
1563 I.Kind = Array;
1564 I.Loc = LBLoc;
1565 I.EndLoc = RBLoc;
1566 I.Arr.TypeQuals = TypeQuals;
1567 I.Arr.hasStatic = isStatic;
1568 I.Arr.isStar = isStar;
1569 I.Arr.NumElts = NumElts;
1570 return I;
1571 }
1572
1573 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1574 /// "TheDeclarator" is the declarator that this will be added to.
1575 static DeclaratorChunk getFunction(bool HasProto,
1576 bool IsAmbiguous,
1577 SourceLocation LParenLoc,
1578 ParamInfo *Params, unsigned NumParams,
1579 SourceLocation EllipsisLoc,
1580 SourceLocation RParenLoc,
1581 unsigned TypeQuals,
1582 bool RefQualifierIsLvalueRef,
1583 SourceLocation RefQualifierLoc,
1584 SourceLocation ConstQualifierLoc,
1585 SourceLocation VolatileQualifierLoc,
1586 SourceLocation RestrictQualifierLoc,
1587 SourceLocation MutableLoc,
1588 ExceptionSpecificationType ESpecType,
1589 SourceRange ESpecRange,
1590 ParsedType *Exceptions,
1591 SourceRange *ExceptionRanges,
1592 unsigned NumExceptions,
1593 Expr *NoexceptExpr,
1594 CachedTokens *ExceptionSpecTokens,
1595 ArrayRef<NamedDecl *> DeclsInPrototype,
1596 SourceLocation LocalRangeBegin,
1597 SourceLocation LocalRangeEnd,
1598 Declarator &TheDeclarator,
1599 TypeResult TrailingReturnType =
1600 TypeResult());
1601
1602 /// Return a DeclaratorChunk for a block.
1603 static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1604 SourceLocation Loc) {
1605 DeclaratorChunk I;
1606 I.Kind = BlockPointer;
1607 I.Loc = Loc;
1608 I.Cls.TypeQuals = TypeQuals;
1609 return I;
1610 }
1611
1612 /// Return a DeclaratorChunk for a block.
1613 static DeclaratorChunk getPipe(unsigned TypeQuals,
1614 SourceLocation Loc) {
1615 DeclaratorChunk I;
1616 I.Kind = Pipe;
1617 I.Loc = Loc;
1618 I.Cls.TypeQuals = TypeQuals;
1619 return I;
1620 }
1621
1622 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1623 unsigned TypeQuals,
1624 SourceLocation Loc) {
1625 DeclaratorChunk I;
1626 I.Kind = MemberPointer;
1627 I.Loc = SS.getBeginLoc();
1628 I.EndLoc = Loc;
1629 I.Mem.TypeQuals = TypeQuals;
1630 new (I.Mem.ScopeMem) CXXScopeSpec(SS);
1631 return I;
1632 }
1633
1634 /// Return a DeclaratorChunk for a paren.
1635 static DeclaratorChunk getParen(SourceLocation LParenLoc,
1636 SourceLocation RParenLoc) {
1637 DeclaratorChunk I;
1638 I.Kind = Paren;
1639 I.Loc = LParenLoc;
1640 I.EndLoc = RParenLoc;
1641 return I;
1642 }
1643
1644 bool isParen() const {
1645 return Kind == Paren;
1646 }
1647};
1648
1649/// A parsed C++17 decomposition declarator of the form
1650/// '[' identifier-list ']'
1651class DecompositionDeclarator {
1652public:
1653 struct Binding {
1654 IdentifierInfo *Name;
1655 SourceLocation NameLoc;
1656 };
1657
1658private:
1659 /// The locations of the '[' and ']' tokens.
1660 SourceLocation LSquareLoc, RSquareLoc;
1661
1662 /// The bindings.
1663 Binding *Bindings;
1664 unsigned NumBindings : 31;
1665 unsigned DeleteBindings : 1;
1666
1667 friend class Declarator;
1668
1669public:
1670 DecompositionDeclarator()
1671 : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {}
1672 DecompositionDeclarator(const DecompositionDeclarator &G) = delete;
1673 DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete;
1674 ~DecompositionDeclarator() {
1675 if (DeleteBindings)
1676 delete[] Bindings;
1677 }
1678
1679 void clear() {
1680 LSquareLoc = RSquareLoc = SourceLocation();
1681 if (DeleteBindings)
1682 delete[] Bindings;
1683 Bindings = nullptr;
1684 NumBindings = 0;
1685 DeleteBindings = false;
1686 }
1687
1688 ArrayRef<Binding> bindings() const {
1689 return llvm::makeArrayRef(Bindings, NumBindings);
1690 }
1691
1692 bool isSet() const { return LSquareLoc.isValid(); }
1693
1694 SourceLocation getLSquareLoc() const { return LSquareLoc; }
1695 SourceLocation getRSquareLoc() const { return RSquareLoc; }
1696 SourceRange getSourceRange() const {
1697 return SourceRange(LSquareLoc, RSquareLoc);
1698 }
1699};
1700
1701/// Described the kind of function definition (if any) provided for
1702/// a function.
1703enum FunctionDefinitionKind {
1704 FDK_Declaration,
1705 FDK_Definition,
1706 FDK_Defaulted,
1707 FDK_Deleted
1708};
1709
1710enum class DeclaratorContext {
1711 FileContext, // File scope declaration.
1712 PrototypeContext, // Within a function prototype.
1713 ObjCResultContext, // An ObjC method result type.
1714 ObjCParameterContext,// An ObjC method parameter type.
1715 KNRTypeListContext, // K&R type definition list for formals.
1716 TypeNameContext, // Abstract declarator for types.
1717 FunctionalCastContext, // Type in a C++ functional cast expression.
1718 MemberContext, // Struct/Union field.
1719 BlockContext, // Declaration within a block in a function.
1720 ForContext, // Declaration within first part of a for loop.
1721 InitStmtContext, // Declaration within optional init stmt of if/switch.
1722 ConditionContext, // Condition declaration in a C++ if/switch/while/for.
1723 TemplateParamContext,// Within a template parameter list.
1724 CXXNewContext, // C++ new-expression.
1725 CXXCatchContext, // C++ catch exception-declaration
1726 ObjCCatchContext, // Objective-C catch exception-declaration
1727 BlockLiteralContext, // Block literal declarator.
1728 LambdaExprContext, // Lambda-expression declarator.
1729 LambdaExprParameterContext, // Lambda-expression parameter declarator.
1730 ConversionIdContext, // C++ conversion-type-id.
1731 TrailingReturnContext, // C++11 trailing-type-specifier.
1732 TrailingReturnVarContext, // C++11 trailing-type-specifier for variable.
1733 TemplateArgContext, // Any template argument (in template argument list).
1734 TemplateTypeArgContext, // Template type argument (in default argument).
1735 AliasDeclContext, // C++11 alias-declaration.
1736 AliasTemplateContext // C++11 alias-declaration template.
1737};
1738
1739
1740/// Information about one declarator, including the parsed type
1741/// information and the identifier.
1742///
1743/// When the declarator is fully formed, this is turned into the appropriate
1744/// Decl object.
1745///
1746/// Declarators come in two types: normal declarators and abstract declarators.
1747/// Abstract declarators are used when parsing types, and don't have an
1748/// identifier. Normal declarators do have ID's.
1749///
1750/// Instances of this class should be a transient object that lives on the
1751/// stack, not objects that are allocated in large quantities on the heap.
1752class Declarator {
1753
1754private:
1755 const DeclSpec &DS;
1756 CXXScopeSpec SS;
1757 UnqualifiedId Name;
1758 SourceRange Range;
1759
1760 /// Where we are parsing this declarator.
1761 DeclaratorContext Context;
1762
1763 /// The C++17 structured binding, if any. This is an alternative to a Name.
1764 DecompositionDeclarator BindingGroup;
1765
1766 /// DeclTypeInfo - This holds each type that the declarator includes as it is
1767 /// parsed. This is pushed from the identifier out, which means that element
1768 /// #0 will be the most closely bound to the identifier, and
1769 /// DeclTypeInfo.back() will be the least closely bound.
1770 SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1771
1772 /// InvalidType - Set by Sema::GetTypeForDeclarator().
1773 unsigned InvalidType : 1;
1774
1775 /// GroupingParens - Set by Parser::ParseParenDeclarator().
1776 unsigned GroupingParens : 1;
1777
1778 /// FunctionDefinition - Is this Declarator for a function or member
1779 /// definition and, if so, what kind?
1780 ///
1781 /// Actually a FunctionDefinitionKind.
1782 unsigned FunctionDefinition : 2;
1783
1784 /// Is this Declarator a redeclaration?
1785 unsigned Redeclaration : 1;
1786
1787 /// true if the declaration is preceded by \c __extension__.
1788 unsigned Extension : 1;
1789
1790 /// Indicates whether this is an Objective-C instance variable.
1791 unsigned ObjCIvar : 1;
1792
1793 /// Indicates whether this is an Objective-C 'weak' property.
1794 unsigned ObjCWeakProperty : 1;
1795
1796 /// Indicates whether the InlineParams / InlineBindings storage has been used.
1797 unsigned InlineStorageUsed : 1;
1798
1799 /// Attrs - Attributes.
1800 ParsedAttributes Attrs;
1801
1802 /// The asm label, if specified.
1803 Expr *AsmLabel;
1804
1805#ifndef _MSC_VER
1806 union {
1807#endif
1808 /// InlineParams - This is a local array used for the first function decl
1809 /// chunk to avoid going to the heap for the common case when we have one
1810 /// function chunk in the declarator.
1811 DeclaratorChunk::ParamInfo InlineParams[16];
1812 DecompositionDeclarator::Binding InlineBindings[16];
1813#ifndef _MSC_VER
1814 };
1815#endif
1816
1817 /// If this is the second or subsequent declarator in this declaration,
1818 /// the location of the comma before this declarator.
1819 SourceLocation CommaLoc;
1820
1821 /// If provided, the source location of the ellipsis used to describe
1822 /// this declarator as a parameter pack.
1823 SourceLocation EllipsisLoc;
1824
1825 friend struct DeclaratorChunk;
1826
1827public:
1828 Declarator(const DeclSpec &ds, DeclaratorContext C)
1829 : DS(ds), Range(ds.getSourceRange()), Context(C),
1830 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1831 GroupingParens(false), FunctionDefinition(FDK_Declaration),
1832 Redeclaration(false), Extension(false), ObjCIvar(false),
1833 ObjCWeakProperty(false), InlineStorageUsed(false),
1834 Attrs(ds.getAttributePool().getFactory()), AsmLabel(nullptr) {}
1835
1836 ~Declarator() {
1837 clear();
1838 }
1839 /// getDeclSpec - Return the declaration-specifier that this declarator was
1840 /// declared with.
1841 const DeclSpec &getDeclSpec() const { return DS; }
1842
1843 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1844 /// should be used with extreme care: declspecs can often be shared between
1845 /// multiple declarators, so mutating the DeclSpec affects all of the
1846 /// Declarators. This should only be done when the declspec is known to not
1847 /// be shared or when in error recovery etc.
1848 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1849
1850 AttributePool &getAttributePool() const {
1851 return Attrs.getPool();
1852 }
1853
1854 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1855 /// nested-name-specifier) that is part of the declarator-id.
1856 const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1857 CXXScopeSpec &getCXXScopeSpec() { return SS; }
1858
1859 /// Retrieve the name specified by this declarator.
1860 UnqualifiedId &getName() { return Name; }
1861
1862 const DecompositionDeclarator &getDecompositionDeclarator() const {
1863 return BindingGroup;
1864 }
1865
1866 DeclaratorContext getContext() const { return Context; }
1867
1868 bool isPrototypeContext() const {
1869 return (Context == DeclaratorContext::PrototypeContext ||
1870 Context == DeclaratorContext::ObjCParameterContext ||
1871 Context == DeclaratorContext::ObjCResultContext ||
1872 Context == DeclaratorContext::LambdaExprParameterContext);
1873 }
1874
1875 /// Get the source range that spans this declarator.
1876 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
1877 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1878 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
1879 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1880 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
1881
1882 void SetSourceRange(SourceRange R) { Range = R; }
1883 /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1884 /// invalid.
1885 void SetRangeBegin(SourceLocation Loc) {
1886 if (!Loc.isInvalid())
1887 Range.setBegin(Loc);
1888 }
1889 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1890 void SetRangeEnd(SourceLocation Loc) {
1891 if (!Loc.isInvalid())
1892 Range.setEnd(Loc);
1893 }
1894 /// ExtendWithDeclSpec - Extend the declarator source range to include the
1895 /// given declspec, unless its location is invalid. Adopts the range start if
1896 /// the current range start is invalid.
1897 void ExtendWithDeclSpec(const DeclSpec &DS) {
1898 SourceRange SR = DS.getSourceRange();
1899 if (Range.getBegin().isInvalid())
1900 Range.setBegin(SR.getBegin());
1901 if (!SR.getEnd().isInvalid())
1902 Range.setEnd(SR.getEnd());
1903 }
1904
1905 /// Reset the contents of this Declarator.
1906 void clear() {
1907 SS.clear();
1908 Name.clear();
1909 Range = DS.getSourceRange();
1910 BindingGroup.clear();
1911
1912 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1913 DeclTypeInfo[i].destroy();
1914 DeclTypeInfo.clear();
1915 Attrs.clear();
1916 AsmLabel = nullptr;
1917 InlineStorageUsed = false;
1918 ObjCIvar = false;
1919 ObjCWeakProperty = false;
1920 CommaLoc = SourceLocation();
1921 EllipsisLoc = SourceLocation();
1922 }
1923
1924 /// mayOmitIdentifier - Return true if the identifier is either optional or
1925 /// not allowed. This is true for typenames, prototypes, and template
1926 /// parameter lists.
1927 bool mayOmitIdentifier() const {
1928 switch (Context) {
1929 case DeclaratorContext::FileContext:
1930 case DeclaratorContext::KNRTypeListContext:
1931 case DeclaratorContext::MemberContext:
1932 case DeclaratorContext::BlockContext:
1933 case DeclaratorContext::ForContext:
1934 case DeclaratorContext::InitStmtContext:
1935 case DeclaratorContext::ConditionContext:
1936 return false;
1937
1938 case DeclaratorContext::TypeNameContext:
1939 case DeclaratorContext::FunctionalCastContext:
1940 case DeclaratorContext::AliasDeclContext:
1941 case DeclaratorContext::AliasTemplateContext:
1942 case DeclaratorContext::PrototypeContext:
1943 case DeclaratorContext::LambdaExprParameterContext:
1944 case DeclaratorContext::ObjCParameterContext:
1945 case DeclaratorContext::ObjCResultContext:
1946 case DeclaratorContext::TemplateParamContext:
1947 case DeclaratorContext::CXXNewContext:
1948 case DeclaratorContext::CXXCatchContext:
1949 case DeclaratorContext::ObjCCatchContext:
1950 case DeclaratorContext::BlockLiteralContext:
1951 case DeclaratorContext::LambdaExprContext:
1952 case DeclaratorContext::ConversionIdContext:
1953 case DeclaratorContext::TemplateArgContext:
1954 case DeclaratorContext::TemplateTypeArgContext:
1955 case DeclaratorContext::TrailingReturnContext:
1956 case DeclaratorContext::TrailingReturnVarContext:
1957 return true;
1958 }
1959 llvm_unreachable("unknown context kind!");
1960 }
1961
1962 /// mayHaveIdentifier - Return true if the identifier is either optional or
1963 /// required. This is true for normal declarators and prototypes, but not
1964 /// typenames.
1965 bool mayHaveIdentifier() const {
1966 switch (Context) {
1967 case DeclaratorContext::FileContext:
1968 case DeclaratorContext::KNRTypeListContext:
1969 case DeclaratorContext::MemberContext:
1970 case DeclaratorContext::BlockContext:
1971 case DeclaratorContext::ForContext:
1972 case DeclaratorContext::InitStmtContext:
1973 case DeclaratorContext::ConditionContext:
1974 case DeclaratorContext::PrototypeContext:
1975 case DeclaratorContext::LambdaExprParameterContext:
1976 case DeclaratorContext::TemplateParamContext:
1977 case DeclaratorContext::CXXCatchContext:
1978 case DeclaratorContext::ObjCCatchContext:
1979 return true;
1980
1981 case DeclaratorContext::TypeNameContext:
1982 case DeclaratorContext::FunctionalCastContext:
1983 case DeclaratorContext::CXXNewContext:
1984 case DeclaratorContext::AliasDeclContext:
1985 case DeclaratorContext::AliasTemplateContext:
1986 case DeclaratorContext::ObjCParameterContext:
1987 case DeclaratorContext::ObjCResultContext:
1988 case DeclaratorContext::BlockLiteralContext:
1989 case DeclaratorContext::LambdaExprContext:
1990 case DeclaratorContext::ConversionIdContext:
1991 case DeclaratorContext::TemplateArgContext:
1992 case DeclaratorContext::TemplateTypeArgContext:
1993 case DeclaratorContext::TrailingReturnContext:
1994 case DeclaratorContext::TrailingReturnVarContext:
1995 return false;
1996 }
1997 llvm_unreachable("unknown context kind!");
1998 }
1999
2000 /// Return true if the context permits a C++17 decomposition declarator.
2001 bool mayHaveDecompositionDeclarator() const {
2002 switch (Context) {
2003 case DeclaratorContext::FileContext:
2004 // FIXME: It's not clear that the proposal meant to allow file-scope
2005 // structured bindings, but it does.
2006 case DeclaratorContext::BlockContext:
2007 case DeclaratorContext::ForContext:
2008 case DeclaratorContext::InitStmtContext:
2009 case DeclaratorContext::ConditionContext:
2010 return true;
2011
2012 case DeclaratorContext::MemberContext:
2013 case DeclaratorContext::PrototypeContext:
2014 case DeclaratorContext::TemplateParamContext:
2015 // Maybe one day...
2016 return false;
2017
2018 // These contexts don't allow any kind of non-abstract declarator.
2019 case DeclaratorContext::KNRTypeListContext:
2020 case DeclaratorContext::TypeNameContext:
2021 case DeclaratorContext::FunctionalCastContext:
2022 case DeclaratorContext::AliasDeclContext:
2023 case DeclaratorContext::AliasTemplateContext:
2024 case DeclaratorContext::LambdaExprParameterContext:
2025 case DeclaratorContext::ObjCParameterContext:
2026 case DeclaratorContext::ObjCResultContext:
2027 case DeclaratorContext::CXXNewContext:
2028 case DeclaratorContext::CXXCatchContext:
2029 case DeclaratorContext::ObjCCatchContext:
2030 case DeclaratorContext::BlockLiteralContext:
2031 case DeclaratorContext::LambdaExprContext:
2032 case DeclaratorContext::ConversionIdContext:
2033 case DeclaratorContext::TemplateArgContext:
2034 case DeclaratorContext::TemplateTypeArgContext:
2035 case DeclaratorContext::TrailingReturnContext:
2036 case DeclaratorContext::TrailingReturnVarContext:
2037 return false;
2038 }
2039 llvm_unreachable("unknown context kind!");
2040 }
2041
2042 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
2043 /// followed by a C++ direct initializer, e.g. "int x(1);".
2044 bool mayBeFollowedByCXXDirectInit() const {
2045 if (hasGroupingParens()) return false;
2046
2047 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2048 return false;
2049
2050 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern &&
2051 Context != DeclaratorContext::FileContext)
2052 return false;
2053
2054 // Special names can't have direct initializers.
2055 if (Name.getKind() != UnqualifiedIdKind::IK_Identifier)
2056 return false;
2057
2058 switch (Context) {
2059 case DeclaratorContext::FileContext:
2060 case DeclaratorContext::BlockContext:
2061 case DeclaratorContext::ForContext:
2062 case DeclaratorContext::InitStmtContext:
2063 case DeclaratorContext::TrailingReturnVarContext:
2064 return true;
2065
2066 case DeclaratorContext::ConditionContext:
2067 // This may not be followed by a direct initializer, but it can't be a
2068 // function declaration either, and we'd prefer to perform a tentative
2069 // parse in order to produce the right diagnostic.
2070 return true;
2071
2072 case DeclaratorContext::KNRTypeListContext:
2073 case DeclaratorContext::MemberContext:
2074 case DeclaratorContext::PrototypeContext:
2075 case DeclaratorContext::LambdaExprParameterContext:
2076 case DeclaratorContext::ObjCParameterContext:
2077 case DeclaratorContext::ObjCResultContext:
2078 case DeclaratorContext::TemplateParamContext:
2079 case DeclaratorContext::CXXCatchContext:
2080 case DeclaratorContext::ObjCCatchContext:
2081 case DeclaratorContext::TypeNameContext:
2082 case DeclaratorContext::FunctionalCastContext: // FIXME
2083 case DeclaratorContext::CXXNewContext:
2084 case DeclaratorContext::AliasDeclContext:
2085 case DeclaratorContext::AliasTemplateContext:
2086 case DeclaratorContext::BlockLiteralContext:
2087 case DeclaratorContext::LambdaExprContext:
2088 case DeclaratorContext::ConversionIdContext:
2089 case DeclaratorContext::TemplateArgContext:
2090 case DeclaratorContext::TemplateTypeArgContext:
2091 case DeclaratorContext::TrailingReturnContext:
2092 return false;
2093 }
2094 llvm_unreachable("unknown context kind!");
2095 }
2096
2097 /// isPastIdentifier - Return true if we have parsed beyond the point where
2098 /// the name would appear. (This may happen even if we haven't actually parsed
2099 /// a name, perhaps because this context doesn't require one.)
2100 bool isPastIdentifier() const { return Name.isValid(); }
2101
2102 /// hasName - Whether this declarator has a name, which might be an
2103 /// identifier (accessible via getIdentifier()) or some kind of
2104 /// special C++ name (constructor, destructor, etc.), or a structured
2105 /// binding (which is not exactly a name, but occupies the same position).
2106 bool hasName() const {
2107 return Name.getKind() != UnqualifiedIdKind::IK_Identifier ||
2108 Name.Identifier || isDecompositionDeclarator();
2109 }
2110
2111 /// Return whether this declarator is a decomposition declarator.
2112 bool isDecompositionDeclarator() const {
2113 return BindingGroup.isSet();
2114 }
2115
2116 IdentifierInfo *getIdentifier() const {
2117 if (Name.getKind() == UnqualifiedIdKind::IK_Identifier)
2118 return Name.Identifier;
2119
2120 return nullptr;
2121 }
2122 SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
2123
2124 /// Set the name of this declarator to be the given identifier.
2125 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
2126 Name.setIdentifier(Id, IdLoc);
2127 }
2128
2129 /// Set the decomposition bindings for this declarator.
2130 void
2131 setDecompositionBindings(SourceLocation LSquareLoc,
2132 ArrayRef<DecompositionDeclarator::Binding> Bindings,
2133 SourceLocation RSquareLoc);
2134
2135 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
2136 /// EndLoc, which should be the last token of the chunk.
2137 /// This function takes attrs by R-Value reference because it takes ownership
2138 /// of those attributes from the parameter.
2139 void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs,
2140 SourceLocation EndLoc) {
2141 DeclTypeInfo.push_back(TI);
2142 DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end());
2143 getAttributePool().takeAllFrom(attrs.getPool());
2144
2145 if (!EndLoc.isInvalid())
2146 SetRangeEnd(EndLoc);
2147 }
2148
2149 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
2150 /// EndLoc, which should be the last token of the chunk.
2151 void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) {
2152 DeclTypeInfo.push_back(TI);
2153
2154 if (!EndLoc.isInvalid())
2155 SetRangeEnd(EndLoc);
2156 }
2157
2158 /// Add a new innermost chunk to this declarator.
2159 void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
2160 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
2161 }
2162
2163 /// Return the number of types applied to this declarator.
2164 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
2165
2166 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
2167 /// closest to the identifier.
2168 const DeclaratorChunk &getTypeObject(unsigned i) const {
2169 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2170 return DeclTypeInfo[i];
2171 }
2172 DeclaratorChunk &getTypeObject(unsigned i) {
2173 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2174 return DeclTypeInfo[i];
2175 }
2176
2177 typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator;
2178 typedef llvm::iterator_range<type_object_iterator> type_object_range;
2179
2180 /// Returns the range of type objects, from the identifier outwards.
2181 type_object_range type_objects() const {
2182 return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end());
2183 }
2184
2185 void DropFirstTypeObject() {
2186 assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
2187 DeclTypeInfo.front().destroy();
2188 DeclTypeInfo.erase(DeclTypeInfo.begin());
2189 }
2190
2191 /// Return the innermost (closest to the declarator) chunk of this
2192 /// declarator that is not a parens chunk, or null if there are no
2193 /// non-parens chunks.
2194 const DeclaratorChunk *getInnermostNonParenChunk() const {
2195 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2196 if (!DeclTypeInfo[i].isParen())
2197 return &DeclTypeInfo[i];
2198 }
2199 return nullptr;
2200 }
2201
2202 /// Return the outermost (furthest from the declarator) chunk of
2203 /// this declarator that is not a parens chunk, or null if there are
2204 /// no non-parens chunks.
2205 const DeclaratorChunk *getOutermostNonParenChunk() const {
2206 for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) {
2207 if (!DeclTypeInfo[i-1].isParen())
2208 return &DeclTypeInfo[i-1];
2209 }
2210 return nullptr;
2211 }
2212
2213 /// isArrayOfUnknownBound - This method returns true if the declarator
2214 /// is a declarator for an array of unknown bound (looking through
2215 /// parentheses).
2216 bool isArrayOfUnknownBound() const {
2217 const DeclaratorChunk *chunk = getInnermostNonParenChunk();
2218 return (chunk && chunk->Kind == DeclaratorChunk::Array &&
2219 !chunk->Arr.NumElts);
2220 }
2221
2222 /// isFunctionDeclarator - This method returns true if the declarator
2223 /// is a function declarator (looking through parentheses).
2224 /// If true is returned, then the reference type parameter idx is
2225 /// assigned with the index of the declaration chunk.
2226 bool isFunctionDeclarator(unsigned& idx) const {
2227 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2228 switch (DeclTypeInfo[i].Kind) {
2229 case DeclaratorChunk::Function:
2230 idx = i;
2231 return true;
2232 case DeclaratorChunk::Paren:
2233 continue;
2234 case DeclaratorChunk::Pointer:
2235 case DeclaratorChunk::Reference:
2236 case DeclaratorChunk::Array:
2237 case DeclaratorChunk::BlockPointer:
2238 case DeclaratorChunk::MemberPointer:
2239 case DeclaratorChunk::Pipe:
2240 return false;
2241 }
2242 llvm_unreachable("Invalid type chunk");
2243 }
2244 return false;
2245 }
2246
2247 /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
2248 /// this method returns true if the identifier is a function declarator
2249 /// (looking through parentheses).
2250 bool isFunctionDeclarator() const {
2251 unsigned index;
2252 return isFunctionDeclarator(index);
2253 }
2254
2255 /// getFunctionTypeInfo - Retrieves the function type info object
2256 /// (looking through parentheses).
2257 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
2258 assert(isFunctionDeclarator() && "Not a function declarator!");
2259 unsigned index = 0;
2260 isFunctionDeclarator(index);
2261 return DeclTypeInfo[index].Fun;
2262 }
2263
2264 /// getFunctionTypeInfo - Retrieves the function type info object
2265 /// (looking through parentheses).
2266 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
2267 return const_cast<Declarator*>(this)->getFunctionTypeInfo();
2268 }
2269
2270 /// Determine whether the declaration that will be produced from
2271 /// this declaration will be a function.
2272 ///
2273 /// A declaration can declare a function even if the declarator itself
2274 /// isn't a function declarator, if the type specifier refers to a function
2275 /// type. This routine checks for both cases.
2276 bool isDeclarationOfFunction() const;
2277
2278 /// Return true if this declaration appears in a context where a
2279 /// function declarator would be a function declaration.
2280 bool isFunctionDeclarationContext() const {
2281 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2282 return false;
2283
2284 switch (Context) {
2285 case DeclaratorContext::FileContext:
2286 case DeclaratorContext::MemberContext:
2287 case DeclaratorContext::BlockContext:
2288 case DeclaratorContext::ForContext:
2289 case DeclaratorContext::InitStmtContext:
2290 return true;
2291
2292 case DeclaratorContext::ConditionContext:
2293 case DeclaratorContext::KNRTypeListContext:
2294 case DeclaratorContext::TypeNameContext:
2295 case DeclaratorContext::FunctionalCastContext:
2296 case DeclaratorContext::AliasDeclContext:
2297 case DeclaratorContext::AliasTemplateContext:
2298 case DeclaratorContext::PrototypeContext:
2299 case DeclaratorContext::LambdaExprParameterContext:
2300 case DeclaratorContext::ObjCParameterContext:
2301 case DeclaratorContext::ObjCResultContext:
2302 case DeclaratorContext::TemplateParamContext:
2303 case DeclaratorContext::CXXNewContext:
2304 case DeclaratorContext::CXXCatchContext:
2305 case DeclaratorContext::ObjCCatchContext:
2306 case DeclaratorContext::BlockLiteralContext:
2307 case DeclaratorContext::LambdaExprContext:
2308 case DeclaratorContext::ConversionIdContext:
2309 case DeclaratorContext::TemplateArgContext:
2310 case DeclaratorContext::TemplateTypeArgContext:
2311 case DeclaratorContext::TrailingReturnContext:
2312 case DeclaratorContext::TrailingReturnVarContext:
2313 return false;
2314 }
2315 llvm_unreachable("unknown context kind!");
2316 }
2317
2318 /// Determine whether this declaration appears in a context where an
2319 /// expression could appear.
2320 bool isExpressionContext() const {
2321 switch (Context) {
2322 case DeclaratorContext::FileContext:
2323 case DeclaratorContext::KNRTypeListContext:
2324 case DeclaratorContext::MemberContext:
2325
2326 // FIXME: sizeof(...) permits an expression.
2327 case DeclaratorContext::TypeNameContext:
2328
2329 case DeclaratorContext::FunctionalCastContext:
2330 case DeclaratorContext::AliasDeclContext:
2331 case DeclaratorContext::AliasTemplateContext:
2332 case DeclaratorContext::PrototypeContext:
2333 case DeclaratorContext::LambdaExprParameterContext:
2334 case DeclaratorContext::ObjCParameterContext:
2335 case DeclaratorContext::ObjCResultContext:
2336 case DeclaratorContext::TemplateParamContext:
2337 case DeclaratorContext::CXXNewContext:
2338 case DeclaratorContext::CXXCatchContext:
2339 case DeclaratorContext::ObjCCatchContext:
2340 case DeclaratorContext::BlockLiteralContext:
2341 case DeclaratorContext::LambdaExprContext:
2342 case DeclaratorContext::ConversionIdContext:
2343 case DeclaratorContext::TrailingReturnContext:
2344 case DeclaratorContext::TrailingReturnVarContext:
2345 case DeclaratorContext::TemplateTypeArgContext:
2346 return false;
2347
2348 case DeclaratorContext::BlockContext:
2349 case DeclaratorContext::ForContext:
2350 case DeclaratorContext::InitStmtContext:
2351 case DeclaratorContext::ConditionContext:
2352 case DeclaratorContext::TemplateArgContext:
2353 return true;
2354 }
2355
2356 llvm_unreachable("unknown context kind!");
2357 }
2358
2359 /// Return true if a function declarator at this position would be a
2360 /// function declaration.
2361 bool isFunctionDeclaratorAFunctionDeclaration() const {
2362 if (!isFunctionDeclarationContext())
2363 return false;
2364
2365 for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I)
2366 if (getTypeObject(I).Kind != DeclaratorChunk::Paren)
2367 return false;
2368
2369 return true;
2370 }
2371
2372 /// Determine whether a trailing return type was written (at any
2373 /// level) within this declarator.
2374 bool hasTrailingReturnType() const {
2375 for (const auto &Chunk : type_objects())
2376 if (Chunk.Kind == DeclaratorChunk::Function &&
2377 Chunk.Fun.hasTrailingReturnType())
2378 return true;
2379 return false;
2380 }
2381
2382 /// takeAttributes - Takes attributes from the given parsed-attributes
2383 /// set and add them to this declarator.
2384 ///
2385 /// These examples both add 3 attributes to "var":
2386 /// short int var __attribute__((aligned(16),common,deprecated));
2387 /// short int x, __attribute__((aligned(16)) var
2388 /// __attribute__((common,deprecated));
2389 ///
2390 /// Also extends the range of the declarator.
2391 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
2392 Attrs.takeAllFrom(attrs);
2393
2394 if (!lastLoc.isInvalid())
2395 SetRangeEnd(lastLoc);
2396 }
2397
2398 const ParsedAttributes &getAttributes() const { return Attrs; }
2399 ParsedAttributes &getAttributes() { return Attrs; }
2400
2401 /// hasAttributes - do we contain any attributes?
2402 bool hasAttributes() const {
2403 if (!getAttributes().empty() || getDeclSpec().hasAttributes())
2404 return true;
2405 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
2406 if (!getTypeObject(i).getAttrs().empty())
2407 return true;
2408 return false;
2409 }
2410
2411 /// Return a source range list of C++11 attributes associated
2412 /// with the declarator.
2413 void getCXX11AttributeRanges(SmallVectorImpl<SourceRange> &Ranges) {
2414 for (const ParsedAttr &AL : Attrs)
2415 if (AL.isCXX11Attribute())
2416 Ranges.push_back(AL.getRange());
2417 }
2418
2419 void setAsmLabel(Expr *E) { AsmLabel = E; }
2420 Expr *getAsmLabel() const { return AsmLabel; }
2421
2422 void setExtension(bool Val = true) { Extension = Val; }
2423 bool getExtension() const { return Extension; }
2424
2425 void setObjCIvar(bool Val = true) { ObjCIvar = Val; }
2426 bool isObjCIvar() const { return ObjCIvar; }
2427
2428 void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; }
2429 bool isObjCWeakProperty() const { return ObjCWeakProperty; }
2430
2431 void setInvalidType(bool Val = true) { InvalidType = Val; }
2432 bool isInvalidType() const {
2433 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
2434 }
2435
2436 void setGroupingParens(bool flag) { GroupingParens = flag; }
2437 bool hasGroupingParens() const { return GroupingParens; }
2438
2439 bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
2440 SourceLocation getCommaLoc() const { return CommaLoc; }
2441 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
2442
2443 bool hasEllipsis() const { return EllipsisLoc.isValid(); }
2444 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
2445 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
2446
2447 void setFunctionDefinitionKind(FunctionDefinitionKind Val) {
2448 FunctionDefinition = Val;
2449 }
2450
2451 bool isFunctionDefinition() const {
2452 return getFunctionDefinitionKind() != FDK_Declaration;
2453 }
2454
2455 FunctionDefinitionKind getFunctionDefinitionKind() const {
2456 return (FunctionDefinitionKind)FunctionDefinition;
2457 }
2458
2459 /// Returns true if this declares a real member and not a friend.
2460 bool isFirstDeclarationOfMember() {
2461 return getContext() == DeclaratorContext::MemberContext &&
2462 !getDeclSpec().isFriendSpecified();
2463 }
2464
2465 /// Returns true if this declares a static member. This cannot be called on a
2466 /// declarator outside of a MemberContext because we won't know until
2467 /// redeclaration time if the decl is static.
2468 bool isStaticMember();
2469
2470 /// Returns true if this declares a constructor or a destructor.
2471 bool isCtorOrDtor();
2472
2473 void setRedeclaration(bool Val) { Redeclaration = Val; }
2474 bool isRedeclaration() const { return Redeclaration; }
2475};
2476
2477/// This little struct is used to capture information about
2478/// structure field declarators, which is basically just a bitfield size.
2479struct FieldDeclarator {
2480 Declarator D;
2481 Expr *BitfieldSize;
2482 explicit FieldDeclarator(const DeclSpec &DS)
2483 : D(DS, DeclaratorContext::MemberContext),
2484 BitfieldSize(nullptr) {}
2485};
2486
2487/// Represents a C++11 virt-specifier-seq.
2488class VirtSpecifiers {
2489public:
2490 enum Specifier {
2491 VS_None = 0,
2492 VS_Override = 1,
2493 VS_Final = 2,
2494 VS_Sealed = 4,
2495 // Represents the __final keyword, which is legal for gcc in pre-C++11 mode.
2496 VS_GNU_Final = 8
2497 };
2498
2499 VirtSpecifiers() : Specifiers(0), LastSpecifier(VS_None) { }
2500
2501 bool SetSpecifier(Specifier VS, SourceLocation Loc,
2502 const char *&PrevSpec);
2503
2504 bool isUnset() const { return Specifiers == 0; }
2505
2506 bool isOverrideSpecified() const { return Specifiers & VS_Override; }
2507 SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
2508
2509 bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); }
2510 bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; }
2511 SourceLocation getFinalLoc() const { return VS_finalLoc; }
2512
2513 void clear() { Specifiers = 0; }
2514
2515 static const char *getSpecifierName(Specifier VS);
2516
2517 SourceLocation getFirstLocation() const { return FirstLocation; }
2518 SourceLocation getLastLocation() const { return LastLocation; }
2519 Specifier getLastSpecifier() const { return LastSpecifier; }
2520
2521private:
2522 unsigned Specifiers;
2523 Specifier LastSpecifier;
2524
2525 SourceLocation VS_overrideLoc, VS_finalLoc;
2526 SourceLocation FirstLocation;
2527 SourceLocation LastLocation;
2528};
2529
2530enum class LambdaCaptureInitKind {
2531 NoInit, //!< [a]
2532 CopyInit, //!< [a = b], [a = {b}]
2533 DirectInit, //!< [a(b)]
2534 ListInit //!< [a{b}]
2535};
2536
2537/// Represents a complete lambda introducer.
2538struct LambdaIntroducer {
2539 /// An individual capture in a lambda introducer.
2540 struct LambdaCapture {
2541 LambdaCaptureKind Kind;
2542 SourceLocation Loc;
2543 IdentifierInfo *Id;
2544 SourceLocation EllipsisLoc;
2545 LambdaCaptureInitKind InitKind;
2546 ExprResult Init;
2547 ParsedType InitCaptureType;
2548 SourceRange ExplicitRange;
2549
2550 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc,
2551 IdentifierInfo *Id, SourceLocation EllipsisLoc,
2552 LambdaCaptureInitKind InitKind, ExprResult Init,
2553 ParsedType InitCaptureType,
2554 SourceRange ExplicitRange)
2555 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc),
2556 InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType),
2557 ExplicitRange(ExplicitRange) {}
2558 };
2559
2560 SourceRange Range;
2561 SourceLocation DefaultLoc;
2562 LambdaCaptureDefault Default;
2563 SmallVector<LambdaCapture, 4> Captures;
2564
2565 LambdaIntroducer()
2566 : Default(LCD_None) {}
2567
2568 /// Append a capture in a lambda introducer.
2569 void addCapture(LambdaCaptureKind Kind,
2570 SourceLocation Loc,
2571 IdentifierInfo* Id,
2572 SourceLocation EllipsisLoc,
2573 LambdaCaptureInitKind InitKind,
2574 ExprResult Init,
2575 ParsedType InitCaptureType,
2576 SourceRange ExplicitRange) {
2577 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
2578 InitCaptureType, ExplicitRange));
2579 }
2580};
2581
2582} // end namespace clang
2583
2584#endif // LLVM_CLANG_SEMA_DECLSPEC_H
2585

Warning: That file was not part of the compilation database. It may have many parsing errors.