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