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