SemaType.cpp revision 194613
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/DeclObjC.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/AST/Expr.h" 19#include "clang/Parse/DeclSpec.h" 20using namespace clang; 21 22/// \brief Perform adjustment on the parameter type of a function. 23/// 24/// This routine adjusts the given parameter type @p T to the actual 25/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 26/// C++ [dcl.fct]p3). The adjusted parameter type is returned. 27QualType Sema::adjustParameterType(QualType T) { 28 // C99 6.7.5.3p7: 29 if (T->isArrayType()) { 30 // C99 6.7.5.3p7: 31 // A declaration of a parameter as "array of type" shall be 32 // adjusted to "qualified pointer to type", where the type 33 // qualifiers (if any) are those specified within the [ and ] of 34 // the array type derivation. 35 return Context.getArrayDecayedType(T); 36 } else if (T->isFunctionType()) 37 // C99 6.7.5.3p8: 38 // A declaration of a parameter as "function returning type" 39 // shall be adjusted to "pointer to function returning type", as 40 // in 6.3.2.1. 41 return Context.getPointerType(T); 42 43 return T; 44} 45 46/// \brief Convert the specified declspec to the appropriate type 47/// object. 48/// \param DS the declaration specifiers 49/// \param DeclLoc The location of the declarator identifier or invalid if none. 50/// \returns The type described by the declaration specifiers. This function 51/// never returns null. 52QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS, 53 SourceLocation DeclLoc, 54 bool &isInvalid) { 55 // FIXME: Should move the logic from DeclSpec::Finish to here for validity 56 // checking. 57 QualType Result; 58 59 switch (DS.getTypeSpecType()) { 60 case DeclSpec::TST_void: 61 Result = Context.VoidTy; 62 break; 63 case DeclSpec::TST_char: 64 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 65 Result = Context.CharTy; 66 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) 67 Result = Context.SignedCharTy; 68 else { 69 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 70 "Unknown TSS value"); 71 Result = Context.UnsignedCharTy; 72 } 73 break; 74 case DeclSpec::TST_wchar: 75 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 76 Result = Context.WCharTy; 77 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { 78 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 79 << DS.getSpecifierName(DS.getTypeSpecType()); 80 Result = Context.getSignedWCharType(); 81 } else { 82 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 83 "Unknown TSS value"); 84 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 85 << DS.getSpecifierName(DS.getTypeSpecType()); 86 Result = Context.getUnsignedWCharType(); 87 } 88 break; 89 case DeclSpec::TST_unspecified: 90 // "<proto1,proto2>" is an objc qualified ID with a missing id. 91 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 92 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 93 DS.getNumProtocolQualifiers()); 94 break; 95 } 96 97 // Unspecified typespec defaults to int in C90. However, the C90 grammar 98 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, 99 // type-qualifier, or storage-class-specifier. If not, emit an extwarn. 100 // Note that the one exception to this is function definitions, which are 101 // allowed to be completely missing a declspec. This is handled in the 102 // parser already though by it pretending to have seen an 'int' in this 103 // case. 104 if (getLangOptions().ImplicitInt) { 105 // In C89 mode, we only warn if there is a completely missing declspec 106 // when one is not allowed. 107 if (DS.isEmpty()) { 108 if (DeclLoc.isInvalid()) 109 DeclLoc = DS.getSourceRange().getBegin(); 110 Diag(DeclLoc, diag::ext_missing_declspec) 111 << DS.getSourceRange() 112 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), 113 "int"); 114 } 115 } else if (!DS.hasTypeSpecifier()) { 116 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: 117 // "At least one type specifier shall be given in the declaration 118 // specifiers in each declaration, and in the specifier-qualifier list in 119 // each struct declaration and type name." 120 // FIXME: Does Microsoft really have the implicit int extension in C++? 121 if (DeclLoc.isInvalid()) 122 DeclLoc = DS.getSourceRange().getBegin(); 123 124 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) 125 Diag(DeclLoc, diag::err_missing_type_specifier) 126 << DS.getSourceRange(); 127 else 128 Diag(DeclLoc, diag::ext_missing_type_specifier) 129 << DS.getSourceRange(); 130 131 // FIXME: If we could guarantee that the result would be well-formed, it 132 // would be useful to have a code insertion hint here. However, after 133 // emitting this warning/error, we often emit other errors. 134 } 135 136 // FALL THROUGH. 137 case DeclSpec::TST_int: { 138 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { 139 switch (DS.getTypeSpecWidth()) { 140 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; 141 case DeclSpec::TSW_short: Result = Context.ShortTy; break; 142 case DeclSpec::TSW_long: Result = Context.LongTy; break; 143 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; 144 } 145 } else { 146 switch (DS.getTypeSpecWidth()) { 147 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; 148 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; 149 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; 150 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; 151 } 152 } 153 break; 154 } 155 case DeclSpec::TST_float: Result = Context.FloatTy; break; 156 case DeclSpec::TST_double: 157 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) 158 Result = Context.LongDoubleTy; 159 else 160 Result = Context.DoubleTy; 161 break; 162 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool 163 case DeclSpec::TST_decimal32: // _Decimal32 164 case DeclSpec::TST_decimal64: // _Decimal64 165 case DeclSpec::TST_decimal128: // _Decimal128 166 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); 167 Result = Context.IntTy; 168 isInvalid = true; 169 break; 170 case DeclSpec::TST_class: 171 case DeclSpec::TST_enum: 172 case DeclSpec::TST_union: 173 case DeclSpec::TST_struct: { 174 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 175 assert(D && "Didn't get a decl for a class/enum/union/struct?"); 176 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 177 DS.getTypeSpecSign() == 0 && 178 "Can't handle qualifiers on typedef names yet!"); 179 // TypeQuals handled by caller. 180 Result = Context.getTypeDeclType(cast<TypeDecl>(D)); 181 182 if (D->isInvalidDecl()) 183 isInvalid = true; 184 break; 185 } 186 case DeclSpec::TST_typename: { 187 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 188 DS.getTypeSpecSign() == 0 && 189 "Can't handle qualifiers on typedef names yet!"); 190 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 191 192 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 193 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have 194 // this "hack" for now... 195 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType()) 196 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(), 197 (ObjCProtocolDecl**)PQ, 198 DS.getNumProtocolQualifiers()); 199 else if (Result == Context.getObjCIdType()) 200 // id<protocol-list> 201 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 202 DS.getNumProtocolQualifiers()); 203 else if (Result == Context.getObjCClassType()) { 204 if (DeclLoc.isInvalid()) 205 DeclLoc = DS.getSourceRange().getBegin(); 206 // Class<protocol-list> 207 Diag(DeclLoc, diag::err_qualified_class_unsupported) 208 << DS.getSourceRange(); 209 } else { 210 if (DeclLoc.isInvalid()) 211 DeclLoc = DS.getSourceRange().getBegin(); 212 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) 213 << DS.getSourceRange(); 214 isInvalid = true; 215 } 216 } 217 218 // If this is a reference to an invalid typedef, propagate the invalidity. 219 if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) 220 if (TDT->getDecl()->isInvalidDecl()) 221 isInvalid = true; 222 223 // TypeQuals handled by caller. 224 break; 225 } 226 case DeclSpec::TST_typeofType: 227 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 228 assert(!Result.isNull() && "Didn't get a type for typeof?"); 229 // TypeQuals handled by caller. 230 Result = Context.getTypeOfType(Result); 231 break; 232 case DeclSpec::TST_typeofExpr: { 233 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 234 assert(E && "Didn't get an expression for typeof?"); 235 // TypeQuals handled by caller. 236 Result = Context.getTypeOfExprType(E); 237 break; 238 } 239 case DeclSpec::TST_error: 240 Result = Context.IntTy; 241 isInvalid = true; 242 break; 243 } 244 245 // Handle complex types. 246 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { 247 if (getLangOptions().Freestanding) 248 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); 249 Result = Context.getComplexType(Result); 250 } 251 252 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && 253 "FIXME: imaginary types not supported yet!"); 254 255 // See if there are any attributes on the declspec that apply to the type (as 256 // opposed to the decl). 257 if (const AttributeList *AL = DS.getAttributes()) 258 ProcessTypeAttributeList(Result, AL); 259 260 // Apply const/volatile/restrict qualifiers to T. 261 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 262 263 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 264 // or incomplete types shall not be restrict-qualified." C++ also allows 265 // restrict-qualified references. 266 if (TypeQuals & QualType::Restrict) { 267 if (Result->isPointerType() || Result->isReferenceType()) { 268 QualType EltTy = Result->isPointerType() ? 269 Result->getAsPointerType()->getPointeeType() : 270 Result->getAsReferenceType()->getPointeeType(); 271 272 // If we have a pointer or reference, the pointee must have an object 273 // incomplete type. 274 if (!EltTy->isIncompleteOrObjectType()) { 275 Diag(DS.getRestrictSpecLoc(), 276 diag::err_typecheck_invalid_restrict_invalid_pointee) 277 << EltTy << DS.getSourceRange(); 278 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 279 } 280 } else { 281 Diag(DS.getRestrictSpecLoc(), 282 diag::err_typecheck_invalid_restrict_not_pointer) 283 << Result << DS.getSourceRange(); 284 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 285 } 286 } 287 288 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 289 // of a function type includes any type qualifiers, the behavior is 290 // undefined." 291 if (Result->isFunctionType() && TypeQuals) { 292 // Get some location to point at, either the C or V location. 293 SourceLocation Loc; 294 if (TypeQuals & QualType::Const) 295 Loc = DS.getConstSpecLoc(); 296 else { 297 assert((TypeQuals & QualType::Volatile) && 298 "Has CV quals but not C or V?"); 299 Loc = DS.getVolatileSpecLoc(); 300 } 301 Diag(Loc, diag::warn_typecheck_function_qualifiers) 302 << Result << DS.getSourceRange(); 303 } 304 305 // C++ [dcl.ref]p1: 306 // Cv-qualified references are ill-formed except when the 307 // cv-qualifiers are introduced through the use of a typedef 308 // (7.1.3) or of a template type argument (14.3), in which 309 // case the cv-qualifiers are ignored. 310 // FIXME: Shouldn't we be checking SCS_typedef here? 311 if (DS.getTypeSpecType() == DeclSpec::TST_typename && 312 TypeQuals && Result->isReferenceType()) { 313 TypeQuals &= ~QualType::Const; 314 TypeQuals &= ~QualType::Volatile; 315 } 316 317 Result = Result.getQualifiedType(TypeQuals); 318 } 319 return Result; 320} 321 322static std::string getPrintableNameForEntity(DeclarationName Entity) { 323 if (Entity) 324 return Entity.getAsString(); 325 326 return "type name"; 327} 328 329/// \brief Build a pointer type. 330/// 331/// \param T The type to which we'll be building a pointer. 332/// 333/// \param Quals The cvr-qualifiers to be applied to the pointer type. 334/// 335/// \param Loc The location of the entity whose type involves this 336/// pointer type or, if there is no such entity, the location of the 337/// type that will have pointer type. 338/// 339/// \param Entity The name of the entity that involves the pointer 340/// type, if known. 341/// 342/// \returns A suitable pointer type, if there are no 343/// errors. Otherwise, returns a NULL type. 344QualType Sema::BuildPointerType(QualType T, unsigned Quals, 345 SourceLocation Loc, DeclarationName Entity) { 346 if (T->isReferenceType()) { 347 // C++ 8.3.2p4: There shall be no ... pointers to references ... 348 Diag(Loc, diag::err_illegal_decl_pointer_to_reference) 349 << getPrintableNameForEntity(Entity); 350 return QualType(); 351 } 352 353 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 354 // object or incomplete types shall not be restrict-qualified." 355 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { 356 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 357 << T; 358 Quals &= ~QualType::Restrict; 359 } 360 361 // Build the pointer type. 362 return Context.getPointerType(T).getQualifiedType(Quals); 363} 364 365/// \brief Build a reference type. 366/// 367/// \param T The type to which we'll be building a reference. 368/// 369/// \param Quals The cvr-qualifiers to be applied to the reference type. 370/// 371/// \param Loc The location of the entity whose type involves this 372/// reference type or, if there is no such entity, the location of the 373/// type that will have reference type. 374/// 375/// \param Entity The name of the entity that involves the reference 376/// type, if known. 377/// 378/// \returns A suitable reference type, if there are no 379/// errors. Otherwise, returns a NULL type. 380QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals, 381 SourceLocation Loc, DeclarationName Entity) { 382 if (LValueRef) { 383 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) { 384 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a 385 // reference to a type T, and attempt to create the type "lvalue 386 // reference to cv TD" creates the type "lvalue reference to T". 387 // We use the qualifiers (restrict or none) of the original reference, 388 // not the new ones. This is consistent with GCC. 389 return Context.getLValueReferenceType(R->getPointeeType()). 390 getQualifiedType(T.getCVRQualifiers()); 391 } 392 } 393 if (T->isReferenceType()) { 394 // C++ [dcl.ref]p4: There shall be no references to references. 395 // 396 // According to C++ DR 106, references to references are only 397 // diagnosed when they are written directly (e.g., "int & &"), 398 // but not when they happen via a typedef: 399 // 400 // typedef int& intref; 401 // typedef intref& intref2; 402 // 403 // Parser::ParserDeclaratorInternal diagnoses the case where 404 // references are written directly; here, we handle the 405 // collapsing of references-to-references as described in C++ 406 // DR 106 and amended by C++ DR 540. 407 return T; 408 } 409 410 // C++ [dcl.ref]p1: 411 // A declarator that specifies the type ���reference to cv void��� 412 // is ill-formed. 413 if (T->isVoidType()) { 414 Diag(Loc, diag::err_reference_to_void); 415 return QualType(); 416 } 417 418 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 419 // object or incomplete types shall not be restrict-qualified." 420 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { 421 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 422 << T; 423 Quals &= ~QualType::Restrict; 424 } 425 426 // C++ [dcl.ref]p1: 427 // [...] Cv-qualified references are ill-formed except when the 428 // cv-qualifiers are introduced through the use of a typedef 429 // (7.1.3) or of a template type argument (14.3), in which case 430 // the cv-qualifiers are ignored. 431 // 432 // We diagnose extraneous cv-qualifiers for the non-typedef, 433 // non-template type argument case within the parser. Here, we just 434 // ignore any extraneous cv-qualifiers. 435 Quals &= ~QualType::Const; 436 Quals &= ~QualType::Volatile; 437 438 // Handle restrict on references. 439 if (LValueRef) 440 return Context.getLValueReferenceType(T).getQualifiedType(Quals); 441 return Context.getRValueReferenceType(T).getQualifiedType(Quals); 442} 443 444/// \brief Build an array type. 445/// 446/// \param T The type of each element in the array. 447/// 448/// \param ASM C99 array size modifier (e.g., '*', 'static'). 449/// 450/// \param ArraySize Expression describing the size of the array. 451/// 452/// \param Quals The cvr-qualifiers to be applied to the array's 453/// element type. 454/// 455/// \param Loc The location of the entity whose type involves this 456/// array type or, if there is no such entity, the location of the 457/// type that will have array type. 458/// 459/// \param Entity The name of the entity that involves the array 460/// type, if known. 461/// 462/// \returns A suitable array type, if there are no errors. Otherwise, 463/// returns a NULL type. 464QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, 465 Expr *ArraySize, unsigned Quals, 466 SourceLocation Loc, DeclarationName Entity) { 467 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 468 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 469 if (RequireCompleteType(Loc, T, 470 diag::err_illegal_decl_array_incomplete_type)) 471 return QualType(); 472 473 if (T->isFunctionType()) { 474 Diag(Loc, diag::err_illegal_decl_array_of_functions) 475 << getPrintableNameForEntity(Entity); 476 return QualType(); 477 } 478 479 // C++ 8.3.2p4: There shall be no ... arrays of references ... 480 if (T->isReferenceType()) { 481 Diag(Loc, diag::err_illegal_decl_array_of_references) 482 << getPrintableNameForEntity(Entity); 483 return QualType(); 484 } 485 486 if (const RecordType *EltTy = T->getAsRecordType()) { 487 // If the element type is a struct or union that contains a variadic 488 // array, accept it as a GNU extension: C99 6.7.2.1p2. 489 if (EltTy->getDecl()->hasFlexibleArrayMember()) 490 Diag(Loc, diag::ext_flexible_array_in_array) << T; 491 } else if (T->isObjCInterfaceType()) { 492 Diag(Loc, diag::err_objc_array_of_interfaces) << T; 493 return QualType(); 494 } 495 496 // C99 6.7.5.2p1: The size expression shall have integer type. 497 if (ArraySize && !ArraySize->isTypeDependent() && 498 !ArraySize->getType()->isIntegerType()) { 499 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) 500 << ArraySize->getType() << ArraySize->getSourceRange(); 501 ArraySize->Destroy(Context); 502 return QualType(); 503 } 504 llvm::APSInt ConstVal(32); 505 if (!ArraySize) { 506 if (ASM == ArrayType::Star) 507 T = Context.getVariableArrayType(T, 0, ASM, Quals); 508 else 509 T = Context.getIncompleteArrayType(T, ASM, Quals); 510 } else if (ArraySize->isValueDependent()) { 511 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals); 512 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 513 (!T->isDependentType() && !T->isConstantSizeType())) { 514 // Per C99, a variable array is an array with either a non-constant 515 // size or an element type that has a non-constant-size 516 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals); 517 } else { 518 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 519 // have a value greater than zero. 520 if (ConstVal.isSigned()) { 521 if (ConstVal.isNegative()) { 522 Diag(ArraySize->getLocStart(), 523 diag::err_typecheck_negative_array_size) 524 << ArraySize->getSourceRange(); 525 return QualType(); 526 } else if (ConstVal == 0) { 527 // GCC accepts zero sized static arrays. 528 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) 529 << ArraySize->getSourceRange(); 530 } 531 } 532 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); 533 } 534 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 535 if (!getLangOptions().C99) { 536 if (ArraySize && !ArraySize->isTypeDependent() && 537 !ArraySize->isValueDependent() && 538 !ArraySize->isIntegerConstantExpr(Context)) 539 Diag(Loc, diag::ext_vla); 540 else if (ASM != ArrayType::Normal || Quals != 0) 541 Diag(Loc, diag::ext_c99_array_usage); 542 } 543 544 return T; 545} 546 547/// \brief Build an ext-vector type. 548/// 549/// Run the required checks for the extended vector type. 550QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, 551 SourceLocation AttrLoc) { 552 553 Expr *Arg = (Expr *)ArraySize.get(); 554 555 // unlike gcc's vector_size attribute, we do not allow vectors to be defined 556 // in conjunction with complex types (pointers, arrays, functions, etc.). 557 if (!T->isDependentType() && 558 !T->isIntegerType() && !T->isRealFloatingType()) { 559 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; 560 return QualType(); 561 } 562 563 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 564 llvm::APSInt vecSize(32); 565 if (!Arg->isIntegerConstantExpr(vecSize, Context)) { 566 Diag(AttrLoc, diag::err_attribute_argument_not_int) 567 << "ext_vector_type" << Arg->getSourceRange(); 568 return QualType(); 569 } 570 571 // unlike gcc's vector_size attribute, the size is specified as the 572 // number of elements, not the number of bytes. 573 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); 574 575 if (vectorSize == 0) { 576 Diag(AttrLoc, diag::err_attribute_zero_size) 577 << Arg->getSourceRange(); 578 return QualType(); 579 } 580 581 if (!T->isDependentType()) 582 return Context.getExtVectorType(T, vectorSize); 583 } 584 585 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), 586 AttrLoc); 587} 588 589/// \brief Build a function type. 590/// 591/// This routine checks the function type according to C++ rules and 592/// under the assumption that the result type and parameter types have 593/// just been instantiated from a template. It therefore duplicates 594/// some of the behavior of GetTypeForDeclarator, but in a much 595/// simpler form that is only suitable for this narrow use case. 596/// 597/// \param T The return type of the function. 598/// 599/// \param ParamTypes The parameter types of the function. This array 600/// will be modified to account for adjustments to the types of the 601/// function parameters. 602/// 603/// \param NumParamTypes The number of parameter types in ParamTypes. 604/// 605/// \param Variadic Whether this is a variadic function type. 606/// 607/// \param Quals The cvr-qualifiers to be applied to the function type. 608/// 609/// \param Loc The location of the entity whose type involves this 610/// function type or, if there is no such entity, the location of the 611/// type that will have function type. 612/// 613/// \param Entity The name of the entity that involves the function 614/// type, if known. 615/// 616/// \returns A suitable function type, if there are no 617/// errors. Otherwise, returns a NULL type. 618QualType Sema::BuildFunctionType(QualType T, 619 QualType *ParamTypes, 620 unsigned NumParamTypes, 621 bool Variadic, unsigned Quals, 622 SourceLocation Loc, DeclarationName Entity) { 623 if (T->isArrayType() || T->isFunctionType()) { 624 Diag(Loc, diag::err_func_returning_array_function) << T; 625 return QualType(); 626 } 627 628 bool Invalid = false; 629 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { 630 QualType ParamType = adjustParameterType(ParamTypes[Idx]); 631 if (ParamType->isVoidType()) { 632 Diag(Loc, diag::err_param_with_void_type); 633 Invalid = true; 634 } 635 636 ParamTypes[Idx] = ParamType; 637 } 638 639 if (Invalid) 640 return QualType(); 641 642 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, 643 Quals); 644} 645 646/// \brief Build a member pointer type \c T Class::*. 647/// 648/// \param T the type to which the member pointer refers. 649/// \param Class the class type into which the member pointer points. 650/// \param Quals Qualifiers applied to the member pointer type 651/// \param Loc the location where this type begins 652/// \param Entity the name of the entity that will have this member pointer type 653/// 654/// \returns a member pointer type, if successful, or a NULL type if there was 655/// an error. 656QualType Sema::BuildMemberPointerType(QualType T, QualType Class, 657 unsigned Quals, SourceLocation Loc, 658 DeclarationName Entity) { 659 // Verify that we're not building a pointer to pointer to function with 660 // exception specification. 661 if (CheckDistantExceptionSpec(T)) { 662 Diag(Loc, diag::err_distant_exception_spec); 663 664 // FIXME: If we're doing this as part of template instantiation, 665 // we should return immediately. 666 667 // Build the type anyway, but use the canonical type so that the 668 // exception specifiers are stripped off. 669 T = Context.getCanonicalType(T); 670 } 671 672 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member 673 // with reference type, or "cv void." 674 if (T->isReferenceType()) { 675 Diag(Loc, diag::err_illegal_decl_pointer_to_reference) 676 << (Entity? Entity.getAsString() : "type name"); 677 return QualType(); 678 } 679 680 if (T->isVoidType()) { 681 Diag(Loc, diag::err_illegal_decl_mempointer_to_void) 682 << (Entity? Entity.getAsString() : "type name"); 683 return QualType(); 684 } 685 686 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 687 // object or incomplete types shall not be restrict-qualified." 688 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) { 689 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 690 << T; 691 692 // FIXME: If we're doing this as part of template instantiation, 693 // we should return immediately. 694 Quals &= ~QualType::Restrict; 695 } 696 697 if (!Class->isDependentType() && !Class->isRecordType()) { 698 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; 699 return QualType(); 700 } 701 702 return Context.getMemberPointerType(T, Class.getTypePtr()) 703 .getQualifiedType(Quals); 704} 705 706/// \brief Build a block pointer type. 707/// 708/// \param T The type to which we'll be building a block pointer. 709/// 710/// \param Quals The cvr-qualifiers to be applied to the block pointer type. 711/// 712/// \param Loc The location of the entity whose type involves this 713/// block pointer type or, if there is no such entity, the location of the 714/// type that will have block pointer type. 715/// 716/// \param Entity The name of the entity that involves the block pointer 717/// type, if known. 718/// 719/// \returns A suitable block pointer type, if there are no 720/// errors. Otherwise, returns a NULL type. 721QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals, 722 SourceLocation Loc, 723 DeclarationName Entity) { 724 if (!T.getTypePtr()->isFunctionType()) { 725 Diag(Loc, diag::err_nonfunction_block_type); 726 return QualType(); 727 } 728 729 return Context.getBlockPointerType(T).getQualifiedType(Quals); 730} 731 732/// GetTypeForDeclarator - Convert the type for the specified 733/// declarator to Type instances. Skip the outermost Skip type 734/// objects. 735/// 736/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq 737/// owns the declaration of a type (e.g., the definition of a struct 738/// type), then *OwnedDecl will receive the owned declaration. 739QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip, 740 TagDecl **OwnedDecl) { 741 bool OmittedReturnType = false; 742 743 if (D.getContext() == Declarator::BlockLiteralContext 744 && Skip == 0 745 && !D.getDeclSpec().hasTypeSpecifier() 746 && (D.getNumTypeObjects() == 0 747 || (D.getNumTypeObjects() == 1 748 && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) 749 OmittedReturnType = true; 750 751 // long long is a C99 feature. 752 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && 753 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) 754 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); 755 756 // Determine the type of the declarator. Not all forms of declarator 757 // have a type. 758 QualType T; 759 switch (D.getKind()) { 760 case Declarator::DK_Abstract: 761 case Declarator::DK_Normal: 762 case Declarator::DK_Operator: { 763 const DeclSpec &DS = D.getDeclSpec(); 764 if (OmittedReturnType) { 765 // We default to a dependent type initially. Can be modified by 766 // the first return statement. 767 T = Context.DependentTy; 768 } else { 769 bool isInvalid = false; 770 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid); 771 if (isInvalid) 772 D.setInvalidType(true); 773 else if (OwnedDecl && DS.isTypeSpecOwned()) 774 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep()); 775 } 776 break; 777 } 778 779 case Declarator::DK_Constructor: 780 case Declarator::DK_Destructor: 781 case Declarator::DK_Conversion: 782 // Constructors and destructors don't have return types. Use 783 // "void" instead. Conversion operators will check their return 784 // types separately. 785 T = Context.VoidTy; 786 break; 787 } 788 789 // The name we're declaring, if any. 790 DeclarationName Name; 791 if (D.getIdentifier()) 792 Name = D.getIdentifier(); 793 794 // Walk the DeclTypeInfo, building the recursive type as we go. 795 // DeclTypeInfos are ordered from the identifier out, which is 796 // opposite of what we want :). 797 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { 798 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); 799 switch (DeclType.Kind) { 800 default: assert(0 && "Unknown decltype!"); 801 case DeclaratorChunk::BlockPointer: 802 // If blocks are disabled, emit an error. 803 if (!LangOpts.Blocks) 804 Diag(DeclType.Loc, diag::err_blocks_disable); 805 806 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), 807 Name); 808 break; 809 case DeclaratorChunk::Pointer: 810 // Verify that we're not building a pointer to pointer to function with 811 // exception specification. 812 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 813 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 814 D.setInvalidType(true); 815 // Build the type anyway. 816 } 817 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); 818 break; 819 case DeclaratorChunk::Reference: 820 // Verify that we're not building a reference to pointer to function with 821 // exception specification. 822 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 823 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 824 D.setInvalidType(true); 825 // Build the type anyway. 826 } 827 T = BuildReferenceType(T, DeclType.Ref.LValueRef, 828 DeclType.Ref.HasRestrict ? QualType::Restrict : 0, 829 DeclType.Loc, Name); 830 break; 831 case DeclaratorChunk::Array: { 832 // Verify that we're not building an array of pointers to function with 833 // exception specification. 834 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { 835 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); 836 D.setInvalidType(true); 837 // Build the type anyway. 838 } 839 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 840 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 841 ArrayType::ArraySizeModifier ASM; 842 if (ATI.isStar) 843 ASM = ArrayType::Star; 844 else if (ATI.hasStatic) 845 ASM = ArrayType::Static; 846 else 847 ASM = ArrayType::Normal; 848 if (ASM == ArrayType::Star && 849 D.getContext() != Declarator::PrototypeContext) { 850 // FIXME: This check isn't quite right: it allows star in prototypes 851 // for function definitions, and disallows some edge cases detailed 852 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html 853 Diag(DeclType.Loc, diag::err_array_star_outside_prototype); 854 ASM = ArrayType::Normal; 855 D.setInvalidType(true); 856 } 857 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name); 858 break; 859 } 860 case DeclaratorChunk::Function: { 861 // If the function declarator has a prototype (i.e. it is not () and 862 // does not have a K&R-style identifier list), then the arguments are part 863 // of the type, otherwise the argument list is (). 864 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 865 866 // C99 6.7.5.3p1: The return type may not be a function or array type. 867 if (T->isArrayType() || T->isFunctionType()) { 868 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; 869 T = Context.IntTy; 870 D.setInvalidType(true); 871 } 872 873 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { 874 // C++ [dcl.fct]p6: 875 // Types shall not be defined in return or parameter types. 876 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); 877 if (Tag->isDefinition()) 878 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) 879 << Context.getTypeDeclType(Tag); 880 } 881 882 // Exception specs are not allowed in typedefs. Complain, but add it 883 // anyway. 884 if (FTI.hasExceptionSpec && 885 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 886 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); 887 888 if (FTI.NumArgs == 0) { 889 if (getLangOptions().CPlusPlus) { 890 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the 891 // function takes no arguments. 892 llvm::SmallVector<QualType, 4> Exceptions; 893 Exceptions.reserve(FTI.NumExceptions); 894 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { 895 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty); 896 // Check that the type is valid for an exception spec, and drop it 897 // if not. 898 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) 899 Exceptions.push_back(ET); 900 } 901 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, 902 FTI.hasExceptionSpec, 903 FTI.hasAnyExceptionSpec, 904 Exceptions.size(), Exceptions.data()); 905 } else if (FTI.isVariadic) { 906 // We allow a zero-parameter variadic function in C if the 907 // function is marked with the "overloadable" 908 // attribute. Scan for this attribute now. 909 bool Overloadable = false; 910 for (const AttributeList *Attrs = D.getAttributes(); 911 Attrs; Attrs = Attrs->getNext()) { 912 if (Attrs->getKind() == AttributeList::AT_overloadable) { 913 Overloadable = true; 914 break; 915 } 916 } 917 918 if (!Overloadable) 919 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); 920 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0); 921 } else { 922 // Simple void foo(), where the incoming T is the result type. 923 T = Context.getFunctionNoProtoType(T); 924 } 925 } else if (FTI.ArgInfo[0].Param == 0) { 926 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. 927 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 928 } else { 929 // Otherwise, we have a function with an argument list that is 930 // potentially variadic. 931 llvm::SmallVector<QualType, 16> ArgTys; 932 933 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 934 ParmVarDecl *Param = 935 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); 936 QualType ArgTy = Param->getType(); 937 assert(!ArgTy.isNull() && "Couldn't parse type?"); 938 939 // Adjust the parameter type. 940 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); 941 942 // Look for 'void'. void is allowed only as a single argument to a 943 // function with no other parameters (C99 6.7.5.3p10). We record 944 // int(void) as a FunctionProtoType with an empty argument list. 945 if (ArgTy->isVoidType()) { 946 // If this is something like 'float(int, void)', reject it. 'void' 947 // is an incomplete type (C99 6.2.5p19) and function decls cannot 948 // have arguments of incomplete type. 949 if (FTI.NumArgs != 1 || FTI.isVariadic) { 950 Diag(DeclType.Loc, diag::err_void_only_param); 951 ArgTy = Context.IntTy; 952 Param->setType(ArgTy); 953 } else if (FTI.ArgInfo[i].Ident) { 954 // Reject, but continue to parse 'int(void abc)'. 955 Diag(FTI.ArgInfo[i].IdentLoc, 956 diag::err_param_with_void_type); 957 ArgTy = Context.IntTy; 958 Param->setType(ArgTy); 959 } else { 960 // Reject, but continue to parse 'float(const void)'. 961 if (ArgTy.getCVRQualifiers()) 962 Diag(DeclType.Loc, diag::err_void_param_qualified); 963 964 // Do not add 'void' to the ArgTys list. 965 break; 966 } 967 } else if (!FTI.hasPrototype) { 968 if (ArgTy->isPromotableIntegerType()) { 969 ArgTy = Context.IntTy; 970 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { 971 if (BTy->getKind() == BuiltinType::Float) 972 ArgTy = Context.DoubleTy; 973 } 974 } 975 976 ArgTys.push_back(ArgTy); 977 } 978 979 llvm::SmallVector<QualType, 4> Exceptions; 980 Exceptions.reserve(FTI.NumExceptions); 981 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { 982 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty); 983 // Check that the type is valid for an exception spec, and drop it if 984 // not. 985 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) 986 Exceptions.push_back(ET); 987 } 988 989 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), 990 FTI.isVariadic, FTI.TypeQuals, 991 FTI.hasExceptionSpec, 992 FTI.hasAnyExceptionSpec, 993 Exceptions.size(), Exceptions.data()); 994 } 995 break; 996 } 997 case DeclaratorChunk::MemberPointer: 998 // The scope spec must refer to a class, or be dependent. 999 QualType ClsType; 1000 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) { 1001 NestedNameSpecifier *NNS 1002 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); 1003 assert(NNS->getAsType() && "Nested-name-specifier must name a type"); 1004 ClsType = QualType(NNS->getAsType(), 0); 1005 } else if (CXXRecordDecl *RD 1006 = dyn_cast_or_null<CXXRecordDecl>( 1007 computeDeclContext(DeclType.Mem.Scope()))) { 1008 ClsType = Context.getTagDeclType(RD); 1009 } else { 1010 Diag(DeclType.Mem.Scope().getBeginLoc(), 1011 diag::err_illegal_decl_mempointer_in_nonclass) 1012 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") 1013 << DeclType.Mem.Scope().getRange(); 1014 D.setInvalidType(true); 1015 } 1016 1017 if (!ClsType.isNull()) 1018 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, 1019 DeclType.Loc, D.getIdentifier()); 1020 if (T.isNull()) { 1021 T = Context.IntTy; 1022 D.setInvalidType(true); 1023 } 1024 break; 1025 } 1026 1027 if (T.isNull()) { 1028 D.setInvalidType(true); 1029 T = Context.IntTy; 1030 } 1031 1032 // See if there are any attributes on this declarator chunk. 1033 if (const AttributeList *AL = DeclType.getAttrs()) 1034 ProcessTypeAttributeList(T, AL); 1035 } 1036 1037 if (getLangOptions().CPlusPlus && T->isFunctionType()) { 1038 const FunctionProtoType *FnTy = T->getAsFunctionProtoType(); 1039 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?"); 1040 1041 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type 1042 // for a nonstatic member function, the function type to which a pointer 1043 // to member refers, or the top-level function type of a function typedef 1044 // declaration. 1045 if (FnTy->getTypeQuals() != 0 && 1046 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && 1047 ((D.getContext() != Declarator::MemberContext && 1048 (!D.getCXXScopeSpec().isSet() || 1049 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) || 1050 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { 1051 if (D.isFunctionDeclarator()) 1052 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); 1053 else 1054 Diag(D.getIdentifierLoc(), 1055 diag::err_invalid_qualified_typedef_function_type_use); 1056 1057 // Strip the cv-quals from the type. 1058 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), 1059 FnTy->getNumArgs(), FnTy->isVariadic(), 0); 1060 } 1061 } 1062 1063 // If there were any type attributes applied to the decl itself (not the 1064 // type, apply the type attribute to the type!) 1065 if (const AttributeList *Attrs = D.getAttributes()) 1066 ProcessTypeAttributeList(T, Attrs); 1067 1068 return T; 1069} 1070 1071/// CheckSpecifiedExceptionType - Check if the given type is valid in an 1072/// exception specification. Incomplete types, or pointers to incomplete types 1073/// other than void are not allowed. 1074bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) { 1075 // FIXME: This may not correctly work with the fix for core issue 437, 1076 // where a class's own type is considered complete within its body. 1077 1078 // C++ 15.4p2: A type denoted in an exception-specification shall not denote 1079 // an incomplete type. 1080 if (T->isIncompleteType()) 1081 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) 1082 << Range << T << /*direct*/0; 1083 1084 // C++ 15.4p2: A type denoted in an exception-specification shall not denote 1085 // an incomplete type a pointer or reference to an incomplete type, other 1086 // than (cv) void*. 1087 int kind; 1088 if (const PointerType* IT = T->getAsPointerType()) { 1089 T = IT->getPointeeType(); 1090 kind = 1; 1091 } else if (const ReferenceType* IT = T->getAsReferenceType()) { 1092 T = IT->getPointeeType(); 1093 kind = 2; 1094 } else 1095 return false; 1096 1097 if (T->isIncompleteType() && !T->isVoidType()) 1098 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) 1099 << Range << T << /*indirect*/kind; 1100 1101 return false; 1102} 1103 1104/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer 1105/// to member to a function with an exception specification. This means that 1106/// it is invalid to add another level of indirection. 1107bool Sema::CheckDistantExceptionSpec(QualType T) { 1108 if (const PointerType *PT = T->getAsPointerType()) 1109 T = PT->getPointeeType(); 1110 else if (const MemberPointerType *PT = T->getAsMemberPointerType()) 1111 T = PT->getPointeeType(); 1112 else 1113 return false; 1114 1115 const FunctionProtoType *FnT = T->getAsFunctionProtoType(); 1116 if (!FnT) 1117 return false; 1118 1119 return FnT->hasExceptionSpec(); 1120} 1121 1122/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition 1123/// declarator 1124QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) { 1125 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>()); 1126 QualType T = MDecl->getResultType(); 1127 llvm::SmallVector<QualType, 16> ArgTys; 1128 1129 // Add the first two invisible argument types for self and _cmd. 1130 if (MDecl->isInstanceMethod()) { 1131 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); 1132 selfTy = Context.getPointerType(selfTy); 1133 ArgTys.push_back(selfTy); 1134 } else 1135 ArgTys.push_back(Context.getObjCIdType()); 1136 ArgTys.push_back(Context.getObjCSelType()); 1137 1138 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), 1139 E = MDecl->param_end(); PI != E; ++PI) { 1140 QualType ArgTy = (*PI)->getType(); 1141 assert(!ArgTy.isNull() && "Couldn't parse type?"); 1142 ArgTy = adjustParameterType(ArgTy); 1143 ArgTys.push_back(ArgTy); 1144 } 1145 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 1146 MDecl->isVariadic(), 0); 1147 return T; 1148} 1149 1150/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that 1151/// may be similar (C++ 4.4), replaces T1 and T2 with the type that 1152/// they point to and return true. If T1 and T2 aren't pointer types 1153/// or pointer-to-member types, or if they are not similar at this 1154/// level, returns false and leaves T1 and T2 unchanged. Top-level 1155/// qualifiers on T1 and T2 are ignored. This function will typically 1156/// be called in a loop that successively "unwraps" pointer and 1157/// pointer-to-member types to compare them at each level. 1158bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { 1159 const PointerType *T1PtrType = T1->getAsPointerType(), 1160 *T2PtrType = T2->getAsPointerType(); 1161 if (T1PtrType && T2PtrType) { 1162 T1 = T1PtrType->getPointeeType(); 1163 T2 = T2PtrType->getPointeeType(); 1164 return true; 1165 } 1166 1167 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(), 1168 *T2MPType = T2->getAsMemberPointerType(); 1169 if (T1MPType && T2MPType && 1170 Context.getCanonicalType(T1MPType->getClass()) == 1171 Context.getCanonicalType(T2MPType->getClass())) { 1172 T1 = T1MPType->getPointeeType(); 1173 T2 = T2MPType->getPointeeType(); 1174 return true; 1175 } 1176 return false; 1177} 1178 1179Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 1180 // C99 6.7.6: Type names have no identifier. This is already validated by 1181 // the parser. 1182 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 1183 1184 TagDecl *OwnedTag = 0; 1185 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag); 1186 if (D.isInvalidType()) 1187 return true; 1188 1189 if (getLangOptions().CPlusPlus) { 1190 // Check that there are no default arguments (C++ only). 1191 CheckExtraCXXDefaultArguments(D); 1192 1193 // C++0x [dcl.type]p3: 1194 // A type-specifier-seq shall not define a class or enumeration 1195 // unless it appears in the type-id of an alias-declaration 1196 // (7.1.3). 1197 if (OwnedTag && OwnedTag->isDefinition()) 1198 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) 1199 << Context.getTypeDeclType(OwnedTag); 1200 } 1201 1202 return T.getAsOpaquePtr(); 1203} 1204 1205 1206 1207//===----------------------------------------------------------------------===// 1208// Type Attribute Processing 1209//===----------------------------------------------------------------------===// 1210 1211/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 1212/// specified type. The attribute contains 1 argument, the id of the address 1213/// space for the type. 1214static void HandleAddressSpaceTypeAttribute(QualType &Type, 1215 const AttributeList &Attr, Sema &S){ 1216 // If this type is already address space qualified, reject it. 1217 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 1218 // for two or more different address spaces." 1219 if (Type.getAddressSpace()) { 1220 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); 1221 return; 1222 } 1223 1224 // Check the attribute arguments. 1225 if (Attr.getNumArgs() != 1) { 1226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1227 return; 1228 } 1229 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); 1230 llvm::APSInt addrSpace(32); 1231 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { 1232 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) 1233 << ASArgExpr->getSourceRange(); 1234 return; 1235 } 1236 1237 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 1238 Type = S.Context.getAddrSpaceQualType(Type, ASIdx); 1239} 1240 1241/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the 1242/// specified type. The attribute contains 1 argument, weak or strong. 1243static void HandleObjCGCTypeAttribute(QualType &Type, 1244 const AttributeList &Attr, Sema &S) { 1245 if (Type.getObjCGCAttr() != QualType::GCNone) { 1246 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); 1247 return; 1248 } 1249 1250 // Check the attribute arguments. 1251 if (!Attr.getParameterName()) { 1252 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) 1253 << "objc_gc" << 1; 1254 return; 1255 } 1256 QualType::GCAttrTypes GCAttr; 1257 if (Attr.getNumArgs() != 0) { 1258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 1259 return; 1260 } 1261 if (Attr.getParameterName()->isStr("weak")) 1262 GCAttr = QualType::Weak; 1263 else if (Attr.getParameterName()->isStr("strong")) 1264 GCAttr = QualType::Strong; 1265 else { 1266 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) 1267 << "objc_gc" << Attr.getParameterName(); 1268 return; 1269 } 1270 1271 Type = S.Context.getObjCGCQualType(Type, GCAttr); 1272} 1273 1274void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { 1275 // Scan through and apply attributes to this type where it makes sense. Some 1276 // attributes (such as __address_space__, __vector_size__, etc) apply to the 1277 // type, but others can be present in the type specifiers even though they 1278 // apply to the decl. Here we apply type attributes and ignore the rest. 1279 for (; AL; AL = AL->getNext()) { 1280 // If this is an attribute we can handle, do so now, otherwise, add it to 1281 // the LeftOverAttrs list for rechaining. 1282 switch (AL->getKind()) { 1283 default: break; 1284 case AttributeList::AT_address_space: 1285 HandleAddressSpaceTypeAttribute(Result, *AL, *this); 1286 break; 1287 case AttributeList::AT_objc_gc: 1288 HandleObjCGCTypeAttribute(Result, *AL, *this); 1289 break; 1290 } 1291 } 1292} 1293 1294/// @brief Ensure that the type T is a complete type. 1295/// 1296/// This routine checks whether the type @p T is complete in any 1297/// context where a complete type is required. If @p T is a complete 1298/// type, returns false. If @p T is a class template specialization, 1299/// this routine then attempts to perform class template 1300/// instantiation. If instantiation fails, or if @p T is incomplete 1301/// and cannot be completed, issues the diagnostic @p diag (giving it 1302/// the type @p T) and returns true. 1303/// 1304/// @param Loc The location in the source that the incomplete type 1305/// diagnostic should refer to. 1306/// 1307/// @param T The type that this routine is examining for completeness. 1308/// 1309/// @param diag The diagnostic value (e.g., 1310/// @c diag::err_typecheck_decl_incomplete_type) that will be used 1311/// for the error message if @p T is incomplete. 1312/// 1313/// @param Range1 An optional range in the source code that will be a 1314/// part of the "incomplete type" error message. 1315/// 1316/// @param Range2 An optional range in the source code that will be a 1317/// part of the "incomplete type" error message. 1318/// 1319/// @param PrintType If non-NULL, the type that should be printed 1320/// instead of @p T. This parameter should be used when the type that 1321/// we're checking for incompleteness isn't the type that should be 1322/// displayed to the user, e.g., when T is a type and PrintType is a 1323/// pointer to T. 1324/// 1325/// @returns @c true if @p T is incomplete and a diagnostic was emitted, 1326/// @c false otherwise. 1327bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag, 1328 SourceRange Range1, SourceRange Range2, 1329 QualType PrintType) { 1330 // FIXME: Add this assertion to help us flush out problems with 1331 // checking for dependent types and type-dependent expressions. 1332 // 1333 // assert(!T->isDependentType() && 1334 // "Can't ask whether a dependent type is complete"); 1335 1336 // If we have a complete type, we're done. 1337 if (!T->isIncompleteType()) 1338 return false; 1339 1340 // If we have a class template specialization or a class member of a 1341 // class template specialization, try to instantiate it. 1342 if (const RecordType *Record = T->getAsRecordType()) { 1343 if (ClassTemplateSpecializationDecl *ClassTemplateSpec 1344 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { 1345 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) { 1346 // Update the class template specialization's location to 1347 // refer to the point of instantiation. 1348 if (Loc.isValid()) 1349 ClassTemplateSpec->setLocation(Loc); 1350 return InstantiateClassTemplateSpecialization(ClassTemplateSpec, 1351 /*ExplicitInstantiation=*/false); 1352 } 1353 } else if (CXXRecordDecl *Rec 1354 = dyn_cast<CXXRecordDecl>(Record->getDecl())) { 1355 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { 1356 // Find the class template specialization that surrounds this 1357 // member class. 1358 ClassTemplateSpecializationDecl *Spec = 0; 1359 for (DeclContext *Parent = Rec->getDeclContext(); 1360 Parent && !Spec; Parent = Parent->getParent()) 1361 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent); 1362 assert(Spec && "Not a member of a class template specialization?"); 1363 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(), 1364 /*ExplicitInstantiation=*/false); 1365 } 1366 } 1367 } 1368 1369 if (PrintType.isNull()) 1370 PrintType = T; 1371 1372 // We have an incomplete type. Produce a diagnostic. 1373 Diag(Loc, diag) << PrintType << Range1 << Range2; 1374 1375 // If the type was a forward declaration of a class/struct/union 1376 // type, produce 1377 const TagType *Tag = 0; 1378 if (const RecordType *Record = T->getAsRecordType()) 1379 Tag = Record; 1380 else if (const EnumType *Enum = T->getAsEnumType()) 1381 Tag = Enum; 1382 1383 if (Tag && !Tag->getDecl()->isInvalidDecl()) 1384 Diag(Tag->getDecl()->getLocation(), 1385 Tag->isBeingDefined() ? diag::note_type_being_defined 1386 : diag::note_forward_declaration) 1387 << QualType(Tag, 0); 1388 1389 return true; 1390} 1391 1392/// \brief Retrieve a version of the type 'T' that is qualified by the 1393/// nested-name-specifier contained in SS. 1394QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { 1395 if (!SS.isSet() || SS.isInvalid() || T.isNull()) 1396 return T; 1397 1398 NestedNameSpecifier *NNS 1399 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 1400 return Context.getQualifiedNameType(NNS, T); 1401} 1402