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