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