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