SemaType.cpp revision 198893
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 "Sema.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/CXXInheritance.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/DeclTemplate.h" 19#include "clang/AST/TypeLoc.h" 20#include "clang/AST/TypeLocVisitor.h" 21#include "clang/AST/Expr.h" 22#include "clang/Basic/PartialDiagnostic.h" 23#include "clang/Parse/DeclSpec.h" 24#include "llvm/ADT/SmallPtrSet.h" 25using namespace clang; 26 27/// \brief Perform adjustment on the parameter type of a function. 28/// 29/// This routine adjusts the given parameter type @p T to the actual 30/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 31/// C++ [dcl.fct]p3). The adjusted parameter type is returned. 32QualType Sema::adjustParameterType(QualType T) { 33 // C99 6.7.5.3p7: 34 // A declaration of a parameter as "array of type" shall be 35 // adjusted to "qualified pointer to type", where the type 36 // qualifiers (if any) are those specified within the [ and ] of 37 // the array type derivation. 38 if (T->isArrayType()) 39 return Context.getArrayDecayedType(T); 40 41 // C99 6.7.5.3p8: 42 // A declaration of a parameter as "function returning type" 43 // shall be adjusted to "pointer to function returning type", as 44 // in 6.3.2.1. 45 if (T->isFunctionType()) 46 return Context.getPointerType(T); 47 48 return T; 49} 50 51 52 53/// isOmittedBlockReturnType - Return true if this declarator is missing a 54/// return type because this is a omitted return type on a block literal. 55static bool isOmittedBlockReturnType(const Declarator &D) { 56 if (D.getContext() != Declarator::BlockLiteralContext || 57 D.getDeclSpec().hasTypeSpecifier()) 58 return false; 59 60 if (D.getNumTypeObjects() == 0) 61 return true; // ^{ ... } 62 63 if (D.getNumTypeObjects() == 1 && 64 D.getTypeObject(0).Kind == DeclaratorChunk::Function) 65 return true; // ^(int X, float Y) { ... } 66 67 return false; 68} 69 70/// \brief Convert the specified declspec to the appropriate type 71/// object. 72/// \param D the declarator containing the declaration specifier. 73/// \returns The type described by the declaration specifiers. This function 74/// never returns null. 75static QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){ 76 // FIXME: Should move the logic from DeclSpec::Finish to here for validity 77 // checking. 78 const DeclSpec &DS = TheDeclarator.getDeclSpec(); 79 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc(); 80 if (DeclLoc.isInvalid()) 81 DeclLoc = DS.getSourceRange().getBegin(); 82 83 ASTContext &Context = TheSema.Context; 84 85 QualType Result; 86 switch (DS.getTypeSpecType()) { 87 case DeclSpec::TST_void: 88 Result = Context.VoidTy; 89 break; 90 case DeclSpec::TST_char: 91 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 92 Result = Context.CharTy; 93 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) 94 Result = Context.SignedCharTy; 95 else { 96 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 97 "Unknown TSS value"); 98 Result = Context.UnsignedCharTy; 99 } 100 break; 101 case DeclSpec::TST_wchar: 102 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 103 Result = Context.WCharTy; 104 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { 105 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 106 << DS.getSpecifierName(DS.getTypeSpecType()); 107 Result = Context.getSignedWCharType(); 108 } else { 109 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 110 "Unknown TSS value"); 111 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 112 << DS.getSpecifierName(DS.getTypeSpecType()); 113 Result = Context.getUnsignedWCharType(); 114 } 115 break; 116 case DeclSpec::TST_char16: 117 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && 118 "Unknown TSS value"); 119 Result = Context.Char16Ty; 120 break; 121 case DeclSpec::TST_char32: 122 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && 123 "Unknown TSS value"); 124 Result = Context.Char32Ty; 125 break; 126 case DeclSpec::TST_unspecified: 127 // "<proto1,proto2>" is an objc qualified ID with a missing id. 128 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 129 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, 130 (ObjCProtocolDecl**)PQ, 131 DS.getNumProtocolQualifiers()); 132 break; 133 } 134 135 // If this is a missing declspec in a block literal return context, then it 136 // is inferred from the return statements inside the block. 137 if (isOmittedBlockReturnType(TheDeclarator)) { 138 Result = Context.DependentTy; 139 break; 140 } 141 142 // Unspecified typespec defaults to int in C90. However, the C90 grammar 143 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, 144 // type-qualifier, or storage-class-specifier. If not, emit an extwarn. 145 // Note that the one exception to this is function definitions, which are 146 // allowed to be completely missing a declspec. This is handled in the 147 // parser already though by it pretending to have seen an 'int' in this 148 // case. 149 if (TheSema.getLangOptions().ImplicitInt) { 150 // In C89 mode, we only warn if there is a completely missing declspec 151 // when one is not allowed. 152 if (DS.isEmpty()) { 153 TheSema.Diag(DeclLoc, diag::ext_missing_declspec) 154 << DS.getSourceRange() 155 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), 156 "int"); 157 } 158 } else if (!DS.hasTypeSpecifier()) { 159 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: 160 // "At least one type specifier shall be given in the declaration 161 // specifiers in each declaration, and in the specifier-qualifier list in 162 // each struct declaration and type name." 163 // FIXME: Does Microsoft really have the implicit int extension in C++? 164 if (TheSema.getLangOptions().CPlusPlus && 165 !TheSema.getLangOptions().Microsoft) { 166 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier) 167 << DS.getSourceRange(); 168 169 // When this occurs in C++ code, often something is very broken with the 170 // value being declared, poison it as invalid so we don't get chains of 171 // errors. 172 TheDeclarator.setInvalidType(true); 173 } else { 174 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier) 175 << DS.getSourceRange(); 176 } 177 } 178 179 // FALL THROUGH. 180 case DeclSpec::TST_int: { 181 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { 182 switch (DS.getTypeSpecWidth()) { 183 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; 184 case DeclSpec::TSW_short: Result = Context.ShortTy; break; 185 case DeclSpec::TSW_long: Result = Context.LongTy; break; 186 case DeclSpec::TSW_longlong: 187 Result = Context.LongLongTy; 188 189 // long long is a C99 feature. 190 if (!TheSema.getLangOptions().C99 && 191 !TheSema.getLangOptions().CPlusPlus0x) 192 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); 193 break; 194 } 195 } else { 196 switch (DS.getTypeSpecWidth()) { 197 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; 198 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; 199 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; 200 case DeclSpec::TSW_longlong: 201 Result = Context.UnsignedLongLongTy; 202 203 // long long is a C99 feature. 204 if (!TheSema.getLangOptions().C99 && 205 !TheSema.getLangOptions().CPlusPlus0x) 206 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); 207 break; 208 } 209 } 210 break; 211 } 212 case DeclSpec::TST_float: Result = Context.FloatTy; break; 213 case DeclSpec::TST_double: 214 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) 215 Result = Context.LongDoubleTy; 216 else 217 Result = Context.DoubleTy; 218 break; 219 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool 220 case DeclSpec::TST_decimal32: // _Decimal32 221 case DeclSpec::TST_decimal64: // _Decimal64 222 case DeclSpec::TST_decimal128: // _Decimal128 223 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); 224 Result = Context.IntTy; 225 TheDeclarator.setInvalidType(true); 226 break; 227 case DeclSpec::TST_class: 228 case DeclSpec::TST_enum: 229 case DeclSpec::TST_union: 230 case DeclSpec::TST_struct: { 231 TypeDecl *D = cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep())); 232 if (!D) { 233 // This can happen in C++ with ambiguous lookups. 234 Result = Context.IntTy; 235 TheDeclarator.setInvalidType(true); 236 break; 237 } 238 239 // If the type is deprecated or unavailable, diagnose it. 240 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc()); 241 242 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 243 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); 244 245 // TypeQuals handled by caller. 246 Result = Context.getTypeDeclType(D); 247 248 // In C++, make an ElaboratedType. 249 if (TheSema.getLangOptions().CPlusPlus) { 250 TagDecl::TagKind Tag 251 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType()); 252 Result = Context.getElaboratedType(Result, Tag); 253 } 254 255 if (D->isInvalidDecl()) 256 TheDeclarator.setInvalidType(true); 257 break; 258 } 259 case DeclSpec::TST_typename: { 260 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 261 DS.getTypeSpecSign() == 0 && 262 "Can't handle qualifiers on typedef names yet!"); 263 Result = TheSema.GetTypeFromParser(DS.getTypeRep()); 264 265 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 266 if (const ObjCInterfaceType * 267 Interface = Result->getAs<ObjCInterfaceType>()) { 268 // It would be nice if protocol qualifiers were only stored with the 269 // ObjCObjectPointerType. Unfortunately, this isn't possible due 270 // to the following typedef idiom (which is uncommon, but allowed): 271 // 272 // typedef Foo<P> T; 273 // static void func() { 274 // Foo<P> *yy; 275 // T *zz; 276 // } 277 Result = Context.getObjCInterfaceType(Interface->getDecl(), 278 (ObjCProtocolDecl**)PQ, 279 DS.getNumProtocolQualifiers()); 280 } else if (Result->isObjCIdType()) 281 // id<protocol-list> 282 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, 283 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); 284 else if (Result->isObjCClassType()) { 285 // Class<protocol-list> 286 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy, 287 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); 288 } else { 289 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) 290 << DS.getSourceRange(); 291 TheDeclarator.setInvalidType(true); 292 } 293 } 294 295 // TypeQuals handled by caller. 296 break; 297 } 298 case DeclSpec::TST_typeofType: 299 // FIXME: Preserve type source info. 300 Result = TheSema.GetTypeFromParser(DS.getTypeRep()); 301 assert(!Result.isNull() && "Didn't get a type for typeof?"); 302 // TypeQuals handled by caller. 303 Result = Context.getTypeOfType(Result); 304 break; 305 case DeclSpec::TST_typeofExpr: { 306 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 307 assert(E && "Didn't get an expression for typeof?"); 308 // TypeQuals handled by caller. 309 Result = Context.getTypeOfExprType(E); 310 break; 311 } 312 case DeclSpec::TST_decltype: { 313 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 314 assert(E && "Didn't get an expression for decltype?"); 315 // TypeQuals handled by caller. 316 Result = TheSema.BuildDecltypeType(E); 317 if (Result.isNull()) { 318 Result = Context.IntTy; 319 TheDeclarator.setInvalidType(true); 320 } 321 break; 322 } 323 case DeclSpec::TST_auto: { 324 // TypeQuals handled by caller. 325 Result = Context.UndeducedAutoTy; 326 break; 327 } 328 329 case DeclSpec::TST_error: 330 Result = Context.IntTy; 331 TheDeclarator.setInvalidType(true); 332 break; 333 } 334 335 // Handle complex types. 336 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { 337 if (TheSema.getLangOptions().Freestanding) 338 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); 339 Result = Context.getComplexType(Result); 340 } 341 342 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && 343 "FIXME: imaginary types not supported yet!"); 344 345 // See if there are any attributes on the declspec that apply to the type (as 346 // opposed to the decl). 347 if (const AttributeList *AL = DS.getAttributes()) 348 TheSema.ProcessTypeAttributeList(Result, AL); 349 350 // Apply const/volatile/restrict qualifiers to T. 351 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 352 353 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 354 // or incomplete types shall not be restrict-qualified." C++ also allows 355 // restrict-qualified references. 356 if (TypeQuals & DeclSpec::TQ_restrict) { 357 if (Result->isPointerType() || Result->isReferenceType()) { 358 QualType EltTy = Result->isPointerType() ? 359 Result->getAs<PointerType>()->getPointeeType() : 360 Result->getAs<ReferenceType>()->getPointeeType(); 361 362 // If we have a pointer or reference, the pointee must have an object 363 // incomplete type. 364 if (!EltTy->isIncompleteOrObjectType()) { 365 TheSema.Diag(DS.getRestrictSpecLoc(), 366 diag::err_typecheck_invalid_restrict_invalid_pointee) 367 << EltTy << DS.getSourceRange(); 368 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. 369 } 370 } else { 371 TheSema.Diag(DS.getRestrictSpecLoc(), 372 diag::err_typecheck_invalid_restrict_not_pointer) 373 << Result << DS.getSourceRange(); 374 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. 375 } 376 } 377 378 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 379 // of a function type includes any type qualifiers, the behavior is 380 // undefined." 381 if (Result->isFunctionType() && TypeQuals) { 382 // Get some location to point at, either the C or V location. 383 SourceLocation Loc; 384 if (TypeQuals & DeclSpec::TQ_const) 385 Loc = DS.getConstSpecLoc(); 386 else if (TypeQuals & DeclSpec::TQ_volatile) 387 Loc = DS.getVolatileSpecLoc(); 388 else { 389 assert((TypeQuals & DeclSpec::TQ_restrict) && 390 "Has CVR quals but not C, V, or R?"); 391 Loc = DS.getRestrictSpecLoc(); 392 } 393 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers) 394 << Result << DS.getSourceRange(); 395 } 396 397 // C++ [dcl.ref]p1: 398 // Cv-qualified references are ill-formed except when the 399 // cv-qualifiers are introduced through the use of a typedef 400 // (7.1.3) or of a template type argument (14.3), in which 401 // case the cv-qualifiers are ignored. 402 // FIXME: Shouldn't we be checking SCS_typedef here? 403 if (DS.getTypeSpecType() == DeclSpec::TST_typename && 404 TypeQuals && Result->isReferenceType()) { 405 TypeQuals &= ~DeclSpec::TQ_const; 406 TypeQuals &= ~DeclSpec::TQ_volatile; 407 } 408 409 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); 410 Result = Context.getQualifiedType(Result, Quals); 411 } 412 413 return Result; 414} 415 416static std::string getPrintableNameForEntity(DeclarationName Entity) { 417 if (Entity) 418 return Entity.getAsString(); 419 420 return "type name"; 421} 422 423/// \brief Build a pointer type. 424/// 425/// \param T The type to which we'll be building a pointer. 426/// 427/// \param Quals The cvr-qualifiers to be applied to the pointer type. 428/// 429/// \param Loc The location of the entity whose type involves this 430/// pointer type or, if there is no such entity, the location of the 431/// type that will have pointer type. 432/// 433/// \param Entity The name of the entity that involves the pointer 434/// type, if known. 435/// 436/// \returns A suitable pointer type, if there are no 437/// errors. Otherwise, returns a NULL type. 438QualType Sema::BuildPointerType(QualType T, unsigned Quals, 439 SourceLocation Loc, DeclarationName Entity) { 440 if (T->isReferenceType()) { 441 // C++ 8.3.2p4: There shall be no ... pointers to references ... 442 Diag(Loc, diag::err_illegal_decl_pointer_to_reference) 443 << getPrintableNameForEntity(Entity) << T; 444 return QualType(); 445 } 446 447 Qualifiers Qs = Qualifiers::fromCVRMask(Quals); 448 449 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 450 // object or incomplete types shall not be restrict-qualified." 451 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) { 452 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 453 << T; 454 Qs.removeRestrict(); 455 } 456 457 // Build the pointer type. 458 return Context.getQualifiedType(Context.getPointerType(T), Qs); 459} 460 461/// \brief Build a reference type. 462/// 463/// \param T The type to which we'll be building a reference. 464/// 465/// \param CVR The cvr-qualifiers to be applied to the reference type. 466/// 467/// \param Loc The location of the entity whose type involves this 468/// reference type or, if there is no such entity, the location of the 469/// type that will have reference type. 470/// 471/// \param Entity The name of the entity that involves the reference 472/// type, if known. 473/// 474/// \returns A suitable reference type, if there are no 475/// errors. Otherwise, returns a NULL type. 476QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, 477 unsigned CVR, SourceLocation Loc, 478 DeclarationName Entity) { 479 Qualifiers Quals = Qualifiers::fromCVRMask(CVR); 480 481 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); 482 483 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a 484 // reference to a type T, and attempt to create the type "lvalue 485 // reference to cv TD" creates the type "lvalue reference to T". 486 // We use the qualifiers (restrict or none) of the original reference, 487 // not the new ones. This is consistent with GCC. 488 489 // C++ [dcl.ref]p4: There shall be no references to references. 490 // 491 // According to C++ DR 106, references to references are only 492 // diagnosed when they are written directly (e.g., "int & &"), 493 // but not when they happen via a typedef: 494 // 495 // typedef int& intref; 496 // typedef intref& intref2; 497 // 498 // Parser::ParseDeclaratorInternal diagnoses the case where 499 // references are written directly; here, we handle the 500 // collapsing of references-to-references as described in C++ 501 // DR 106 and amended by C++ DR 540. 502 503 // C++ [dcl.ref]p1: 504 // A declarator that specifies the type "reference to cv void" 505 // is ill-formed. 506 if (T->isVoidType()) { 507 Diag(Loc, diag::err_reference_to_void); 508 return QualType(); 509 } 510 511 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 512 // object or incomplete types shall not be restrict-qualified." 513 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { 514 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 515 << T; 516 Quals.removeRestrict(); 517 } 518 519 // C++ [dcl.ref]p1: 520 // [...] Cv-qualified references are ill-formed except when the 521 // cv-qualifiers are introduced through the use of a typedef 522 // (7.1.3) or of a template type argument (14.3), in which case 523 // the cv-qualifiers are ignored. 524 // 525 // We diagnose extraneous cv-qualifiers for the non-typedef, 526 // non-template type argument case within the parser. Here, we just 527 // ignore any extraneous cv-qualifiers. 528 Quals.removeConst(); 529 Quals.removeVolatile(); 530 531 // Handle restrict on references. 532 if (LValueRef) 533 return Context.getQualifiedType( 534 Context.getLValueReferenceType(T, SpelledAsLValue), Quals); 535 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals); 536} 537 538/// \brief Build an array type. 539/// 540/// \param T The type of each element in the array. 541/// 542/// \param ASM C99 array size modifier (e.g., '*', 'static'). 543/// 544/// \param ArraySize Expression describing the size of the array. 545/// 546/// \param Quals The cvr-qualifiers to be applied to the array's 547/// element type. 548/// 549/// \param Loc The location of the entity whose type involves this 550/// array type or, if there is no such entity, the location of the 551/// type that will have array type. 552/// 553/// \param Entity The name of the entity that involves the array 554/// type, if known. 555/// 556/// \returns A suitable array type, if there are no errors. Otherwise, 557/// returns a NULL type. 558QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, 559 Expr *ArraySize, unsigned Quals, 560 SourceRange Brackets, DeclarationName Entity) { 561 562 SourceLocation Loc = Brackets.getBegin(); 563 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 564 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 565 if (RequireCompleteType(Loc, T, 566 diag::err_illegal_decl_array_incomplete_type)) 567 return QualType(); 568 569 if (T->isFunctionType()) { 570 Diag(Loc, diag::err_illegal_decl_array_of_functions) 571 << getPrintableNameForEntity(Entity) << T; 572 return QualType(); 573 } 574 575 // C++ 8.3.2p4: There shall be no ... arrays of references ... 576 if (T->isReferenceType()) { 577 Diag(Loc, diag::err_illegal_decl_array_of_references) 578 << getPrintableNameForEntity(Entity) << T; 579 return QualType(); 580 } 581 582 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { 583 Diag(Loc, diag::err_illegal_decl_array_of_auto) 584 << getPrintableNameForEntity(Entity); 585 return QualType(); 586 } 587 588 if (const RecordType *EltTy = T->getAs<RecordType>()) { 589 // If the element type is a struct or union that contains a variadic 590 // array, accept it as a GNU extension: C99 6.7.2.1p2. 591 if (EltTy->getDecl()->hasFlexibleArrayMember()) 592 Diag(Loc, diag::ext_flexible_array_in_array) << T; 593 } else if (T->isObjCInterfaceType()) { 594 Diag(Loc, diag::err_objc_array_of_interfaces) << T; 595 return QualType(); 596 } 597 598 // C99 6.7.5.2p1: The size expression shall have integer type. 599 if (ArraySize && !ArraySize->isTypeDependent() && 600 !ArraySize->getType()->isIntegerType()) { 601 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) 602 << ArraySize->getType() << ArraySize->getSourceRange(); 603 ArraySize->Destroy(Context); 604 return QualType(); 605 } 606 llvm::APSInt ConstVal(32); 607 if (!ArraySize) { 608 if (ASM == ArrayType::Star) 609 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); 610 else 611 T = Context.getIncompleteArrayType(T, ASM, Quals); 612 } else if (ArraySize->isValueDependent()) { 613 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); 614 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 615 (!T->isDependentType() && !T->isConstantSizeType())) { 616 // Per C99, a variable array is an array with either a non-constant 617 // size or an element type that has a non-constant-size 618 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); 619 } else { 620 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 621 // have a value greater than zero. 622 if (ConstVal.isSigned()) { 623 if (ConstVal.isNegative()) { 624 Diag(ArraySize->getLocStart(), 625 diag::err_typecheck_negative_array_size) 626 << ArraySize->getSourceRange(); 627 return QualType(); 628 } else if (ConstVal == 0) { 629 // GCC accepts zero sized static arrays. 630 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) 631 << ArraySize->getSourceRange(); 632 } 633 } 634 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); 635 } 636 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 637 if (!getLangOptions().C99) { 638 if (ArraySize && !ArraySize->isTypeDependent() && 639 !ArraySize->isValueDependent() && 640 !ArraySize->isIntegerConstantExpr(Context)) 641 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla); 642 else if (ASM != ArrayType::Normal || Quals != 0) 643 Diag(Loc, 644 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx 645 : diag::ext_c99_array_usage); 646 } 647 648 return T; 649} 650 651/// \brief Build an ext-vector type. 652/// 653/// Run the required checks for the extended vector type. 654QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, 655 SourceLocation AttrLoc) { 656 657 Expr *Arg = (Expr *)ArraySize.get(); 658 659 // unlike gcc's vector_size attribute, we do not allow vectors to be defined 660 // in conjunction with complex types (pointers, arrays, functions, etc.). 661 if (!T->isDependentType() && 662 !T->isIntegerType() && !T->isRealFloatingType()) { 663 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; 664 return QualType(); 665 } 666 667 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 668 llvm::APSInt vecSize(32); 669 if (!Arg->isIntegerConstantExpr(vecSize, Context)) { 670 Diag(AttrLoc, diag::err_attribute_argument_not_int) 671 << "ext_vector_type" << Arg->getSourceRange(); 672 return QualType(); 673 } 674 675 // unlike gcc's vector_size attribute, the size is specified as the 676 // number of elements, not the number of bytes. 677 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); 678 679 if (vectorSize == 0) { 680 Diag(AttrLoc, diag::err_attribute_zero_size) 681 << Arg->getSourceRange(); 682 return QualType(); 683 } 684 685 if (!T->isDependentType()) 686 return Context.getExtVectorType(T, vectorSize); 687 } 688 689 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), 690 AttrLoc); 691} 692 693/// \brief Build a function type. 694/// 695/// This routine checks the function type according to C++ rules and 696/// under the assumption that the result type and parameter types have 697/// just been instantiated from a template. It therefore duplicates 698/// some of the behavior of GetTypeForDeclarator, but in a much 699/// simpler form that is only suitable for this narrow use case. 700/// 701/// \param T The return type of the function. 702/// 703/// \param ParamTypes The parameter types of the function. This array 704/// will be modified to account for adjustments to the types of the 705/// function parameters. 706/// 707/// \param NumParamTypes The number of parameter types in ParamTypes. 708/// 709/// \param Variadic Whether this is a variadic function type. 710/// 711/// \param Quals The cvr-qualifiers to be applied to the function type. 712/// 713/// \param Loc The location of the entity whose type involves this 714/// function type or, if there is no such entity, the location of the 715/// type that will have function type. 716/// 717/// \param Entity The name of the entity that involves the function 718/// type, if known. 719/// 720/// \returns A suitable function type, if there are no 721/// errors. Otherwise, returns a NULL type. 722QualType Sema::BuildFunctionType(QualType T, 723 QualType *ParamTypes, 724 unsigned NumParamTypes, 725 bool Variadic, unsigned Quals, 726 SourceLocation Loc, DeclarationName Entity) { 727 if (T->isArrayType() || T->isFunctionType()) { 728 Diag(Loc, diag::err_func_returning_array_function) << T; 729 return QualType(); 730 } 731 732 bool Invalid = false; 733 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { 734 QualType ParamType = adjustParameterType(ParamTypes[Idx]); 735 if (ParamType->isVoidType()) { 736 Diag(Loc, diag::err_param_with_void_type); 737 Invalid = true; 738 } 739 740 ParamTypes[Idx] = ParamType; 741 } 742 743 if (Invalid) 744 return QualType(); 745 746 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, 747 Quals); 748} 749 750/// \brief Build a member pointer type \c T Class::*. 751/// 752/// \param T the type to which the member pointer refers. 753/// \param Class the class type into which the member pointer points. 754/// \param CVR Qualifiers applied to the member pointer type 755/// \param Loc the location where this type begins 756/// \param Entity the name of the entity that will have this member pointer type 757/// 758/// \returns a member pointer type, if successful, or a NULL type if there was 759/// an error. 760QualType Sema::BuildMemberPointerType(QualType T, QualType Class, 761 unsigned CVR, SourceLocation Loc, 762 DeclarationName Entity) { 763 Qualifiers Quals = Qualifiers::fromCVRMask(CVR); 764 765 // Verify that we're not building a pointer to pointer to function with 766 // exception specification. 767 if (CheckDistantExceptionSpec(T)) { 768 Diag(Loc, diag::err_distant_exception_spec); 769 770 // FIXME: If we're doing this as part of template instantiation, 771 // we should return immediately. 772 773 // Build the type anyway, but use the canonical type so that the 774 // exception specifiers are stripped off. 775 T = Context.getCanonicalType(T); 776 } 777 778 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member 779 // with reference type, or "cv void." 780 if (T->isReferenceType()) { 781 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) 782 << (Entity? Entity.getAsString() : "type name") << T; 783 return QualType(); 784 } 785 786 if (T->isVoidType()) { 787 Diag(Loc, diag::err_illegal_decl_mempointer_to_void) 788 << (Entity? Entity.getAsString() : "type name"); 789 return QualType(); 790 } 791 792 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 793 // object or incomplete types shall not be restrict-qualified." 794 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { 795 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 796 << T; 797 798 // FIXME: If we're doing this as part of template instantiation, 799 // we should return immediately. 800 Quals.removeRestrict(); 801 } 802 803 if (!Class->isDependentType() && !Class->isRecordType()) { 804 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; 805 return QualType(); 806 } 807 808 return Context.getQualifiedType( 809 Context.getMemberPointerType(T, Class.getTypePtr()), Quals); 810} 811 812/// \brief Build a block pointer type. 813/// 814/// \param T The type to which we'll be building a block pointer. 815/// 816/// \param CVR The cvr-qualifiers to be applied to the block pointer type. 817/// 818/// \param Loc The location of the entity whose type involves this 819/// block pointer type or, if there is no such entity, the location of the 820/// type that will have block pointer type. 821/// 822/// \param Entity The name of the entity that involves the block pointer 823/// type, if known. 824/// 825/// \returns A suitable block pointer type, if there are no 826/// errors. Otherwise, returns a NULL type. 827QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR, 828 SourceLocation Loc, 829 DeclarationName Entity) { 830 if (!T->isFunctionType()) { 831 Diag(Loc, diag::err_nonfunction_block_type); 832 return QualType(); 833 } 834 835 Qualifiers Quals = Qualifiers::fromCVRMask(CVR); 836 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals); 837} 838 839QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) { 840 QualType QT = QualType::getFromOpaquePtr(Ty); 841 if (QT.isNull()) { 842 if (DInfo) *DInfo = 0; 843 return QualType(); 844 } 845 846 DeclaratorInfo *DI = 0; 847 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { 848 QT = LIT->getType(); 849 DI = LIT->getDeclaratorInfo(); 850 } 851 852 if (DInfo) *DInfo = DI; 853 return QT; 854} 855 856/// GetTypeForDeclarator - Convert the type for the specified 857/// declarator to Type instances. 858/// 859/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq 860/// owns the declaration of a type (e.g., the definition of a struct 861/// type), then *OwnedDecl will receive the owned declaration. 862QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, 863 DeclaratorInfo **DInfo, 864 TagDecl **OwnedDecl) { 865 // Determine the type of the declarator. Not all forms of declarator 866 // have a type. 867 QualType T; 868 869 switch (D.getName().getKind()) { 870 case UnqualifiedId::IK_Identifier: 871 case UnqualifiedId::IK_OperatorFunctionId: 872 case UnqualifiedId::IK_TemplateId: 873 T = ConvertDeclSpecToType(D, *this); 874 875 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned()) 876 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); 877 break; 878 879 case UnqualifiedId::IK_ConstructorName: 880 case UnqualifiedId::IK_DestructorName: 881 case UnqualifiedId::IK_ConversionFunctionId: 882 // Constructors and destructors don't have return types. Use 883 // "void" instead. Conversion operators will check their return 884 // types separately. 885 T = Context.VoidTy; 886 break; 887 } 888 889 if (T == Context.UndeducedAutoTy) { 890 int Error = -1; 891 892 switch (D.getContext()) { 893 case Declarator::KNRTypeListContext: 894 assert(0 && "K&R type lists aren't allowed in C++"); 895 break; 896 case Declarator::PrototypeContext: 897 Error = 0; // Function prototype 898 break; 899 case Declarator::MemberContext: 900 switch (cast<TagDecl>(CurContext)->getTagKind()) { 901 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break; 902 case TagDecl::TK_struct: Error = 1; /* Struct member */ break; 903 case TagDecl::TK_union: Error = 2; /* Union member */ break; 904 case TagDecl::TK_class: Error = 3; /* Class member */ break; 905 } 906 break; 907 case Declarator::CXXCatchContext: 908 Error = 4; // Exception declaration 909 break; 910 case Declarator::TemplateParamContext: 911 Error = 5; // Template parameter 912 break; 913 case Declarator::BlockLiteralContext: 914 Error = 6; // Block literal 915 break; 916 case Declarator::FileContext: 917 case Declarator::BlockContext: 918 case Declarator::ForContext: 919 case Declarator::ConditionContext: 920 case Declarator::TypeNameContext: 921 break; 922 } 923 924 if (Error != -1) { 925 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) 926 << Error; 927 T = Context.IntTy; 928 D.setInvalidType(true); 929 } 930 } 931 932 // The name we're declaring, if any. 933 DeclarationName Name; 934 if (D.getIdentifier()) 935 Name = D.getIdentifier(); 936 937 // Walk the DeclTypeInfo, building the recursive type as we go. 938 // DeclTypeInfos are ordered from the identifier out, which is 939 // opposite of what we want :). 940 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 941 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); 942 switch (DeclType.Kind) { 943 default: assert(0 && "Unknown decltype!"); 944 case DeclaratorChunk::BlockPointer: 945 // If blocks are disabled, emit an error. 946 if (!LangOpts.Blocks) 947 Diag(DeclType.Loc, diag::err_blocks_disable); 948 949 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), 950 Name); 951 break; 952 case DeclaratorChunk::Pointer: 953 // Verify that we're not building a pointer to pointer to function with 954 // exception specification. 955 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 956 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 957 D.setInvalidType(true); 958 // Build the type anyway. 959 } 960 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) { 961 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>(); 962 T = Context.getObjCObjectPointerType(T, 963 (ObjCProtocolDecl **)OIT->qual_begin(), 964 OIT->getNumProtocols()); 965 break; 966 } 967 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); 968 break; 969 case DeclaratorChunk::Reference: { 970 Qualifiers Quals; 971 if (DeclType.Ref.HasRestrict) Quals.addRestrict(); 972 973 // Verify that we're not building a reference to pointer to function with 974 // exception specification. 975 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 976 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 977 D.setInvalidType(true); 978 // Build the type anyway. 979 } 980 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals, 981 DeclType.Loc, Name); 982 break; 983 } 984 case DeclaratorChunk::Array: { 985 // Verify that we're not building an array of pointers to function with 986 // exception specification. 987 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 988 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 989 D.setInvalidType(true); 990 // Build the type anyway. 991 } 992 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 993 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 994 ArrayType::ArraySizeModifier ASM; 995 if (ATI.isStar) 996 ASM = ArrayType::Star; 997 else if (ATI.hasStatic) 998 ASM = ArrayType::Static; 999 else 1000 ASM = ArrayType::Normal; 1001 if (ASM == ArrayType::Star && 1002 D.getContext() != Declarator::PrototypeContext) { 1003 // FIXME: This check isn't quite right: it allows star in prototypes 1004 // for function definitions, and disallows some edge cases detailed 1005 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html 1006 Diag(DeclType.Loc, diag::err_array_star_outside_prototype); 1007 ASM = ArrayType::Normal; 1008 D.setInvalidType(true); 1009 } 1010 T = BuildArrayType(T, ASM, ArraySize, 1011 Qualifiers::fromCVRMask(ATI.TypeQuals), 1012 SourceRange(DeclType.Loc, DeclType.EndLoc), Name); 1013 break; 1014 } 1015 case DeclaratorChunk::Function: { 1016 // If the function declarator has a prototype (i.e. it is not () and 1017 // does not have a K&R-style identifier list), then the arguments are part 1018 // of the type, otherwise the argument list is (). 1019 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 1020 1021 // C99 6.7.5.3p1: The return type may not be a function or array type. 1022 if (T->isArrayType() || T->isFunctionType()) { 1023 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; 1024 T = Context.IntTy; 1025 D.setInvalidType(true); 1026 } 1027 1028 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { 1029 // C++ [dcl.fct]p6: 1030 // Types shall not be defined in return or parameter types. 1031 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); 1032 if (Tag->isDefinition()) 1033 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) 1034 << Context.getTypeDeclType(Tag); 1035 } 1036 1037 // Exception specs are not allowed in typedefs. Complain, but add it 1038 // anyway. 1039 if (FTI.hasExceptionSpec && 1040 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 1041 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); 1042 1043 if (FTI.NumArgs == 0) { 1044 if (getLangOptions().CPlusPlus) { 1045 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the 1046 // function takes no arguments. 1047 llvm::SmallVector<QualType, 4> Exceptions; 1048 Exceptions.reserve(FTI.NumExceptions); 1049 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { 1050 // FIXME: Preserve type source info. 1051 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); 1052 // Check that the type is valid for an exception spec, and drop it 1053 // if not. 1054 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) 1055 Exceptions.push_back(ET); 1056 } 1057 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, 1058 FTI.hasExceptionSpec, 1059 FTI.hasAnyExceptionSpec, 1060 Exceptions.size(), Exceptions.data()); 1061 } else if (FTI.isVariadic) { 1062 // We allow a zero-parameter variadic function in C if the 1063 // function is marked with the "overloadable" 1064 // attribute. Scan for this attribute now. 1065 bool Overloadable = false; 1066 for (const AttributeList *Attrs = D.getAttributes(); 1067 Attrs; Attrs = Attrs->getNext()) { 1068 if (Attrs->getKind() == AttributeList::AT_overloadable) { 1069 Overloadable = true; 1070 break; 1071 } 1072 } 1073 1074 if (!Overloadable) 1075 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); 1076 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); 1077 } else { 1078 // Simple void foo(), where the incoming T is the result type. 1079 T = Context.getFunctionNoProtoType(T); 1080 } 1081 } else if (FTI.ArgInfo[0].Param == 0) { 1082 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. 1083 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 1084 D.setInvalidType(true); 1085 } else { 1086 // Otherwise, we have a function with an argument list that is 1087 // potentially variadic. 1088 llvm::SmallVector<QualType, 16> ArgTys; 1089 1090 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 1091 ParmVarDecl *Param = 1092 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); 1093 QualType ArgTy = Param->getType(); 1094 assert(!ArgTy.isNull() && "Couldn't parse type?"); 1095 1096 // Adjust the parameter type. 1097 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); 1098 1099 // Look for 'void'. void is allowed only as a single argument to a 1100 // function with no other parameters (C99 6.7.5.3p10). We record 1101 // int(void) as a FunctionProtoType with an empty argument list. 1102 if (ArgTy->isVoidType()) { 1103 // If this is something like 'float(int, void)', reject it. 'void' 1104 // is an incomplete type (C99 6.2.5p19) and function decls cannot 1105 // have arguments of incomplete type. 1106 if (FTI.NumArgs != 1 || FTI.isVariadic) { 1107 Diag(DeclType.Loc, diag::err_void_only_param); 1108 ArgTy = Context.IntTy; 1109 Param->setType(ArgTy); 1110 } else if (FTI.ArgInfo[i].Ident) { 1111 // Reject, but continue to parse 'int(void abc)'. 1112 Diag(FTI.ArgInfo[i].IdentLoc, 1113 diag::err_param_with_void_type); 1114 ArgTy = Context.IntTy; 1115 Param->setType(ArgTy); 1116 } else { 1117 // Reject, but continue to parse 'float(const void)'. 1118 if (ArgTy.hasQualifiers()) 1119 Diag(DeclType.Loc, diag::err_void_param_qualified); 1120 1121 // Do not add 'void' to the ArgTys list. 1122 break; 1123 } 1124 } else if (!FTI.hasPrototype) { 1125 if (ArgTy->isPromotableIntegerType()) { 1126 ArgTy = Context.getPromotedIntegerType(ArgTy); 1127 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { 1128 if (BTy->getKind() == BuiltinType::Float) 1129 ArgTy = Context.DoubleTy; 1130 } 1131 } 1132 1133 ArgTys.push_back(ArgTy); 1134 } 1135 1136 llvm::SmallVector<QualType, 4> Exceptions; 1137 Exceptions.reserve(FTI.NumExceptions); 1138 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { 1139 // FIXME: Preserve type source info. 1140 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); 1141 // Check that the type is valid for an exception spec, and drop it if 1142 // not. 1143 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) 1144 Exceptions.push_back(ET); 1145 } 1146 1147 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), 1148 FTI.isVariadic, FTI.TypeQuals, 1149 FTI.hasExceptionSpec, 1150 FTI.hasAnyExceptionSpec, 1151 Exceptions.size(), Exceptions.data()); 1152 } 1153 break; 1154 } 1155 case DeclaratorChunk::MemberPointer: 1156 // Verify that we're not building a pointer to pointer to function with 1157 // exception specification. 1158 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 1159 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 1160 D.setInvalidType(true); 1161 // Build the type anyway. 1162 } 1163 // The scope spec must refer to a class, or be dependent. 1164 QualType ClsType; 1165 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) { 1166 NestedNameSpecifier *NNS 1167 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); 1168 assert(NNS->getAsType() && "Nested-name-specifier must name a type"); 1169 ClsType = QualType(NNS->getAsType(), 0); 1170 } else if (CXXRecordDecl *RD 1171 = dyn_cast_or_null<CXXRecordDecl>( 1172 computeDeclContext(DeclType.Mem.Scope()))) { 1173 ClsType = Context.getTagDeclType(RD); 1174 } else { 1175 Diag(DeclType.Mem.Scope().getBeginLoc(), 1176 diag::err_illegal_decl_mempointer_in_nonclass) 1177 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") 1178 << DeclType.Mem.Scope().getRange(); 1179 D.setInvalidType(true); 1180 } 1181 1182 if (!ClsType.isNull()) 1183 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, 1184 DeclType.Loc, D.getIdentifier()); 1185 if (T.isNull()) { 1186 T = Context.IntTy; 1187 D.setInvalidType(true); 1188 } 1189 break; 1190 } 1191 1192 if (T.isNull()) { 1193 D.setInvalidType(true); 1194 T = Context.IntTy; 1195 } 1196 1197 // See if there are any attributes on this declarator chunk. 1198 if (const AttributeList *AL = DeclType.getAttrs()) 1199 ProcessTypeAttributeList(T, AL); 1200 } 1201 1202 if (getLangOptions().CPlusPlus && T->isFunctionType()) { 1203 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); 1204 assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); 1205 1206 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type 1207 // for a nonstatic member function, the function type to which a pointer 1208 // to member refers, or the top-level function type of a function typedef 1209 // declaration. 1210 if (FnTy->getTypeQuals() != 0 && 1211 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && 1212 ((D.getContext() != Declarator::MemberContext && 1213 (!D.getCXXScopeSpec().isSet() || 1214 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true) 1215 ->isRecord())) || 1216 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { 1217 if (D.isFunctionDeclarator()) 1218 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); 1219 else 1220 Diag(D.getIdentifierLoc(), 1221 diag::err_invalid_qualified_typedef_function_type_use); 1222 1223 // Strip the cv-quals from the type. 1224 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), 1225 FnTy->getNumArgs(), FnTy->isVariadic(), 0); 1226 } 1227 } 1228 1229 // If there were any type attributes applied to the decl itself (not the 1230 // type, apply the type attribute to the type!) 1231 if (const AttributeList *Attrs = D.getAttributes()) 1232 ProcessTypeAttributeList(T, Attrs); 1233 1234 if (DInfo) { 1235 if (D.isInvalidType()) 1236 *DInfo = 0; 1237 else 1238 *DInfo = GetDeclaratorInfoForDeclarator(D, T); 1239 } 1240 1241 return T; 1242} 1243 1244namespace { 1245 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { 1246 const DeclSpec &DS; 1247 1248 public: 1249 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {} 1250 1251 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1252 Visit(TL.getUnqualifiedLoc()); 1253 } 1254 void VisitTypedefTypeLoc(TypedefTypeLoc TL) { 1255 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1256 } 1257 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 1258 TL.setNameLoc(DS.getTypeSpecTypeLoc()); 1259 1260 if (DS.getProtocolQualifiers()) { 1261 assert(TL.getNumProtocols() > 0); 1262 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); 1263 TL.setLAngleLoc(DS.getProtocolLAngleLoc()); 1264 TL.setRAngleLoc(DS.getSourceRange().getEnd()); 1265 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) 1266 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); 1267 } else { 1268 assert(TL.getNumProtocols() == 0); 1269 TL.setLAngleLoc(SourceLocation()); 1270 TL.setRAngleLoc(SourceLocation()); 1271 } 1272 } 1273 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1274 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); 1275 1276 TL.setStarLoc(SourceLocation()); 1277 1278 if (DS.getProtocolQualifiers()) { 1279 assert(TL.getNumProtocols() > 0); 1280 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); 1281 TL.setHasProtocolsAsWritten(true); 1282 TL.setLAngleLoc(DS.getProtocolLAngleLoc()); 1283 TL.setRAngleLoc(DS.getSourceRange().getEnd()); 1284 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) 1285 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); 1286 1287 } else { 1288 assert(TL.getNumProtocols() == 0); 1289 TL.setHasProtocolsAsWritten(false); 1290 TL.setLAngleLoc(SourceLocation()); 1291 TL.setRAngleLoc(SourceLocation()); 1292 } 1293 1294 // This might not have been written with an inner type. 1295 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 1296 TL.setHasBaseTypeAsWritten(false); 1297 TL.getBaseTypeLoc().initialize(SourceLocation()); 1298 } else { 1299 TL.setHasBaseTypeAsWritten(true); 1300 Visit(TL.getBaseTypeLoc()); 1301 } 1302 } 1303 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { 1304 DeclaratorInfo *DInfo = 0; 1305 Sema::GetTypeFromParser(DS.getTypeRep(), &DInfo); 1306 1307 // If we got no declarator info from previous Sema routines, 1308 // just fill with the typespec loc. 1309 if (!DInfo) { 1310 TL.initialize(DS.getTypeSpecTypeLoc()); 1311 return; 1312 } 1313 1314 TemplateSpecializationTypeLoc OldTL = 1315 cast<TemplateSpecializationTypeLoc>(DInfo->getTypeLoc()); 1316 TL.copy(OldTL); 1317 } 1318 void VisitTypeLoc(TypeLoc TL) { 1319 // FIXME: add other typespec types and change this to an assert. 1320 TL.initialize(DS.getTypeSpecTypeLoc()); 1321 } 1322 }; 1323 1324 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { 1325 const DeclaratorChunk &Chunk; 1326 1327 public: 1328 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {} 1329 1330 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1331 llvm::llvm_unreachable("qualified type locs not expected here!"); 1332 } 1333 1334 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 1335 assert(Chunk.Kind == DeclaratorChunk::BlockPointer); 1336 TL.setCaretLoc(Chunk.Loc); 1337 } 1338 void VisitPointerTypeLoc(PointerTypeLoc TL) { 1339 assert(Chunk.Kind == DeclaratorChunk::Pointer); 1340 TL.setStarLoc(Chunk.Loc); 1341 } 1342 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1343 assert(Chunk.Kind == DeclaratorChunk::Pointer); 1344 TL.setStarLoc(Chunk.Loc); 1345 TL.setHasBaseTypeAsWritten(true); 1346 TL.setHasProtocolsAsWritten(false); 1347 TL.setLAngleLoc(SourceLocation()); 1348 TL.setRAngleLoc(SourceLocation()); 1349 } 1350 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 1351 assert(Chunk.Kind == DeclaratorChunk::MemberPointer); 1352 TL.setStarLoc(Chunk.Loc); 1353 // FIXME: nested name specifier 1354 } 1355 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 1356 assert(Chunk.Kind == DeclaratorChunk::Reference); 1357 // 'Amp' is misleading: this might have been originally 1358 /// spelled with AmpAmp. 1359 TL.setAmpLoc(Chunk.Loc); 1360 } 1361 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 1362 assert(Chunk.Kind == DeclaratorChunk::Reference); 1363 assert(!Chunk.Ref.LValueRef); 1364 TL.setAmpAmpLoc(Chunk.Loc); 1365 } 1366 void VisitArrayTypeLoc(ArrayTypeLoc TL) { 1367 assert(Chunk.Kind == DeclaratorChunk::Array); 1368 TL.setLBracketLoc(Chunk.Loc); 1369 TL.setRBracketLoc(Chunk.EndLoc); 1370 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); 1371 } 1372 void VisitFunctionTypeLoc(FunctionTypeLoc TL) { 1373 assert(Chunk.Kind == DeclaratorChunk::Function); 1374 TL.setLParenLoc(Chunk.Loc); 1375 TL.setRParenLoc(Chunk.EndLoc); 1376 1377 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; 1378 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { 1379 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); 1380 TL.setArg(tpi++, Param); 1381 } 1382 // FIXME: exception specs 1383 } 1384 1385 void VisitTypeLoc(TypeLoc TL) { 1386 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!"); 1387 } 1388 }; 1389} 1390 1391/// \brief Create and instantiate a DeclaratorInfo with type source information. 1392/// 1393/// \param T QualType referring to the type as written in source code. 1394DeclaratorInfo * 1395Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) { 1396 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T); 1397 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc(); 1398 1399 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 1400 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL); 1401 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); 1402 } 1403 1404 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL); 1405 1406 return DInfo; 1407} 1408 1409/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo. 1410QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) { 1411 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser 1412 // and Sema during declaration parsing. Try deallocating/caching them when 1413 // it's appropriate, instead of allocating them and keeping them around. 1414 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); 1415 new (LocT) LocInfoType(T, DInfo); 1416 assert(LocT->getTypeClass() != T->getTypeClass() && 1417 "LocInfoType's TypeClass conflicts with an existing Type class"); 1418 return QualType(LocT, 0); 1419} 1420 1421void LocInfoType::getAsStringInternal(std::string &Str, 1422 const PrintingPolicy &Policy) const { 1423 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" 1424 " was used directly instead of getting the QualType through" 1425 " GetTypeFromParser"); 1426} 1427 1428/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition 1429/// declarator 1430QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { 1431 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); 1432 QualType T = MDecl->getResultType(); 1433 llvm::SmallVector<QualType, 16> ArgTys; 1434 1435 // Add the first two invisible argument types for self and _cmd. 1436 if (MDecl->isInstanceMethod()) { 1437 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); 1438 selfTy = Context.getPointerType(selfTy); 1439 ArgTys.push_back(selfTy); 1440 } else 1441 ArgTys.push_back(Context.getObjCIdType()); 1442 ArgTys.push_back(Context.getObjCSelType()); 1443 1444 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), 1445 E = MDecl->param_end(); PI != E; ++PI) { 1446 QualType ArgTy = (*PI)->getType(); 1447 assert(!ArgTy.isNull() && "Couldn't parse type?"); 1448 ArgTy = adjustParameterType(ArgTy); 1449 ArgTys.push_back(ArgTy); 1450 } 1451 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 1452 MDecl->isVariadic(), 0); 1453 return T; 1454} 1455 1456/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that 1457/// may be similar (C++ 4.4), replaces T1 and T2 with the type that 1458/// they point to and return true. If T1 and T2 aren't pointer types 1459/// or pointer-to-member types, or if they are not similar at this 1460/// level, returns false and leaves T1 and T2 unchanged. Top-level 1461/// qualifiers on T1 and T2 are ignored. This function will typically 1462/// be called in a loop that successively "unwraps" pointer and 1463/// pointer-to-member types to compare them at each level. 1464bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { 1465 const PointerType *T1PtrType = T1->getAs<PointerType>(), 1466 *T2PtrType = T2->getAs<PointerType>(); 1467 if (T1PtrType && T2PtrType) { 1468 T1 = T1PtrType->getPointeeType(); 1469 T2 = T2PtrType->getPointeeType(); 1470 return true; 1471 } 1472 1473 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), 1474 *T2MPType = T2->getAs<MemberPointerType>(); 1475 if (T1MPType && T2MPType && 1476 Context.getCanonicalType(T1MPType->getClass()) == 1477 Context.getCanonicalType(T2MPType->getClass())) { 1478 T1 = T1MPType->getPointeeType(); 1479 T2 = T2MPType->getPointeeType(); 1480 return true; 1481 } 1482 return false; 1483} 1484 1485Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 1486 // C99 6.7.6: Type names have no identifier. This is already validated by 1487 // the parser. 1488 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 1489 1490 DeclaratorInfo *DInfo = 0; 1491 TagDecl *OwnedTag = 0; 1492 QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag); 1493 if (D.isInvalidType()) 1494 return true; 1495 1496 if (getLangOptions().CPlusPlus) { 1497 // Check that there are no default arguments (C++ only). 1498 CheckExtraCXXDefaultArguments(D); 1499 1500 // C++0x [dcl.type]p3: 1501 // A type-specifier-seq shall not define a class or enumeration 1502 // unless it appears in the type-id of an alias-declaration 1503 // (7.1.3). 1504 if (OwnedTag && OwnedTag->isDefinition()) 1505 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) 1506 << Context.getTypeDeclType(OwnedTag); 1507 } 1508 1509 if (DInfo) 1510 T = CreateLocInfoType(T, DInfo); 1511 1512 return T.getAsOpaquePtr(); 1513} 1514 1515 1516 1517//===----------------------------------------------------------------------===// 1518// Type Attribute Processing 1519//===----------------------------------------------------------------------===// 1520 1521/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 1522/// specified type. The attribute contains 1 argument, the id of the address 1523/// space for the type. 1524static void HandleAddressSpaceTypeAttribute(QualType &Type, 1525 const AttributeList &Attr, Sema &S){ 1526 1527 // If this type is already address space qualified, reject it. 1528 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 1529 // for two or more different address spaces." 1530 if (Type.getAddressSpace()) { 1531 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); 1532 return; 1533 } 1534 1535 // Check the attribute arguments. 1536 if (Attr.getNumArgs() != 1) { 1537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1538 return; 1539 } 1540 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); 1541 llvm::APSInt addrSpace(32); 1542 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { 1543 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) 1544 << ASArgExpr->getSourceRange(); 1545 return; 1546 } 1547 1548 // Bounds checking. 1549 if (addrSpace.isSigned()) { 1550 if (addrSpace.isNegative()) { 1551 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) 1552 << ASArgExpr->getSourceRange(); 1553 return; 1554 } 1555 addrSpace.setIsSigned(false); 1556 } 1557 llvm::APSInt max(addrSpace.getBitWidth()); 1558 max = Qualifiers::MaxAddressSpace; 1559 if (addrSpace > max) { 1560 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) 1561 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); 1562 return; 1563 } 1564 1565 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 1566 Type = S.Context.getAddrSpaceQualType(Type, ASIdx); 1567} 1568 1569/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the 1570/// specified type. The attribute contains 1 argument, weak or strong. 1571static void HandleObjCGCTypeAttribute(QualType &Type, 1572 const AttributeList &Attr, Sema &S) { 1573 if (Type.getObjCGCAttr() != Qualifiers::GCNone) { 1574 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); 1575 return; 1576 } 1577 1578 // Check the attribute arguments. 1579 if (!Attr.getParameterName()) { 1580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1581 << "objc_gc" << 1; 1582 return; 1583 } 1584 Qualifiers::GC GCAttr; 1585 if (Attr.getNumArgs() != 0) { 1586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1587 return; 1588 } 1589 if (Attr.getParameterName()->isStr("weak")) 1590 GCAttr = Qualifiers::Weak; 1591 else if (Attr.getParameterName()->isStr("strong")) 1592 GCAttr = Qualifiers::Strong; 1593 else { 1594 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1595 << "objc_gc" << Attr.getParameterName(); 1596 return; 1597 } 1598 1599 Type = S.Context.getObjCGCQualType(Type, GCAttr); 1600} 1601 1602/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the 1603/// specified type. The attribute contains 0 arguments. 1604static void HandleNoReturnTypeAttribute(QualType &Type, 1605 const AttributeList &Attr, Sema &S) { 1606 if (Attr.getNumArgs() != 0) 1607 return; 1608 1609 // We only apply this to a pointer to function or a pointer to block. 1610 if (!Type->isFunctionPointerType() 1611 && !Type->isBlockPointerType() 1612 && !Type->isFunctionType()) 1613 return; 1614 1615 Type = S.Context.getNoReturnType(Type); 1616} 1617 1618void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { 1619 // Scan through and apply attributes to this type where it makes sense. Some 1620 // attributes (such as __address_space__, __vector_size__, etc) apply to the 1621 // type, but others can be present in the type specifiers even though they 1622 // apply to the decl. Here we apply type attributes and ignore the rest. 1623 for (; AL; AL = AL->getNext()) { 1624 // If this is an attribute we can handle, do so now, otherwise, add it to 1625 // the LeftOverAttrs list for rechaining. 1626 switch (AL->getKind()) { 1627 default: break; 1628 case AttributeList::AT_address_space: 1629 HandleAddressSpaceTypeAttribute(Result, *AL, *this); 1630 break; 1631 case AttributeList::AT_objc_gc: 1632 HandleObjCGCTypeAttribute(Result, *AL, *this); 1633 break; 1634 case AttributeList::AT_noreturn: 1635 HandleNoReturnTypeAttribute(Result, *AL, *this); 1636 break; 1637 } 1638 } 1639} 1640 1641/// @brief Ensure that the type T is a complete type. 1642/// 1643/// This routine checks whether the type @p T is complete in any 1644/// context where a complete type is required. If @p T is a complete 1645/// type, returns false. If @p T is a class template specialization, 1646/// this routine then attempts to perform class template 1647/// instantiation. If instantiation fails, or if @p T is incomplete 1648/// and cannot be completed, issues the diagnostic @p diag (giving it 1649/// the type @p T) and returns true. 1650/// 1651/// @param Loc The location in the source that the incomplete type 1652/// diagnostic should refer to. 1653/// 1654/// @param T The type that this routine is examining for completeness. 1655/// 1656/// @param PD The partial diagnostic that will be printed out if T is not a 1657/// complete type. 1658/// 1659/// @returns @c true if @p T is incomplete and a diagnostic was emitted, 1660/// @c false otherwise. 1661bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, 1662 const PartialDiagnostic &PD, 1663 std::pair<SourceLocation, 1664 PartialDiagnostic> Note) { 1665 unsigned diag = PD.getDiagID(); 1666 1667 // FIXME: Add this assertion to make sure we always get instantiation points. 1668 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); 1669 // FIXME: Add this assertion to help us flush out problems with 1670 // checking for dependent types and type-dependent expressions. 1671 // 1672 // assert(!T->isDependentType() && 1673 // "Can't ask whether a dependent type is complete"); 1674 1675 // If we have a complete type, we're done. 1676 if (!T->isIncompleteType()) 1677 return false; 1678 1679 // If we have a class template specialization or a class member of a 1680 // class template specialization, try to instantiate it. 1681 if (const RecordType *Record = T->getAs<RecordType>()) { 1682 if (ClassTemplateSpecializationDecl *ClassTemplateSpec 1683 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { 1684 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) 1685 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, 1686 TSK_ImplicitInstantiation, 1687 /*Complain=*/diag != 0); 1688 } else if (CXXRecordDecl *Rec 1689 = dyn_cast<CXXRecordDecl>(Record->getDecl())) { 1690 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { 1691 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); 1692 assert(MSInfo && "Missing member specialization information?"); 1693 // This record was instantiated from a class within a template. 1694 if (MSInfo->getTemplateSpecializationKind() 1695 != TSK_ExplicitSpecialization) 1696 return InstantiateClass(Loc, Rec, Pattern, 1697 getTemplateInstantiationArgs(Rec), 1698 TSK_ImplicitInstantiation, 1699 /*Complain=*/diag != 0); 1700 } 1701 } 1702 } 1703 1704 if (diag == 0) 1705 return true; 1706 1707 // We have an incomplete type. Produce a diagnostic. 1708 Diag(Loc, PD) << T; 1709 1710 // If we have a note, produce it. 1711 if (!Note.first.isInvalid()) 1712 Diag(Note.first, Note.second); 1713 1714 // If the type was a forward declaration of a class/struct/union 1715 // type, produce 1716 const TagType *Tag = 0; 1717 if (const RecordType *Record = T->getAs<RecordType>()) 1718 Tag = Record; 1719 else if (const EnumType *Enum = T->getAs<EnumType>()) 1720 Tag = Enum; 1721 1722 if (Tag && !Tag->getDecl()->isInvalidDecl()) 1723 Diag(Tag->getDecl()->getLocation(), 1724 Tag->isBeingDefined() ? diag::note_type_being_defined 1725 : diag::note_forward_declaration) 1726 << QualType(Tag, 0); 1727 1728 return true; 1729} 1730 1731/// \brief Retrieve a version of the type 'T' that is qualified by the 1732/// nested-name-specifier contained in SS. 1733QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { 1734 if (!SS.isSet() || SS.isInvalid() || T.isNull()) 1735 return T; 1736 1737 NestedNameSpecifier *NNS 1738 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 1739 return Context.getQualifiedNameType(NNS, T); 1740} 1741 1742QualType Sema::BuildTypeofExprType(Expr *E) { 1743 return Context.getTypeOfExprType(E); 1744} 1745 1746QualType Sema::BuildDecltypeType(Expr *E) { 1747 if (E->getType() == Context.OverloadTy) { 1748 Diag(E->getLocStart(), 1749 diag::err_cannot_determine_declared_type_of_overloaded_function); 1750 return QualType(); 1751 } 1752 return Context.getDecltypeType(E); 1753} 1754