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