SemaType.cpp revision fd509f767badc944f8fce4527bd8178862059983
1//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// 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 type-related semantic analysis. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Sema/SemaInternal.h" 15#include "clang/Sema/Template.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/CXXInheritance.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclTemplate.h" 20#include "clang/AST/TypeLoc.h" 21#include "clang/AST/TypeLocVisitor.h" 22#include "clang/AST/Expr.h" 23#include "clang/Basic/PartialDiagnostic.h" 24#include "clang/Basic/TargetInfo.h" 25#include "clang/Sema/DeclSpec.h" 26#include "llvm/ADT/SmallPtrSet.h" 27#include "llvm/Support/ErrorHandling.h" 28using namespace clang; 29 30/// \brief Perform adjustment on the parameter type of a function. 31/// 32/// This routine adjusts the given parameter type @p T to the actual 33/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 34/// C++ [dcl.fct]p3). The adjusted parameter type is returned. 35QualType Sema::adjustParameterType(QualType T) { 36 // C99 6.7.5.3p7: 37 // A declaration of a parameter as "array of type" shall be 38 // adjusted to "qualified pointer to type", where the type 39 // qualifiers (if any) are those specified within the [ and ] of 40 // the array type derivation. 41 if (T->isArrayType()) 42 return Context.getArrayDecayedType(T); 43 44 // C99 6.7.5.3p8: 45 // A declaration of a parameter as "function returning type" 46 // shall be adjusted to "pointer to function returning type", as 47 // in 6.3.2.1. 48 if (T->isFunctionType()) 49 return Context.getPointerType(T); 50 51 return T; 52} 53 54 55 56/// isOmittedBlockReturnType - Return true if this declarator is missing a 57/// return type because this is a omitted return type on a block literal. 58static bool isOmittedBlockReturnType(const Declarator &D) { 59 if (D.getContext() != Declarator::BlockLiteralContext || 60 D.getDeclSpec().hasTypeSpecifier()) 61 return false; 62 63 if (D.getNumTypeObjects() == 0) 64 return true; // ^{ ... } 65 66 if (D.getNumTypeObjects() == 1 && 67 D.getTypeObject(0).Kind == DeclaratorChunk::Function) 68 return true; // ^(int X, float Y) { ... } 69 70 return false; 71} 72 73typedef std::pair<const AttributeList*,QualType> DelayedAttribute; 74typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet; 75 76static void ProcessTypeAttributeList(Sema &S, QualType &Type, 77 bool IsDeclSpec, 78 const AttributeList *Attrs, 79 DelayedAttributeSet &DelayedFnAttrs); 80static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr); 81 82static void ProcessDelayedFnAttrs(Sema &S, QualType &Type, 83 DelayedAttributeSet &Attrs) { 84 for (DelayedAttributeSet::iterator I = Attrs.begin(), 85 E = Attrs.end(); I != E; ++I) 86 if (ProcessFnAttr(S, Type, *I->first)) { 87 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) 88 << I->first->getName() << I->second; 89 // Avoid any further processing of this attribute. 90 I->first->setInvalid(); 91 } 92 Attrs.clear(); 93} 94 95static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) { 96 for (DelayedAttributeSet::iterator I = Attrs.begin(), 97 E = Attrs.end(); I != E; ++I) { 98 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) 99 << I->first->getName() << I->second; 100 // Avoid any further processing of this attribute. 101 I->first->setInvalid(); 102 } 103 Attrs.clear(); 104} 105 106/// \brief Convert the specified declspec to the appropriate type 107/// object. 108/// \param D the declarator containing the declaration specifier. 109/// \returns The type described by the declaration specifiers. This function 110/// never returns null. 111static QualType ConvertDeclSpecToType(Sema &TheSema, 112 Declarator &TheDeclarator, 113 DelayedAttributeSet &Delayed) { 114 // FIXME: Should move the logic from DeclSpec::Finish to here for validity 115 // checking. 116 const DeclSpec &DS = TheDeclarator.getDeclSpec(); 117 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc(); 118 if (DeclLoc.isInvalid()) 119 DeclLoc = DS.getSourceRange().getBegin(); 120 121 ASTContext &Context = TheSema.Context; 122 123 QualType Result; 124 switch (DS.getTypeSpecType()) { 125 case DeclSpec::TST_void: 126 Result = Context.VoidTy; 127 break; 128 case DeclSpec::TST_char: 129 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 130 Result = Context.CharTy; 131 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) 132 Result = Context.SignedCharTy; 133 else { 134 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 135 "Unknown TSS value"); 136 Result = Context.UnsignedCharTy; 137 } 138 break; 139 case DeclSpec::TST_wchar: 140 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 141 Result = Context.WCharTy; 142 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { 143 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 144 << DS.getSpecifierName(DS.getTypeSpecType()); 145 Result = Context.getSignedWCharType(); 146 } else { 147 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 148 "Unknown TSS value"); 149 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 150 << DS.getSpecifierName(DS.getTypeSpecType()); 151 Result = Context.getUnsignedWCharType(); 152 } 153 break; 154 case DeclSpec::TST_char16: 155 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && 156 "Unknown TSS value"); 157 Result = Context.Char16Ty; 158 break; 159 case DeclSpec::TST_char32: 160 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && 161 "Unknown TSS value"); 162 Result = Context.Char32Ty; 163 break; 164 case DeclSpec::TST_unspecified: 165 // "<proto1,proto2>" is an objc qualified ID with a missing id. 166 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 167 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 168 (ObjCProtocolDecl**)PQ, 169 DS.getNumProtocolQualifiers()); 170 Result = Context.getObjCObjectPointerType(Result); 171 break; 172 } 173 174 // If this is a missing declspec in a block literal return context, then it 175 // is inferred from the return statements inside the block. 176 if (isOmittedBlockReturnType(TheDeclarator)) { 177 Result = Context.DependentTy; 178 break; 179 } 180 181 // Unspecified typespec defaults to int in C90. However, the C90 grammar 182 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, 183 // type-qualifier, or storage-class-specifier. If not, emit an extwarn. 184 // Note that the one exception to this is function definitions, which are 185 // allowed to be completely missing a declspec. This is handled in the 186 // parser already though by it pretending to have seen an 'int' in this 187 // case. 188 if (TheSema.getLangOptions().ImplicitInt) { 189 // In C89 mode, we only warn if there is a completely missing declspec 190 // when one is not allowed. 191 if (DS.isEmpty()) { 192 TheSema.Diag(DeclLoc, diag::ext_missing_declspec) 193 << DS.getSourceRange() 194 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int"); 195 } 196 } else if (!DS.hasTypeSpecifier()) { 197 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: 198 // "At least one type specifier shall be given in the declaration 199 // specifiers in each declaration, and in the specifier-qualifier list in 200 // each struct declaration and type name." 201 // FIXME: Does Microsoft really have the implicit int extension in C++? 202 if (TheSema.getLangOptions().CPlusPlus && 203 !TheSema.getLangOptions().Microsoft) { 204 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier) 205 << DS.getSourceRange(); 206 207 // When this occurs in C++ code, often something is very broken with the 208 // value being declared, poison it as invalid so we don't get chains of 209 // errors. 210 TheDeclarator.setInvalidType(true); 211 } else { 212 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier) 213 << DS.getSourceRange(); 214 } 215 } 216 217 // FALL THROUGH. 218 case DeclSpec::TST_int: { 219 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { 220 switch (DS.getTypeSpecWidth()) { 221 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; 222 case DeclSpec::TSW_short: Result = Context.ShortTy; break; 223 case DeclSpec::TSW_long: Result = Context.LongTy; break; 224 case DeclSpec::TSW_longlong: 225 Result = Context.LongLongTy; 226 227 // long long is a C99 feature. 228 if (!TheSema.getLangOptions().C99 && 229 !TheSema.getLangOptions().CPlusPlus0x) 230 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); 231 break; 232 } 233 } else { 234 switch (DS.getTypeSpecWidth()) { 235 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; 236 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; 237 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; 238 case DeclSpec::TSW_longlong: 239 Result = Context.UnsignedLongLongTy; 240 241 // long long is a C99 feature. 242 if (!TheSema.getLangOptions().C99 && 243 !TheSema.getLangOptions().CPlusPlus0x) 244 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); 245 break; 246 } 247 } 248 break; 249 } 250 case DeclSpec::TST_float: Result = Context.FloatTy; break; 251 case DeclSpec::TST_double: 252 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) 253 Result = Context.LongDoubleTy; 254 else 255 Result = Context.DoubleTy; 256 break; 257 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool 258 case DeclSpec::TST_decimal32: // _Decimal32 259 case DeclSpec::TST_decimal64: // _Decimal64 260 case DeclSpec::TST_decimal128: // _Decimal128 261 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); 262 Result = Context.IntTy; 263 TheDeclarator.setInvalidType(true); 264 break; 265 case DeclSpec::TST_class: 266 case DeclSpec::TST_enum: 267 case DeclSpec::TST_union: 268 case DeclSpec::TST_struct: { 269 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl()); 270 if (!D) { 271 // This can happen in C++ with ambiguous lookups. 272 Result = Context.IntTy; 273 TheDeclarator.setInvalidType(true); 274 break; 275 } 276 277 // If the type is deprecated or unavailable, diagnose it. 278 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc()); 279 280 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 281 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); 282 283 // TypeQuals handled by caller. 284 Result = Context.getTypeDeclType(D); 285 286 // In C++, make an ElaboratedType. 287 if (TheSema.getLangOptions().CPlusPlus) { 288 ElaboratedTypeKeyword Keyword 289 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); 290 Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(), 291 Result); 292 } 293 if (D->isInvalidDecl()) 294 TheDeclarator.setInvalidType(true); 295 break; 296 } 297 case DeclSpec::TST_typename: { 298 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 299 DS.getTypeSpecSign() == 0 && 300 "Can't handle qualifiers on typedef names yet!"); 301 Result = TheSema.GetTypeFromParser(DS.getRepAsType()); 302 if (Result.isNull()) 303 TheDeclarator.setInvalidType(true); 304 else if (DeclSpec::ProtocolQualifierListTy PQ 305 = DS.getProtocolQualifiers()) { 306 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) { 307 // Silently drop any existing protocol qualifiers. 308 // TODO: determine whether that's the right thing to do. 309 if (ObjT->getNumProtocols()) 310 Result = ObjT->getBaseType(); 311 312 if (DS.getNumProtocolQualifiers()) 313 Result = Context.getObjCObjectType(Result, 314 (ObjCProtocolDecl**) PQ, 315 DS.getNumProtocolQualifiers()); 316 } else if (Result->isObjCIdType()) { 317 // id<protocol-list> 318 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 319 (ObjCProtocolDecl**) PQ, 320 DS.getNumProtocolQualifiers()); 321 Result = Context.getObjCObjectPointerType(Result); 322 } else if (Result->isObjCClassType()) { 323 // Class<protocol-list> 324 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 325 (ObjCProtocolDecl**) PQ, 326 DS.getNumProtocolQualifiers()); 327 Result = Context.getObjCObjectPointerType(Result); 328 } else { 329 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) 330 << DS.getSourceRange(); 331 TheDeclarator.setInvalidType(true); 332 } 333 } 334 335 // TypeQuals handled by caller. 336 break; 337 } 338 case DeclSpec::TST_typeofType: 339 // FIXME: Preserve type source info. 340 Result = TheSema.GetTypeFromParser(DS.getRepAsType()); 341 assert(!Result.isNull() && "Didn't get a type for typeof?"); 342 if (!Result->isDependentType()) 343 if (const TagType *TT = Result->getAs<TagType>()) 344 TheSema.DiagnoseUseOfDecl(TT->getDecl(), 345 DS.getTypeSpecTypeLoc()); 346 // TypeQuals handled by caller. 347 Result = Context.getTypeOfType(Result); 348 break; 349 case DeclSpec::TST_typeofExpr: { 350 Expr *E = DS.getRepAsExpr(); 351 assert(E && "Didn't get an expression for typeof?"); 352 // TypeQuals handled by caller. 353 Result = TheSema.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc()); 354 if (Result.isNull()) { 355 Result = Context.IntTy; 356 TheDeclarator.setInvalidType(true); 357 } 358 break; 359 } 360 case DeclSpec::TST_decltype: { 361 Expr *E = DS.getRepAsExpr(); 362 assert(E && "Didn't get an expression for decltype?"); 363 // TypeQuals handled by caller. 364 Result = TheSema.BuildDecltypeType(E, DS.getTypeSpecTypeLoc()); 365 if (Result.isNull()) { 366 Result = Context.IntTy; 367 TheDeclarator.setInvalidType(true); 368 } 369 break; 370 } 371 case DeclSpec::TST_auto: { 372 // TypeQuals handled by caller. 373 Result = Context.UndeducedAutoTy; 374 break; 375 } 376 377 case DeclSpec::TST_error: 378 Result = Context.IntTy; 379 TheDeclarator.setInvalidType(true); 380 break; 381 } 382 383 // Handle complex types. 384 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { 385 if (TheSema.getLangOptions().Freestanding) 386 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); 387 Result = Context.getComplexType(Result); 388 } else if (DS.isTypeAltiVecVector()) { 389 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); 390 assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); 391 VectorType::AltiVecSpecific AltiVecSpec = VectorType::AltiVec; 392 if (DS.isTypeAltiVecPixel()) 393 AltiVecSpec = VectorType::Pixel; 394 else if (DS.isTypeAltiVecBool()) 395 AltiVecSpec = VectorType::Bool; 396 Result = Context.getVectorType(Result, 128/typeSize, AltiVecSpec); 397 } 398 399 // FIXME: Imaginary. 400 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) 401 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); 402 403 // See if there are any attributes on the declspec that apply to the type (as 404 // opposed to the decl). 405 if (const AttributeList *AL = DS.getAttributes()) 406 ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed); 407 408 // Apply const/volatile/restrict qualifiers to T. 409 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 410 411 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 412 // or incomplete types shall not be restrict-qualified." C++ also allows 413 // restrict-qualified references. 414 if (TypeQuals & DeclSpec::TQ_restrict) { 415 if (Result->isAnyPointerType() || Result->isReferenceType()) { 416 QualType EltTy; 417 if (Result->isObjCObjectPointerType()) 418 EltTy = Result; 419 else 420 EltTy = Result->isPointerType() ? 421 Result->getAs<PointerType>()->getPointeeType() : 422 Result->getAs<ReferenceType>()->getPointeeType(); 423 424 // If we have a pointer or reference, the pointee must have an object 425 // incomplete type. 426 if (!EltTy->isIncompleteOrObjectType()) { 427 TheSema.Diag(DS.getRestrictSpecLoc(), 428 diag::err_typecheck_invalid_restrict_invalid_pointee) 429 << EltTy << DS.getSourceRange(); 430 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. 431 } 432 } else { 433 TheSema.Diag(DS.getRestrictSpecLoc(), 434 diag::err_typecheck_invalid_restrict_not_pointer) 435 << Result << DS.getSourceRange(); 436 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. 437 } 438 } 439 440 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 441 // of a function type includes any type qualifiers, the behavior is 442 // undefined." 443 if (Result->isFunctionType() && TypeQuals) { 444 // Get some location to point at, either the C or V location. 445 SourceLocation Loc; 446 if (TypeQuals & DeclSpec::TQ_const) 447 Loc = DS.getConstSpecLoc(); 448 else if (TypeQuals & DeclSpec::TQ_volatile) 449 Loc = DS.getVolatileSpecLoc(); 450 else { 451 assert((TypeQuals & DeclSpec::TQ_restrict) && 452 "Has CVR quals but not C, V, or R?"); 453 Loc = DS.getRestrictSpecLoc(); 454 } 455 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers) 456 << Result << DS.getSourceRange(); 457 } 458 459 // C++ [dcl.ref]p1: 460 // Cv-qualified references are ill-formed except when the 461 // cv-qualifiers are introduced through the use of a typedef 462 // (7.1.3) or of a template type argument (14.3), in which 463 // case the cv-qualifiers are ignored. 464 // FIXME: Shouldn't we be checking SCS_typedef here? 465 if (DS.getTypeSpecType() == DeclSpec::TST_typename && 466 TypeQuals && Result->isReferenceType()) { 467 TypeQuals &= ~DeclSpec::TQ_const; 468 TypeQuals &= ~DeclSpec::TQ_volatile; 469 } 470 471 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); 472 Result = Context.getQualifiedType(Result, Quals); 473 } 474 475 return Result; 476} 477 478static std::string getPrintableNameForEntity(DeclarationName Entity) { 479 if (Entity) 480 return Entity.getAsString(); 481 482 return "type name"; 483} 484 485QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, 486 Qualifiers Qs) { 487 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 488 // object or incomplete types shall not be restrict-qualified." 489 if (Qs.hasRestrict()) { 490 unsigned DiagID = 0; 491 QualType ProblemTy; 492 493 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 494 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) { 495 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) { 496 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; 497 ProblemTy = T->getAs<ReferenceType>()->getPointeeType(); 498 } 499 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { 500 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { 501 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; 502 ProblemTy = T->getAs<PointerType>()->getPointeeType(); 503 } 504 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) { 505 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { 506 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; 507 ProblemTy = T->getAs<PointerType>()->getPointeeType(); 508 } 509 } else if (!Ty->isDependentType()) { 510 // FIXME: this deserves a proper diagnostic 511 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; 512 ProblemTy = T; 513 } 514 515 if (DiagID) { 516 Diag(Loc, DiagID) << ProblemTy; 517 Qs.removeRestrict(); 518 } 519 } 520 521 return Context.getQualifiedType(T, Qs); 522} 523 524/// \brief Build a pointer type. 525/// 526/// \param T The type to which we'll be building a pointer. 527/// 528/// \param Loc The location of the entity whose type involves this 529/// pointer type or, if there is no such entity, the location of the 530/// type that will have pointer type. 531/// 532/// \param Entity The name of the entity that involves the pointer 533/// type, if known. 534/// 535/// \returns A suitable pointer type, if there are no 536/// errors. Otherwise, returns a NULL type. 537QualType Sema::BuildPointerType(QualType T, 538 SourceLocation Loc, DeclarationName Entity) { 539 if (T->isReferenceType()) { 540 // C++ 8.3.2p4: There shall be no ... pointers to references ... 541 Diag(Loc, diag::err_illegal_decl_pointer_to_reference) 542 << getPrintableNameForEntity(Entity) << T; 543 return QualType(); 544 } 545 546 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); 547 548 // Build the pointer type. 549 return Context.getPointerType(T); 550} 551 552/// \brief Build a reference type. 553/// 554/// \param T The type to which we'll be building a reference. 555/// 556/// \param Loc The location of the entity whose type involves this 557/// reference type or, if there is no such entity, the location of the 558/// type that will have reference type. 559/// 560/// \param Entity The name of the entity that involves the reference 561/// type, if known. 562/// 563/// \returns A suitable reference type, if there are no 564/// errors. Otherwise, returns a NULL type. 565QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, 566 SourceLocation Loc, 567 DeclarationName Entity) { 568 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); 569 570 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a 571 // reference to a type T, and attempt to create the type "lvalue 572 // reference to cv TD" creates the type "lvalue reference to T". 573 // We use the qualifiers (restrict or none) of the original reference, 574 // not the new ones. This is consistent with GCC. 575 576 // C++ [dcl.ref]p4: There shall be no references to references. 577 // 578 // According to C++ DR 106, references to references are only 579 // diagnosed when they are written directly (e.g., "int & &"), 580 // but not when they happen via a typedef: 581 // 582 // typedef int& intref; 583 // typedef intref& intref2; 584 // 585 // Parser::ParseDeclaratorInternal diagnoses the case where 586 // references are written directly; here, we handle the 587 // collapsing of references-to-references as described in C++ 588 // DR 106 and amended by C++ DR 540. 589 590 // C++ [dcl.ref]p1: 591 // A declarator that specifies the type "reference to cv void" 592 // is ill-formed. 593 if (T->isVoidType()) { 594 Diag(Loc, diag::err_reference_to_void); 595 return QualType(); 596 } 597 598 // Handle restrict on references. 599 if (LValueRef) 600 return Context.getLValueReferenceType(T, SpelledAsLValue); 601 return Context.getRValueReferenceType(T); 602} 603 604/// \brief Build an array type. 605/// 606/// \param T The type of each element in the array. 607/// 608/// \param ASM C99 array size modifier (e.g., '*', 'static'). 609/// 610/// \param ArraySize Expression describing the size of the array. 611/// 612/// \param Loc The location of the entity whose type involves this 613/// array type or, if there is no such entity, the location of the 614/// type that will have array type. 615/// 616/// \param Entity The name of the entity that involves the array 617/// type, if known. 618/// 619/// \returns A suitable array type, if there are no errors. Otherwise, 620/// returns a NULL type. 621QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, 622 Expr *ArraySize, unsigned Quals, 623 SourceRange Brackets, DeclarationName Entity) { 624 625 SourceLocation Loc = Brackets.getBegin(); 626 if (getLangOptions().CPlusPlus) { 627 // C++ [dcl.array]p1: 628 // T is called the array element type; this type shall not be a reference 629 // type, the (possibly cv-qualified) type void, a function type or an 630 // abstract class type. 631 // 632 // Note: function types are handled in the common path with C. 633 if (T->isReferenceType()) { 634 Diag(Loc, diag::err_illegal_decl_array_of_references) 635 << getPrintableNameForEntity(Entity) << T; 636 return QualType(); 637 } 638 639 if (T->isVoidType()) { 640 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; 641 return QualType(); 642 } 643 644 if (RequireNonAbstractType(Brackets.getBegin(), T, 645 diag::err_array_of_abstract_type)) 646 return QualType(); 647 648 } else { 649 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 650 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 651 if (RequireCompleteType(Loc, T, 652 diag::err_illegal_decl_array_incomplete_type)) 653 return QualType(); 654 } 655 656 if (T->isFunctionType()) { 657 Diag(Loc, diag::err_illegal_decl_array_of_functions) 658 << getPrintableNameForEntity(Entity) << T; 659 return QualType(); 660 } 661 662 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { 663 Diag(Loc, diag::err_illegal_decl_array_of_auto) 664 << getPrintableNameForEntity(Entity); 665 return QualType(); 666 } 667 668 if (const RecordType *EltTy = T->getAs<RecordType>()) { 669 // If the element type is a struct or union that contains a variadic 670 // array, accept it as a GNU extension: C99 6.7.2.1p2. 671 if (EltTy->getDecl()->hasFlexibleArrayMember()) 672 Diag(Loc, diag::ext_flexible_array_in_array) << T; 673 } else if (T->isObjCObjectType()) { 674 Diag(Loc, diag::err_objc_array_of_interfaces) << T; 675 return QualType(); 676 } 677 678 // C99 6.7.5.2p1: The size expression shall have integer type. 679 if (ArraySize && !ArraySize->isTypeDependent() && 680 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { 681 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) 682 << ArraySize->getType() << ArraySize->getSourceRange(); 683 return QualType(); 684 } 685 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); 686 if (!ArraySize) { 687 if (ASM == ArrayType::Star) 688 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); 689 else 690 T = Context.getIncompleteArrayType(T, ASM, Quals); 691 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { 692 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); 693 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 694 (!T->isDependentType() && !T->isIncompleteType() && 695 !T->isConstantSizeType())) { 696 // Per C99, a variable array is an array with either a non-constant 697 // size or an element type that has a non-constant-size 698 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); 699 } else { 700 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 701 // have a value greater than zero. 702 if (ConstVal.isSigned() && ConstVal.isNegative()) { 703 Diag(ArraySize->getLocStart(), 704 diag::err_typecheck_negative_array_size) 705 << ArraySize->getSourceRange(); 706 return QualType(); 707 } 708 if (ConstVal == 0) { 709 // GCC accepts zero sized static arrays. We allow them when 710 // we're not in a SFINAE context. 711 Diag(ArraySize->getLocStart(), 712 isSFINAEContext()? diag::err_typecheck_zero_array_size 713 : diag::ext_typecheck_zero_array_size) 714 << ArraySize->getSourceRange(); 715 } else if (!T->isDependentType() && !T->isVariablyModifiedType() && 716 !T->isIncompleteType()) { 717 // Is the array too large? 718 unsigned ActiveSizeBits 719 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal); 720 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) 721 Diag(ArraySize->getLocStart(), diag::err_array_too_large) 722 << ConstVal.toString(10) 723 << ArraySize->getSourceRange(); 724 } 725 726 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); 727 } 728 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 729 if (!getLangOptions().C99) { 730 if (T->isVariableArrayType()) { 731 // Prohibit the use of non-POD types in VLAs. 732 if (!T->isDependentType() && 733 !Context.getBaseElementType(T)->isPODType()) { 734 Diag(Loc, diag::err_vla_non_pod) 735 << Context.getBaseElementType(T); 736 return QualType(); 737 } 738 // Prohibit the use of VLAs during template argument deduction. 739 else if (isSFINAEContext()) { 740 Diag(Loc, diag::err_vla_in_sfinae); 741 return QualType(); 742 } 743 // Just extwarn about VLAs. 744 else 745 Diag(Loc, diag::ext_vla); 746 } else if (ASM != ArrayType::Normal || Quals != 0) 747 Diag(Loc, 748 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx 749 : diag::ext_c99_array_usage); 750 } 751 752 return T; 753} 754 755/// \brief Build an ext-vector type. 756/// 757/// Run the required checks for the extended vector type. 758QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, 759 SourceLocation AttrLoc) { 760 // unlike gcc's vector_size attribute, we do not allow vectors to be defined 761 // in conjunction with complex types (pointers, arrays, functions, etc.). 762 if (!T->isDependentType() && 763 !T->isIntegerType() && !T->isRealFloatingType()) { 764 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; 765 return QualType(); 766 } 767 768 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { 769 llvm::APSInt vecSize(32); 770 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { 771 Diag(AttrLoc, diag::err_attribute_argument_not_int) 772 << "ext_vector_type" << ArraySize->getSourceRange(); 773 return QualType(); 774 } 775 776 // unlike gcc's vector_size attribute, the size is specified as the 777 // number of elements, not the number of bytes. 778 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); 779 780 if (vectorSize == 0) { 781 Diag(AttrLoc, diag::err_attribute_zero_size) 782 << ArraySize->getSourceRange(); 783 return QualType(); 784 } 785 786 if (!T->isDependentType()) 787 return Context.getExtVectorType(T, vectorSize); 788 } 789 790 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); 791} 792 793/// \brief Build a function type. 794/// 795/// This routine checks the function type according to C++ rules and 796/// under the assumption that the result type and parameter types have 797/// just been instantiated from a template. It therefore duplicates 798/// some of the behavior of GetTypeForDeclarator, but in a much 799/// simpler form that is only suitable for this narrow use case. 800/// 801/// \param T The return type of the function. 802/// 803/// \param ParamTypes The parameter types of the function. This array 804/// will be modified to account for adjustments to the types of the 805/// function parameters. 806/// 807/// \param NumParamTypes The number of parameter types in ParamTypes. 808/// 809/// \param Variadic Whether this is a variadic function type. 810/// 811/// \param Quals The cvr-qualifiers to be applied to the function type. 812/// 813/// \param Loc The location of the entity whose type involves this 814/// function type or, if there is no such entity, the location of the 815/// type that will have function type. 816/// 817/// \param Entity The name of the entity that involves the function 818/// type, if known. 819/// 820/// \returns A suitable function type, if there are no 821/// errors. Otherwise, returns a NULL type. 822QualType Sema::BuildFunctionType(QualType T, 823 QualType *ParamTypes, 824 unsigned NumParamTypes, 825 bool Variadic, unsigned Quals, 826 SourceLocation Loc, DeclarationName Entity, 827 const FunctionType::ExtInfo &Info) { 828 if (T->isArrayType() || T->isFunctionType()) { 829 Diag(Loc, diag::err_func_returning_array_function) 830 << T->isFunctionType() << T; 831 return QualType(); 832 } 833 834 bool Invalid = false; 835 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { 836 QualType ParamType = adjustParameterType(ParamTypes[Idx]); 837 if (ParamType->isVoidType()) { 838 Diag(Loc, diag::err_param_with_void_type); 839 Invalid = true; 840 } 841 842 ParamTypes[Idx] = ParamType; 843 } 844 845 if (Invalid) 846 return QualType(); 847 848 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, 849 Quals, false, false, 0, 0, Info); 850} 851 852/// \brief Build a member pointer type \c T Class::*. 853/// 854/// \param T the type to which the member pointer refers. 855/// \param Class the class type into which the member pointer points. 856/// \param CVR Qualifiers applied to the member pointer type 857/// \param Loc the location where this type begins 858/// \param Entity the name of the entity that will have this member pointer type 859/// 860/// \returns a member pointer type, if successful, or a NULL type if there was 861/// an error. 862QualType Sema::BuildMemberPointerType(QualType T, QualType Class, 863 SourceLocation Loc, 864 DeclarationName Entity) { 865 // Verify that we're not building a pointer to pointer to function with 866 // exception specification. 867 if (CheckDistantExceptionSpec(T)) { 868 Diag(Loc, diag::err_distant_exception_spec); 869 870 // FIXME: If we're doing this as part of template instantiation, 871 // we should return immediately. 872 873 // Build the type anyway, but use the canonical type so that the 874 // exception specifiers are stripped off. 875 T = Context.getCanonicalType(T); 876 } 877 878 // C++ 8.3.3p3: A pointer to member shall not point to ... a member 879 // with reference type, or "cv void." 880 if (T->isReferenceType()) { 881 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) 882 << (Entity? Entity.getAsString() : "type name") << T; 883 return QualType(); 884 } 885 886 if (T->isVoidType()) { 887 Diag(Loc, diag::err_illegal_decl_mempointer_to_void) 888 << (Entity? Entity.getAsString() : "type name"); 889 return QualType(); 890 } 891 892 if (!Class->isDependentType() && !Class->isRecordType()) { 893 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; 894 return QualType(); 895 } 896 897 // In the Microsoft ABI, the class is allowed to be an incomplete 898 // type. In such cases, the compiler makes a worst-case assumption. 899 // We make no such assumption right now, so emit an error if the 900 // class isn't a complete type. 901 if (Context.Target.getCXXABI() == CXXABI_Microsoft && 902 RequireCompleteType(Loc, Class, diag::err_incomplete_type)) 903 return QualType(); 904 905 return Context.getMemberPointerType(T, Class.getTypePtr()); 906} 907 908/// \brief Build a block pointer type. 909/// 910/// \param T The type to which we'll be building a block pointer. 911/// 912/// \param CVR The cvr-qualifiers to be applied to the block pointer type. 913/// 914/// \param Loc The location of the entity whose type involves this 915/// block pointer type or, if there is no such entity, the location of the 916/// type that will have block pointer type. 917/// 918/// \param Entity The name of the entity that involves the block pointer 919/// type, if known. 920/// 921/// \returns A suitable block pointer type, if there are no 922/// errors. Otherwise, returns a NULL type. 923QualType Sema::BuildBlockPointerType(QualType T, 924 SourceLocation Loc, 925 DeclarationName Entity) { 926 if (!T->isFunctionType()) { 927 Diag(Loc, diag::err_nonfunction_block_type); 928 return QualType(); 929 } 930 931 return Context.getBlockPointerType(T); 932} 933 934QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { 935 QualType QT = Ty.get(); 936 if (QT.isNull()) { 937 if (TInfo) *TInfo = 0; 938 return QualType(); 939 } 940 941 TypeSourceInfo *DI = 0; 942 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { 943 QT = LIT->getType(); 944 DI = LIT->getTypeSourceInfo(); 945 } 946 947 if (TInfo) *TInfo = DI; 948 return QT; 949} 950 951/// GetTypeForDeclarator - Convert the type for the specified 952/// declarator to Type instances. 953/// 954/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq 955/// owns the declaration of a type (e.g., the definition of a struct 956/// type), then *OwnedDecl will receive the owned declaration. 957/// 958/// The result of this call will never be null, but the associated 959/// type may be a null type if there's an unrecoverable error. 960TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S, 961 TagDecl **OwnedDecl) { 962 // Determine the type of the declarator. Not all forms of declarator 963 // have a type. 964 QualType T; 965 TypeSourceInfo *ReturnTypeInfo = 0; 966 967 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec; 968 969 switch (D.getName().getKind()) { 970 case UnqualifiedId::IK_Identifier: 971 case UnqualifiedId::IK_OperatorFunctionId: 972 case UnqualifiedId::IK_LiteralOperatorId: 973 case UnqualifiedId::IK_TemplateId: 974 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec); 975 976 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { 977 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); 978 // Owned is embedded if it was defined here, or if it is the 979 // very first (i.e., canonical) declaration of this tag type. 980 Owned->setEmbeddedInDeclarator(Owned->isDefinition() || 981 Owned->isCanonicalDecl()); 982 if (OwnedDecl) *OwnedDecl = Owned; 983 } 984 break; 985 986 case UnqualifiedId::IK_ConstructorName: 987 case UnqualifiedId::IK_ConstructorTemplateId: 988 case UnqualifiedId::IK_DestructorName: 989 // Constructors and destructors don't have return types. Use 990 // "void" instead. 991 T = Context.VoidTy; 992 break; 993 994 case UnqualifiedId::IK_ConversionFunctionId: 995 // The result type of a conversion function is the type that it 996 // converts to. 997 T = GetTypeFromParser(D.getName().ConversionFunctionId, 998 &ReturnTypeInfo); 999 break; 1000 } 1001 1002 // Check for auto functions and trailing return type and adjust the 1003 // return type accordingly. 1004 if (getLangOptions().CPlusPlus0x && D.isFunctionDeclarator()) { 1005 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; 1006 if (T == Context.UndeducedAutoTy) { 1007 if (FTI.TrailingReturnType) { 1008 T = GetTypeFromParser(ParsedType::getFromOpaquePtr(FTI.TrailingReturnType), 1009 &ReturnTypeInfo); 1010 } 1011 else { 1012 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), 1013 diag::err_auto_missing_trailing_return); 1014 T = Context.IntTy; 1015 D.setInvalidType(true); 1016 } 1017 } 1018 else if (FTI.TrailingReturnType) { 1019 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), 1020 diag::err_trailing_return_without_auto); 1021 D.setInvalidType(true); 1022 } 1023 } 1024 1025 if (T.isNull()) 1026 return Context.getNullTypeSourceInfo(); 1027 1028 if (T == Context.UndeducedAutoTy) { 1029 int Error = -1; 1030 1031 switch (D.getContext()) { 1032 case Declarator::KNRTypeListContext: 1033 assert(0 && "K&R type lists aren't allowed in C++"); 1034 break; 1035 case Declarator::PrototypeContext: 1036 Error = 0; // Function prototype 1037 break; 1038 case Declarator::MemberContext: 1039 switch (cast<TagDecl>(CurContext)->getTagKind()) { 1040 case TTK_Enum: assert(0 && "unhandled tag kind"); break; 1041 case TTK_Struct: Error = 1; /* Struct member */ break; 1042 case TTK_Union: Error = 2; /* Union member */ break; 1043 case TTK_Class: Error = 3; /* Class member */ break; 1044 } 1045 break; 1046 case Declarator::CXXCatchContext: 1047 Error = 4; // Exception declaration 1048 break; 1049 case Declarator::TemplateParamContext: 1050 Error = 5; // Template parameter 1051 break; 1052 case Declarator::BlockLiteralContext: 1053 Error = 6; // Block literal 1054 break; 1055 case Declarator::FileContext: 1056 case Declarator::BlockContext: 1057 case Declarator::ForContext: 1058 case Declarator::ConditionContext: 1059 case Declarator::TypeNameContext: 1060 break; 1061 } 1062 1063 if (Error != -1) { 1064 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) 1065 << Error; 1066 T = Context.IntTy; 1067 D.setInvalidType(true); 1068 } 1069 } 1070 1071 // The name we're declaring, if any. 1072 DeclarationName Name; 1073 if (D.getIdentifier()) 1074 Name = D.getIdentifier(); 1075 1076 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk; 1077 1078 // Walk the DeclTypeInfo, building the recursive type as we go. 1079 // DeclTypeInfos are ordered from the identifier out, which is 1080 // opposite of what we want :). 1081 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 1082 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); 1083 switch (DeclType.Kind) { 1084 default: assert(0 && "Unknown decltype!"); 1085 case DeclaratorChunk::BlockPointer: 1086 // If blocks are disabled, emit an error. 1087 if (!LangOpts.Blocks) 1088 Diag(DeclType.Loc, diag::err_blocks_disable); 1089 1090 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name); 1091 if (DeclType.Cls.TypeQuals) 1092 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); 1093 break; 1094 case DeclaratorChunk::Pointer: 1095 // Verify that we're not building a pointer to pointer to function with 1096 // exception specification. 1097 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 1098 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 1099 D.setInvalidType(true); 1100 // Build the type anyway. 1101 } 1102 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) { 1103 T = Context.getObjCObjectPointerType(T); 1104 if (DeclType.Ptr.TypeQuals) 1105 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); 1106 break; 1107 } 1108 T = BuildPointerType(T, DeclType.Loc, Name); 1109 if (DeclType.Ptr.TypeQuals) 1110 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); 1111 break; 1112 case DeclaratorChunk::Reference: { 1113 // Verify that we're not building a reference to pointer to function with 1114 // exception specification. 1115 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 1116 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 1117 D.setInvalidType(true); 1118 // Build the type anyway. 1119 } 1120 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); 1121 1122 Qualifiers Quals; 1123 if (DeclType.Ref.HasRestrict) 1124 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); 1125 break; 1126 } 1127 case DeclaratorChunk::Array: { 1128 // Verify that we're not building an array of pointers to function with 1129 // exception specification. 1130 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 1131 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 1132 D.setInvalidType(true); 1133 // Build the type anyway. 1134 } 1135 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 1136 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 1137 ArrayType::ArraySizeModifier ASM; 1138 if (ATI.isStar) 1139 ASM = ArrayType::Star; 1140 else if (ATI.hasStatic) 1141 ASM = ArrayType::Static; 1142 else 1143 ASM = ArrayType::Normal; 1144 if (ASM == ArrayType::Star && 1145 D.getContext() != Declarator::PrototypeContext) { 1146 // FIXME: This check isn't quite right: it allows star in prototypes 1147 // for function definitions, and disallows some edge cases detailed 1148 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html 1149 Diag(DeclType.Loc, diag::err_array_star_outside_prototype); 1150 ASM = ArrayType::Normal; 1151 D.setInvalidType(true); 1152 } 1153 T = BuildArrayType(T, ASM, ArraySize, 1154 Qualifiers::fromCVRMask(ATI.TypeQuals), 1155 SourceRange(DeclType.Loc, DeclType.EndLoc), Name); 1156 break; 1157 } 1158 case DeclaratorChunk::Function: { 1159 // If the function declarator has a prototype (i.e. it is not () and 1160 // does not have a K&R-style identifier list), then the arguments are part 1161 // of the type, otherwise the argument list is (). 1162 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 1163 1164 // C99 6.7.5.3p1: The return type may not be a function or array type. 1165 // For conversion functions, we'll diagnose this particular error later. 1166 if ((T->isArrayType() || T->isFunctionType()) && 1167 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { 1168 Diag(DeclType.Loc, diag::err_func_returning_array_function) 1169 << T->isFunctionType() << T; 1170 T = Context.IntTy; 1171 D.setInvalidType(true); 1172 } 1173 1174 // cv-qualifiers on return types are pointless except when the type is a 1175 // class type in C++. 1176 if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && 1177 (!getLangOptions().CPlusPlus || 1178 (!T->isDependentType() && !T->isRecordType()))) { 1179 unsigned Quals = D.getDeclSpec().getTypeQualifiers(); 1180 std::string QualStr; 1181 unsigned NumQuals = 0; 1182 SourceLocation Loc; 1183 if (Quals & Qualifiers::Const) { 1184 Loc = D.getDeclSpec().getConstSpecLoc(); 1185 ++NumQuals; 1186 QualStr = "const"; 1187 } 1188 if (Quals & Qualifiers::Volatile) { 1189 if (NumQuals == 0) { 1190 Loc = D.getDeclSpec().getVolatileSpecLoc(); 1191 QualStr = "volatile"; 1192 } else 1193 QualStr += " volatile"; 1194 ++NumQuals; 1195 } 1196 if (Quals & Qualifiers::Restrict) { 1197 if (NumQuals == 0) { 1198 Loc = D.getDeclSpec().getRestrictSpecLoc(); 1199 QualStr = "restrict"; 1200 } else 1201 QualStr += " restrict"; 1202 ++NumQuals; 1203 } 1204 assert(NumQuals > 0 && "No known qualifiers?"); 1205 1206 SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type); 1207 DB << QualStr << NumQuals; 1208 if (Quals & Qualifiers::Const) 1209 DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc()); 1210 if (Quals & Qualifiers::Volatile) 1211 DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc()); 1212 if (Quals & Qualifiers::Restrict) 1213 DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc()); 1214 } 1215 1216 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { 1217 // C++ [dcl.fct]p6: 1218 // Types shall not be defined in return or parameter types. 1219 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); 1220 if (Tag->isDefinition()) 1221 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) 1222 << Context.getTypeDeclType(Tag); 1223 } 1224 1225 // Exception specs are not allowed in typedefs. Complain, but add it 1226 // anyway. 1227 if (FTI.hasExceptionSpec && 1228 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 1229 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); 1230 1231 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) { 1232 // Simple void foo(), where the incoming T is the result type. 1233 T = Context.getFunctionNoProtoType(T); 1234 } else { 1235 // We allow a zero-parameter variadic function in C if the 1236 // function is marked with the "overloadable" attribute. Scan 1237 // for this attribute now. 1238 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) { 1239 bool Overloadable = false; 1240 for (const AttributeList *Attrs = D.getAttributes(); 1241 Attrs; Attrs = Attrs->getNext()) { 1242 if (Attrs->getKind() == AttributeList::AT_overloadable) { 1243 Overloadable = true; 1244 break; 1245 } 1246 } 1247 1248 if (!Overloadable) 1249 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); 1250 } 1251 1252 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) { 1253 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function 1254 // definition. 1255 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 1256 D.setInvalidType(true); 1257 break; 1258 } 1259 1260 // Otherwise, we have a function with an argument list that is 1261 // potentially variadic. 1262 llvm::SmallVector<QualType, 16> ArgTys; 1263 ArgTys.reserve(FTI.NumArgs); 1264 1265 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 1266 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); 1267 QualType ArgTy = Param->getType(); 1268 assert(!ArgTy.isNull() && "Couldn't parse type?"); 1269 1270 // Adjust the parameter type. 1271 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); 1272 1273 // Look for 'void'. void is allowed only as a single argument to a 1274 // function with no other parameters (C99 6.7.5.3p10). We record 1275 // int(void) as a FunctionProtoType with an empty argument list. 1276 if (ArgTy->isVoidType()) { 1277 // If this is something like 'float(int, void)', reject it. 'void' 1278 // is an incomplete type (C99 6.2.5p19) and function decls cannot 1279 // have arguments of incomplete type. 1280 if (FTI.NumArgs != 1 || FTI.isVariadic) { 1281 Diag(DeclType.Loc, diag::err_void_only_param); 1282 ArgTy = Context.IntTy; 1283 Param->setType(ArgTy); 1284 } else if (FTI.ArgInfo[i].Ident) { 1285 // Reject, but continue to parse 'int(void abc)'. 1286 Diag(FTI.ArgInfo[i].IdentLoc, 1287 diag::err_param_with_void_type); 1288 ArgTy = Context.IntTy; 1289 Param->setType(ArgTy); 1290 } else { 1291 // Reject, but continue to parse 'float(const void)'. 1292 if (ArgTy.hasQualifiers()) 1293 Diag(DeclType.Loc, diag::err_void_param_qualified); 1294 1295 // Do not add 'void' to the ArgTys list. 1296 break; 1297 } 1298 } else if (!FTI.hasPrototype) { 1299 if (ArgTy->isPromotableIntegerType()) { 1300 ArgTy = Context.getPromotedIntegerType(ArgTy); 1301 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { 1302 if (BTy->getKind() == BuiltinType::Float) 1303 ArgTy = Context.DoubleTy; 1304 } 1305 } 1306 1307 ArgTys.push_back(ArgTy); 1308 } 1309 1310 llvm::SmallVector<QualType, 4> Exceptions; 1311 Exceptions.reserve(FTI.NumExceptions); 1312 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { 1313 // FIXME: Preserve type source info. 1314 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); 1315 // Check that the type is valid for an exception spec, and drop it if 1316 // not. 1317 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) 1318 Exceptions.push_back(ET); 1319 } 1320 1321 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), 1322 FTI.isVariadic, FTI.TypeQuals, 1323 FTI.hasExceptionSpec, 1324 FTI.hasAnyExceptionSpec, 1325 Exceptions.size(), Exceptions.data(), 1326 FunctionType::ExtInfo()); 1327 } 1328 1329 // For GCC compatibility, we allow attributes that apply only to 1330 // function types to be placed on a function's return type 1331 // instead (as long as that type doesn't happen to be function 1332 // or function-pointer itself). 1333 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk); 1334 1335 break; 1336 } 1337 case DeclaratorChunk::MemberPointer: 1338 // The scope spec must refer to a class, or be dependent. 1339 CXXScopeSpec &SS = DeclType.Mem.Scope(); 1340 QualType ClsType; 1341 if (SS.isInvalid()) { 1342 // Avoid emitting extra errors if we already errored on the scope. 1343 D.setInvalidType(true); 1344 } else if (isDependentScopeSpecifier(SS) || 1345 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) { 1346 NestedNameSpecifier *NNS 1347 = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 1348 NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); 1349 switch (NNS->getKind()) { 1350 case NestedNameSpecifier::Identifier: 1351 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, 1352 NNS->getAsIdentifier()); 1353 break; 1354 1355 case NestedNameSpecifier::Namespace: 1356 case NestedNameSpecifier::Global: 1357 llvm_unreachable("Nested-name-specifier must name a type"); 1358 break; 1359 1360 case NestedNameSpecifier::TypeSpec: 1361 case NestedNameSpecifier::TypeSpecWithTemplate: 1362 ClsType = QualType(NNS->getAsType(), 0); 1363 // Note: if NNS is dependent, then its prefix (if any) is already 1364 // included in ClsType; this does not hold if the NNS is 1365 // nondependent: in this case (if there is indeed a prefix) 1366 // ClsType needs to be wrapped into an elaborated type. 1367 if (NNSPrefix && !NNS->isDependent()) 1368 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType); 1369 break; 1370 } 1371 } else { 1372 Diag(DeclType.Mem.Scope().getBeginLoc(), 1373 diag::err_illegal_decl_mempointer_in_nonclass) 1374 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") 1375 << DeclType.Mem.Scope().getRange(); 1376 D.setInvalidType(true); 1377 } 1378 1379 if (!ClsType.isNull()) 1380 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier()); 1381 if (T.isNull()) { 1382 T = Context.IntTy; 1383 D.setInvalidType(true); 1384 } else if (DeclType.Mem.TypeQuals) { 1385 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); 1386 } 1387 break; 1388 } 1389 1390 if (T.isNull()) { 1391 D.setInvalidType(true); 1392 T = Context.IntTy; 1393 } 1394 1395 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); 1396 1397 // See if there are any attributes on this declarator chunk. 1398 if (const AttributeList *AL = DeclType.getAttrs()) 1399 ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk); 1400 } 1401 1402 if (getLangOptions().CPlusPlus && T->isFunctionType()) { 1403 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); 1404 assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); 1405 1406 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type 1407 // for a nonstatic member function, the function type to which a pointer 1408 // to member refers, or the top-level function type of a function typedef 1409 // declaration. 1410 bool FreeFunction = (D.getContext() != Declarator::MemberContext && 1411 (!D.getCXXScopeSpec().isSet() || 1412 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)->isRecord())); 1413 if (FnTy->getTypeQuals() != 0 && 1414 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && 1415 (FreeFunction || 1416 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { 1417 if (D.isFunctionDeclarator()) 1418 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); 1419 else 1420 Diag(D.getIdentifierLoc(), 1421 diag::err_invalid_qualified_typedef_function_type_use) 1422 << FreeFunction; 1423 1424 // Strip the cv-quals from the type. 1425 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), 1426 FnTy->getNumArgs(), FnTy->isVariadic(), 0, 1427 false, false, 0, 0, FunctionType::ExtInfo()); 1428 } 1429 } 1430 1431 // If there's a constexpr specifier, treat it as a top-level const. 1432 if (D.getDeclSpec().isConstexprSpecified()) { 1433 T.addConst(); 1434 } 1435 1436 // Process any function attributes we might have delayed from the 1437 // declaration-specifiers. 1438 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec); 1439 1440 // If there were any type attributes applied to the decl itself, not 1441 // the type, apply them to the result type. But don't do this for 1442 // block-literal expressions, which are parsed wierdly. 1443 if (D.getContext() != Declarator::BlockLiteralContext) 1444 if (const AttributeList *Attrs = D.getAttributes()) 1445 ProcessTypeAttributeList(*this, T, false, Attrs, 1446 FnAttrsFromPreviousChunk); 1447 1448 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); 1449 1450 if (T.isNull()) 1451 return Context.getNullTypeSourceInfo(); 1452 else if (D.isInvalidType()) 1453 return Context.getTrivialTypeSourceInfo(T); 1454 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo); 1455} 1456 1457namespace { 1458 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { 1459 const DeclSpec &DS; 1460 1461 public: 1462 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {} 1463 1464 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1465 Visit(TL.getUnqualifiedLoc()); 1466 } 1467 void VisitTypedefTypeLoc(TypedefTypeLoc TL) { 1468 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1469 } 1470 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 1471 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1472 } 1473 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 1474 // Handle the base type, which might not have been written explicitly. 1475 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 1476 TL.setHasBaseTypeAsWritten(false); 1477 TL.getBaseLoc().initialize(SourceLocation()); 1478 } else { 1479 TL.setHasBaseTypeAsWritten(true); 1480 Visit(TL.getBaseLoc()); 1481 } 1482 1483 // Protocol qualifiers. 1484 if (DS.getProtocolQualifiers()) { 1485 assert(TL.getNumProtocols() > 0); 1486 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); 1487 TL.setLAngleLoc(DS.getProtocolLAngleLoc()); 1488 TL.setRAngleLoc(DS.getSourceRange().getEnd()); 1489 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) 1490 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); 1491 } else { 1492 assert(TL.getNumProtocols() == 0); 1493 TL.setLAngleLoc(SourceLocation()); 1494 TL.setRAngleLoc(SourceLocation()); 1495 } 1496 } 1497 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1498 TL.setStarLoc(SourceLocation()); 1499 Visit(TL.getPointeeLoc()); 1500 } 1501 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { 1502 TypeSourceInfo *TInfo = 0; 1503 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); 1504 1505 // If we got no declarator info from previous Sema routines, 1506 // just fill with the typespec loc. 1507 if (!TInfo) { 1508 TL.initialize(DS.getTypeSpecTypeLoc()); 1509 return; 1510 } 1511 1512 TypeLoc OldTL = TInfo->getTypeLoc(); 1513 if (TInfo->getType()->getAs<ElaboratedType>()) { 1514 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL); 1515 TemplateSpecializationTypeLoc NamedTL = 1516 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc()); 1517 TL.copy(NamedTL); 1518 } 1519 else 1520 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL)); 1521 } 1522 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 1523 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); 1524 TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); 1525 TL.setParensRange(DS.getTypeofParensRange()); 1526 } 1527 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 1528 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); 1529 TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); 1530 TL.setParensRange(DS.getTypeofParensRange()); 1531 assert(DS.getRepAsType()); 1532 TypeSourceInfo *TInfo = 0; 1533 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); 1534 TL.setUnderlyingTInfo(TInfo); 1535 } 1536 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 1537 // By default, use the source location of the type specifier. 1538 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); 1539 if (TL.needsExtraLocalData()) { 1540 // Set info for the written builtin specifiers. 1541 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); 1542 // Try to have a meaningful source location. 1543 if (TL.getWrittenSignSpec() != TSS_unspecified) 1544 // Sign spec loc overrides the others (e.g., 'unsigned long'). 1545 TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); 1546 else if (TL.getWrittenWidthSpec() != TSW_unspecified) 1547 // Width spec loc overrides type spec loc (e.g., 'short int'). 1548 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); 1549 } 1550 } 1551 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 1552 ElaboratedTypeKeyword Keyword 1553 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); 1554 if (Keyword == ETK_Typename) { 1555 TypeSourceInfo *TInfo = 0; 1556 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); 1557 if (TInfo) { 1558 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc())); 1559 return; 1560 } 1561 } 1562 TL.setKeywordLoc(Keyword != ETK_None 1563 ? DS.getTypeSpecTypeLoc() 1564 : SourceLocation()); 1565 const CXXScopeSpec& SS = DS.getTypeSpecScope(); 1566 TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange()); 1567 Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); 1568 } 1569 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 1570 ElaboratedTypeKeyword Keyword 1571 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); 1572 if (Keyword == ETK_Typename) { 1573 TypeSourceInfo *TInfo = 0; 1574 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); 1575 if (TInfo) { 1576 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc())); 1577 return; 1578 } 1579 } 1580 TL.setKeywordLoc(Keyword != ETK_None 1581 ? DS.getTypeSpecTypeLoc() 1582 : SourceLocation()); 1583 const CXXScopeSpec& SS = DS.getTypeSpecScope(); 1584 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange()); 1585 // FIXME: load appropriate source location. 1586 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1587 } 1588 void VisitDependentTemplateSpecializationTypeLoc( 1589 DependentTemplateSpecializationTypeLoc TL) { 1590 ElaboratedTypeKeyword Keyword 1591 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); 1592 if (Keyword == ETK_Typename) { 1593 TypeSourceInfo *TInfo = 0; 1594 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); 1595 if (TInfo) { 1596 TL.copy(cast<DependentTemplateSpecializationTypeLoc>( 1597 TInfo->getTypeLoc())); 1598 return; 1599 } 1600 } 1601 TL.initializeLocal(SourceLocation()); 1602 TL.setKeywordLoc(Keyword != ETK_None 1603 ? DS.getTypeSpecTypeLoc() 1604 : SourceLocation()); 1605 const CXXScopeSpec& SS = DS.getTypeSpecScope(); 1606 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange()); 1607 // FIXME: load appropriate source location. 1608 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1609 } 1610 1611 void VisitTypeLoc(TypeLoc TL) { 1612 // FIXME: add other typespec types and change this to an assert. 1613 TL.initialize(DS.getTypeSpecTypeLoc()); 1614 } 1615 }; 1616 1617 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { 1618 const DeclaratorChunk &Chunk; 1619 1620 public: 1621 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {} 1622 1623 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1624 llvm_unreachable("qualified type locs not expected here!"); 1625 } 1626 1627 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 1628 assert(Chunk.Kind == DeclaratorChunk::BlockPointer); 1629 TL.setCaretLoc(Chunk.Loc); 1630 } 1631 void VisitPointerTypeLoc(PointerTypeLoc TL) { 1632 assert(Chunk.Kind == DeclaratorChunk::Pointer); 1633 TL.setStarLoc(Chunk.Loc); 1634 } 1635 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1636 assert(Chunk.Kind == DeclaratorChunk::Pointer); 1637 TL.setStarLoc(Chunk.Loc); 1638 } 1639 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 1640 assert(Chunk.Kind == DeclaratorChunk::MemberPointer); 1641 TL.setStarLoc(Chunk.Loc); 1642 // FIXME: nested name specifier 1643 } 1644 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 1645 assert(Chunk.Kind == DeclaratorChunk::Reference); 1646 // 'Amp' is misleading: this might have been originally 1647 /// spelled with AmpAmp. 1648 TL.setAmpLoc(Chunk.Loc); 1649 } 1650 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 1651 assert(Chunk.Kind == DeclaratorChunk::Reference); 1652 assert(!Chunk.Ref.LValueRef); 1653 TL.setAmpAmpLoc(Chunk.Loc); 1654 } 1655 void VisitArrayTypeLoc(ArrayTypeLoc TL) { 1656 assert(Chunk.Kind == DeclaratorChunk::Array); 1657 TL.setLBracketLoc(Chunk.Loc); 1658 TL.setRBracketLoc(Chunk.EndLoc); 1659 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); 1660 } 1661 void VisitFunctionTypeLoc(FunctionTypeLoc TL) { 1662 assert(Chunk.Kind == DeclaratorChunk::Function); 1663 TL.setLParenLoc(Chunk.Loc); 1664 TL.setRParenLoc(Chunk.EndLoc); 1665 TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType); 1666 1667 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; 1668 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { 1669 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); 1670 TL.setArg(tpi++, Param); 1671 } 1672 // FIXME: exception specs 1673 } 1674 1675 void VisitTypeLoc(TypeLoc TL) { 1676 llvm_unreachable("unsupported TypeLoc kind in declarator!"); 1677 } 1678 }; 1679} 1680 1681/// \brief Create and instantiate a TypeSourceInfo with type source information. 1682/// 1683/// \param T QualType referring to the type as written in source code. 1684/// 1685/// \param ReturnTypeInfo For declarators whose return type does not show 1686/// up in the normal place in the declaration specifiers (such as a C++ 1687/// conversion function), this pointer will refer to a type source information 1688/// for that return type. 1689TypeSourceInfo * 1690Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, 1691 TypeSourceInfo *ReturnTypeInfo) { 1692 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); 1693 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); 1694 1695 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 1696 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL); 1697 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); 1698 } 1699 1700 // If we have different source information for the return type, use 1701 // that. This really only applies to C++ conversion functions. 1702 if (ReturnTypeInfo) { 1703 TypeLoc TL = ReturnTypeInfo->getTypeLoc(); 1704 assert(TL.getFullDataSize() == CurrTL.getFullDataSize()); 1705 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); 1706 } else { 1707 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL); 1708 } 1709 1710 return TInfo; 1711} 1712 1713/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. 1714ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) { 1715 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser 1716 // and Sema during declaration parsing. Try deallocating/caching them when 1717 // it's appropriate, instead of allocating them and keeping them around. 1718 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); 1719 new (LocT) LocInfoType(T, TInfo); 1720 assert(LocT->getTypeClass() != T->getTypeClass() && 1721 "LocInfoType's TypeClass conflicts with an existing Type class"); 1722 return ParsedType::make(QualType(LocT, 0)); 1723} 1724 1725void LocInfoType::getAsStringInternal(std::string &Str, 1726 const PrintingPolicy &Policy) const { 1727 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" 1728 " was used directly instead of getting the QualType through" 1729 " GetTypeFromParser"); 1730} 1731 1732TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 1733 // C99 6.7.6: Type names have no identifier. This is already validated by 1734 // the parser. 1735 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 1736 1737 TagDecl *OwnedTag = 0; 1738 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); 1739 QualType T = TInfo->getType(); 1740 if (D.isInvalidType()) 1741 return true; 1742 1743 if (getLangOptions().CPlusPlus) { 1744 // Check that there are no default arguments (C++ only). 1745 CheckExtraCXXDefaultArguments(D); 1746 1747 // C++0x [dcl.type]p3: 1748 // A type-specifier-seq shall not define a class or enumeration 1749 // unless it appears in the type-id of an alias-declaration 1750 // (7.1.3). 1751 if (OwnedTag && OwnedTag->isDefinition()) 1752 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) 1753 << Context.getTypeDeclType(OwnedTag); 1754 } 1755 1756 return CreateParsedType(T, TInfo); 1757} 1758 1759 1760 1761//===----------------------------------------------------------------------===// 1762// Type Attribute Processing 1763//===----------------------------------------------------------------------===// 1764 1765/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 1766/// specified type. The attribute contains 1 argument, the id of the address 1767/// space for the type. 1768static void HandleAddressSpaceTypeAttribute(QualType &Type, 1769 const AttributeList &Attr, Sema &S){ 1770 1771 // If this type is already address space qualified, reject it. 1772 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 1773 // for two or more different address spaces." 1774 if (Type.getAddressSpace()) { 1775 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); 1776 Attr.setInvalid(); 1777 return; 1778 } 1779 1780 // Check the attribute arguments. 1781 if (Attr.getNumArgs() != 1) { 1782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1783 Attr.setInvalid(); 1784 return; 1785 } 1786 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); 1787 llvm::APSInt addrSpace(32); 1788 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || 1789 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { 1790 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) 1791 << ASArgExpr->getSourceRange(); 1792 Attr.setInvalid(); 1793 return; 1794 } 1795 1796 // Bounds checking. 1797 if (addrSpace.isSigned()) { 1798 if (addrSpace.isNegative()) { 1799 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) 1800 << ASArgExpr->getSourceRange(); 1801 Attr.setInvalid(); 1802 return; 1803 } 1804 addrSpace.setIsSigned(false); 1805 } 1806 llvm::APSInt max(addrSpace.getBitWidth()); 1807 max = Qualifiers::MaxAddressSpace; 1808 if (addrSpace > max) { 1809 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) 1810 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); 1811 Attr.setInvalid(); 1812 return; 1813 } 1814 1815 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 1816 Type = S.Context.getAddrSpaceQualType(Type, ASIdx); 1817} 1818 1819/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the 1820/// specified type. The attribute contains 1 argument, weak or strong. 1821static void HandleObjCGCTypeAttribute(QualType &Type, 1822 const AttributeList &Attr, Sema &S) { 1823 if (Type.getObjCGCAttr() != Qualifiers::GCNone) { 1824 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); 1825 Attr.setInvalid(); 1826 return; 1827 } 1828 1829 // Check the attribute arguments. 1830 if (!Attr.getParameterName()) { 1831 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1832 << "objc_gc" << 1; 1833 Attr.setInvalid(); 1834 return; 1835 } 1836 Qualifiers::GC GCAttr; 1837 if (Attr.getNumArgs() != 0) { 1838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1839 Attr.setInvalid(); 1840 return; 1841 } 1842 if (Attr.getParameterName()->isStr("weak")) 1843 GCAttr = Qualifiers::Weak; 1844 else if (Attr.getParameterName()->isStr("strong")) 1845 GCAttr = Qualifiers::Strong; 1846 else { 1847 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1848 << "objc_gc" << Attr.getParameterName(); 1849 Attr.setInvalid(); 1850 return; 1851 } 1852 1853 Type = S.Context.getObjCGCQualType(Type, GCAttr); 1854} 1855 1856/// Process an individual function attribute. Returns true if the 1857/// attribute does not make sense to apply to this type. 1858bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) { 1859 if (Attr.getKind() == AttributeList::AT_noreturn) { 1860 // Complain immediately if the arg count is wrong. 1861 if (Attr.getNumArgs() != 0) { 1862 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1863 Attr.setInvalid(); 1864 return false; 1865 } 1866 1867 // Delay if this is not a function or pointer to block. 1868 if (!Type->isFunctionPointerType() 1869 && !Type->isBlockPointerType() 1870 && !Type->isFunctionType() 1871 && !Type->isMemberFunctionPointerType()) 1872 return true; 1873 1874 // Otherwise we can process right away. 1875 Type = S.Context.getNoReturnType(Type); 1876 return false; 1877 } 1878 1879 if (Attr.getKind() == AttributeList::AT_regparm) { 1880 // The warning is emitted elsewhere 1881 if (Attr.getNumArgs() != 1) { 1882 return false; 1883 } 1884 1885 // Delay if this is not a function or pointer to block. 1886 if (!Type->isFunctionPointerType() 1887 && !Type->isBlockPointerType() 1888 && !Type->isFunctionType() 1889 && !Type->isMemberFunctionPointerType()) 1890 return true; 1891 1892 // Otherwise we can process right away. 1893 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); 1894 llvm::APSInt NumParams(32); 1895 1896 // The warning is emitted elsewhere 1897 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || 1898 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) 1899 return false; 1900 1901 if (S.Context.Target.getRegParmMax() == 0) { 1902 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) 1903 << NumParamsExpr->getSourceRange(); 1904 Attr.setInvalid(); 1905 return; 1906 } 1907 1908 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { 1909 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) 1910 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); 1911 Attr.setInvalid(); 1912 return; 1913 } 1914 1915 Type = S.Context.getRegParmType(Type, NumParams.getZExtValue()); 1916 return false; 1917 } 1918 1919 // Otherwise, a calling convention. 1920 if (Attr.getNumArgs() != 0) { 1921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; 1922 Attr.setInvalid(); 1923 return false; 1924 } 1925 1926 QualType T = Type; 1927 if (const PointerType *PT = Type->getAs<PointerType>()) 1928 T = PT->getPointeeType(); 1929 else if (const BlockPointerType *BPT = Type->getAs<BlockPointerType>()) 1930 T = BPT->getPointeeType(); 1931 else if (const MemberPointerType *MPT = Type->getAs<MemberPointerType>()) 1932 T = MPT->getPointeeType(); 1933 else if (const ReferenceType *RT = Type->getAs<ReferenceType>()) 1934 T = RT->getPointeeType(); 1935 const FunctionType *Fn = T->getAs<FunctionType>(); 1936 1937 // Delay if the type didn't work out to a function. 1938 if (!Fn) return true; 1939 1940 // TODO: diagnose uses of these conventions on the wrong target. 1941 CallingConv CC; 1942 switch (Attr.getKind()) { 1943 case AttributeList::AT_cdecl: CC = CC_C; break; 1944 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; 1945 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; 1946 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; 1947 case AttributeList::AT_pascal: CC = CC_X86Pascal; break; 1948 default: llvm_unreachable("unexpected attribute kind"); return false; 1949 } 1950 1951 CallingConv CCOld = Fn->getCallConv(); 1952 if (S.Context.getCanonicalCallConv(CC) == 1953 S.Context.getCanonicalCallConv(CCOld)) { 1954 Attr.setInvalid(); 1955 return false; 1956 } 1957 1958 if (CCOld != CC_Default) { 1959 // Should we diagnose reapplications of the same convention? 1960 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) 1961 << FunctionType::getNameForCallConv(CC) 1962 << FunctionType::getNameForCallConv(CCOld); 1963 Attr.setInvalid(); 1964 return false; 1965 } 1966 1967 // Diagnose the use of X86 fastcall on varargs or unprototyped functions. 1968 if (CC == CC_X86FastCall) { 1969 if (isa<FunctionNoProtoType>(Fn)) { 1970 S.Diag(Attr.getLoc(), diag::err_cconv_knr) 1971 << FunctionType::getNameForCallConv(CC); 1972 Attr.setInvalid(); 1973 return false; 1974 } 1975 1976 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn); 1977 if (FnP->isVariadic()) { 1978 S.Diag(Attr.getLoc(), diag::err_cconv_varargs) 1979 << FunctionType::getNameForCallConv(CC); 1980 Attr.setInvalid(); 1981 return false; 1982 } 1983 } 1984 1985 Type = S.Context.getCallConvType(Type, CC); 1986 return false; 1987} 1988 1989/// HandleVectorSizeAttribute - this attribute is only applicable to integral 1990/// and float scalars, although arrays, pointers, and function return values are 1991/// allowed in conjunction with this construct. Aggregates with this attribute 1992/// are invalid, even if they are of the same size as a corresponding scalar. 1993/// The raw attribute should contain precisely 1 argument, the vector size for 1994/// the variable, measured in bytes. If curType and rawAttr are well formed, 1995/// this routine will return a new vector type. 1996static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, 1997 Sema &S) { 1998 // Check the attribute arugments. 1999 if (Attr.getNumArgs() != 1) { 2000 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 2001 Attr.setInvalid(); 2002 return; 2003 } 2004 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); 2005 llvm::APSInt vecSize(32); 2006 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || 2007 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { 2008 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) 2009 << "vector_size" << sizeExpr->getSourceRange(); 2010 Attr.setInvalid(); 2011 return; 2012 } 2013 // the base type must be integer or float, and can't already be a vector. 2014 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { 2015 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; 2016 Attr.setInvalid(); 2017 return; 2018 } 2019 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); 2020 // vecSize is specified in bytes - convert to bits. 2021 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); 2022 2023 // the vector size needs to be an integral multiple of the type size. 2024 if (vectorSize % typeSize) { 2025 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) 2026 << sizeExpr->getSourceRange(); 2027 Attr.setInvalid(); 2028 return; 2029 } 2030 if (vectorSize == 0) { 2031 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) 2032 << sizeExpr->getSourceRange(); 2033 Attr.setInvalid(); 2034 return; 2035 } 2036 2037 // Success! Instantiate the vector type, the number of elements is > 0, and 2038 // not required to be a power of 2, unlike GCC. 2039 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, 2040 VectorType::NotAltiVec); 2041} 2042 2043void ProcessTypeAttributeList(Sema &S, QualType &Result, 2044 bool IsDeclSpec, const AttributeList *AL, 2045 DelayedAttributeSet &FnAttrs) { 2046 // Scan through and apply attributes to this type where it makes sense. Some 2047 // attributes (such as __address_space__, __vector_size__, etc) apply to the 2048 // type, but others can be present in the type specifiers even though they 2049 // apply to the decl. Here we apply type attributes and ignore the rest. 2050 for (; AL; AL = AL->getNext()) { 2051 // Skip attributes that were marked to be invalid. 2052 if (AL->isInvalid()) 2053 continue; 2054 2055 // If this is an attribute we can handle, do so now, 2056 // otherwise, add it to the FnAttrs list for rechaining. 2057 switch (AL->getKind()) { 2058 default: break; 2059 2060 case AttributeList::AT_address_space: 2061 HandleAddressSpaceTypeAttribute(Result, *AL, S); 2062 break; 2063 case AttributeList::AT_objc_gc: 2064 HandleObjCGCTypeAttribute(Result, *AL, S); 2065 break; 2066 case AttributeList::AT_vector_size: 2067 HandleVectorSizeAttr(Result, *AL, S); 2068 break; 2069 2070 case AttributeList::AT_noreturn: 2071 case AttributeList::AT_cdecl: 2072 case AttributeList::AT_fastcall: 2073 case AttributeList::AT_stdcall: 2074 case AttributeList::AT_thiscall: 2075 case AttributeList::AT_pascal: 2076 case AttributeList::AT_regparm: 2077 // Don't process these on the DeclSpec. 2078 if (IsDeclSpec || 2079 ProcessFnAttr(S, Result, *AL)) 2080 FnAttrs.push_back(DelayedAttribute(AL, Result)); 2081 break; 2082 } 2083 } 2084} 2085 2086/// @brief Ensure that the type T is a complete type. 2087/// 2088/// This routine checks whether the type @p T is complete in any 2089/// context where a complete type is required. If @p T is a complete 2090/// type, returns false. If @p T is a class template specialization, 2091/// this routine then attempts to perform class template 2092/// instantiation. If instantiation fails, or if @p T is incomplete 2093/// and cannot be completed, issues the diagnostic @p diag (giving it 2094/// the type @p T) and returns true. 2095/// 2096/// @param Loc The location in the source that the incomplete type 2097/// diagnostic should refer to. 2098/// 2099/// @param T The type that this routine is examining for completeness. 2100/// 2101/// @param PD The partial diagnostic that will be printed out if T is not a 2102/// complete type. 2103/// 2104/// @returns @c true if @p T is incomplete and a diagnostic was emitted, 2105/// @c false otherwise. 2106bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, 2107 const PartialDiagnostic &PD, 2108 std::pair<SourceLocation, 2109 PartialDiagnostic> Note) { 2110 unsigned diag = PD.getDiagID(); 2111 2112 // FIXME: Add this assertion to make sure we always get instantiation points. 2113 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); 2114 // FIXME: Add this assertion to help us flush out problems with 2115 // checking for dependent types and type-dependent expressions. 2116 // 2117 // assert(!T->isDependentType() && 2118 // "Can't ask whether a dependent type is complete"); 2119 2120 // If we have a complete type, we're done. 2121 if (!T->isIncompleteType()) 2122 return false; 2123 2124 // If we have a class template specialization or a class member of a 2125 // class template specialization, or an array with known size of such, 2126 // try to instantiate it. 2127 QualType MaybeTemplate = T; 2128 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) 2129 MaybeTemplate = Array->getElementType(); 2130 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { 2131 if (ClassTemplateSpecializationDecl *ClassTemplateSpec 2132 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { 2133 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) 2134 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, 2135 TSK_ImplicitInstantiation, 2136 /*Complain=*/diag != 0); 2137 } else if (CXXRecordDecl *Rec 2138 = dyn_cast<CXXRecordDecl>(Record->getDecl())) { 2139 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { 2140 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); 2141 assert(MSInfo && "Missing member specialization information?"); 2142 // This record was instantiated from a class within a template. 2143 if (MSInfo->getTemplateSpecializationKind() 2144 != TSK_ExplicitSpecialization) 2145 return InstantiateClass(Loc, Rec, Pattern, 2146 getTemplateInstantiationArgs(Rec), 2147 TSK_ImplicitInstantiation, 2148 /*Complain=*/diag != 0); 2149 } 2150 } 2151 } 2152 2153 if (diag == 0) 2154 return true; 2155 2156 const TagType *Tag = 0; 2157 if (const RecordType *Record = T->getAs<RecordType>()) 2158 Tag = Record; 2159 else if (const EnumType *Enum = T->getAs<EnumType>()) 2160 Tag = Enum; 2161 2162 // Avoid diagnosing invalid decls as incomplete. 2163 if (Tag && Tag->getDecl()->isInvalidDecl()) 2164 return true; 2165 2166 // We have an incomplete type. Produce a diagnostic. 2167 Diag(Loc, PD) << T; 2168 2169 // If we have a note, produce it. 2170 if (!Note.first.isInvalid()) 2171 Diag(Note.first, Note.second); 2172 2173 // If the type was a forward declaration of a class/struct/union 2174 // type, produce a note. 2175 if (Tag && !Tag->getDecl()->isInvalidDecl()) 2176 Diag(Tag->getDecl()->getLocation(), 2177 Tag->isBeingDefined() ? diag::note_type_being_defined 2178 : diag::note_forward_declaration) 2179 << QualType(Tag, 0); 2180 2181 return true; 2182} 2183 2184bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, 2185 const PartialDiagnostic &PD) { 2186 return RequireCompleteType(Loc, T, PD, 2187 std::make_pair(SourceLocation(), PDiag(0))); 2188} 2189 2190bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, 2191 unsigned DiagID) { 2192 return RequireCompleteType(Loc, T, PDiag(DiagID), 2193 std::make_pair(SourceLocation(), PDiag(0))); 2194} 2195 2196/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword 2197/// and qualified by the nested-name-specifier contained in SS. 2198QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, 2199 const CXXScopeSpec &SS, QualType T) { 2200 if (T.isNull()) 2201 return T; 2202 NestedNameSpecifier *NNS; 2203 if (SS.isValid()) 2204 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 2205 else { 2206 if (Keyword == ETK_None) 2207 return T; 2208 NNS = 0; 2209 } 2210 return Context.getElaboratedType(Keyword, NNS, T); 2211} 2212 2213QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) { 2214 ExprResult ER = CheckPlaceholderExpr(E, Loc); 2215 if (ER.isInvalid()) return QualType(); 2216 E = ER.take(); 2217 2218 if (!E->isTypeDependent()) { 2219 QualType T = E->getType(); 2220 if (const TagType *TT = T->getAs<TagType>()) 2221 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc()); 2222 } 2223 return Context.getTypeOfExprType(E); 2224} 2225 2226QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) { 2227 ExprResult ER = CheckPlaceholderExpr(E, Loc); 2228 if (ER.isInvalid()) return QualType(); 2229 E = ER.take(); 2230 2231 return Context.getDecltypeType(E); 2232} 2233