1//===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
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// This file implements semantic analysis for declaration specifiers.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Sema/DeclSpec.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/LocInfoType.h"
18#include "clang/AST/TypeLoc.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/Specifiers.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Sema/ParsedTemplate.h"
24#include "clang/Sema/Sema.h"
25#include <cstring>
26using namespace clang;
27
28
29void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
30 assert(TemplateId && "NULL template-id annotation?");
31 assert(!TemplateId->isInvalid() &&
32 "should not convert invalid template-ids to unqualified-ids");
33
34 Kind = UnqualifiedIdKind::IK_TemplateId;
35 this->TemplateId = TemplateId;
36 StartLocation = TemplateId->TemplateNameLoc;
37 EndLocation = TemplateId->RAngleLoc;
38}
39
40void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
41 assert(TemplateId && "NULL template-id annotation?");
42 assert(!TemplateId->isInvalid() &&
43 "should not convert invalid template-ids to unqualified-ids");
44
45 Kind = UnqualifiedIdKind::IK_ConstructorTemplateId;
46 this->TemplateId = TemplateId;
47 StartLocation = TemplateId->TemplateNameLoc;
48 EndLocation = TemplateId->RAngleLoc;
49}
50
51void CXXScopeSpec::Make(ASTContext &Context, TypeLoc TL,
52 SourceLocation ColonColonLoc) {
53 Builder.Make(Context, TL, ColonColonLoc);
54 if (Range.getBegin().isInvalid())
55 Range.setBegin(TL.getBeginLoc());
56 Range.setEnd(ColonColonLoc);
57
58 assert(Range == Builder.getSourceRange() &&
59 "NestedNameSpecifierLoc range computation incorrect");
60}
61
62void CXXScopeSpec::Extend(ASTContext &Context, NamespaceBaseDecl *Namespace,
63 SourceLocation NamespaceLoc,
64 SourceLocation ColonColonLoc) {
65 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
66
67 if (Range.getBegin().isInvalid())
68 Range.setBegin(NamespaceLoc);
69 Range.setEnd(ColonColonLoc);
70
71 assert(Range == Builder.getSourceRange() &&
72 "NestedNameSpecifierLoc range computation incorrect");
73}
74
75void CXXScopeSpec::MakeGlobal(ASTContext &Context,
76 SourceLocation ColonColonLoc) {
77 Builder.MakeGlobal(Context, ColonColonLoc);
78
79 Range = SourceRange(ColonColonLoc);
80
81 assert(Range == Builder.getSourceRange() &&
82 "NestedNameSpecifierLoc range computation incorrect");
83}
84
85void CXXScopeSpec::MakeMicrosoftSuper(ASTContext &Context, CXXRecordDecl *RD,
86 SourceLocation SuperLoc,
87 SourceLocation ColonColonLoc) {
88 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc, ColonColonLoc);
89
90 Range.setBegin(SuperLoc);
91 Range.setEnd(ColonColonLoc);
92
93 assert(Range == Builder.getSourceRange() &&
94 "NestedNameSpecifierLoc range computation incorrect");
95}
96
97void CXXScopeSpec::MakeTrivial(ASTContext &Context,
98 NestedNameSpecifier Qualifier, SourceRange R) {
99 Builder.MakeTrivial(Context, Qualifier, R);
100 Range = R;
101}
102
103void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
104 if (!Other) {
105 Range = SourceRange();
106 Builder.Clear();
107 return;
108 }
109
110 Range = Other.getSourceRange();
111 Builder.Adopt(Other);
112 assert(Range == Builder.getSourceRange() &&
113 "NestedNameSpecifierLoc range computation incorrect");
114}
115
116SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
117 if (!Builder.getRepresentation())
118 return SourceLocation();
119 return Builder.getTemporary().getLocalBeginLoc();
120}
121
122NestedNameSpecifierLoc
123CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
124 if (!Builder.getRepresentation())
125 return NestedNameSpecifierLoc();
126
127 return Builder.getWithLocInContext(Context);
128}
129
130/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
131/// "TheDeclarator" is the declarator that this will be added to.
132DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
133 bool isAmbiguous,
134 SourceLocation LParenLoc,
135 ParamInfo *Params,
136 unsigned NumParams,
137 SourceLocation EllipsisLoc,
138 SourceLocation RParenLoc,
139 bool RefQualifierIsLvalueRef,
140 SourceLocation RefQualifierLoc,
141 SourceLocation MutableLoc,
142 ExceptionSpecificationType
143 ESpecType,
144 SourceRange ESpecRange,
145 ParsedType *Exceptions,
146 SourceRange *ExceptionRanges,
147 unsigned NumExceptions,
148 Expr *NoexceptExpr,
149 CachedTokens *ExceptionSpecTokens,
150 ArrayRef<NamedDecl*>
151 DeclsInPrototype,
152 SourceLocation LocalRangeBegin,
153 SourceLocation LocalRangeEnd,
154 Declarator &TheDeclarator,
155 TypeResult TrailingReturnType,
156 SourceLocation
157 TrailingReturnTypeLoc,
158 DeclSpec *MethodQualifiers) {
159 assert(!(MethodQualifiers && MethodQualifiers->getTypeQualifiers() & DeclSpec::TQ_atomic) &&
160 "function cannot have _Atomic qualifier");
161
162 DeclaratorChunk I;
163 I.Kind = Function;
164 I.Loc = LocalRangeBegin;
165 I.EndLoc = LocalRangeEnd;
166 new (&I.Fun) FunctionTypeInfo;
167 I.Fun.hasPrototype = hasProto;
168 I.Fun.isVariadic = EllipsisLoc.isValid();
169 I.Fun.isAmbiguous = isAmbiguous;
170 I.Fun.LParenLoc = LParenLoc;
171 I.Fun.EllipsisLoc = EllipsisLoc;
172 I.Fun.RParenLoc = RParenLoc;
173 I.Fun.DeleteParams = false;
174 I.Fun.NumParams = NumParams;
175 I.Fun.Params = nullptr;
176 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
177 I.Fun.RefQualifierLoc = RefQualifierLoc;
178 I.Fun.MutableLoc = MutableLoc;
179 I.Fun.ExceptionSpecType = ESpecType;
180 I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin();
181 I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd();
182 I.Fun.NumExceptionsOrDecls = 0;
183 I.Fun.Exceptions = nullptr;
184 I.Fun.NoexceptExpr = nullptr;
185 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
186 TrailingReturnType.isInvalid();
187 I.Fun.TrailingReturnType = TrailingReturnType.get();
188 I.Fun.TrailingReturnTypeLoc = TrailingReturnTypeLoc;
189 I.Fun.MethodQualifiers = nullptr;
190 I.Fun.QualAttrFactory = nullptr;
191
192 if (MethodQualifiers && (MethodQualifiers->getTypeQualifiers() ||
193 MethodQualifiers->getAttributes().size())) {
194 auto &attrs = MethodQualifiers->getAttributes();
195 I.Fun.MethodQualifiers = new DeclSpec(attrs.getPool().getFactory());
196 MethodQualifiers->forEachCVRUQualifier(
197 Handle: [&](DeclSpec::TQ TypeQual, StringRef PrintName, SourceLocation SL) {
198 I.Fun.MethodQualifiers->SetTypeQual(T: TypeQual, Loc: SL);
199 });
200 I.Fun.MethodQualifiers->getAttributes().takeAllPrependingFrom(Other&: attrs);
201 I.Fun.MethodQualifiers->getAttributePool().takeAllFrom(pool&: attrs.getPool());
202 }
203
204 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
205
206 // new[] a parameter array if needed.
207 if (NumParams) {
208 // If the 'InlineParams' in Declarator is unused and big enough, put our
209 // parameter list there (in an effort to avoid new/delete traffic). If it
210 // is already used (consider a function returning a function pointer) or too
211 // small (function with too many parameters), go to the heap.
212 if (!TheDeclarator.InlineStorageUsed &&
213 NumParams <= std::size(TheDeclarator.InlineParams)) {
214 I.Fun.Params = TheDeclarator.InlineParams;
215 new (I.Fun.Params) ParamInfo[NumParams];
216 I.Fun.DeleteParams = false;
217 TheDeclarator.InlineStorageUsed = true;
218 } else {
219 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
220 I.Fun.DeleteParams = true;
221 }
222 for (unsigned i = 0; i < NumParams; i++)
223 I.Fun.Params[i] = std::move(Params[i]);
224 }
225
226 // Check what exception specification information we should actually store.
227 switch (ESpecType) {
228 default: break; // By default, save nothing.
229 case EST_Dynamic:
230 // new[] an exception array if needed
231 if (NumExceptions) {
232 I.Fun.NumExceptionsOrDecls = NumExceptions;
233 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
234 for (unsigned i = 0; i != NumExceptions; ++i) {
235 I.Fun.Exceptions[i].Ty = Exceptions[i];
236 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
237 }
238 }
239 break;
240
241 case EST_DependentNoexcept:
242 case EST_NoexceptFalse:
243 case EST_NoexceptTrue:
244 I.Fun.NoexceptExpr = NoexceptExpr;
245 break;
246
247 case EST_Unparsed:
248 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
249 break;
250 }
251
252 if (!DeclsInPrototype.empty()) {
253 assert(ESpecType == EST_None && NumExceptions == 0 &&
254 "cannot have exception specifiers and decls in prototype");
255 I.Fun.NumExceptionsOrDecls = DeclsInPrototype.size();
256 // Copy the array of decls into stable heap storage.
257 I.Fun.DeclsInPrototype = new NamedDecl *[DeclsInPrototype.size()];
258 for (size_t J = 0; J < DeclsInPrototype.size(); ++J)
259 I.Fun.DeclsInPrototype[J] = DeclsInPrototype[J];
260 }
261
262 return I;
263}
264
265void Declarator::setDecompositionBindings(
266 SourceLocation LSquareLoc,
267 MutableArrayRef<DecompositionDeclarator::Binding> Bindings,
268 SourceLocation RSquareLoc) {
269 assert(!hasName() && "declarator given multiple names!");
270
271 BindingGroup.LSquareLoc = LSquareLoc;
272 BindingGroup.RSquareLoc = RSquareLoc;
273 BindingGroup.NumBindings = Bindings.size();
274 Range.setEnd(RSquareLoc);
275
276 // We're now past the identifier.
277 SetIdentifier(Id: nullptr, IdLoc: LSquareLoc);
278 Name.EndLocation = RSquareLoc;
279
280 // Allocate storage for bindings and stash them away.
281 if (Bindings.size()) {
282 if (!InlineStorageUsed && Bindings.size() <= std::size(InlineBindings)) {
283 BindingGroup.Bindings = InlineBindings;
284 BindingGroup.DeleteBindings = false;
285 InlineStorageUsed = true;
286 } else {
287 BindingGroup.Bindings =
288 new DecompositionDeclarator::Binding[Bindings.size()];
289 BindingGroup.DeleteBindings = true;
290 }
291 std::uninitialized_move(first: Bindings.begin(), last: Bindings.end(),
292 result: BindingGroup.Bindings);
293 }
294}
295
296bool Declarator::isDeclarationOfFunction() const {
297 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
298 switch (DeclTypeInfo[i].Kind) {
299 case DeclaratorChunk::Function:
300 return true;
301 case DeclaratorChunk::Paren:
302 continue;
303 case DeclaratorChunk::Pointer:
304 case DeclaratorChunk::Reference:
305 case DeclaratorChunk::Array:
306 case DeclaratorChunk::BlockPointer:
307 case DeclaratorChunk::MemberPointer:
308 case DeclaratorChunk::Pipe:
309 return false;
310 }
311 llvm_unreachable("Invalid type chunk");
312 }
313
314 switch (DS.getTypeSpecType()) {
315 case TST_atomic:
316 case TST_auto:
317 case TST_auto_type:
318 case TST_bool:
319 case TST_char:
320 case TST_char8:
321 case TST_char16:
322 case TST_char32:
323 case TST_class:
324 case TST_decimal128:
325 case TST_decimal32:
326 case TST_decimal64:
327 case TST_double:
328 case TST_Accum:
329 case TST_Fract:
330 case TST_Float16:
331 case TST_float128:
332 case TST_ibm128:
333 case TST_enum:
334 case TST_error:
335 case TST_float:
336 case TST_half:
337 case TST_int:
338 case TST_int128:
339 case TST_bitint:
340 case TST_struct:
341 case TST_interface:
342 case TST_union:
343 case TST_unknown_anytype:
344 case TST_unspecified:
345 case TST_void:
346 case TST_wchar:
347 case TST_BFloat16:
348 case TST_typename_pack_indexing:
349#define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
350#include "clang/Basic/OpenCLImageTypes.def"
351#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case TST_##Name:
352#include "clang/Basic/HLSLIntangibleTypes.def"
353 return false;
354
355 case TST_decltype_auto:
356 // This must have an initializer, so can't be a function declaration,
357 // even if the initializer has function type.
358 return false;
359
360 case TST_decltype:
361 case TST_typeof_unqualExpr:
362 case TST_typeofExpr:
363 if (Expr *E = DS.getRepAsExpr())
364 return E->getType()->isFunctionType();
365 return false;
366
367#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case TST_##Trait:
368#include "clang/Basic/TransformTypeTraits.def"
369 case TST_typename:
370 case TST_typeof_unqualType:
371 case TST_typeofType: {
372 QualType QT = DS.getRepAsType().get();
373 if (QT.isNull())
374 return false;
375
376 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(Val&: QT))
377 QT = LIT->getType();
378
379 if (QT.isNull())
380 return false;
381
382 return QT->isFunctionType();
383 }
384 }
385
386 llvm_unreachable("Invalid TypeSpecType!");
387}
388
389bool Declarator::isStaticMember() {
390 assert(getContext() == DeclaratorContext::Member);
391 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
392 (!isDeclarationOfFunction() && !getTemplateParameterLists().empty()) ||
393 (getName().getKind() == UnqualifiedIdKind::IK_OperatorFunctionId &&
394 CXXMethodDecl::isStaticOverloadedOperator(
395 OOK: getName().OperatorFunctionId.Operator));
396}
397
398bool Declarator::isExplicitObjectMemberFunction() {
399 if (!isFunctionDeclarator())
400 return false;
401 DeclaratorChunk::FunctionTypeInfo &Fun = getFunctionTypeInfo();
402 if (Fun.NumParams) {
403 auto *P = dyn_cast_or_null<ParmVarDecl>(Val: Fun.Params[0].Param);
404 if (P && P->isExplicitObjectParameter())
405 return true;
406 }
407 return false;
408}
409
410bool Declarator::isCtorOrDtor() {
411 return (getName().getKind() == UnqualifiedIdKind::IK_ConstructorName) ||
412 (getName().getKind() == UnqualifiedIdKind::IK_DestructorName);
413}
414
415void DeclSpec::forEachCVRUQualifier(
416 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
417 if (TypeQualifiers & TQ_const)
418 Handle(TQ_const, "const", TQ_constLoc);
419 if (TypeQualifiers & TQ_volatile)
420 Handle(TQ_volatile, "volatile", TQ_volatileLoc);
421 if (TypeQualifiers & TQ_restrict)
422 Handle(TQ_restrict, "restrict", TQ_restrictLoc);
423 if (TypeQualifiers & TQ_unaligned)
424 Handle(TQ_unaligned, "unaligned", TQ_unalignedLoc);
425}
426
427void DeclSpec::forEachQualifier(
428 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
429 forEachCVRUQualifier(Handle);
430 // FIXME: Add code below to iterate through the attributes and call Handle.
431}
432
433bool DeclSpec::hasTagDefinition() const {
434 if (!TypeSpecOwned)
435 return false;
436 return cast<TagDecl>(Val: getRepAsDecl())->isCompleteDefinition();
437}
438
439/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
440/// declaration specifier includes.
441///
442unsigned DeclSpec::getParsedSpecifiers() const {
443 unsigned Res = 0;
444 if (StorageClassSpec != SCS_unspecified ||
445 ThreadStorageClassSpec != TSCS_unspecified)
446 Res |= PQ_StorageClassSpecifier;
447
448 if (TypeQualifiers != TQ_unspecified)
449 Res |= PQ_TypeQualifier;
450
451 if (hasTypeSpecifier())
452 Res |= PQ_TypeSpecifier;
453
454 if (FS_inline_specified || FS_virtual_specified || hasExplicitSpecifier() ||
455 FS_noreturn_specified || FS_forceinline_specified)
456 Res |= PQ_FunctionSpecifier;
457 return Res;
458}
459
460template <class T> static bool BadSpecifier(T TNew, T TPrev,
461 const char *&PrevSpec,
462 unsigned &DiagID,
463 bool IsExtension = true) {
464 PrevSpec = DeclSpec::getSpecifierName(TPrev);
465 if (TNew != TPrev)
466 DiagID = diag::err_invalid_decl_spec_combination;
467 else
468 DiagID = IsExtension ? diag::ext_warn_duplicate_declspec :
469 diag::warn_duplicate_declspec;
470 return true;
471}
472
473const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
474 switch (S) {
475 case DeclSpec::SCS_unspecified: return "unspecified";
476 case DeclSpec::SCS_typedef: return "typedef";
477 case DeclSpec::SCS_extern: return "extern";
478 case DeclSpec::SCS_static: return "static";
479 case DeclSpec::SCS_auto: return "auto";
480 case DeclSpec::SCS_register: return "register";
481 case DeclSpec::SCS_private_extern: return "__private_extern__";
482 case DeclSpec::SCS_mutable: return "mutable";
483 }
484 llvm_unreachable("Unknown typespec!");
485}
486
487const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
488 switch (S) {
489 case DeclSpec::TSCS_unspecified: return "unspecified";
490 case DeclSpec::TSCS___thread: return "__thread";
491 case DeclSpec::TSCS_thread_local: return "thread_local";
492 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
493 }
494 llvm_unreachable("Unknown typespec!");
495}
496
497const char *DeclSpec::getSpecifierName(TypeSpecifierWidth W) {
498 switch (W) {
499 case TypeSpecifierWidth::Unspecified:
500 return "unspecified";
501 case TypeSpecifierWidth::Short:
502 return "short";
503 case TypeSpecifierWidth::Long:
504 return "long";
505 case TypeSpecifierWidth::LongLong:
506 return "long long";
507 }
508 llvm_unreachable("Unknown typespec!");
509}
510
511const char *DeclSpec::getSpecifierName(TSC C) {
512 switch (C) {
513 case TSC_unspecified: return "unspecified";
514 case TSC_imaginary: return "imaginary";
515 case TSC_complex: return "complex";
516 }
517 llvm_unreachable("Unknown typespec!");
518}
519
520const char *DeclSpec::getSpecifierName(TypeSpecifierSign S) {
521 switch (S) {
522 case TypeSpecifierSign::Unspecified:
523 return "unspecified";
524 case TypeSpecifierSign::Signed:
525 return "signed";
526 case TypeSpecifierSign::Unsigned:
527 return "unsigned";
528 }
529 llvm_unreachable("Unknown typespec!");
530}
531
532const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
533 const PrintingPolicy &Policy) {
534 switch (T) {
535 case DeclSpec::TST_unspecified: return "unspecified";
536 case DeclSpec::TST_void: return "void";
537 case DeclSpec::TST_char: return "char";
538 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
539 case DeclSpec::TST_char8: return "char8_t";
540 case DeclSpec::TST_char16: return "char16_t";
541 case DeclSpec::TST_char32: return "char32_t";
542 case DeclSpec::TST_int: return "int";
543 case DeclSpec::TST_int128: return "__int128";
544 case DeclSpec::TST_bitint: return "_BitInt";
545 case DeclSpec::TST_half: return "half";
546 case DeclSpec::TST_float: return "float";
547 case DeclSpec::TST_double: return "double";
548 case DeclSpec::TST_accum: return "_Accum";
549 case DeclSpec::TST_fract: return "_Fract";
550 case DeclSpec::TST_float16: return "_Float16";
551 case DeclSpec::TST_float128: return "__float128";
552 case DeclSpec::TST_ibm128: return "__ibm128";
553 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
554 case DeclSpec::TST_decimal32: return "_Decimal32";
555 case DeclSpec::TST_decimal64: return "_Decimal64";
556 case DeclSpec::TST_decimal128: return "_Decimal128";
557 case DeclSpec::TST_enum: return "enum";
558 case DeclSpec::TST_class: return "class";
559 case DeclSpec::TST_union: return "union";
560 case DeclSpec::TST_struct: return "struct";
561 case DeclSpec::TST_interface: return "__interface";
562 case DeclSpec::TST_typename: return "type-name";
563 case DeclSpec::TST_typename_pack_indexing:
564 return "type-name-pack-indexing";
565 case DeclSpec::TST_typeofType:
566 case DeclSpec::TST_typeofExpr: return "typeof";
567 case DeclSpec::TST_typeof_unqualType:
568 case DeclSpec::TST_typeof_unqualExpr: return "typeof_unqual";
569 case DeclSpec::TST_auto: return "auto";
570 case DeclSpec::TST_auto_type: return "__auto_type";
571 case DeclSpec::TST_decltype: return "(decltype)";
572 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
573#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
574 case DeclSpec::TST_##Trait: \
575 return "__" #Trait;
576#include "clang/Basic/TransformTypeTraits.def"
577 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
578 case DeclSpec::TST_atomic: return "_Atomic";
579 case DeclSpec::TST_BFloat16: return "__bf16";
580#define GENERIC_IMAGE_TYPE(ImgType, Id) \
581 case DeclSpec::TST_##ImgType##_t: \
582 return #ImgType "_t";
583#include "clang/Basic/OpenCLImageTypes.def"
584#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
585 case DeclSpec::TST_##Name: \
586 return #Name;
587#include "clang/Basic/HLSLIntangibleTypes.def"
588 case DeclSpec::TST_error: return "(error)";
589 }
590 llvm_unreachable("Unknown typespec!");
591}
592
593const char *DeclSpec::getSpecifierName(ConstexprSpecKind C) {
594 switch (C) {
595 case ConstexprSpecKind::Unspecified:
596 return "unspecified";
597 case ConstexprSpecKind::Constexpr:
598 return "constexpr";
599 case ConstexprSpecKind::Consteval:
600 return "consteval";
601 case ConstexprSpecKind::Constinit:
602 return "constinit";
603 }
604 llvm_unreachable("Unknown ConstexprSpecKind");
605}
606
607const char *DeclSpec::getSpecifierName(TQ T) {
608 switch (T) {
609 case DeclSpec::TQ_unspecified: return "unspecified";
610 case DeclSpec::TQ_const: return "const";
611 case DeclSpec::TQ_restrict: return "restrict";
612 case DeclSpec::TQ_volatile: return "volatile";
613 case DeclSpec::TQ_atomic: return "_Atomic";
614 case DeclSpec::TQ_unaligned: return "__unaligned";
615 }
616 llvm_unreachable("Unknown typespec!");
617}
618
619const char *DeclSpec::getSpecifierName(OverflowBehaviorState S) {
620 switch (S) {
621 case OverflowBehaviorState::Unspecified:
622 return "unspecified";
623 case OverflowBehaviorState::Wrap:
624 return "__ob_wrap";
625 case OverflowBehaviorState::Trap:
626 return "__ob_trap";
627 }
628 llvm_unreachable("Unknown overflow behavior state!");
629}
630
631bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
632 const char *&PrevSpec,
633 unsigned &DiagID,
634 const PrintingPolicy &Policy) {
635 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
636 // specifiers are not supported.
637 // It seems sensible to prohibit private_extern too
638 // The cl_clang_storage_class_specifiers extension enables support for
639 // these storage-class specifiers.
640 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
641 // specifiers are not supported."
642 if (S.getLangOpts().OpenCL &&
643 !S.getOpenCLOptions().isAvailableOption(
644 Ext: "cl_clang_storage_class_specifiers", LO: S.getLangOpts())) {
645 switch (SC) {
646 case SCS_extern:
647 case SCS_private_extern:
648 case SCS_static:
649 if (S.getLangOpts().getOpenCLCompatibleVersion() < 120) {
650 DiagID = diag::err_opencl_unknown_type_specifier;
651 PrevSpec = getSpecifierName(S: SC);
652 return true;
653 }
654 break;
655 case SCS_auto:
656 case SCS_register:
657 DiagID = diag::err_opencl_unknown_type_specifier;
658 PrevSpec = getSpecifierName(S: SC);
659 return true;
660 default:
661 break;
662 }
663 }
664
665 if (StorageClassSpec != SCS_unspecified) {
666 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
667 bool isInvalid = true;
668 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
669 if (SC == SCS_auto)
670 return SetTypeSpecType(T: TST_auto, Loc, PrevSpec, DiagID, Policy);
671 if (StorageClassSpec == SCS_auto) {
672 isInvalid = SetTypeSpecType(T: TST_auto, Loc: StorageClassSpecLoc,
673 PrevSpec, DiagID, Policy);
674 assert(!isInvalid && "auto SCS -> TST recovery failed");
675 }
676 }
677
678 // Changing storage class is allowed only if the previous one
679 // was the 'extern' that is part of a linkage specification and
680 // the new storage class is 'typedef'.
681 if (isInvalid &&
682 !(SCS_extern_in_linkage_spec &&
683 StorageClassSpec == SCS_extern &&
684 SC == SCS_typedef))
685 return BadSpecifier(TNew: SC, TPrev: (SCS)StorageClassSpec, PrevSpec, DiagID);
686 }
687 StorageClassSpec = SC;
688 StorageClassSpecLoc = Loc;
689 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
690 return false;
691}
692
693bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
694 const char *&PrevSpec,
695 unsigned &DiagID) {
696 if (ThreadStorageClassSpec != TSCS_unspecified)
697 return BadSpecifier(TNew: TSC, TPrev: (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
698
699 ThreadStorageClassSpec = TSC;
700 ThreadStorageClassSpecLoc = Loc;
701 return false;
702}
703
704/// These methods set the specified attribute of the DeclSpec, but return true
705/// and ignore the request if invalid (e.g. "extern" then "auto" is
706/// specified).
707bool DeclSpec::SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc,
708 const char *&PrevSpec, unsigned &DiagID,
709 const PrintingPolicy &Policy) {
710 // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
711 // for 'long long' we will keep the source location of the first 'long'.
712 if (getTypeSpecWidth() == TypeSpecifierWidth::Unspecified)
713 TSWRange.setBegin(Loc);
714 // Allow turning long -> long long.
715 else if (W != TypeSpecifierWidth::LongLong ||
716 getTypeSpecWidth() != TypeSpecifierWidth::Long)
717 return BadSpecifier(TNew: W, TPrev: getTypeSpecWidth(), PrevSpec, DiagID);
718 TypeSpecWidth = static_cast<unsigned>(W);
719 // Remember location of the last 'long'
720 TSWRange.setEnd(Loc);
721 return false;
722}
723
724bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
725 const char *&PrevSpec,
726 unsigned &DiagID) {
727 if (TypeSpecComplex != TSC_unspecified)
728 return BadSpecifier(TNew: C, TPrev: (TSC)TypeSpecComplex, PrevSpec, DiagID);
729 TypeSpecComplex = C;
730 TSCLoc = Loc;
731 return false;
732}
733
734bool DeclSpec::SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc,
735 const char *&PrevSpec, unsigned &DiagID) {
736 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified)
737 return BadSpecifier(TNew: S, TPrev: getTypeSpecSign(), PrevSpec, DiagID);
738 TypeSpecSign = static_cast<unsigned>(S);
739 TSSLoc = Loc;
740 return false;
741}
742
743bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
744 const char *&PrevSpec,
745 unsigned &DiagID,
746 ParsedType Rep,
747 const PrintingPolicy &Policy) {
748 return SetTypeSpecType(T, TagKwLoc: Loc, TagNameLoc: Loc, PrevSpec, DiagID, Rep, Policy);
749}
750
751bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
752 SourceLocation TagNameLoc,
753 const char *&PrevSpec,
754 unsigned &DiagID,
755 ParsedType Rep,
756 const PrintingPolicy &Policy) {
757 assert(isTypeRep(T) && "T does not store a type");
758 assert(Rep && "no type provided!");
759 if (TypeSpecType == TST_error)
760 return false;
761 if (TypeSpecType != TST_unspecified) {
762 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
763 DiagID = diag::err_invalid_decl_spec_combination;
764 return true;
765 }
766 TypeSpecType = T;
767 TypeRep = Rep;
768 TSTLoc = TagKwLoc;
769 TSTNameLoc = TagNameLoc;
770 TypeSpecOwned = false;
771
772 if (T == TST_typename_pack_indexing) {
773 // we got there from a an annotation. Reconstruct the type
774 // Ugly...
775 QualType QT = Rep.get();
776 const PackIndexingType *LIT = cast<PackIndexingType>(Val&: QT);
777 TypeRep = ParsedType::make(P: LIT->getPattern());
778 PackIndexingExpr = LIT->getIndexExpr();
779 }
780 return false;
781}
782
783bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
784 const char *&PrevSpec,
785 unsigned &DiagID,
786 Expr *Rep,
787 const PrintingPolicy &Policy) {
788 assert(isExprRep(T) && "T does not store an expr");
789 assert(Rep && "no expression provided!");
790 if (TypeSpecType == TST_error)
791 return false;
792 if (TypeSpecType != TST_unspecified) {
793 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
794 DiagID = diag::err_invalid_decl_spec_combination;
795 return true;
796 }
797 TypeSpecType = T;
798 ExprRep = Rep;
799 TSTLoc = Loc;
800 TSTNameLoc = Loc;
801 TypeSpecOwned = false;
802 return false;
803}
804
805bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
806 const char *&PrevSpec,
807 unsigned &DiagID,
808 Decl *Rep, bool Owned,
809 const PrintingPolicy &Policy) {
810 return SetTypeSpecType(T, TagKwLoc: Loc, TagNameLoc: Loc, PrevSpec, DiagID, Rep, Owned, Policy);
811}
812
813bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
814 SourceLocation TagNameLoc,
815 const char *&PrevSpec,
816 unsigned &DiagID,
817 Decl *Rep, bool Owned,
818 const PrintingPolicy &Policy) {
819 assert(isDeclRep(T) && "T does not store a decl");
820 // Unlike the other cases, we don't assert that we actually get a decl.
821
822 if (TypeSpecType == TST_error)
823 return false;
824 if (TypeSpecType != TST_unspecified) {
825 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
826 DiagID = diag::err_invalid_decl_spec_combination;
827 return true;
828 }
829 TypeSpecType = T;
830 DeclRep = Rep;
831 TSTLoc = TagKwLoc;
832 TSTNameLoc = TagNameLoc;
833 TypeSpecOwned = Owned && Rep != nullptr;
834 return false;
835}
836
837bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
838 unsigned &DiagID, TemplateIdAnnotation *Rep,
839 const PrintingPolicy &Policy) {
840 assert(T == TST_auto || T == TST_decltype_auto);
841 ConstrainedAuto = true;
842 TemplateIdRep = Rep;
843 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Policy);
844}
845
846bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
847 const char *&PrevSpec,
848 unsigned &DiagID,
849 const PrintingPolicy &Policy) {
850 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
851 "rep required for these type-spec kinds!");
852 if (TypeSpecType == TST_error)
853 return false;
854 if (TypeSpecType != TST_unspecified) {
855 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
856 DiagID = diag::err_invalid_decl_spec_combination;
857 return true;
858 }
859 TSTLoc = Loc;
860 TSTNameLoc = Loc;
861 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
862 TypeAltiVecBool = true;
863 return false;
864 }
865 TypeSpecType = T;
866 TypeSpecOwned = false;
867 return false;
868}
869
870bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
871 unsigned &DiagID) {
872 // Cannot set twice
873 if (TypeSpecSat) {
874 DiagID = diag::warn_duplicate_declspec;
875 PrevSpec = "_Sat";
876 return true;
877 }
878 TypeSpecSat = true;
879 TSSatLoc = Loc;
880 return false;
881}
882
883bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
884 const char *&PrevSpec, unsigned &DiagID,
885 const PrintingPolicy &Policy) {
886 if (TypeSpecType == TST_error)
887 return false;
888 if (TypeSpecType != TST_unspecified) {
889 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
890 DiagID = diag::err_invalid_vector_decl_spec_combination;
891 return true;
892 }
893 TypeAltiVecVector = isAltiVecVector;
894 AltiVecLoc = Loc;
895 return false;
896}
897
898bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
899 const char *&PrevSpec, unsigned &DiagID,
900 const PrintingPolicy &Policy) {
901 if (TypeSpecType == TST_error)
902 return false;
903 if (TypeSpecType != TST_unspecified) {
904 PrevSpec = DeclSpec::getSpecifierName(T: (TST)TypeSpecType, Policy);
905 DiagID = diag::err_invalid_decl_spec_combination;
906 return true;
907 }
908
909 if (isPipe) {
910 TypeSpecPipe = static_cast<unsigned>(TypeSpecifiersPipe::Pipe);
911 }
912 return false;
913}
914
915bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
916 const char *&PrevSpec, unsigned &DiagID,
917 const PrintingPolicy &Policy) {
918 if (TypeSpecType == TST_error)
919 return false;
920 if (!TypeAltiVecVector || TypeAltiVecPixel ||
921 (TypeSpecType != TST_unspecified)) {
922 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
923 DiagID = diag::err_invalid_pixel_decl_spec_combination;
924 return true;
925 }
926 TypeAltiVecPixel = isAltiVecPixel;
927 TSTLoc = Loc;
928 TSTNameLoc = Loc;
929 return false;
930}
931
932bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
933 const char *&PrevSpec, unsigned &DiagID,
934 const PrintingPolicy &Policy) {
935 if (TypeSpecType == TST_error)
936 return false;
937 if (!TypeAltiVecVector || TypeAltiVecBool ||
938 (TypeSpecType != TST_unspecified)) {
939 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
940 DiagID = diag::err_invalid_vector_bool_decl_spec;
941 return true;
942 }
943 TypeAltiVecBool = isAltiVecBool;
944 TSTLoc = Loc;
945 TSTNameLoc = Loc;
946 return false;
947}
948
949bool DeclSpec::SetTypeSpecError() {
950 TypeSpecType = TST_error;
951 TypeSpecOwned = false;
952 TSTLoc = SourceLocation();
953 TSTNameLoc = SourceLocation();
954 return false;
955}
956
957bool DeclSpec::SetBitIntType(SourceLocation KWLoc, Expr *BitsExpr,
958 const char *&PrevSpec, unsigned &DiagID,
959 const PrintingPolicy &Policy) {
960 assert(BitsExpr && "no expression provided!");
961 if (TypeSpecType == TST_error)
962 return false;
963
964 if (TypeSpecType != TST_unspecified) {
965 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
966 DiagID = diag::err_invalid_decl_spec_combination;
967 return true;
968 }
969
970 TypeSpecType = TST_bitint;
971 ExprRep = BitsExpr;
972 TSTLoc = KWLoc;
973 TSTNameLoc = KWLoc;
974 TypeSpecOwned = false;
975 return false;
976}
977
978void DeclSpec::SetPackIndexingExpr(SourceLocation EllipsisLoc,
979 Expr *IndexingExpr) {
980 assert(TypeSpecType == TST_typename &&
981 "pack indexing can only be applied to typename");
982 TypeSpecType = TST_typename_pack_indexing;
983 PackIndexingExpr = IndexingExpr;
984 this->EllipsisLoc = EllipsisLoc;
985}
986
987bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
988 unsigned &DiagID, const LangOptions &Lang) {
989 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
990 // C++. However, since this is likely not what the user intended, we will
991 // always warn. We do not need to set the qualifier's location since we
992 // already have it.
993 if (TypeQualifiers & T) {
994 bool IsExtension = true;
995 if (Lang.C99)
996 IsExtension = false;
997 return BadSpecifier(TNew: T, TPrev: T, PrevSpec, DiagID, IsExtension);
998 }
999
1000 return SetTypeQual(T, Loc);
1001}
1002
1003bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
1004 TypeQualifiers |= T;
1005
1006 switch (T) {
1007 case TQ_unspecified: break;
1008 case TQ_const: TQ_constLoc = Loc; return false;
1009 case TQ_restrict: TQ_restrictLoc = Loc; return false;
1010 case TQ_volatile: TQ_volatileLoc = Loc; return false;
1011 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
1012 case TQ_atomic: TQ_atomicLoc = Loc; return false;
1013 }
1014
1015 llvm_unreachable("Unknown type qualifier!");
1016}
1017
1018bool DeclSpec::SetOverflowBehavior(
1019 OverflowBehaviorType::OverflowBehaviorKind Kind, SourceLocation Loc,
1020 const char *&PrevSpec, unsigned &DiagID) {
1021 OverflowBehaviorState NewState =
1022 (Kind == OverflowBehaviorType::OverflowBehaviorKind::Wrap)
1023 ? OverflowBehaviorState::Wrap
1024 : OverflowBehaviorState::Trap;
1025
1026 OverflowBehaviorState CurrentState = getOverflowBehaviorState();
1027
1028 if (CurrentState != OverflowBehaviorState::Unspecified) {
1029 return BadSpecifier(TNew: NewState, TPrev: CurrentState, PrevSpec, DiagID);
1030 }
1031
1032 OB_state = static_cast<unsigned>(NewState);
1033 OB_Loc = Loc;
1034 return false;
1035}
1036
1037bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
1038 unsigned &DiagID) {
1039 // 'inline inline' is ok. However, since this is likely not what the user
1040 // intended, we will always warn, similar to duplicates of type qualifiers.
1041 if (FS_inline_specified) {
1042 DiagID = diag::warn_duplicate_declspec;
1043 PrevSpec = "inline";
1044 return true;
1045 }
1046 FS_inline_specified = true;
1047 FS_inlineLoc = Loc;
1048 return false;
1049}
1050
1051bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
1052 unsigned &DiagID) {
1053 if (FS_forceinline_specified) {
1054 DiagID = diag::warn_duplicate_declspec;
1055 PrevSpec = "__forceinline";
1056 return true;
1057 }
1058 FS_forceinline_specified = true;
1059 FS_forceinlineLoc = Loc;
1060 return false;
1061}
1062
1063bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
1064 const char *&PrevSpec,
1065 unsigned &DiagID) {
1066 // 'virtual virtual' is ok, but warn as this is likely not what the user
1067 // intended.
1068 if (FS_virtual_specified) {
1069 DiagID = diag::warn_duplicate_declspec;
1070 PrevSpec = "virtual";
1071 return true;
1072 }
1073 FS_virtual_specified = true;
1074 FS_virtualLoc = Loc;
1075 return false;
1076}
1077
1078bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
1079 const char *&PrevSpec, unsigned &DiagID,
1080 ExplicitSpecifier ExplicitSpec,
1081 SourceLocation CloseParenLoc) {
1082 // 'explicit explicit' is ok, but warn as this is likely not what the user
1083 // intended.
1084 if (hasExplicitSpecifier()) {
1085 DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
1086 ? diag::err_duplicate_declspec
1087 : diag::ext_warn_duplicate_declspec;
1088 PrevSpec = "explicit";
1089 return true;
1090 }
1091 FS_explicit_specifier = ExplicitSpec;
1092 FS_explicitLoc = Loc;
1093 FS_explicitCloseParenLoc = CloseParenLoc;
1094 return false;
1095}
1096
1097bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
1098 const char *&PrevSpec,
1099 unsigned &DiagID) {
1100 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1101 // intended.
1102 if (FS_noreturn_specified) {
1103 DiagID = diag::warn_duplicate_declspec;
1104 PrevSpec = "_Noreturn";
1105 return true;
1106 }
1107 FS_noreturn_specified = true;
1108 FS_noreturnLoc = Loc;
1109 return false;
1110}
1111
1112bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
1113 unsigned &DiagID) {
1114 if (isFriendSpecified()) {
1115 PrevSpec = "friend";
1116 DiagID = diag::warn_duplicate_declspec;
1117 return true;
1118 }
1119
1120 FriendSpecifiedFirst = isEmpty();
1121 FriendLoc = Loc;
1122 return false;
1123}
1124
1125bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
1126 unsigned &DiagID) {
1127 if (isModulePrivateSpecified()) {
1128 PrevSpec = "__module_private__";
1129 DiagID = diag::ext_warn_duplicate_declspec;
1130 return true;
1131 }
1132
1133 ModulePrivateLoc = Loc;
1134 return false;
1135}
1136
1137bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
1138 SourceLocation Loc, const char *&PrevSpec,
1139 unsigned &DiagID) {
1140 if (getConstexprSpecifier() != ConstexprSpecKind::Unspecified)
1141 return BadSpecifier(TNew: ConstexprKind, TPrev: getConstexprSpecifier(), PrevSpec,
1142 DiagID);
1143 ConstexprSpecifier = static_cast<unsigned>(ConstexprKind);
1144 ConstexprLoc = Loc;
1145 return false;
1146}
1147
1148void DeclSpec::SaveWrittenBuiltinSpecs() {
1149 writtenBS.Sign = static_cast<int>(getTypeSpecSign());
1150 writtenBS.Width = static_cast<int>(getTypeSpecWidth());
1151 writtenBS.Type = getTypeSpecType();
1152 // Search the list of attributes for the presence of a mode attribute.
1153 writtenBS.ModeAttr = getAttributes().hasAttribute(K: ParsedAttr::AT_Mode);
1154}
1155
1156/// Finish - This does final analysis of the declspec, rejecting things like
1157/// "_Complex" (lacking an FP type). After calling this method, DeclSpec is
1158/// guaranteed to be self-consistent, even if an error occurred.
1159void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
1160 // Before possibly changing their values, save specs as written.
1161 SaveWrittenBuiltinSpecs();
1162
1163 // Check the type specifier components first. No checking for an invalid
1164 // type.
1165 if (TypeSpecType == TST_error)
1166 return;
1167
1168 // If decltype(auto) is used, no other type specifiers are permitted.
1169 if (TypeSpecType == TST_decltype_auto &&
1170 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified ||
1171 TypeSpecComplex != TSC_unspecified ||
1172 getTypeSpecSign() != TypeSpecifierSign::Unspecified ||
1173 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
1174 TypeQualifiers)) {
1175 const unsigned NumLocs = 9;
1176 SourceLocation ExtraLocs[NumLocs] = {
1177 TSWRange.getBegin(), TSCLoc, TSSLoc,
1178 AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
1179 TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
1180 FixItHint Hints[NumLocs];
1181 SourceLocation FirstLoc;
1182 for (unsigned I = 0; I != NumLocs; ++I) {
1183 if (ExtraLocs[I].isValid()) {
1184 if (FirstLoc.isInvalid() ||
1185 S.getSourceManager().isBeforeInTranslationUnit(LHS: ExtraLocs[I],
1186 RHS: FirstLoc))
1187 FirstLoc = ExtraLocs[I];
1188 Hints[I] = FixItHint::CreateRemoval(RemoveRange: ExtraLocs[I]);
1189 }
1190 }
1191 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Unspecified);
1192 TypeSpecComplex = TSC_unspecified;
1193 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1194 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
1195 TypeQualifiers = 0;
1196 S.Diag(Loc: TSTLoc, DiagID: diag::err_decltype_auto_cannot_be_combined)
1197 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
1198 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
1199 }
1200
1201 // Validate and finalize AltiVec vector declspec.
1202 if (TypeAltiVecVector) {
1203 // No vector long long without VSX (or ZVector).
1204 if ((getTypeSpecWidth() == TypeSpecifierWidth::LongLong) &&
1205 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1206 !S.getLangOpts().ZVector)
1207 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_long_decl_spec);
1208
1209 // No vector __int128 prior to Power8 (or ZVector).
1210 if ((TypeSpecType == TST_int128) &&
1211 !S.Context.getTargetInfo().hasFeature(Feature: "power8-vector") &&
1212 !S.getLangOpts().ZVector)
1213 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_int128_decl_spec);
1214
1215 // Complex vector types are not supported.
1216 if (TypeSpecComplex != TSC_unspecified)
1217 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_vector_complex_decl_spec);
1218 else if (TypeAltiVecBool) {
1219 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1220 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1221 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1222 << getSpecifierName(S: getTypeSpecSign());
1223 }
1224 // Only char/int are valid with vector bool prior to Power10.
1225 // Power10 adds instructions that produce vector bool data
1226 // for quadwords as well so allow vector bool __int128.
1227 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1228 (TypeSpecType != TST_int) && (TypeSpecType != TST_int128)) ||
1229 TypeAltiVecPixel) {
1230 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1231 << (TypeAltiVecPixel ? "__pixel" :
1232 getSpecifierName(T: (TST)TypeSpecType, Policy));
1233 }
1234 // vector bool __int128 requires Power10 (or ZVector).
1235 if ((TypeSpecType == TST_int128) &&
1236 (!S.Context.getTargetInfo().hasFeature(Feature: "power10-vector") &&
1237 !S.getLangOpts().ZVector))
1238 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_int128_decl_spec);
1239
1240 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1241 if ((getTypeSpecWidth() != TypeSpecifierWidth::Unspecified) &&
1242 (getTypeSpecWidth() != TypeSpecifierWidth::Short) &&
1243 (getTypeSpecWidth() != TypeSpecifierWidth::LongLong))
1244 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_bool_decl_spec)
1245 << getSpecifierName(W: getTypeSpecWidth());
1246
1247 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1248 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1249 (TypeSpecType == TST_int128) ||
1250 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified))
1251 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1252 } else if (TypeSpecType == TST_double) {
1253 // vector long double and vector long long double are never allowed.
1254 // vector double is OK for Power7 and later, and ZVector.
1255 if (getTypeSpecWidth() == TypeSpecifierWidth::Long ||
1256 getTypeSpecWidth() == TypeSpecifierWidth::LongLong)
1257 S.Diag(Loc: TSWRange.getBegin(),
1258 DiagID: diag::err_invalid_vector_long_double_decl_spec);
1259 else if (!S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1260 !S.getLangOpts().ZVector)
1261 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_double_decl_spec);
1262 } else if (TypeSpecType == TST_float) {
1263 // vector float is unsupported for ZVector unless we have the
1264 // vector-enhancements facility 1 (ISA revision 12).
1265 if (S.getLangOpts().ZVector &&
1266 !S.Context.getTargetInfo().hasFeature(Feature: "arch12"))
1267 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_float_decl_spec);
1268 } else if (getTypeSpecWidth() == TypeSpecifierWidth::Long) {
1269 // Vector long is unsupported for ZVector, or without VSX, and deprecated
1270 // for AltiVec.
1271 // It has also been historically deprecated on AIX (as an alias for
1272 // "vector int" in both 32-bit and 64-bit modes). It was then made
1273 // unsupported in the Clang-based XL compiler since the deprecated type
1274 // has a number of conflicting semantics and continuing to support it
1275 // is a disservice to users.
1276 if (S.getLangOpts().ZVector ||
1277 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") ||
1278 S.Context.getTargetInfo().getTriple().isOSAIX())
1279 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_decl_spec);
1280 else
1281 S.Diag(Loc: TSWRange.getBegin(),
1282 DiagID: diag::warn_vector_long_decl_spec_combination)
1283 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1284 }
1285
1286 if (TypeAltiVecPixel) {
1287 //TODO: perform validation
1288 TypeSpecType = TST_int;
1289 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1290 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Short);
1291 TypeSpecOwned = false;
1292 }
1293 }
1294
1295 bool IsFixedPointType =
1296 TypeSpecType == TST_accum || TypeSpecType == TST_fract;
1297
1298 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
1299 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1300 if (TypeSpecType == TST_unspecified)
1301 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1302 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1303 TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
1304 !IsFixedPointType && TypeSpecType != TST_bitint) {
1305 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_sign_spec)
1306 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1307 // signed double -> double.
1308 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1309 }
1310 }
1311
1312 // Validate the width of the type.
1313 switch (getTypeSpecWidth()) {
1314 case TypeSpecifierWidth::Unspecified:
1315 break;
1316 case TypeSpecifierWidth::Short: // short int
1317 case TypeSpecifierWidth::LongLong: // long long int
1318 if (TypeSpecType == TST_unspecified)
1319 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1320 else if (!(TypeSpecType == TST_int ||
1321 (IsFixedPointType &&
1322 getTypeSpecWidth() != TypeSpecifierWidth::LongLong))) {
1323 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1324 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1325 TypeSpecType = TST_int;
1326 TypeSpecSat = false;
1327 TypeSpecOwned = false;
1328 }
1329 break;
1330 case TypeSpecifierWidth::Long: // long double, long int
1331 if (TypeSpecType == TST_unspecified)
1332 TypeSpecType = TST_int; // long -> long int.
1333 else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
1334 !IsFixedPointType) {
1335 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1336 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1337 TypeSpecType = TST_int;
1338 TypeSpecSat = false;
1339 TypeSpecOwned = false;
1340 }
1341 break;
1342 }
1343
1344 // TODO: if the implementation does not implement _Complex, disallow their
1345 // use. Need information about the backend.
1346 if (TypeSpecComplex != TSC_unspecified) {
1347 if (TypeSpecType == TST_unspecified) {
1348 S.Diag(Loc: TSCLoc, DiagID: diag::ext_plain_complex)
1349 << FixItHint::CreateInsertion(
1350 InsertionLoc: S.getLocForEndOfToken(Loc: getTypeSpecComplexLoc()),
1351 Code: " double");
1352 TypeSpecType = TST_double; // _Complex -> _Complex double.
1353 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1354 // Note that this intentionally doesn't include _Complex _Bool.
1355 if (!S.getLangOpts().CPlusPlus)
1356 S.Diag(Loc: TSTLoc, DiagID: diag::ext_integer_complex);
1357 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double &&
1358 TypeSpecType != TST_float128 && TypeSpecType != TST_float16 &&
1359 TypeSpecType != TST_ibm128) {
1360 // FIXME: __fp16?
1361 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_complex_spec)
1362 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1363 TypeSpecComplex = TSC_unspecified;
1364 }
1365 }
1366
1367 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1368 // _Thread_local can only appear with the 'static' and 'extern' storage class
1369 // specifiers. We also allow __private_extern__ as an extension.
1370 if (ThreadStorageClassSpec != TSCS_unspecified) {
1371 switch (StorageClassSpec) {
1372 case SCS_unspecified:
1373 case SCS_extern:
1374 case SCS_private_extern:
1375 case SCS_static:
1376 break;
1377 default:
1378 if (S.getSourceManager().isBeforeInTranslationUnit(
1379 LHS: getThreadStorageClassSpecLoc(), RHS: getStorageClassSpecLoc()))
1380 S.Diag(Loc: getStorageClassSpecLoc(),
1381 DiagID: diag::err_invalid_decl_spec_combination)
1382 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1383 << SourceRange(getThreadStorageClassSpecLoc());
1384 else
1385 S.Diag(Loc: getThreadStorageClassSpecLoc(),
1386 DiagID: diag::err_invalid_decl_spec_combination)
1387 << DeclSpec::getSpecifierName(S: getStorageClassSpec())
1388 << SourceRange(getStorageClassSpecLoc());
1389 // Discard the thread storage class specifier to recover.
1390 ThreadStorageClassSpec = TSCS_unspecified;
1391 ThreadStorageClassSpecLoc = SourceLocation();
1392 }
1393 if (S.getLangOpts().C23 &&
1394 getConstexprSpecifier() == ConstexprSpecKind::Constexpr) {
1395 S.Diag(Loc: ConstexprLoc, DiagID: diag::err_invalid_decl_spec_combination)
1396 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1397 << SourceRange(getThreadStorageClassSpecLoc());
1398 }
1399 }
1400
1401 if (S.getLangOpts().C23 &&
1402 getConstexprSpecifier() == ConstexprSpecKind::Constexpr &&
1403 getTypeSpecType() != TST_unspecified &&
1404 (StorageClassSpec == SCS_extern || StorageClassSpec == SCS_auto)) {
1405 S.Diag(Loc: ConstexprLoc, DiagID: diag::err_invalid_decl_spec_combination)
1406 << DeclSpec::getSpecifierName(S: getStorageClassSpec())
1407 << SourceRange(getStorageClassSpecLoc());
1408 }
1409
1410 // If no type specifier was provided and we're parsing a language where
1411 // the type specifier is not optional, but we got 'auto' as a storage
1412 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1413 // type specifier.
1414 if (S.getLangOpts().CPlusPlus &&
1415 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1416 TypeSpecType = TST_auto;
1417 StorageClassSpec = SCS_unspecified;
1418 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1419 StorageClassSpecLoc = SourceLocation();
1420 }
1421 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1422 // specifier in a pre-C++11 dialect of C++ or in a pre-C23 dialect of C.
1423 if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().C23 &&
1424 TypeSpecType == TST_auto)
1425 S.Diag(Loc: TSTLoc, DiagID: diag::ext_auto_type_specifier) << /*C++*/ 0;
1426 if (S.getLangOpts().HLSL &&
1427 S.getLangOpts().getHLSLVersion() < LangOptions::HLSL_202y &&
1428 TypeSpecType == TST_auto)
1429 S.Diag(Loc: TSTLoc, DiagID: diag::ext_hlsl_auto_type_specifier) << /*HLSL*/ 1;
1430 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1431 StorageClassSpec == SCS_auto)
1432 S.Diag(Loc: StorageClassSpecLoc, DiagID: diag::warn_auto_storage_class)
1433 << FixItHint::CreateRemoval(RemoveRange: StorageClassSpecLoc);
1434 if (TypeSpecType == TST_char8)
1435 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx17_compat_unicode_type);
1436 else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1437 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx98_compat_unicode_type)
1438 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1439 if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr)
1440 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx98_compat_constexpr);
1441 else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval)
1442 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_consteval);
1443 else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit)
1444 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_constinit);
1445 // C++ [class.friend]p6:
1446 // No storage-class-specifier shall appear in the decl-specifier-seq
1447 // of a friend declaration.
1448 if (isFriendSpecified() &&
1449 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1450 SmallString<32> SpecName;
1451 SourceLocation SCLoc;
1452 FixItHint StorageHint, ThreadHint;
1453
1454 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1455 SpecName = getSpecifierName(S: SC);
1456 SCLoc = getStorageClassSpecLoc();
1457 StorageHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1458 }
1459
1460 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1461 if (!SpecName.empty()) SpecName += " ";
1462 SpecName += getSpecifierName(S: TSC);
1463 SCLoc = getThreadStorageClassSpecLoc();
1464 ThreadHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1465 }
1466
1467 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1468 << SpecName << StorageHint << ThreadHint;
1469
1470 ClearStorageClassSpecs();
1471 }
1472
1473 // C++11 [dcl.fct.spec]p5:
1474 // The virtual specifier shall be used only in the initial
1475 // declaration of a non-static class member function;
1476 // C++11 [dcl.fct.spec]p6:
1477 // The explicit specifier shall be used only in the declaration of
1478 // a constructor or conversion function within its class
1479 // definition;
1480 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
1481 StringRef Keyword;
1482 FixItHint Hint;
1483 SourceLocation SCLoc;
1484
1485 if (isVirtualSpecified()) {
1486 Keyword = "virtual";
1487 SCLoc = getVirtualSpecLoc();
1488 Hint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1489 } else {
1490 Keyword = "explicit";
1491 SCLoc = getExplicitSpecLoc();
1492 Hint = FixItHint::CreateRemoval(RemoveRange: getExplicitSpecRange());
1493 }
1494
1495 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1496 << Keyword << Hint;
1497
1498 FS_virtual_specified = false;
1499 FS_explicit_specifier = ExplicitSpecifier();
1500 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1501 }
1502
1503 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1504
1505 // Okay, now we can infer the real type.
1506
1507 // TODO: return "auto function" and other bad things based on the real type.
1508
1509 // 'data definition has no type or storage class'?
1510}
1511
1512bool DeclSpec::isMissingDeclaratorOk() {
1513 TST tst = getTypeSpecType();
1514 return isDeclRep(T: tst) && getRepAsDecl() != nullptr &&
1515 StorageClassSpec != DeclSpec::SCS_typedef;
1516}
1517
1518void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1519 OverloadedOperatorKind Op,
1520 SourceLocation SymbolLocations[3]) {
1521 Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
1522 StartLocation = OperatorLoc;
1523 EndLocation = OperatorLoc;
1524 new (&OperatorFunctionId) struct OFI;
1525 OperatorFunctionId.Operator = Op;
1526 for (unsigned I = 0; I != 3; ++I) {
1527 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I];
1528
1529 if (SymbolLocations[I].isValid())
1530 EndLocation = SymbolLocations[I];
1531 }
1532}
1533
1534bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1535 const char *&PrevSpec) {
1536 if (!FirstLocation.isValid())
1537 FirstLocation = Loc;
1538 LastLocation = Loc;
1539 LastSpecifier = VS;
1540
1541 if (Specifiers & VS) {
1542 PrevSpec = getSpecifierName(VS);
1543 return true;
1544 }
1545
1546 Specifiers |= VS;
1547
1548 switch (VS) {
1549 default: llvm_unreachable("Unknown specifier!");
1550 case VS_Override: VS_overrideLoc = Loc; break;
1551 case VS_GNU_Final:
1552 case VS_Sealed:
1553 case VS_Final: VS_finalLoc = Loc; break;
1554 case VS_Abstract: VS_abstractLoc = Loc; break;
1555 }
1556
1557 return false;
1558}
1559
1560const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1561 switch (VS) {
1562 default: llvm_unreachable("Unknown specifier");
1563 case VS_Override: return "override";
1564 case VS_Final: return "final";
1565 case VS_GNU_Final: return "__final";
1566 case VS_Sealed: return "sealed";
1567 case VS_Abstract: return "abstract";
1568 }
1569}
1570