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