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