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