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