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