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