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
619bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
620 const char *&PrevSpec,
621 unsigned &DiagID,
622 const PrintingPolicy &Policy) {
623 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
624 // specifiers are not supported.
625 // It seems sensible to prohibit private_extern too
626 // The cl_clang_storage_class_specifiers extension enables support for
627 // these storage-class specifiers.
628 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
629 // specifiers are not supported."
630 if (S.getLangOpts().OpenCL &&
631 !S.getOpenCLOptions().isAvailableOption(
632 Ext: "cl_clang_storage_class_specifiers", LO: S.getLangOpts())) {
633 switch (SC) {
634 case SCS_extern:
635 case SCS_private_extern:
636 case SCS_static:
637 if (S.getLangOpts().getOpenCLCompatibleVersion() < 120) {
638 DiagID = diag::err_opencl_unknown_type_specifier;
639 PrevSpec = getSpecifierName(S: SC);
640 return true;
641 }
642 break;
643 case SCS_auto:
644 case SCS_register:
645 DiagID = diag::err_opencl_unknown_type_specifier;
646 PrevSpec = getSpecifierName(S: SC);
647 return true;
648 default:
649 break;
650 }
651 }
652
653 if (StorageClassSpec != SCS_unspecified) {
654 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
655 bool isInvalid = true;
656 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
657 if (SC == SCS_auto)
658 return SetTypeSpecType(T: TST_auto, Loc, PrevSpec, DiagID, Policy);
659 if (StorageClassSpec == SCS_auto) {
660 isInvalid = SetTypeSpecType(T: TST_auto, Loc: StorageClassSpecLoc,
661 PrevSpec, DiagID, Policy);
662 assert(!isInvalid && "auto SCS -> TST recovery failed");
663 }
664 }
665
666 // Changing storage class is allowed only if the previous one
667 // was the 'extern' that is part of a linkage specification and
668 // the new storage class is 'typedef'.
669 if (isInvalid &&
670 !(SCS_extern_in_linkage_spec &&
671 StorageClassSpec == SCS_extern &&
672 SC == SCS_typedef))
673 return BadSpecifier(TNew: SC, TPrev: (SCS)StorageClassSpec, PrevSpec, DiagID);
674 }
675 StorageClassSpec = SC;
676 StorageClassSpecLoc = Loc;
677 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
678 return false;
679}
680
681bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
682 const char *&PrevSpec,
683 unsigned &DiagID) {
684 if (ThreadStorageClassSpec != TSCS_unspecified)
685 return BadSpecifier(TNew: TSC, TPrev: (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
686
687 ThreadStorageClassSpec = TSC;
688 ThreadStorageClassSpecLoc = Loc;
689 return false;
690}
691
692/// These methods set the specified attribute of the DeclSpec, but return true
693/// and ignore the request if invalid (e.g. "extern" then "auto" is
694/// specified).
695bool DeclSpec::SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc,
696 const char *&PrevSpec, unsigned &DiagID,
697 const PrintingPolicy &Policy) {
698 // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
699 // for 'long long' we will keep the source location of the first 'long'.
700 if (getTypeSpecWidth() == TypeSpecifierWidth::Unspecified)
701 TSWRange.setBegin(Loc);
702 // Allow turning long -> long long.
703 else if (W != TypeSpecifierWidth::LongLong ||
704 getTypeSpecWidth() != TypeSpecifierWidth::Long)
705 return BadSpecifier(TNew: W, TPrev: getTypeSpecWidth(), PrevSpec, DiagID);
706 TypeSpecWidth = static_cast<unsigned>(W);
707 // Remember location of the last 'long'
708 TSWRange.setEnd(Loc);
709 return false;
710}
711
712bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
713 const char *&PrevSpec,
714 unsigned &DiagID) {
715 if (TypeSpecComplex != TSC_unspecified)
716 return BadSpecifier(TNew: C, TPrev: (TSC)TypeSpecComplex, PrevSpec, DiagID);
717 TypeSpecComplex = C;
718 TSCLoc = Loc;
719 return false;
720}
721
722bool DeclSpec::SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc,
723 const char *&PrevSpec, unsigned &DiagID) {
724 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified)
725 return BadSpecifier(TNew: S, TPrev: getTypeSpecSign(), PrevSpec, DiagID);
726 TypeSpecSign = static_cast<unsigned>(S);
727 TSSLoc = Loc;
728 return false;
729}
730
731bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
732 const char *&PrevSpec,
733 unsigned &DiagID,
734 ParsedType Rep,
735 const PrintingPolicy &Policy) {
736 return SetTypeSpecType(T, TagKwLoc: Loc, TagNameLoc: Loc, PrevSpec, DiagID, Rep, Policy);
737}
738
739bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
740 SourceLocation TagNameLoc,
741 const char *&PrevSpec,
742 unsigned &DiagID,
743 ParsedType Rep,
744 const PrintingPolicy &Policy) {
745 assert(isTypeRep(T) && "T does not store a type");
746 assert(Rep && "no type provided!");
747 if (TypeSpecType == TST_error)
748 return false;
749 if (TypeSpecType != TST_unspecified) {
750 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
751 DiagID = diag::err_invalid_decl_spec_combination;
752 return true;
753 }
754 TypeSpecType = T;
755 TypeRep = Rep;
756 TSTLoc = TagKwLoc;
757 TSTNameLoc = TagNameLoc;
758 TypeSpecOwned = false;
759
760 if (T == TST_typename_pack_indexing) {
761 // we got there from a an annotation. Reconstruct the type
762 // Ugly...
763 QualType QT = Rep.get();
764 const PackIndexingType *LIT = cast<PackIndexingType>(Val&: QT);
765 TypeRep = ParsedType::make(P: LIT->getPattern());
766 PackIndexingExpr = LIT->getIndexExpr();
767 }
768 return false;
769}
770
771bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
772 const char *&PrevSpec,
773 unsigned &DiagID,
774 Expr *Rep,
775 const PrintingPolicy &Policy) {
776 assert(isExprRep(T) && "T does not store an expr");
777 assert(Rep && "no expression provided!");
778 if (TypeSpecType == TST_error)
779 return false;
780 if (TypeSpecType != TST_unspecified) {
781 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
782 DiagID = diag::err_invalid_decl_spec_combination;
783 return true;
784 }
785 TypeSpecType = T;
786 ExprRep = Rep;
787 TSTLoc = Loc;
788 TSTNameLoc = Loc;
789 TypeSpecOwned = false;
790 return false;
791}
792
793bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
794 const char *&PrevSpec,
795 unsigned &DiagID,
796 Decl *Rep, bool Owned,
797 const PrintingPolicy &Policy) {
798 return SetTypeSpecType(T, TagKwLoc: Loc, TagNameLoc: Loc, PrevSpec, DiagID, Rep, Owned, Policy);
799}
800
801bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
802 SourceLocation TagNameLoc,
803 const char *&PrevSpec,
804 unsigned &DiagID,
805 Decl *Rep, bool Owned,
806 const PrintingPolicy &Policy) {
807 assert(isDeclRep(T) && "T does not store a decl");
808 // Unlike the other cases, we don't assert that we actually get a decl.
809
810 if (TypeSpecType == TST_error)
811 return false;
812 if (TypeSpecType != TST_unspecified) {
813 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
814 DiagID = diag::err_invalid_decl_spec_combination;
815 return true;
816 }
817 TypeSpecType = T;
818 DeclRep = Rep;
819 TSTLoc = TagKwLoc;
820 TSTNameLoc = TagNameLoc;
821 TypeSpecOwned = Owned && Rep != nullptr;
822 return false;
823}
824
825bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
826 unsigned &DiagID, TemplateIdAnnotation *Rep,
827 const PrintingPolicy &Policy) {
828 assert(T == TST_auto || T == TST_decltype_auto);
829 ConstrainedAuto = true;
830 TemplateIdRep = Rep;
831 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Policy);
832}
833
834bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
835 const char *&PrevSpec,
836 unsigned &DiagID,
837 const PrintingPolicy &Policy) {
838 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
839 "rep required for these type-spec kinds!");
840 if (TypeSpecType == TST_error)
841 return false;
842 if (TypeSpecType != TST_unspecified) {
843 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
844 DiagID = diag::err_invalid_decl_spec_combination;
845 return true;
846 }
847 TSTLoc = Loc;
848 TSTNameLoc = Loc;
849 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
850 TypeAltiVecBool = true;
851 return false;
852 }
853 TypeSpecType = T;
854 TypeSpecOwned = false;
855 return false;
856}
857
858bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
859 unsigned &DiagID) {
860 // Cannot set twice
861 if (TypeSpecSat) {
862 DiagID = diag::warn_duplicate_declspec;
863 PrevSpec = "_Sat";
864 return true;
865 }
866 TypeSpecSat = true;
867 TSSatLoc = Loc;
868 return false;
869}
870
871bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
872 const char *&PrevSpec, unsigned &DiagID,
873 const PrintingPolicy &Policy) {
874 if (TypeSpecType == TST_error)
875 return false;
876 if (TypeSpecType != TST_unspecified) {
877 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
878 DiagID = diag::err_invalid_vector_decl_spec_combination;
879 return true;
880 }
881 TypeAltiVecVector = isAltiVecVector;
882 AltiVecLoc = Loc;
883 return false;
884}
885
886bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
887 const char *&PrevSpec, unsigned &DiagID,
888 const PrintingPolicy &Policy) {
889 if (TypeSpecType == TST_error)
890 return false;
891 if (TypeSpecType != TST_unspecified) {
892 PrevSpec = DeclSpec::getSpecifierName(T: (TST)TypeSpecType, Policy);
893 DiagID = diag::err_invalid_decl_spec_combination;
894 return true;
895 }
896
897 if (isPipe) {
898 TypeSpecPipe = static_cast<unsigned>(TypeSpecifiersPipe::Pipe);
899 }
900 return false;
901}
902
903bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
904 const char *&PrevSpec, unsigned &DiagID,
905 const PrintingPolicy &Policy) {
906 if (TypeSpecType == TST_error)
907 return false;
908 if (!TypeAltiVecVector || TypeAltiVecPixel ||
909 (TypeSpecType != TST_unspecified)) {
910 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
911 DiagID = diag::err_invalid_pixel_decl_spec_combination;
912 return true;
913 }
914 TypeAltiVecPixel = isAltiVecPixel;
915 TSTLoc = Loc;
916 TSTNameLoc = Loc;
917 return false;
918}
919
920bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
921 const char *&PrevSpec, unsigned &DiagID,
922 const PrintingPolicy &Policy) {
923 if (TypeSpecType == TST_error)
924 return false;
925 if (!TypeAltiVecVector || TypeAltiVecBool ||
926 (TypeSpecType != TST_unspecified)) {
927 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
928 DiagID = diag::err_invalid_vector_bool_decl_spec;
929 return true;
930 }
931 TypeAltiVecBool = isAltiVecBool;
932 TSTLoc = Loc;
933 TSTNameLoc = Loc;
934 return false;
935}
936
937bool DeclSpec::SetTypeSpecError() {
938 TypeSpecType = TST_error;
939 TypeSpecOwned = false;
940 TSTLoc = SourceLocation();
941 TSTNameLoc = SourceLocation();
942 return false;
943}
944
945bool DeclSpec::SetBitIntType(SourceLocation KWLoc, Expr *BitsExpr,
946 const char *&PrevSpec, unsigned &DiagID,
947 const PrintingPolicy &Policy) {
948 assert(BitsExpr && "no expression provided!");
949 if (TypeSpecType == TST_error)
950 return false;
951
952 if (TypeSpecType != TST_unspecified) {
953 PrevSpec = DeclSpec::getSpecifierName(T: (TST) TypeSpecType, Policy);
954 DiagID = diag::err_invalid_decl_spec_combination;
955 return true;
956 }
957
958 TypeSpecType = TST_bitint;
959 ExprRep = BitsExpr;
960 TSTLoc = KWLoc;
961 TSTNameLoc = KWLoc;
962 TypeSpecOwned = false;
963 return false;
964}
965
966void DeclSpec::SetPackIndexingExpr(SourceLocation EllipsisLoc,
967 Expr *IndexingExpr) {
968 assert(TypeSpecType == TST_typename &&
969 "pack indexing can only be applied to typename");
970 TypeSpecType = TST_typename_pack_indexing;
971 PackIndexingExpr = IndexingExpr;
972 this->EllipsisLoc = EllipsisLoc;
973}
974
975bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
976 unsigned &DiagID, const LangOptions &Lang) {
977 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
978 // C++. However, since this is likely not what the user intended, we will
979 // always warn. We do not need to set the qualifier's location since we
980 // already have it.
981 if (TypeQualifiers & T) {
982 bool IsExtension = true;
983 if (Lang.C99)
984 IsExtension = false;
985 return BadSpecifier(TNew: T, TPrev: T, PrevSpec, DiagID, IsExtension);
986 }
987
988 return SetTypeQual(T, Loc);
989}
990
991bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
992 TypeQualifiers |= T;
993
994 switch (T) {
995 case TQ_unspecified: break;
996 case TQ_const: TQ_constLoc = Loc; return false;
997 case TQ_restrict: TQ_restrictLoc = Loc; return false;
998 case TQ_volatile: TQ_volatileLoc = Loc; return false;
999 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
1000 case TQ_atomic: TQ_atomicLoc = Loc; return false;
1001 }
1002
1003 llvm_unreachable("Unknown type qualifier!");
1004}
1005
1006bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
1007 unsigned &DiagID) {
1008 // 'inline inline' is ok. However, since this is likely not what the user
1009 // intended, we will always warn, similar to duplicates of type qualifiers.
1010 if (FS_inline_specified) {
1011 DiagID = diag::warn_duplicate_declspec;
1012 PrevSpec = "inline";
1013 return true;
1014 }
1015 FS_inline_specified = true;
1016 FS_inlineLoc = Loc;
1017 return false;
1018}
1019
1020bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
1021 unsigned &DiagID) {
1022 if (FS_forceinline_specified) {
1023 DiagID = diag::warn_duplicate_declspec;
1024 PrevSpec = "__forceinline";
1025 return true;
1026 }
1027 FS_forceinline_specified = true;
1028 FS_forceinlineLoc = Loc;
1029 return false;
1030}
1031
1032bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
1033 const char *&PrevSpec,
1034 unsigned &DiagID) {
1035 // 'virtual virtual' is ok, but warn as this is likely not what the user
1036 // intended.
1037 if (FS_virtual_specified) {
1038 DiagID = diag::warn_duplicate_declspec;
1039 PrevSpec = "virtual";
1040 return true;
1041 }
1042 FS_virtual_specified = true;
1043 FS_virtualLoc = Loc;
1044 return false;
1045}
1046
1047bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
1048 const char *&PrevSpec, unsigned &DiagID,
1049 ExplicitSpecifier ExplicitSpec,
1050 SourceLocation CloseParenLoc) {
1051 // 'explicit explicit' is ok, but warn as this is likely not what the user
1052 // intended.
1053 if (hasExplicitSpecifier()) {
1054 DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
1055 ? diag::err_duplicate_declspec
1056 : diag::ext_warn_duplicate_declspec;
1057 PrevSpec = "explicit";
1058 return true;
1059 }
1060 FS_explicit_specifier = ExplicitSpec;
1061 FS_explicitLoc = Loc;
1062 FS_explicitCloseParenLoc = CloseParenLoc;
1063 return false;
1064}
1065
1066bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
1067 const char *&PrevSpec,
1068 unsigned &DiagID) {
1069 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1070 // intended.
1071 if (FS_noreturn_specified) {
1072 DiagID = diag::warn_duplicate_declspec;
1073 PrevSpec = "_Noreturn";
1074 return true;
1075 }
1076 FS_noreturn_specified = true;
1077 FS_noreturnLoc = Loc;
1078 return false;
1079}
1080
1081bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
1082 unsigned &DiagID) {
1083 if (isFriendSpecified()) {
1084 PrevSpec = "friend";
1085 DiagID = diag::warn_duplicate_declspec;
1086 return true;
1087 }
1088
1089 FriendSpecifiedFirst = isEmpty();
1090 FriendLoc = Loc;
1091 return false;
1092}
1093
1094bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
1095 unsigned &DiagID) {
1096 if (isModulePrivateSpecified()) {
1097 PrevSpec = "__module_private__";
1098 DiagID = diag::ext_warn_duplicate_declspec;
1099 return true;
1100 }
1101
1102 ModulePrivateLoc = Loc;
1103 return false;
1104}
1105
1106bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
1107 SourceLocation Loc, const char *&PrevSpec,
1108 unsigned &DiagID) {
1109 if (getConstexprSpecifier() != ConstexprSpecKind::Unspecified)
1110 return BadSpecifier(TNew: ConstexprKind, TPrev: getConstexprSpecifier(), PrevSpec,
1111 DiagID);
1112 ConstexprSpecifier = static_cast<unsigned>(ConstexprKind);
1113 ConstexprLoc = Loc;
1114 return false;
1115}
1116
1117void DeclSpec::SaveWrittenBuiltinSpecs() {
1118 writtenBS.Sign = static_cast<int>(getTypeSpecSign());
1119 writtenBS.Width = static_cast<int>(getTypeSpecWidth());
1120 writtenBS.Type = getTypeSpecType();
1121 // Search the list of attributes for the presence of a mode attribute.
1122 writtenBS.ModeAttr = getAttributes().hasAttribute(K: ParsedAttr::AT_Mode);
1123}
1124
1125/// Finish - This does final analysis of the declspec, rejecting things like
1126/// "_Complex" (lacking an FP type). After calling this method, DeclSpec is
1127/// guaranteed to be self-consistent, even if an error occurred.
1128void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
1129 // Before possibly changing their values, save specs as written.
1130 SaveWrittenBuiltinSpecs();
1131
1132 // Check the type specifier components first. No checking for an invalid
1133 // type.
1134 if (TypeSpecType == TST_error)
1135 return;
1136
1137 // If decltype(auto) is used, no other type specifiers are permitted.
1138 if (TypeSpecType == TST_decltype_auto &&
1139 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified ||
1140 TypeSpecComplex != TSC_unspecified ||
1141 getTypeSpecSign() != TypeSpecifierSign::Unspecified ||
1142 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
1143 TypeQualifiers)) {
1144 const unsigned NumLocs = 9;
1145 SourceLocation ExtraLocs[NumLocs] = {
1146 TSWRange.getBegin(), TSCLoc, TSSLoc,
1147 AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
1148 TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
1149 FixItHint Hints[NumLocs];
1150 SourceLocation FirstLoc;
1151 for (unsigned I = 0; I != NumLocs; ++I) {
1152 if (ExtraLocs[I].isValid()) {
1153 if (FirstLoc.isInvalid() ||
1154 S.getSourceManager().isBeforeInTranslationUnit(LHS: ExtraLocs[I],
1155 RHS: FirstLoc))
1156 FirstLoc = ExtraLocs[I];
1157 Hints[I] = FixItHint::CreateRemoval(RemoveRange: ExtraLocs[I]);
1158 }
1159 }
1160 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Unspecified);
1161 TypeSpecComplex = TSC_unspecified;
1162 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1163 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
1164 TypeQualifiers = 0;
1165 S.Diag(Loc: TSTLoc, DiagID: diag::err_decltype_auto_cannot_be_combined)
1166 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
1167 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
1168 }
1169
1170 // Validate and finalize AltiVec vector declspec.
1171 if (TypeAltiVecVector) {
1172 // No vector long long without VSX (or ZVector).
1173 if ((getTypeSpecWidth() == TypeSpecifierWidth::LongLong) &&
1174 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1175 !S.getLangOpts().ZVector)
1176 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_long_decl_spec);
1177
1178 // No vector __int128 prior to Power8 (or ZVector).
1179 if ((TypeSpecType == TST_int128) &&
1180 !S.Context.getTargetInfo().hasFeature(Feature: "power8-vector") &&
1181 !S.getLangOpts().ZVector)
1182 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_int128_decl_spec);
1183
1184 // Complex vector types are not supported.
1185 if (TypeSpecComplex != TSC_unspecified)
1186 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_vector_complex_decl_spec);
1187 else if (TypeAltiVecBool) {
1188 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1189 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1190 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1191 << getSpecifierName(S: getTypeSpecSign());
1192 }
1193 // Only char/int are valid with vector bool prior to Power10.
1194 // Power10 adds instructions that produce vector bool data
1195 // for quadwords as well so allow vector bool __int128.
1196 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1197 (TypeSpecType != TST_int) && (TypeSpecType != TST_int128)) ||
1198 TypeAltiVecPixel) {
1199 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_decl_spec)
1200 << (TypeAltiVecPixel ? "__pixel" :
1201 getSpecifierName(T: (TST)TypeSpecType, Policy));
1202 }
1203 // vector bool __int128 requires Power10 (or ZVector).
1204 if ((TypeSpecType == TST_int128) &&
1205 (!S.Context.getTargetInfo().hasFeature(Feature: "power10-vector") &&
1206 !S.getLangOpts().ZVector))
1207 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_bool_int128_decl_spec);
1208
1209 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1210 if ((getTypeSpecWidth() != TypeSpecifierWidth::Unspecified) &&
1211 (getTypeSpecWidth() != TypeSpecifierWidth::Short) &&
1212 (getTypeSpecWidth() != TypeSpecifierWidth::LongLong))
1213 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_bool_decl_spec)
1214 << getSpecifierName(W: getTypeSpecWidth());
1215
1216 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1217 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1218 (TypeSpecType == TST_int128) ||
1219 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified))
1220 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1221 } else if (TypeSpecType == TST_double) {
1222 // vector long double and vector long long double are never allowed.
1223 // vector double is OK for Power7 and later, and ZVector.
1224 if (getTypeSpecWidth() == TypeSpecifierWidth::Long ||
1225 getTypeSpecWidth() == TypeSpecifierWidth::LongLong)
1226 S.Diag(Loc: TSWRange.getBegin(),
1227 DiagID: diag::err_invalid_vector_long_double_decl_spec);
1228 else if (!S.Context.getTargetInfo().hasFeature(Feature: "vsx") &&
1229 !S.getLangOpts().ZVector)
1230 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_double_decl_spec);
1231 } else if (TypeSpecType == TST_float) {
1232 // vector float is unsupported for ZVector unless we have the
1233 // vector-enhancements facility 1 (ISA revision 12).
1234 if (S.getLangOpts().ZVector &&
1235 !S.Context.getTargetInfo().hasFeature(Feature: "arch12"))
1236 S.Diag(Loc: TSTLoc, DiagID: diag::err_invalid_vector_float_decl_spec);
1237 } else if (getTypeSpecWidth() == TypeSpecifierWidth::Long) {
1238 // Vector long is unsupported for ZVector, or without VSX, and deprecated
1239 // for AltiVec.
1240 // It has also been historically deprecated on AIX (as an alias for
1241 // "vector int" in both 32-bit and 64-bit modes). It was then made
1242 // unsupported in the Clang-based XL compiler since the deprecated type
1243 // has a number of conflicting semantics and continuing to support it
1244 // is a disservice to users.
1245 if (S.getLangOpts().ZVector ||
1246 !S.Context.getTargetInfo().hasFeature(Feature: "vsx") ||
1247 S.Context.getTargetInfo().getTriple().isOSAIX())
1248 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_vector_long_decl_spec);
1249 else
1250 S.Diag(Loc: TSWRange.getBegin(),
1251 DiagID: diag::warn_vector_long_decl_spec_combination)
1252 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1253 }
1254
1255 if (TypeAltiVecPixel) {
1256 //TODO: perform validation
1257 TypeSpecType = TST_int;
1258 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1259 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Short);
1260 TypeSpecOwned = false;
1261 }
1262 }
1263
1264 bool IsFixedPointType =
1265 TypeSpecType == TST_accum || TypeSpecType == TST_fract;
1266
1267 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
1268 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1269 if (TypeSpecType == TST_unspecified)
1270 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1271 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1272 TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
1273 !IsFixedPointType && TypeSpecType != TST_bitint) {
1274 S.Diag(Loc: TSSLoc, DiagID: diag::err_invalid_sign_spec)
1275 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1276 // signed double -> double.
1277 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1278 }
1279 }
1280
1281 // Validate the width of the type.
1282 switch (getTypeSpecWidth()) {
1283 case TypeSpecifierWidth::Unspecified:
1284 break;
1285 case TypeSpecifierWidth::Short: // short int
1286 case TypeSpecifierWidth::LongLong: // long long int
1287 if (TypeSpecType == TST_unspecified)
1288 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1289 else if (!(TypeSpecType == TST_int ||
1290 (IsFixedPointType &&
1291 getTypeSpecWidth() != TypeSpecifierWidth::LongLong))) {
1292 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1293 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1294 TypeSpecType = TST_int;
1295 TypeSpecSat = false;
1296 TypeSpecOwned = false;
1297 }
1298 break;
1299 case TypeSpecifierWidth::Long: // long double, long int
1300 if (TypeSpecType == TST_unspecified)
1301 TypeSpecType = TST_int; // long -> long int.
1302 else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
1303 !IsFixedPointType) {
1304 S.Diag(Loc: TSWRange.getBegin(), DiagID: diag::err_invalid_width_spec)
1305 << (int)TypeSpecWidth << getSpecifierName(T: (TST)TypeSpecType, Policy);
1306 TypeSpecType = TST_int;
1307 TypeSpecSat = false;
1308 TypeSpecOwned = false;
1309 }
1310 break;
1311 }
1312
1313 // TODO: if the implementation does not implement _Complex, disallow their
1314 // use. Need information about the backend.
1315 if (TypeSpecComplex != TSC_unspecified) {
1316 if (TypeSpecType == TST_unspecified) {
1317 S.Diag(Loc: TSCLoc, DiagID: diag::ext_plain_complex)
1318 << FixItHint::CreateInsertion(
1319 InsertionLoc: S.getLocForEndOfToken(Loc: getTypeSpecComplexLoc()),
1320 Code: " double");
1321 TypeSpecType = TST_double; // _Complex -> _Complex double.
1322 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1323 // Note that this intentionally doesn't include _Complex _Bool.
1324 if (!S.getLangOpts().CPlusPlus)
1325 S.Diag(Loc: TSTLoc, DiagID: diag::ext_integer_complex);
1326 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double &&
1327 TypeSpecType != TST_float128 && TypeSpecType != TST_float16 &&
1328 TypeSpecType != TST_ibm128) {
1329 // FIXME: __fp16?
1330 S.Diag(Loc: TSCLoc, DiagID: diag::err_invalid_complex_spec)
1331 << getSpecifierName(T: (TST)TypeSpecType, Policy);
1332 TypeSpecComplex = TSC_unspecified;
1333 }
1334 }
1335
1336 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1337 // _Thread_local can only appear with the 'static' and 'extern' storage class
1338 // specifiers. We also allow __private_extern__ as an extension.
1339 if (ThreadStorageClassSpec != TSCS_unspecified) {
1340 switch (StorageClassSpec) {
1341 case SCS_unspecified:
1342 case SCS_extern:
1343 case SCS_private_extern:
1344 case SCS_static:
1345 break;
1346 default:
1347 if (S.getSourceManager().isBeforeInTranslationUnit(
1348 LHS: getThreadStorageClassSpecLoc(), RHS: getStorageClassSpecLoc()))
1349 S.Diag(Loc: getStorageClassSpecLoc(),
1350 DiagID: diag::err_invalid_decl_spec_combination)
1351 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1352 << SourceRange(getThreadStorageClassSpecLoc());
1353 else
1354 S.Diag(Loc: getThreadStorageClassSpecLoc(),
1355 DiagID: diag::err_invalid_decl_spec_combination)
1356 << DeclSpec::getSpecifierName(S: getStorageClassSpec())
1357 << SourceRange(getStorageClassSpecLoc());
1358 // Discard the thread storage class specifier to recover.
1359 ThreadStorageClassSpec = TSCS_unspecified;
1360 ThreadStorageClassSpecLoc = SourceLocation();
1361 }
1362 if (S.getLangOpts().C23 &&
1363 getConstexprSpecifier() == ConstexprSpecKind::Constexpr) {
1364 S.Diag(Loc: ConstexprLoc, DiagID: diag::err_invalid_decl_spec_combination)
1365 << DeclSpec::getSpecifierName(S: getThreadStorageClassSpec())
1366 << SourceRange(getThreadStorageClassSpecLoc());
1367 }
1368 }
1369
1370 if (S.getLangOpts().C23 &&
1371 getConstexprSpecifier() == ConstexprSpecKind::Constexpr &&
1372 getTypeSpecType() != TST_unspecified &&
1373 (StorageClassSpec == SCS_extern || StorageClassSpec == SCS_auto)) {
1374 S.Diag(Loc: ConstexprLoc, DiagID: diag::err_invalid_decl_spec_combination)
1375 << DeclSpec::getSpecifierName(S: getStorageClassSpec())
1376 << SourceRange(getStorageClassSpecLoc());
1377 }
1378
1379 // If no type specifier was provided and we're parsing a language where
1380 // the type specifier is not optional, but we got 'auto' as a storage
1381 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1382 // type specifier.
1383 if (S.getLangOpts().CPlusPlus &&
1384 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1385 TypeSpecType = TST_auto;
1386 StorageClassSpec = SCS_unspecified;
1387 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1388 StorageClassSpecLoc = SourceLocation();
1389 }
1390 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1391 // specifier in a pre-C++11 dialect of C++ or in a pre-C23 dialect of C.
1392 if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().C23 &&
1393 TypeSpecType == TST_auto)
1394 S.Diag(Loc: TSTLoc, DiagID: diag::ext_auto_type_specifier) << /*C++*/ 0;
1395 if (S.getLangOpts().HLSL &&
1396 S.getLangOpts().getHLSLVersion() < LangOptions::HLSL_202y &&
1397 TypeSpecType == TST_auto)
1398 S.Diag(Loc: TSTLoc, DiagID: diag::ext_hlsl_auto_type_specifier) << /*HLSL*/ 1;
1399 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1400 StorageClassSpec == SCS_auto)
1401 S.Diag(Loc: StorageClassSpecLoc, DiagID: diag::warn_auto_storage_class)
1402 << FixItHint::CreateRemoval(RemoveRange: StorageClassSpecLoc);
1403 if (TypeSpecType == TST_char8)
1404 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx17_compat_unicode_type);
1405 else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1406 S.Diag(Loc: TSTLoc, DiagID: diag::warn_cxx98_compat_unicode_type)
1407 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1408 if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr)
1409 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx98_compat_constexpr);
1410 else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval)
1411 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_consteval);
1412 else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit)
1413 S.Diag(Loc: ConstexprLoc, DiagID: diag::warn_cxx20_compat_constinit);
1414 // C++ [class.friend]p6:
1415 // No storage-class-specifier shall appear in the decl-specifier-seq
1416 // of a friend declaration.
1417 if (isFriendSpecified() &&
1418 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1419 SmallString<32> SpecName;
1420 SourceLocation SCLoc;
1421 FixItHint StorageHint, ThreadHint;
1422
1423 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1424 SpecName = getSpecifierName(S: SC);
1425 SCLoc = getStorageClassSpecLoc();
1426 StorageHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1427 }
1428
1429 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1430 if (!SpecName.empty()) SpecName += " ";
1431 SpecName += getSpecifierName(S: TSC);
1432 SCLoc = getThreadStorageClassSpecLoc();
1433 ThreadHint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1434 }
1435
1436 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1437 << SpecName << StorageHint << ThreadHint;
1438
1439 ClearStorageClassSpecs();
1440 }
1441
1442 // C++11 [dcl.fct.spec]p5:
1443 // The virtual specifier shall be used only in the initial
1444 // declaration of a non-static class member function;
1445 // C++11 [dcl.fct.spec]p6:
1446 // The explicit specifier shall be used only in the declaration of
1447 // a constructor or conversion function within its class
1448 // definition;
1449 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
1450 StringRef Keyword;
1451 FixItHint Hint;
1452 SourceLocation SCLoc;
1453
1454 if (isVirtualSpecified()) {
1455 Keyword = "virtual";
1456 SCLoc = getVirtualSpecLoc();
1457 Hint = FixItHint::CreateRemoval(RemoveRange: SCLoc);
1458 } else {
1459 Keyword = "explicit";
1460 SCLoc = getExplicitSpecLoc();
1461 Hint = FixItHint::CreateRemoval(RemoveRange: getExplicitSpecRange());
1462 }
1463
1464 S.Diag(Loc: SCLoc, DiagID: diag::err_friend_decl_spec)
1465 << Keyword << Hint;
1466
1467 FS_virtual_specified = false;
1468 FS_explicit_specifier = ExplicitSpecifier();
1469 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1470 }
1471
1472 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1473
1474 // Okay, now we can infer the real type.
1475
1476 // TODO: return "auto function" and other bad things based on the real type.
1477
1478 // 'data definition has no type or storage class'?
1479}
1480
1481bool DeclSpec::isMissingDeclaratorOk() {
1482 TST tst = getTypeSpecType();
1483 return isDeclRep(T: tst) && getRepAsDecl() != nullptr &&
1484 StorageClassSpec != DeclSpec::SCS_typedef;
1485}
1486
1487void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1488 OverloadedOperatorKind Op,
1489 SourceLocation SymbolLocations[3]) {
1490 Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
1491 StartLocation = OperatorLoc;
1492 EndLocation = OperatorLoc;
1493 new (&OperatorFunctionId) struct OFI;
1494 OperatorFunctionId.Operator = Op;
1495 for (unsigned I = 0; I != 3; ++I) {
1496 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I];
1497
1498 if (SymbolLocations[I].isValid())
1499 EndLocation = SymbolLocations[I];
1500 }
1501}
1502
1503bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1504 const char *&PrevSpec) {
1505 if (!FirstLocation.isValid())
1506 FirstLocation = Loc;
1507 LastLocation = Loc;
1508 LastSpecifier = VS;
1509
1510 if (Specifiers & VS) {
1511 PrevSpec = getSpecifierName(VS);
1512 return true;
1513 }
1514
1515 Specifiers |= VS;
1516
1517 switch (VS) {
1518 default: llvm_unreachable("Unknown specifier!");
1519 case VS_Override: VS_overrideLoc = Loc; break;
1520 case VS_GNU_Final:
1521 case VS_Sealed:
1522 case VS_Final: VS_finalLoc = Loc; break;
1523 case VS_Abstract: VS_abstractLoc = Loc; break;
1524 }
1525
1526 return false;
1527}
1528
1529const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1530 switch (VS) {
1531 default: llvm_unreachable("Unknown specifier");
1532 case VS_Override: return "override";
1533 case VS_Final: return "final";
1534 case VS_GNU_Final: return "__final";
1535 case VS_Sealed: return "sealed";
1536 case VS_Abstract: return "abstract";
1537 }
1538}
1539