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/Traits.inc"
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/Traits.inc"
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 setConflictingTypeSpecifier(T, Loc: TagKwLoc, NameLoc: TagNameLoc, Rep);
763 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
764 DiagID = diag::err_invalid_decl_spec_combination;
765 return true;
766 }
767 TypeSpecType = T;
768 TypeRep = Rep;
769 TSTLoc = TagKwLoc;
770 TSTNameLoc = TagNameLoc;
771 TypeSpecOwned = false;
772
773 if (T == TST_typename_pack_indexing) {
774 // we got there from a an annotation. Reconstruct the type
775 // Ugly...
776 QualType QT = Rep.get();
777 const PackIndexingType *LIT = cast<PackIndexingType>(Val&: QT);
778 TypeRep = ParsedType::make(P: LIT->getPattern());
779 PackIndexingExpr = LIT->getIndexExpr();
780 }
781 return false;
782}
783
784bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
785 const char *&PrevSpec,
786 unsigned &DiagID,
787 Expr *Rep,
788 const PrintingPolicy &Policy) {
789 assert(isExprRep(T) && "T does not store an expr");
790 assert(Rep && "no expression provided!");
791 if (TypeSpecType == TST_error)
792 return false;
793 if (TypeSpecType != TST_unspecified) {
794 setConflictingTypeSpecifier(T, Loc, Rep);
795 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
796 DiagID = diag::err_invalid_decl_spec_combination;
797 return true;
798 }
799 TypeSpecType = T;
800 ExprRep = Rep;
801 TSTLoc = Loc;
802 TSTNameLoc = Loc;
803 TypeSpecOwned = false;
804 return false;
805}
806
807bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
808 const char *&PrevSpec,
809 unsigned &DiagID,
810 Decl *Rep, bool Owned,
811 const PrintingPolicy &Policy) {
812 return SetTypeSpecType(T, TagKwLoc: Loc, TagNameLoc: Loc, PrevSpec, DiagID, Rep, Owned, Policy);
813}
814
815bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
816 SourceLocation TagNameLoc,
817 const char *&PrevSpec,
818 unsigned &DiagID,
819 Decl *Rep, bool Owned,
820 const PrintingPolicy &Policy) {
821 assert(isDeclRep(T) && "T does not store a decl");
822 // Unlike the other cases, we don't assert that we actually get a decl.
823
824 if (TypeSpecType == TST_error)
825 return false;
826 if (TypeSpecType != TST_unspecified) {
827 setConflictingTypeSpecifier(T, Loc: TagKwLoc, NameLoc: TagNameLoc, Rep, Owned);
828 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
829 DiagID = diag::err_invalid_decl_spec_combination;
830 return true;
831 }
832 TypeSpecType = T;
833 DeclRep = Rep;
834 TSTLoc = TagKwLoc;
835 TSTNameLoc = TagNameLoc;
836 TypeSpecOwned = Owned && Rep != nullptr;
837 return false;
838}
839
840bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
841 unsigned &DiagID, TemplateIdAnnotation *Rep,
842 const PrintingPolicy &Policy) {
843 assert(T == TST_auto || T == TST_decltype_auto);
844 ConstrainedAuto = true;
845 TemplateIdRep = Rep;
846 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Policy);
847}
848
849bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
850 const char *&PrevSpec,
851 unsigned &DiagID,
852 const PrintingPolicy &Policy) {
853 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
854 "rep required for these type-spec kinds!");
855 if (TypeSpecType == TST_error)
856 return false;
857 if (TypeSpecType != TST_unspecified) {
858 setConflictingTypeSpecifier(T, Loc);
859 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
860 DiagID = diag::err_invalid_decl_spec_combination;
861 return true;
862 }
863 TSTLoc = Loc;
864 TSTNameLoc = Loc;
865 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
866 TypeAltiVecBool = true;
867 return false;
868 }
869 TypeSpecType = T;
870 TypeSpecOwned = false;
871 return false;
872}
873
874bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
875 unsigned &DiagID) {
876 // Cannot set twice
877 if (TypeSpecSat) {
878 DiagID = diag::warn_duplicate_declspec;
879 PrevSpec = "_Sat";
880 return true;
881 }
882 TypeSpecSat = true;
883 TSSatLoc = Loc;
884 return false;
885}
886
887bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
888 const char *&PrevSpec, unsigned &DiagID,
889 const PrintingPolicy &Policy) {
890 if (TypeSpecType == TST_error)
891 return false;
892 if (TypeSpecType != TST_unspecified) {
893 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
894 DiagID = diag::err_invalid_vector_decl_spec_combination;
895 return true;
896 }
897 TypeAltiVecVector = isAltiVecVector;
898 AltiVecLoc = Loc;
899 return false;
900}
901
902bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
903 const char *&PrevSpec, unsigned &DiagID,
904 const PrintingPolicy &Policy) {
905 if (TypeSpecType == TST_error)
906 return false;
907 if (TypeSpecType != TST_unspecified) {
908 PrevSpec = DeclSpec::getSpecifierName(T: (TST)TypeSpecType, Policy);
909 DiagID = diag::err_invalid_decl_spec_combination;
910 return true;
911 }
912
913 if (isPipe) {
914 TypeSpecPipe = static_cast<unsigned>(TypeSpecifiersPipe::Pipe);
915 }
916 return false;
917}
918
919bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
920 const char *&PrevSpec, unsigned &DiagID,
921 const PrintingPolicy &Policy) {
922 if (TypeSpecType == TST_error)
923 return false;
924 if (!TypeAltiVecVector || TypeAltiVecPixel ||
925 (TypeSpecType != TST_unspecified)) {
926 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
927 DiagID = diag::err_invalid_pixel_decl_spec_combination;
928 return true;
929 }
930 TypeAltiVecPixel = isAltiVecPixel;
931 TSTLoc = Loc;
932 TSTNameLoc = Loc;
933 return false;
934}
935
936bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
937 const char *&PrevSpec, unsigned &DiagID,
938 const PrintingPolicy &Policy) {
939 if (TypeSpecType == TST_error)
940 return false;
941 if (!TypeAltiVecVector || TypeAltiVecBool ||
942 (TypeSpecType != TST_unspecified)) {
943 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
944 DiagID = diag::err_invalid_vector_bool_decl_spec;
945 return true;
946 }
947 TypeAltiVecBool = isAltiVecBool;
948 TSTLoc = Loc;
949 TSTNameLoc = Loc;
950 return false;
951}
952
953bool DeclSpec::SetTypeSpecError() {
954 TypeSpecType = TST_error;
955 TypeSpecOwned = false;
956 TSTLoc = SourceLocation();
957 TSTNameLoc = SourceLocation();
958 return false;
959}
960
961bool DeclSpec::SetBitIntType(SourceLocation KWLoc, Expr *BitsExpr,
962 const char *&PrevSpec, unsigned &DiagID,
963 const PrintingPolicy &Policy) {
964 assert(BitsExpr && "no expression provided!");
965 if (TypeSpecType == TST_error)
966 return false;
967
968 if (TypeSpecType != TST_unspecified) {
969 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
970 DiagID = diag::err_invalid_decl_spec_combination;
971 return true;
972 }
973
974 TypeSpecType = TST_bitint;
975 ExprRep = BitsExpr;
976 TSTLoc = KWLoc;
977 TSTNameLoc = KWLoc;
978 TypeSpecOwned = false;
979 return false;
980}
981
982void DeclSpec::SetPackIndexingExpr(SourceLocation EllipsisLoc,
983 Expr *IndexingExpr) {
984 assert(TypeSpecType == TST_typename &&
985 "pack indexing can only be applied to typename");
986 TypeSpecType = TST_typename_pack_indexing;
987 PackIndexingExpr = IndexingExpr;
988 this->EllipsisLoc = EllipsisLoc;
989}
990
991bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
992 unsigned &DiagID, const LangOptions &Lang) {
993 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
994 // C++. However, since this is likely not what the user intended, we will
995 // always warn. We do not need to set the qualifier's location since we
996 // already have it.
997 if (TypeQualifiers & T) {
998 bool IsExtension = true;
999 if (Lang.C99)
1000 IsExtension = false;
1001 return BadSpecifier(TNew: T, TPrev: T, PrevSpec, DiagID, IsExtension);
1002 }
1003
1004 return SetTypeQual(T, Loc);
1005}
1006
1007bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
1008 TypeQualifiers |= T;
1009
1010 switch (T) {
1011 case TQ_unspecified: break;
1012 case TQ_const: TQ_constLoc = Loc; return false;
1013 case TQ_restrict: TQ_restrictLoc = Loc; return false;
1014 case TQ_volatile: TQ_volatileLoc = Loc; return false;
1015 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
1016 case TQ_atomic: TQ_atomicLoc = Loc; return false;
1017 }
1018
1019 llvm_unreachable("Unknown type qualifier!");
1020}
1021
1022bool DeclSpec::SetOverflowBehavior(
1023 OverflowBehaviorType::OverflowBehaviorKind Kind, SourceLocation Loc,
1024 const char *&PrevSpec, unsigned &DiagID) {
1025 OverflowBehaviorState NewState =
1026 (Kind == OverflowBehaviorType::OverflowBehaviorKind::Wrap)
1027 ? OverflowBehaviorState::Wrap
1028 : OverflowBehaviorState::Trap;
1029
1030 OverflowBehaviorState CurrentState = getOverflowBehaviorState();
1031
1032 if (CurrentState != OverflowBehaviorState::Unspecified) {
1033 return BadSpecifier(TNew: NewState, TPrev: CurrentState, PrevSpec, DiagID);
1034 }
1035
1036 OB_state = static_cast<unsigned>(NewState);
1037 OB_Loc = Loc;
1038 return false;
1039}
1040
1041bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
1042 unsigned &DiagID) {
1043 // 'inline inline' is ok. However, since this is likely not what the user
1044 // intended, we will always warn, similar to duplicates of type qualifiers.
1045 if (FS_inline_specified) {
1046 DiagID = diag::warn_duplicate_declspec;
1047 PrevSpec = "inline";
1048 return true;
1049 }
1050 FS_inline_specified = true;
1051 FS_inlineLoc = Loc;
1052 return false;
1053}
1054
1055bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
1056 unsigned &DiagID) {
1057 if (FS_forceinline_specified) {
1058 DiagID = diag::warn_duplicate_declspec;
1059 PrevSpec = "__forceinline";
1060 return true;
1061 }
1062 FS_forceinline_specified = true;
1063 FS_forceinlineLoc = Loc;
1064 return false;
1065}
1066
1067bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
1068 const char *&PrevSpec,
1069 unsigned &DiagID) {
1070 // 'virtual virtual' is ok, but warn as this is likely not what the user
1071 // intended.
1072 if (FS_virtual_specified) {
1073 DiagID = diag::warn_duplicate_declspec;
1074 PrevSpec = "virtual";
1075 return true;
1076 }
1077 FS_virtual_specified = true;
1078 FS_virtualLoc = Loc;
1079 return false;
1080}
1081
1082bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
1083 const char *&PrevSpec, unsigned &DiagID,
1084 ExplicitSpecifier ExplicitSpec,
1085 SourceLocation CloseParenLoc) {
1086 // 'explicit explicit' is ok, but warn as this is likely not what the user
1087 // intended.
1088 if (hasExplicitSpecifier()) {
1089 DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
1090 ? diag::err_duplicate_declspec
1091 : diag::ext_warn_duplicate_declspec;
1092 PrevSpec = "explicit";
1093 return true;
1094 }
1095 FS_explicit_specifier = ExplicitSpec;
1096 FS_explicitLoc = Loc;
1097 FS_explicitCloseParenLoc = CloseParenLoc;
1098 return false;
1099}
1100
1101bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
1102 const char *&PrevSpec,
1103 unsigned &DiagID) {
1104 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1105 // intended.
1106 if (FS_noreturn_specified) {
1107 DiagID = diag::warn_duplicate_declspec;
1108 PrevSpec = "_Noreturn";
1109 return true;
1110 }
1111 FS_noreturn_specified = true;
1112 FS_noreturnLoc = Loc;
1113 return false;
1114}
1115
1116bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
1117 unsigned &DiagID) {
1118 if (isFriendSpecified()) {
1119 PrevSpec = "friend";
1120 DiagID = diag::warn_duplicate_declspec;
1121 return true;
1122 }
1123
1124 FriendSpecifiedFirst = isEmpty();
1125 FriendLoc = Loc;
1126 return false;
1127}
1128
1129bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
1130 unsigned &DiagID) {
1131 if (isModulePrivateSpecified()) {
1132 PrevSpec = "__module_private__";
1133 DiagID = diag::ext_warn_duplicate_declspec;
1134 return true;
1135 }
1136
1137 ModulePrivateLoc = Loc;
1138 return false;
1139}
1140
1141bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
1142 SourceLocation Loc, const char *&PrevSpec,
1143 unsigned &DiagID) {
1144 if (getConstexprSpecifier() != ConstexprSpecKind::Unspecified)
1145 return BadSpecifier(TNew: ConstexprKind, TPrev: getConstexprSpecifier(), PrevSpec,
1146 DiagID);
1147 ConstexprSpecifier = static_cast<unsigned>(ConstexprKind);
1148 ConstexprLoc = Loc;
1149 return false;
1150}
1151
1152void DeclSpec::SaveWrittenBuiltinSpecs() {
1153 writtenBS.Sign = static_cast<int>(getTypeSpecSign());
1154 writtenBS.Width = static_cast<int>(getTypeSpecWidth());
1155 writtenBS.Type = getTypeSpecType();
1156 // Search the list of attributes for the presence of a mode attribute.
1157 writtenBS.ModeAttr = getAttributes().hasAttribute(K: ParsedAttr::AT_Mode);
1158}
1159
1160/// Finish - This does final analysis of the declspec, rejecting things like
1161/// "_Complex" (lacking an FP type). After calling this method, DeclSpec is
1162/// guaranteed to be self-consistent, even if an error occurred.
1163void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
1164 // Before possibly changing their values, save specs as written.
1165 SaveWrittenBuiltinSpecs();
1166
1167 // Check the type specifier components first. No checking for an invalid
1168 // type.
1169 CheckTypeSpec(S, Policy);
1170
1171 CheckFriendSpec(S, Policy);
1172
1173 assert(!TypeSpecOwned || isDeclRep((TST)TypeSpecType));
1174
1175 // Okay, now we can infer the real type.
1176
1177 // TODO: return "auto function" and other bad things based on the real type.
1178
1179 // 'data definition has no type or storage class'?
1180}
1181
1182void DeclSpec::CheckTypeSpec(Sema &S, const PrintingPolicy &Policy) {
1183 if (TypeSpecType == TST_error)
1184 return;
1185
1186 // If decltype(auto) is used, no other type specifiers are permitted.
1187 if (TypeSpecType == TST_decltype_auto &&
1188 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified ||
1189 TypeSpecComplex != TSC_unspecified ||
1190 getTypeSpecSign() != TypeSpecifierSign::Unspecified ||
1191 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
1192 TypeQualifiers)) {
1193 const unsigned NumLocs = 9;
1194 SourceLocation ExtraLocs[NumLocs] = {
1195 TSWRange.getBegin(), TSCLoc, TSSLoc,
1196 AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
1197 TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
1198 FixItHint Hints[NumLocs];
1199 SourceLocation FirstLoc;
1200 for (unsigned I = 0; I != NumLocs; ++I) {
1201 if (ExtraLocs[I].isValid()) {
1202 if (FirstLoc.isInvalid() ||
1203 S.getSourceManager().isBeforeInTranslationUnit(LHS: ExtraLocs[I],
1204 RHS: FirstLoc))
1205 FirstLoc = ExtraLocs[I];
1206 Hints[I] = FixItHint::CreateRemoval(RemoveRange: ExtraLocs[I]);
1207 }
1208 }
1209 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Unspecified);
1210 TypeSpecComplex = TSC_unspecified;
1211 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1212 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
1213 TypeQualifiers = 0;
1214 S.Diag(Loc: TSTLoc, DiagID: diag::err_decltype_auto_cannot_be_combined)
1215 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
1216 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
1217 }
1218
1219 // If 'auto' type specifier is combined with another type specifier, we need
1220 // to handle it based on the language:
1221 // - In C++11+: Emit error (cannot combine type specifiers)
1222 // - In C23: Convert 'auto' to storage class (valid)
1223 // - In OpenCL: Convert 'auto' to storage class, then OpenCL will reject it
1224 // Handle both cases:
1225 // - "auto int" (TypeSpecType == TST_auto, ConflictingTypeSpecifier ==
1226 // TST_int)
1227 // - "int auto" (TypeSpecType == TST_int, ConflictingTypeSpecifier ==
1228 // TST_auto)
1229 if (ConflictingTypeSpecifier != TST_unspecified) {
1230 // Special case: "auto auto" - duplicate 'auto', emit error
1231 if (TypeSpecType == TST_auto && ConflictingTypeSpecifier == TST_auto) {
1232 // Both are 'auto', emit "cannot combine with previous 'auto' declaration
1233 // specifier" error This matches GCC's "duplicate 'auto'" behavior Note:
1234 // We keep TypeSpecType = TST_auto (don't set to TST_error) so that later
1235 // checks in SemaType.cpp can emit "not allowed in function prototype" and
1236 // "not allowed in function return type" errors as needed.
1237 const char *PrevSpec = "auto";
1238 unsigned DiagID = diag::err_invalid_decl_spec_combination;
1239 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID) << PrevSpec << PrevSpec;
1240 // Clear the conflict tracking but keep TypeSpecType = TST_auto
1241 ConflictingTypeSpecifier = TST_unspecified;
1242 ConflictingTypeSpecifierLoc = SourceLocation();
1243 } else if ((S.getLangOpts().C23 && !S.getLangOpts().CPlusPlus) ||
1244 (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11)) {
1245 // In C23 or C++98, convert 'auto' to storage class specifier
1246 if (TypeSpecType == TST_auto) {
1247 // "auto int" case: Convert 'auto' to storage class specifier
1248 StorageClassSpec = SCS_auto;
1249 StorageClassSpecLoc = TSTLoc;
1250 TypeSpecType = ConflictingTypeSpecifier;
1251 TSTLoc = ConflictingTypeSpecifierLoc;
1252 // Clear the conflict tracking
1253 ConflictingTypeSpecifier = TST_unspecified;
1254 ConflictingTypeSpecifierLoc = SourceLocation();
1255 } else if (ConflictingTypeSpecifier == TST_auto) {
1256 // "int auto" case: Convert 'auto' to storage class specifier
1257 // In C23, if 'constexpr' is present, treat 'auto' as a type specifier
1258 // conflict with 'int' rather than converting it to storage class, so we
1259 // emit the "cannot combine with previous 'int' declaration specifier"
1260 // error and mark the type as error to prevent further processing.
1261 // Otherwise, convert 'auto' to storage class specifier (no type
1262 // conflict error).
1263 if (S.getLangOpts().C23 && !S.getLangOpts().CPlusPlus &&
1264 getConstexprSpecifier() != ConstexprSpecKind::Unspecified) {
1265 // constexpr int auto: treat as type specifier conflict
1266 const char *PrevSpec =
1267 getSpecifierName(T: (TST)TypeSpecType, Policy: S.getPrintingPolicy());
1268 unsigned DiagID = diag::err_invalid_decl_spec_combination;
1269 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID) << PrevSpec << "auto";
1270 TypeSpecType = TST_error;
1271 ConflictingTypeSpecifier = TST_unspecified;
1272 ConflictingTypeSpecifierLoc = SourceLocation();
1273 return;
1274 }
1275 // int auto (without constexpr): Convert 'auto' to storage class
1276 // specifier. No type conflict error - auto is treated as storage class,
1277 // not type specifier.
1278 StorageClassSpec = SCS_auto;
1279 StorageClassSpecLoc = ConflictingTypeSpecifierLoc;
1280 // TypeSpecType already has the correct type (e.g., TST_int)
1281 // Clear the conflict tracking
1282 ConflictingTypeSpecifier = TST_unspecified;
1283 ConflictingTypeSpecifierLoc = SourceLocation();
1284 }
1285 } else if (S.getLangOpts().OpenCL) {
1286 // For OpenCL (C or C++), convert 'auto' to storage class specifier first
1287 // (OpenCL will then reject it via SetStorageClassSpec checks)
1288 // This must come before the C++11+ check to handle OpenCL C++
1289 if (TypeSpecType == TST_auto) {
1290 // "auto int" case: Convert 'auto' to storage class specifier
1291 // Use SetStorageClassSpec to trigger OpenCL-specific error checking
1292 const char *PrevSpec = nullptr;
1293 unsigned DiagID = 0;
1294 if (SetStorageClassSpec(
1295 S, SC: SCS_auto, Loc: TSTLoc, PrevSpec, DiagID,
1296 Policy: S.getPrintingPolicy())) { // OpenCL rejected it, emit the error
1297 // with version string
1298 if (S.getLangOpts().OpenCL && S.getLangOpts().CPlusPlus) {
1299 S.Diag(Loc: TSTLoc, DiagID)
1300 << S.getLangOpts().getOpenCLVersionString() << PrevSpec << 1;
1301 } else {
1302 S.Diag(Loc: TSTLoc, DiagID) << PrevSpec;
1303 }
1304 TypeSpecType = TST_error;
1305 } else {
1306 StorageClassSpec = SCS_auto;
1307 StorageClassSpecLoc = TSTLoc;
1308 TypeSpecType = ConflictingTypeSpecifier;
1309 TSTLoc = ConflictingTypeSpecifierLoc;
1310 }
1311 // Clear the conflict tracking
1312 ConflictingTypeSpecifier = TST_unspecified;
1313 ConflictingTypeSpecifierLoc = SourceLocation();
1314 } else if (ConflictingTypeSpecifier == TST_auto) {
1315 // "int auto" case: Convert 'auto' to storage class specifier
1316 // Use SetStorageClassSpec to trigger OpenCL-specific error checking
1317 const char *PrevSpec = nullptr;
1318 unsigned DiagID = 0;
1319 if (SetStorageClassSpec(S, SC: SCS_auto, Loc: ConflictingTypeSpecifierLoc,
1320 PrevSpec, DiagID, Policy: S.getPrintingPolicy())) {
1321 // OpenCL rejected it, emit the error with version string
1322 if (S.getLangOpts().OpenCL && S.getLangOpts().CPlusPlus) {
1323 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID)
1324 << S.getLangOpts().getOpenCLVersionString() << PrevSpec << 1;
1325 } else {
1326 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID) << PrevSpec;
1327 }
1328 TypeSpecType = TST_error;
1329 } else {
1330 StorageClassSpec = SCS_auto;
1331 StorageClassSpecLoc = ConflictingTypeSpecifierLoc;
1332 }
1333 // Clear the conflict tracking
1334 ConflictingTypeSpecifier = TST_unspecified;
1335 ConflictingTypeSpecifierLoc = SourceLocation();
1336 }
1337 } else if (S.getLangOpts().CPlusPlus && S.getLangOpts().CPlusPlus11 &&
1338 !S.getLangOpts().C23) {
1339 // In C++11+ (but not C23 or OpenCL), emit error (cannot combine type
1340 // specifiers)
1341 if (TypeSpecType == TST_auto) {
1342 // "auto int" case
1343 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID: diag::err_auto_type_specifier)
1344 << FixItHint::CreateRemoval(RemoveRange: ConflictingTypeSpecifierLoc);
1345 } else if (ConflictingTypeSpecifier == TST_auto) {
1346 // "int auto" case
1347 S.Diag(Loc: ConflictingTypeSpecifierLoc, DiagID: diag::err_auto_type_specifier)
1348 << FixItHint::CreateRemoval(RemoveRange: ConflictingTypeSpecifierLoc);
1349 }
1350 // Mark as error to prevent further processing
1351 TypeSpecType = TST_error;
1352 TypeSpecOwned = false;
1353 } else if (!S.getLangOpts().CPlusPlus) {
1354 // For C, C23, etc., convert 'auto' to storage class specifier
1355 // (This is already handled above for C23, but keep for other C dialects)
1356 // In C, C23, OpenCL, etc., convert 'auto' to storage class specifier
1357 if (TypeSpecType == TST_auto) {
1358 // "auto int" case: Convert 'auto' to storage class specifier
1359 StorageClassSpec = SCS_auto;
1360 StorageClassSpecLoc = TSTLoc;
1361 TypeSpecType = ConflictingTypeSpecifier;
1362 TSTLoc = ConflictingTypeSpecifierLoc;
1363 // Clear the conflict tracking
1364 ConflictingTypeSpecifier = TST_unspecified;
1365 ConflictingTypeSpecifierLoc = SourceLocation();
1366 } else if (ConflictingTypeSpecifier == TST_auto) {
1367 // "int auto" case: Convert 'auto' to storage class specifier
1368 StorageClassSpec = SCS_auto;
1369 StorageClassSpecLoc = ConflictingTypeSpecifierLoc;
1370 // TypeSpecType already has the correct type (e.g., TST_int)
1371 // Clear the conflict tracking
1372 ConflictingTypeSpecifier = TST_unspecified;
1373 ConflictingTypeSpecifierLoc = SourceLocation();
1374 }
1375 }
1376 }
1377
1378 // Validate and finalize AltiVec vector declspec.
1379 if (TypeAltiVecVector) {
1380 // No vector long long without VSX (or ZVector).
1381 if ((getTypeSpecWidth() == TypeSpecifierWidth::LongLong) &&
1382 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1383 !S.getLangOpts().ZVector)
1384 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_long_decl_spec);
1385
1386 // No vector __int128 prior to Power8 (or ZVector).
1387 if ((TypeSpecType == TST_int128) &&
1388 !S.Context.getTargetInfo().hasFeature(Feature: "power8-vector") &&
1389 !S.getLangOpts().ZVector)
1390 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_int128_decl_spec);
1391
1392 // Complex vector types are not supported.
1393 if (TypeSpecComplex != TSC_unspecified)
1394 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_vector_complex_decl_spec);
1395 else if (TypeAltiVecBool) {
1396 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1397 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1398 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1399 << getSpecifierName(S: getTypeSpecSign());
1400 }
1401 // Only char/int are valid with vector bool prior to Power10.
1402 // Power10 adds instructions that produce vector bool data
1403 // for quadwords as well so allow vector bool __int128.
1404 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1405 (TypeSpecType != TST_int) && (TypeSpecType != TST_int128)) ||
1406 TypeAltiVecPixel) {
1407 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1408 << (TypeAltiVecPixel ? "__pixel" :
1409 getSpecifierName(T: (TST)TypeSpecType, Policy));
1410 }
1411 // vector bool __int128 requires Power10 (or ZVector).
1412 if ((TypeSpecType == TST_int128) &&
1413 (!S.Context.getTargetInfo().hasFeature(Feature: "power10-vector") &&
1414 !S.getLangOpts().ZVector))
1415 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_int128_decl_spec);
1416
1417 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1418 if ((getTypeSpecWidth() != TypeSpecifierWidth::Unspecified) &&
1419 (getTypeSpecWidth() != TypeSpecifierWidth::Short) &&
1420 (getTypeSpecWidth() != TypeSpecifierWidth::LongLong))
1421 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_bool_decl_spec)
1422 << getSpecifierName(W: getTypeSpecWidth());
1423
1424 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1425 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1426 (TypeSpecType == TST_int128) ||
1427 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified))
1428 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1429 } else if (TypeSpecType == TST_double) {
1430 // vector long double and vector long long double are never allowed.
1431 // vector double is OK for Power7 and later, and ZVector.
1432 if (getTypeSpecWidth() == TypeSpecifierWidth::Long ||
1433 getTypeSpecWidth() == TypeSpecifierWidth::LongLong)
1434 S.Diag(Loc: TSWRange.getBegin(),
1435 DiagID: diag::err_invalid_vector_long_double_decl_spec);
1436 else if (!S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1437 !S.getLangOpts().ZVector)
1438 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_double_decl_spec);
1439 } else if (TypeSpecType == TST_float) {
1440 // vector float is unsupported for ZVector unless we have the
1441 // vector-enhancements facility 1 (ISA revision 12).
1442 if (S.getLangOpts().ZVector &&
1443 !S.Context.getTargetInfo().hasFeature(Feature: "arch12"))
1444 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_float_decl_spec);
1445 } else if (getTypeSpecWidth() == TypeSpecifierWidth::Long) {
1446 // Vector long is unsupported for ZVector, or without VSX, and deprecated
1447 // for AltiVec.
1448 // It has also been historically deprecated on AIX (as an alias for
1449 // "vector int" in both 32-bit and 64-bit modes). It was then made
1450 // unsupported in the Clang-based XL compiler since the deprecated type
1451 // has a number of conflicting semantics and continuing to support it
1452 // is a disservice to users.
1453 if (S.getLangOpts().ZVector ||
1454 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") ||
1455 S.Context.getTargetInfo().getTriple().isOSAIX())
1456 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_decl_spec);
1457 else
1458 S.Diag(Loc: TSWRange.getBegin(),
1459 DiagID: diag::warn_vector_long_decl_spec_combination)
1460 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1461 }
1462
1463 if (TypeAltiVecPixel) {
1464 //TODO: perform validation
1465 TypeSpecType = TST_int;
1466 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1467 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Short);
1468 TypeSpecOwned = false;
1469 }
1470 }
1471
1472 bool IsFixedPointType =
1473 TypeSpecType == TST_accum || TypeSpecType == TST_fract;
1474
1475 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
1476 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1477 if (TypeSpecType == TST_unspecified)
1478 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1479 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1480 TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
1481 !IsFixedPointType && TypeSpecType != TST_bitint) {
1482 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_sign_spec)
1483 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1484 // signed double -> double.
1485 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1486 }
1487 }
1488
1489 // Validate the width of the type.
1490 switch (getTypeSpecWidth()) {
1491 case TypeSpecifierWidth::Unspecified:
1492 break;
1493 case TypeSpecifierWidth::Short: // short int
1494 case TypeSpecifierWidth::LongLong: // long long int
1495 if (TypeSpecType == TST_unspecified)
1496 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1497 else if (!(TypeSpecType == TST_int ||
1498 (IsFixedPointType &&
1499 getTypeSpecWidth() != TypeSpecifierWidth::LongLong))) {
1500 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1501 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1502 TypeSpecType = TST_int;
1503 TypeSpecSat = false;
1504 TypeSpecOwned = false;
1505 }
1506 break;
1507 case TypeSpecifierWidth::Long: // long double, long int
1508 if (TypeSpecType == TST_unspecified)
1509 TypeSpecType = TST_int; // long -> long int.
1510 else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
1511 !IsFixedPointType) {
1512 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1513 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1514 TypeSpecType = TST_int;
1515 TypeSpecSat = false;
1516 TypeSpecOwned = false;
1517 }
1518 break;
1519 }
1520
1521 // TODO: if the implementation does not implement _Complex, disallow their
1522 // use. Need information about the backend.
1523 if (TypeSpecComplex != TSC_unspecified) {
1524 if (TypeSpecType == TST_unspecified) {
1525 S.Diag(Loc: TSCLoc, DiagID: diag::ext_plain_complex)
1526 << FixItHint::CreateInsertion(
1527 InsertionLoc: S.getLocForEndOfToken(Loc: getTypeSpecComplexLoc()),
1528 Code: " double");
1529 TypeSpecType = TST_double; // _Complex -> _Complex double.
1530 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1531 // Note that this intentionally doesn't include _Complex _Bool.
1532 if (!S.getLangOpts().CPlusPlus)
1533 S.Diag(Loc: TSTLoc, DiagID: diag::ext_integer_complex);
1534 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double &&
1535 TypeSpecType != TST_float128 && TypeSpecType != TST_float16 &&
1536 TypeSpecType != TST_ibm128) {
1537 // FIXME: __fp16?
1538 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_complex_spec)
1539 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1540 TypeSpecComplex = TSC_unspecified;
1541 }
1542 }
1543
1544 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1545 // _Thread_local can only appear with the 'static' and 'extern' storage class
1546 // specifiers. We also allow __private_extern__ as an extension.
1547 if (ThreadStorageClassSpec != TSCS_unspecified) {
1548 switch (StorageClassSpec) {
1549 case SCS_unspecified:
1550 case SCS_extern:
1551 case SCS_private_extern:
1552 case SCS_static:
1553 break;
1554 default:
1555 if (S.getSourceManager().isBeforeInTranslationUnit(
1556 LHS: getThreadStorageClassSpecLoc(), RHS: getStorageClassSpecLoc()))
1557 S.Diag(Loc: getStorageClassSpecLoc(),
1558 DiagID: diag::err_invalid_decl_spec_combination)
1559 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1560 << SourceRange(getThreadStorageClassSpecLoc());
1561 else
1562 S.Diag(Loc: getThreadStorageClassSpecLoc(),
1563 DiagID: diag::err_invalid_decl_spec_combination)
1564 << DeclSpec::getSpecifierName(S: getStorageClassSpec())
1565 << SourceRange(getStorageClassSpecLoc());
1566 // Discard the thread storage class specifier to recover.
1567 ThreadStorageClassSpec = TSCS_unspecified;
1568 ThreadStorageClassSpecLoc = SourceLocation();
1569 }
1570 if (S.getLangOpts().C23 &&
1571 getConstexprSpecifier() == ConstexprSpecKind::Constexpr) {
1572 S.Diag(Loc: ConstexprLoc, DiagID: diag::err_invalid_decl_spec_combination)
1573 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1574 << SourceRange(getThreadStorageClassSpecLoc());
1575 }
1576 }
1577
1578 if (S.getLangOpts().C23 && getTypeSpecType() != DeclSpec::TST_unspecified &&
1579 getConstexprSpecifier() == ConstexprSpecKind::Constexpr &&
1580 (StorageClassSpec == SCS_extern || StorageClassSpec == SCS_auto)) {
1581 S.Diag(Loc: getStorageClassSpecLoc(), DiagID: diag::err_invalid_decl_spec_combination)
1582 << DeclSpec::getSpecifierName(C: getConstexprSpecifier())
1583 << SourceRange(getConstexprSpecLoc());
1584 }
1585
1586 // If no type specifier was provided and we're parsing a language where
1587 // the type specifier is not optional, but we got 'auto' as a storage
1588 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1589 // type specifier.
1590 if (S.getLangOpts().CPlusPlus &&
1591 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1592 TypeSpecType = TST_auto;
1593 StorageClassSpec = SCS_unspecified;
1594 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1595 StorageClassSpecLoc = SourceLocation();
1596 }
1597 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1598 // specifier in a pre-C++11 dialect of C++ or in a pre-C23 dialect of C.
1599 if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().C23 &&
1600 TypeSpecType == TST_auto)
1601 S.Diag(Loc: TSTLoc, DiagID: diag::ext_auto_type_specifier) << /*C++*/ 0;
1602 if (S.getLangOpts().HLSL &&
1603 S.getLangOpts().getHLSLVersion() < LangOptions::HLSL_202y &&
1604 TypeSpecType == TST_auto)
1605 S.Diag(Loc: TSTLoc, DiagID: diag::ext_hlsl_auto_type_specifier) << /*HLSL*/ 1;
1606 // Emit warning for 'auto' storage class in pre-C++11 dialects
1607 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1608 StorageClassSpec == SCS_auto)
1609 S.Diag(Loc: StorageClassSpecLoc, DiagID: diag::warn_auto_storage_class)
1610 << FixItHint::CreateRemoval(RemoveRange: StorageClassSpecLoc);
1611 if (TypeSpecType == TST_char8)
1612 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx17_compat_unicode_type);
1613 else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1614 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx98_compat_unicode_type)
1615 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1616 if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr)
1617 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx98_compat_constexpr);
1618 else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval)
1619 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_consteval);
1620 else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit)
1621 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_constinit);
1622}
1623
1624void DeclSpec::CheckFriendSpec(Sema &S, const PrintingPolicy &Policy) {
1625 // C++ [class.friend]p6:
1626 // No storage-class-specifier shall appear in the decl-specifier-seq
1627 // of a friend declaration.
1628 if (isFriendSpecified() &&
1629 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1630 SmallString<32> SpecName;
1631 SourceLocation SCLoc;
1632 FixItHint StorageHint, ThreadHint;
1633
1634 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1635 SpecName = getSpecifierName(S: SC);
1636 SCLoc = getStorageClassSpecLoc();
1637 StorageHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1638 }
1639
1640 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1641 if (!SpecName.empty()) SpecName += " ";
1642 SpecName += getSpecifierName(S: TSC);
1643 SCLoc = getThreadStorageClassSpecLoc();
1644 ThreadHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1645 }
1646
1647 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1648 << SpecName << StorageHint << ThreadHint;
1649
1650 ClearStorageClassSpecs();
1651 }
1652
1653 // C++11 [dcl.fct.spec]p5:
1654 // The virtual specifier shall be used only in the initial
1655 // declaration of a non-static class member function;
1656 // C++11 [dcl.fct.spec]p6:
1657 // The explicit specifier shall be used only in the declaration of
1658 // a constructor or conversion function within its class
1659 // definition;
1660 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
1661 StringRef Keyword;
1662 FixItHint Hint;
1663 SourceLocation SCLoc;
1664
1665 if (isVirtualSpecified()) {
1666 Keyword = "virtual";
1667 SCLoc = getVirtualSpecLoc();
1668 Hint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1669 } else {
1670 Keyword = "explicit";
1671 SCLoc = getExplicitSpecLoc();
1672 Hint = FixItHint::CreateRemoval(RemoveRange: getExplicitSpecRange());
1673 }
1674
1675 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1676 << Keyword << Hint;
1677
1678 FS_virtual_specified = false;
1679 FS_explicit_specifier = ExplicitSpecifier();
1680 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1681 }
1682}
1683
1684bool DeclSpec::isMissingDeclaratorOk() {
1685 TST tst = getTypeSpecType();
1686 return isDeclRep(T: tst) && getRepAsDecl() != nullptr &&
1687 StorageClassSpec != DeclSpec::SCS_typedef;
1688}
1689
1690void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1691 OverloadedOperatorKind Op,
1692 SourceLocation SymbolLocations[3]) {
1693 Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
1694 StartLocation = OperatorLoc;
1695 EndLocation = OperatorLoc;
1696 new (&OperatorFunctionId) struct OFI;
1697 OperatorFunctionId.Operator = Op;
1698 for (unsigned I = 0; I != 3; ++I) {
1699 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I];
1700
1701 if (SymbolLocations[I].isValid())
1702 EndLocation = SymbolLocations[I];
1703 }
1704}
1705
1706bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1707 const char *&PrevSpec) {
1708 if (!FirstLocation.isValid())
1709 FirstLocation = Loc;
1710 LastLocation = Loc;
1711 LastSpecifier = VS;
1712
1713 if (Specifiers & VS) {
1714 PrevSpec = getSpecifierName(VS);
1715 return true;
1716 }
1717
1718 Specifiers |= VS;
1719
1720 switch (VS) {
1721 default: llvm_unreachable("Unknown specifier!");
1722 case VS_Override: VS_overrideLoc = Loc; break;
1723 case VS_GNU_Final:
1724 case VS_Sealed:
1725 case VS_Final: VS_finalLoc = Loc; break;
1726 case VS_Abstract: VS_abstractLoc = Loc; break;
1727 }
1728
1729 return false;
1730}
1731
1732const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1733 switch (VS) {
1734 default: llvm_unreachable("Unknown specifier");
1735 case VS_Override: return "override";
1736 case VS_Final: return "final";
1737 case VS_GNU_Final: return "__final";
1738 case VS_Sealed: return "sealed";
1739 case VS_Abstract: return "abstract";
1740 }
1741}
1742