DeclSpec.cpp revision e2f82f71385051ce5abfba317d2f592aa332c588
1//===--- SemaDeclSpec.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/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency! 15#include "clang/Sema/DeclSpec.h" 16#include "clang/Sema/ParsedTemplate.h" 17#include "clang/Lex/Preprocessor.h" 18#include "clang/Basic/LangOptions.h" 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/Support/ErrorHandling.h" 21#include <cstring> 22using namespace clang; 23 24 25static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc, 26 unsigned DiagID) { 27 return D.Report(Loc, DiagID); 28} 29 30 31void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) { 32 assert(TemplateId && "NULL template-id annotation?"); 33 Kind = IK_TemplateId; 34 this->TemplateId = TemplateId; 35 StartLocation = TemplateId->TemplateNameLoc; 36 EndLocation = TemplateId->RAngleLoc; 37} 38 39void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) { 40 assert(TemplateId && "NULL template-id annotation?"); 41 Kind = IK_ConstructorTemplateId; 42 this->TemplateId = TemplateId; 43 StartLocation = TemplateId->TemplateNameLoc; 44 EndLocation = TemplateId->RAngleLoc; 45} 46 47/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 48/// "TheDeclarator" is the declarator that this will be added to. 49DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs, 50 bool hasProto, bool isVariadic, 51 SourceLocation EllipsisLoc, 52 ParamInfo *ArgInfo, 53 unsigned NumArgs, 54 unsigned TypeQuals, 55 bool RefQualifierIsLvalueRef, 56 SourceLocation RefQualifierLoc, 57 bool hasExceptionSpec, 58 SourceLocation ThrowLoc, 59 bool hasAnyExceptionSpec, 60 ParsedType *Exceptions, 61 SourceRange *ExceptionRanges, 62 unsigned NumExceptions, 63 SourceLocation LPLoc, 64 SourceLocation RPLoc, 65 Declarator &TheDeclarator, 66 ParsedType TrailingReturnType) { 67 DeclaratorChunk I; 68 I.Kind = Function; 69 I.Loc = LPLoc; 70 I.EndLoc = RPLoc; 71 I.Fun.AttrList = attrs.getList(); 72 I.Fun.hasPrototype = hasProto; 73 I.Fun.isVariadic = isVariadic; 74 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding(); 75 I.Fun.DeleteArgInfo = false; 76 I.Fun.TypeQuals = TypeQuals; 77 I.Fun.NumArgs = NumArgs; 78 I.Fun.ArgInfo = 0; 79 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef; 80 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); 81 I.Fun.hasExceptionSpec = hasExceptionSpec; 82 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding(); 83 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec; 84 I.Fun.NumExceptions = NumExceptions; 85 I.Fun.Exceptions = 0; 86 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr(); 87 88 // new[] an argument array if needed. 89 if (NumArgs) { 90 // If the 'InlineParams' in Declarator is unused and big enough, put our 91 // parameter list there (in an effort to avoid new/delete traffic). If it 92 // is already used (consider a function returning a function pointer) or too 93 // small (function taking too many arguments), go to the heap. 94 if (!TheDeclarator.InlineParamsUsed && 95 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) { 96 I.Fun.ArgInfo = TheDeclarator.InlineParams; 97 I.Fun.DeleteArgInfo = false; 98 TheDeclarator.InlineParamsUsed = true; 99 } else { 100 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs]; 101 I.Fun.DeleteArgInfo = true; 102 } 103 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs); 104 } 105 // new[] an exception array if needed 106 if (NumExceptions) { 107 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions]; 108 for (unsigned i = 0; i != NumExceptions; ++i) { 109 I.Fun.Exceptions[i].Ty = Exceptions[i]; 110 I.Fun.Exceptions[i].Range = ExceptionRanges[i]; 111 } 112 } 113 return I; 114} 115 116/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this 117/// declaration specifier includes. 118/// 119unsigned DeclSpec::getParsedSpecifiers() const { 120 unsigned Res = 0; 121 if (StorageClassSpec != SCS_unspecified || 122 SCS_thread_specified) 123 Res |= PQ_StorageClassSpecifier; 124 125 if (TypeQualifiers != TQ_unspecified) 126 Res |= PQ_TypeQualifier; 127 128 if (hasTypeSpecifier()) 129 Res |= PQ_TypeSpecifier; 130 131 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) 132 Res |= PQ_FunctionSpecifier; 133 return Res; 134} 135 136template <class T> static bool BadSpecifier(T TNew, T TPrev, 137 const char *&PrevSpec, 138 unsigned &DiagID) { 139 PrevSpec = DeclSpec::getSpecifierName(TPrev); 140 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec 141 : diag::err_invalid_decl_spec_combination); 142 return true; 143} 144 145const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { 146 switch (S) { 147 case DeclSpec::SCS_unspecified: return "unspecified"; 148 case DeclSpec::SCS_typedef: return "typedef"; 149 case DeclSpec::SCS_extern: return "extern"; 150 case DeclSpec::SCS_static: return "static"; 151 case DeclSpec::SCS_auto: return "auto"; 152 case DeclSpec::SCS_register: return "register"; 153 case DeclSpec::SCS_private_extern: return "__private_extern__"; 154 case DeclSpec::SCS_mutable: return "mutable"; 155 } 156 llvm_unreachable("Unknown typespec!"); 157} 158 159const char *DeclSpec::getSpecifierName(TSW W) { 160 switch (W) { 161 case TSW_unspecified: return "unspecified"; 162 case TSW_short: return "short"; 163 case TSW_long: return "long"; 164 case TSW_longlong: return "long long"; 165 } 166 llvm_unreachable("Unknown typespec!"); 167} 168 169const char *DeclSpec::getSpecifierName(TSC C) { 170 switch (C) { 171 case TSC_unspecified: return "unspecified"; 172 case TSC_imaginary: return "imaginary"; 173 case TSC_complex: return "complex"; 174 } 175 llvm_unreachable("Unknown typespec!"); 176} 177 178 179const char *DeclSpec::getSpecifierName(TSS S) { 180 switch (S) { 181 case TSS_unspecified: return "unspecified"; 182 case TSS_signed: return "signed"; 183 case TSS_unsigned: return "unsigned"; 184 } 185 llvm_unreachable("Unknown typespec!"); 186} 187 188const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { 189 switch (T) { 190 case DeclSpec::TST_unspecified: return "unspecified"; 191 case DeclSpec::TST_void: return "void"; 192 case DeclSpec::TST_char: return "char"; 193 case DeclSpec::TST_wchar: return "wchar_t"; 194 case DeclSpec::TST_char16: return "char16_t"; 195 case DeclSpec::TST_char32: return "char32_t"; 196 case DeclSpec::TST_int: return "int"; 197 case DeclSpec::TST_float: return "float"; 198 case DeclSpec::TST_double: return "double"; 199 case DeclSpec::TST_bool: return "_Bool"; 200 case DeclSpec::TST_decimal32: return "_Decimal32"; 201 case DeclSpec::TST_decimal64: return "_Decimal64"; 202 case DeclSpec::TST_decimal128: return "_Decimal128"; 203 case DeclSpec::TST_enum: return "enum"; 204 case DeclSpec::TST_class: return "class"; 205 case DeclSpec::TST_union: return "union"; 206 case DeclSpec::TST_struct: return "struct"; 207 case DeclSpec::TST_typename: return "type-name"; 208 case DeclSpec::TST_typeofType: 209 case DeclSpec::TST_typeofExpr: return "typeof"; 210 case DeclSpec::TST_auto: return "auto"; 211 case DeclSpec::TST_decltype: return "(decltype)"; 212 case DeclSpec::TST_error: return "(error)"; 213 } 214 llvm_unreachable("Unknown typespec!"); 215} 216 217const char *DeclSpec::getSpecifierName(TQ T) { 218 switch (T) { 219 case DeclSpec::TQ_unspecified: return "unspecified"; 220 case DeclSpec::TQ_const: return "const"; 221 case DeclSpec::TQ_restrict: return "restrict"; 222 case DeclSpec::TQ_volatile: return "volatile"; 223 } 224 llvm_unreachable("Unknown typespec!"); 225} 226 227bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc, 228 const char *&PrevSpec, 229 unsigned &DiagID, 230 const LangOptions &Lang) { 231 // OpenCL prohibits extern, auto, register, and static 232 // It seems sensible to prohibit private_extern too 233 if (Lang.OpenCL) { 234 switch (S) { 235 case SCS_extern: 236 case SCS_private_extern: 237 case SCS_auto: 238 case SCS_register: 239 case SCS_static: 240 DiagID = diag::err_not_opencl_storage_class_specifier; 241 PrevSpec = getSpecifierName(S); 242 return true; 243 default: 244 break; 245 } 246 } 247 248 if (StorageClassSpec != SCS_unspecified) { 249 // Changing storage class is allowed only if the previous one 250 // was the 'extern' that is part of a linkage specification and 251 // the new storage class is 'typedef'. 252 if (!(SCS_extern_in_linkage_spec && 253 StorageClassSpec == SCS_extern && 254 S == SCS_typedef)) 255 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID); 256 } 257 StorageClassSpec = S; 258 StorageClassSpecLoc = Loc; 259 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield"); 260 return false; 261} 262 263bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, 264 const char *&PrevSpec, 265 unsigned &DiagID) { 266 if (SCS_thread_specified) { 267 PrevSpec = "__thread"; 268 DiagID = diag::ext_duplicate_declspec; 269 return true; 270 } 271 SCS_thread_specified = true; 272 SCS_threadLoc = Loc; 273 return false; 274} 275 276/// These methods set the specified attribute of the DeclSpec, but return true 277/// and ignore the request if invalid (e.g. "extern" then "auto" is 278/// specified). 279bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, 280 const char *&PrevSpec, 281 unsigned &DiagID) { 282 if (TypeSpecWidth != TSW_unspecified && 283 // Allow turning long -> long long. 284 (W != TSW_longlong || TypeSpecWidth != TSW_long)) 285 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); 286 TypeSpecWidth = W; 287 TSWLoc = Loc; 288 if (TypeAltiVecVector && !TypeAltiVecBool && 289 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) { 290 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 291 DiagID = diag::warn_vector_long_decl_spec_combination; 292 return true; 293 } 294 return false; 295} 296 297bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, 298 const char *&PrevSpec, 299 unsigned &DiagID) { 300 if (TypeSpecComplex != TSC_unspecified) 301 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); 302 TypeSpecComplex = C; 303 TSCLoc = Loc; 304 return false; 305} 306 307bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, 308 const char *&PrevSpec, 309 unsigned &DiagID) { 310 if (TypeSpecSign != TSS_unspecified) 311 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); 312 TypeSpecSign = S; 313 TSSLoc = Loc; 314 return false; 315} 316 317bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 318 const char *&PrevSpec, 319 unsigned &DiagID, 320 ParsedType Rep) { 321 assert(isTypeRep(T) && "T does not store a type"); 322 assert(Rep && "no type provided!"); 323 if (TypeSpecType != TST_unspecified) { 324 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 325 DiagID = diag::err_invalid_decl_spec_combination; 326 return true; 327 } 328 TypeSpecType = T; 329 TypeRep = Rep; 330 TSTLoc = Loc; 331 TypeSpecOwned = false; 332 return false; 333} 334 335bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 336 const char *&PrevSpec, 337 unsigned &DiagID, 338 Expr *Rep) { 339 assert(isExprRep(T) && "T does not store an expr"); 340 assert(Rep && "no expression provided!"); 341 if (TypeSpecType != TST_unspecified) { 342 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 343 DiagID = diag::err_invalid_decl_spec_combination; 344 return true; 345 } 346 TypeSpecType = T; 347 ExprRep = Rep; 348 TSTLoc = Loc; 349 TypeSpecOwned = false; 350 return false; 351} 352 353bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 354 const char *&PrevSpec, 355 unsigned &DiagID, 356 Decl *Rep, bool Owned) { 357 assert(isDeclRep(T) && "T does not store a decl"); 358 // Unlike the other cases, we don't assert that we actually get a decl. 359 360 if (TypeSpecType != TST_unspecified) { 361 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 362 DiagID = diag::err_invalid_decl_spec_combination; 363 return true; 364 } 365 TypeSpecType = T; 366 DeclRep = Rep; 367 TSTLoc = Loc; 368 TypeSpecOwned = Owned; 369 return false; 370} 371 372bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, 373 const char *&PrevSpec, 374 unsigned &DiagID) { 375 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && 376 "rep required for these type-spec kinds!"); 377 if (TypeSpecType != TST_unspecified) { 378 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 379 DiagID = diag::err_invalid_decl_spec_combination; 380 return true; 381 } 382 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { 383 TypeAltiVecBool = true; 384 TSTLoc = Loc; 385 return false; 386 } 387 TypeSpecType = T; 388 TSTLoc = Loc; 389 TypeSpecOwned = false; 390 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) { 391 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 392 DiagID = diag::err_invalid_vector_decl_spec; 393 return true; 394 } 395 return false; 396} 397 398bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, 399 const char *&PrevSpec, unsigned &DiagID) { 400 if (TypeSpecType != TST_unspecified) { 401 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 402 DiagID = diag::err_invalid_vector_decl_spec_combination; 403 return true; 404 } 405 TypeAltiVecVector = isAltiVecVector; 406 AltiVecLoc = Loc; 407 return false; 408} 409 410bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, 411 const char *&PrevSpec, unsigned &DiagID) { 412 if (!TypeAltiVecVector || TypeAltiVecPixel || 413 (TypeSpecType != TST_unspecified)) { 414 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); 415 DiagID = diag::err_invalid_pixel_decl_spec_combination; 416 return true; 417 } 418 TypeAltiVecPixel = isAltiVecPixel; 419 TSTLoc = Loc; 420 return false; 421} 422 423bool DeclSpec::SetTypeSpecError() { 424 TypeSpecType = TST_error; 425 TypeSpecOwned = false; 426 TSTLoc = SourceLocation(); 427 return false; 428} 429 430bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, 431 unsigned &DiagID, const LangOptions &Lang) { 432 // Duplicates turn into warnings pre-C99. 433 if ((TypeQualifiers & T) && !Lang.C99) 434 return BadSpecifier(T, T, PrevSpec, DiagID); 435 TypeQualifiers |= T; 436 437 switch (T) { 438 default: assert(0 && "Unknown type qualifier!"); 439 case TQ_const: TQ_constLoc = Loc; break; 440 case TQ_restrict: TQ_restrictLoc = Loc; break; 441 case TQ_volatile: TQ_volatileLoc = Loc; break; 442 } 443 return false; 444} 445 446bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, 447 unsigned &DiagID) { 448 // 'inline inline' is ok. 449 FS_inline_specified = true; 450 FS_inlineLoc = Loc; 451 return false; 452} 453 454bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, 455 unsigned &DiagID) { 456 // 'virtual virtual' is ok. 457 FS_virtual_specified = true; 458 FS_virtualLoc = Loc; 459 return false; 460} 461 462bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, 463 unsigned &DiagID) { 464 // 'explicit explicit' is ok. 465 FS_explicit_specified = true; 466 FS_explicitLoc = Loc; 467 return false; 468} 469 470bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, 471 unsigned &DiagID) { 472 if (Friend_specified) { 473 PrevSpec = "friend"; 474 DiagID = diag::ext_duplicate_declspec; 475 return true; 476 } 477 478 Friend_specified = true; 479 FriendLoc = Loc; 480 return false; 481} 482 483bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, 484 unsigned &DiagID) { 485 // 'constexpr constexpr' is ok. 486 Constexpr_specified = true; 487 ConstexprLoc = Loc; 488 return false; 489} 490 491void DeclSpec::setProtocolQualifiers(Decl * const *Protos, 492 unsigned NP, 493 SourceLocation *ProtoLocs, 494 SourceLocation LAngleLoc) { 495 if (NP == 0) return; 496 ProtocolQualifiers = new Decl*[NP]; 497 ProtocolLocs = new SourceLocation[NP]; 498 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP); 499 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); 500 NumProtocolQualifiers = NP; 501 ProtocolLAngleLoc = LAngleLoc; 502} 503 504void DeclSpec::SaveWrittenBuiltinSpecs() { 505 writtenBS.Sign = getTypeSpecSign(); 506 writtenBS.Width = getTypeSpecWidth(); 507 writtenBS.Type = getTypeSpecType(); 508 // Search the list of attributes for the presence of a mode attribute. 509 writtenBS.ModeAttr = false; 510 AttributeList* attrs = getAttributes().getList(); 511 while (attrs) { 512 if (attrs->getKind() == AttributeList::AT_mode) { 513 writtenBS.ModeAttr = true; 514 break; 515 } 516 attrs = attrs->getNext(); 517 } 518} 519 520void DeclSpec::SaveStorageSpecifierAsWritten() { 521 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern) 522 // If 'extern' is part of a linkage specification, 523 // then it is not a storage class "as written". 524 StorageClassSpecAsWritten = SCS_unspecified; 525 else 526 StorageClassSpecAsWritten = StorageClassSpec; 527} 528 529/// Finish - This does final analysis of the declspec, rejecting things like 530/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or 531/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, 532/// DeclSpec is guaranteed self-consistent, even if an error occurred. 533void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) { 534 // Before possibly changing their values, save specs as written. 535 SaveWrittenBuiltinSpecs(); 536 SaveStorageSpecifierAsWritten(); 537 538 // Check the type specifier components first. 539 540 // Validate and finalize AltiVec vector declspec. 541 if (TypeAltiVecVector) { 542 if (TypeAltiVecBool) { 543 // Sign specifiers are not allowed with vector bool. (PIM 2.1) 544 if (TypeSpecSign != TSS_unspecified) { 545 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) 546 << getSpecifierName((TSS)TypeSpecSign); 547 } 548 549 // Only char/int are valid with vector bool. (PIM 2.1) 550 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && 551 (TypeSpecType != TST_int)) || TypeAltiVecPixel) { 552 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) 553 << (TypeAltiVecPixel ? "__pixel" : 554 getSpecifierName((TST)TypeSpecType)); 555 } 556 557 // Only 'short' is valid with vector bool. (PIM 2.1) 558 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) 559 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) 560 << getSpecifierName((TSW)TypeSpecWidth); 561 562 // Elements of vector bool are interpreted as unsigned. (PIM 2.1) 563 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || 564 (TypeSpecWidth != TSW_unspecified)) 565 TypeSpecSign = TSS_unsigned; 566 } 567 568 if (TypeAltiVecPixel) { 569 //TODO: perform validation 570 TypeSpecType = TST_int; 571 TypeSpecSign = TSS_unsigned; 572 TypeSpecWidth = TSW_short; 573 TypeSpecOwned = false; 574 } 575 } 576 577 // signed/unsigned are only valid with int/char/wchar_t. 578 if (TypeSpecSign != TSS_unspecified) { 579 if (TypeSpecType == TST_unspecified) 580 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. 581 else if (TypeSpecType != TST_int && 582 TypeSpecType != TST_char && TypeSpecType != TST_wchar) { 583 Diag(D, TSSLoc, diag::err_invalid_sign_spec) 584 << getSpecifierName((TST)TypeSpecType); 585 // signed double -> double. 586 TypeSpecSign = TSS_unspecified; 587 } 588 } 589 590 // Validate the width of the type. 591 switch (TypeSpecWidth) { 592 case TSW_unspecified: break; 593 case TSW_short: // short int 594 case TSW_longlong: // long long int 595 if (TypeSpecType == TST_unspecified) 596 TypeSpecType = TST_int; // short -> short int, long long -> long long int. 597 else if (TypeSpecType != TST_int) { 598 Diag(D, TSWLoc, 599 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec 600 : diag::err_invalid_longlong_spec) 601 << getSpecifierName((TST)TypeSpecType); 602 TypeSpecType = TST_int; 603 TypeSpecOwned = false; 604 } 605 break; 606 case TSW_long: // long double, long int 607 if (TypeSpecType == TST_unspecified) 608 TypeSpecType = TST_int; // long -> long int. 609 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { 610 Diag(D, TSWLoc, diag::err_invalid_long_spec) 611 << getSpecifierName((TST)TypeSpecType); 612 TypeSpecType = TST_int; 613 TypeSpecOwned = false; 614 } 615 break; 616 } 617 618 // TODO: if the implementation does not implement _Complex or _Imaginary, 619 // disallow their use. Need information about the backend. 620 if (TypeSpecComplex != TSC_unspecified) { 621 if (TypeSpecType == TST_unspecified) { 622 Diag(D, TSCLoc, diag::ext_plain_complex) 623 << FixItHint::CreateInsertion( 624 PP.getLocForEndOfToken(getTypeSpecComplexLoc()), 625 " double"); 626 TypeSpecType = TST_double; // _Complex -> _Complex double. 627 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { 628 // Note that this intentionally doesn't include _Complex _Bool. 629 Diag(D, TSTLoc, diag::ext_integer_complex); 630 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { 631 Diag(D, TSCLoc, diag::err_invalid_complex_spec) 632 << getSpecifierName((TST)TypeSpecType); 633 TypeSpecComplex = TSC_unspecified; 634 } 635 } 636 637 // C++ [class.friend]p6: 638 // No storage-class-specifier shall appear in the decl-specifier-seq 639 // of a friend declaration. 640 if (isFriendSpecified() && getStorageClassSpec()) { 641 DeclSpec::SCS SC = getStorageClassSpec(); 642 const char *SpecName = getSpecifierName(SC); 643 644 SourceLocation SCLoc = getStorageClassSpecLoc(); 645 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName)); 646 647 Diag(D, SCLoc, diag::err_friend_storage_spec) 648 << SpecName 649 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc)); 650 651 ClearStorageClassSpecs(); 652 } 653 654 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); 655 656 // Okay, now we can infer the real type. 657 658 // TODO: return "auto function" and other bad things based on the real type. 659 660 // 'data definition has no type or storage class'? 661} 662 663bool DeclSpec::isMissingDeclaratorOk() { 664 TST tst = getTypeSpecType(); 665 return isDeclRep(tst) && getRepAsDecl() != 0 && 666 StorageClassSpec != DeclSpec::SCS_typedef; 667} 668 669void UnqualifiedId::clear() { 670 if (Kind == IK_TemplateId) 671 TemplateId->Destroy(); 672 673 Kind = IK_Identifier; 674 Identifier = 0; 675 StartLocation = SourceLocation(); 676 EndLocation = SourceLocation(); 677} 678 679void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, 680 OverloadedOperatorKind Op, 681 SourceLocation SymbolLocations[3]) { 682 Kind = IK_OperatorFunctionId; 683 StartLocation = OperatorLoc; 684 EndLocation = OperatorLoc; 685 OperatorFunctionId.Operator = Op; 686 for (unsigned I = 0; I != 3; ++I) { 687 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); 688 689 if (SymbolLocations[I].isValid()) 690 EndLocation = SymbolLocations[I]; 691 } 692} 693 694bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc, 695 const char *&PrevSpec) { 696 if (Specifiers & VS) { 697 PrevSpec = getSpecifierName(VS); 698 return true; 699 } 700 701 Specifiers |= VS; 702 703 switch (VS) { 704 default: assert(0 && "Unknown specifier!"); 705 case VS_Override: VS_overrideLoc = Loc; break; 706 case VS_Final: VS_finalLoc = Loc; break; 707 case VS_New: VS_newLoc = Loc; break; 708 } 709 710 return false; 711} 712 713const char *VirtSpecifiers::getSpecifierName(Specifier VS) { 714 switch (VS) { 715 default: assert(0 && "Unknown specifier"); 716 case VS_Override: return "override"; 717 case VS_Final: return "final"; 718 case VS_New: return "new"; 719 } 720} 721 722bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc, 723 const char *&PrevSpec) { 724 if (Specifiers & CVS) { 725 PrevSpec = getSpecifierName(CVS); 726 return true; 727 } 728 729 Specifiers |= CVS; 730 731 switch (CVS) { 732 default: assert(0 && "Unknown specifier!"); 733 case CVS_Final: CVS_finalLoc = Loc; break; 734 case CVS_Explicit: CVS_explicitLoc = Loc; break; 735 } 736 737 return false; 738} 739 740const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) { 741 switch (CVS) { 742 default: assert(0 && "Unknown specifier"); 743 case CVS_Final: return "final"; 744 case CVS_Explicit: return "explicit"; 745 } 746} 747 748