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