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