SemaType.cpp revision 0bfe54fdc83b7b4e37c40e652d86d15aa89885b2
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/Expr.h" 18#include "clang/Parse/DeclSpec.h" 19using namespace clang; 20 21/// \brief Convert the specified declspec to the appropriate type 22/// object. 23/// \param DS the declaration specifiers 24/// \returns The type described by the declaration specifiers, or NULL 25/// if there was an error. 26QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) { 27 // FIXME: Should move the logic from DeclSpec::Finish to here for validity 28 // checking. 29 QualType Result; 30 31 switch (DS.getTypeSpecType()) { 32 default: assert(0 && "Unknown TypeSpecType!"); 33 case DeclSpec::TST_void: 34 Result = Context.VoidTy; 35 break; 36 case DeclSpec::TST_char: 37 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 38 Result = Context.CharTy; 39 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) 40 Result = Context.SignedCharTy; 41 else { 42 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 43 "Unknown TSS value"); 44 Result = Context.UnsignedCharTy; 45 } 46 break; 47 case DeclSpec::TST_wchar: 48 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 49 Result = Context.WCharTy; 50 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { 51 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 52 << DS.getSpecifierName(DS.getTypeSpecType()); 53 Result = Context.getSignedWCharType(); 54 } else { 55 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 56 "Unknown TSS value"); 57 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) 58 << DS.getSpecifierName(DS.getTypeSpecType()); 59 Result = Context.getUnsignedWCharType(); 60 } 61 break; 62 case DeclSpec::TST_unspecified: 63 // "<proto1,proto2>" is an objc qualified ID with a missing id. 64 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 65 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 66 DS.getNumProtocolQualifiers()); 67 break; 68 } 69 70 // Unspecified typespec defaults to int in C90. However, the C90 grammar 71 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, 72 // type-qualifier, or storage-class-specifier. If not, emit an extwarn. 73 // Note that the one exception to this is function definitions, which are 74 // allowed to be completely missing a declspec. This is handled in the 75 // parser already though by it pretending to have seen an 'int' in this 76 // case. 77 if (getLangOptions().ImplicitInt) { 78 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier | 79 DeclSpec::PQ_TypeSpecifier | 80 DeclSpec::PQ_TypeQualifier)) == 0) 81 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec); 82 } else { 83 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: 84 // "At least one type specifier shall be given in the declaration 85 // specifiers in each declaration, and in the specifier-qualifier list in 86 // each struct declaration and type name." 87 // FIXME: this should be a hard error in C++ 88 if (!DS.hasTypeSpecifier()) 89 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier); 90 } 91 92 // FALL THROUGH. 93 case DeclSpec::TST_int: { 94 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { 95 switch (DS.getTypeSpecWidth()) { 96 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; 97 case DeclSpec::TSW_short: Result = Context.ShortTy; break; 98 case DeclSpec::TSW_long: Result = Context.LongTy; break; 99 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; 100 } 101 } else { 102 switch (DS.getTypeSpecWidth()) { 103 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; 104 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; 105 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; 106 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; 107 } 108 } 109 break; 110 } 111 case DeclSpec::TST_float: Result = Context.FloatTy; break; 112 case DeclSpec::TST_double: 113 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) 114 Result = Context.LongDoubleTy; 115 else 116 Result = Context.DoubleTy; 117 break; 118 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool 119 case DeclSpec::TST_decimal32: // _Decimal32 120 case DeclSpec::TST_decimal64: // _Decimal64 121 case DeclSpec::TST_decimal128: // _Decimal128 122 assert(0 && "FIXME: GNU decimal extensions not supported yet!"); 123 case DeclSpec::TST_class: 124 case DeclSpec::TST_enum: 125 case DeclSpec::TST_union: 126 case DeclSpec::TST_struct: { 127 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 128 assert(D && "Didn't get a decl for a class/enum/union/struct?"); 129 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 130 DS.getTypeSpecSign() == 0 && 131 "Can't handle qualifiers on typedef names yet!"); 132 // TypeQuals handled by caller. 133 Result = Context.getTypeDeclType(cast<TypeDecl>(D)); 134 break; 135 } 136 case DeclSpec::TST_typename: { 137 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 138 DS.getTypeSpecSign() == 0 && 139 "Can't handle qualifiers on typedef names yet!"); 140 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 141 142 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 143 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so 144 // we have this "hack" for now... 145 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType()) 146 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(), 147 (ObjCProtocolDecl**)PQ, 148 DS.getNumProtocolQualifiers()); 149 else if (Result == Context.getObjCIdType()) 150 // id<protocol-list> 151 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 152 DS.getNumProtocolQualifiers()); 153 } 154 // TypeQuals handled by caller. 155 break; 156 } 157 case DeclSpec::TST_typeofType: 158 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 159 assert(!Result.isNull() && "Didn't get a type for typeof?"); 160 // TypeQuals handled by caller. 161 Result = Context.getTypeOfType(Result); 162 break; 163 case DeclSpec::TST_typeofExpr: { 164 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 165 assert(E && "Didn't get an expression for typeof?"); 166 // TypeQuals handled by caller. 167 Result = Context.getTypeOfExpr(E); 168 break; 169 } 170 } 171 172 // Handle complex types. 173 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) 174 Result = Context.getComplexType(Result); 175 176 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && 177 "FIXME: imaginary types not supported yet!"); 178 179 // See if there are any attributes on the declspec that apply to the type (as 180 // opposed to the decl). 181 if (const AttributeList *AL = DS.getAttributes()) 182 ProcessTypeAttributeList(Result, AL); 183 184 // Apply const/volatile/restrict qualifiers to T. 185 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 186 187 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 188 // or incomplete types shall not be restrict-qualified." C++ also allows 189 // restrict-qualified references. 190 if (TypeQuals & QualType::Restrict) { 191 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) { 192 QualType EltTy = PT->getPointeeType(); 193 194 // If we have a pointer or reference, the pointee must have an object or 195 // incomplete type. 196 if (!EltTy->isIncompleteOrObjectType()) { 197 Diag(DS.getRestrictSpecLoc(), 198 diag::err_typecheck_invalid_restrict_invalid_pointee) 199 << EltTy << DS.getSourceRange(); 200 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 201 } 202 } else { 203 Diag(DS.getRestrictSpecLoc(), 204 diag::err_typecheck_invalid_restrict_not_pointer) 205 << Result << DS.getSourceRange(); 206 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 207 } 208 } 209 210 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 211 // of a function type includes any type qualifiers, the behavior is 212 // undefined." 213 if (Result->isFunctionType() && TypeQuals) { 214 // Get some location to point at, either the C or V location. 215 SourceLocation Loc; 216 if (TypeQuals & QualType::Const) 217 Loc = DS.getConstSpecLoc(); 218 else { 219 assert((TypeQuals & QualType::Volatile) && 220 "Has CV quals but not C or V?"); 221 Loc = DS.getVolatileSpecLoc(); 222 } 223 Diag(Loc, diag::warn_typecheck_function_qualifiers) 224 << Result << DS.getSourceRange(); 225 } 226 227 // C++ [dcl.ref]p1: 228 // Cv-qualified references are ill-formed except when the 229 // cv-qualifiers are introduced through the use of a typedef 230 // (7.1.3) or of a template type argument (14.3), in which 231 // case the cv-qualifiers are ignored. 232 // FIXME: Shouldn't we be checking SCS_typedef here? 233 if (DS.getTypeSpecType() == DeclSpec::TST_typename && 234 TypeQuals && Result->isReferenceType()) { 235 TypeQuals &= ~QualType::Const; 236 TypeQuals &= ~QualType::Volatile; 237 } 238 239 Result = Result.getQualifiedType(TypeQuals); 240 } 241 return Result; 242} 243 244/// GetTypeForDeclarator - Convert the type for the specified 245/// declarator to Type instances. Skip the outermost Skip type 246/// objects. 247QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) { 248 bool OmittedReturnType = false; 249 250 if (D.getContext() == Declarator::BlockLiteralContext 251 && Skip == 0 252 && !D.getDeclSpec().hasTypeSpecifier() 253 && (D.getNumTypeObjects() == 0 254 || (D.getNumTypeObjects() == 1 255 && D.getTypeObject(0).Kind == DeclaratorChunk::Function))) 256 OmittedReturnType = true; 257 258 // long long is a C99 feature. 259 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && 260 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) 261 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); 262 263 // Determine the type of the declarator. Not all forms of declarator 264 // have a type. 265 QualType T; 266 switch (D.getKind()) { 267 case Declarator::DK_Abstract: 268 case Declarator::DK_Normal: 269 case Declarator::DK_Operator: { 270 const DeclSpec& DS = D.getDeclSpec(); 271 if (OmittedReturnType) 272 // We default to a dependent type initially. Can be modified by 273 // the first return statement. 274 T = Context.DependentTy; 275 else 276 T = ConvertDeclSpecToType(DS); 277 break; 278 } 279 280 case Declarator::DK_Constructor: 281 case Declarator::DK_Destructor: 282 case Declarator::DK_Conversion: 283 // Constructors and destructors don't have return types. Use 284 // "void" instead. Conversion operators will check their return 285 // types separately. 286 T = Context.VoidTy; 287 break; 288 } 289 290 // Walk the DeclTypeInfo, building the recursive type as we go. 291 // DeclTypeInfos are ordered from the identifier out, which is 292 // opposite of what we want :). 293 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) { 294 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip); 295 switch (DeclType.Kind) { 296 default: assert(0 && "Unknown decltype!"); 297 case DeclaratorChunk::BlockPointer: 298 if (DeclType.Cls.TypeQuals) 299 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type); 300 if (!T.getTypePtr()->isFunctionType()) 301 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type); 302 else 303 T = Context.getBlockPointerType(T); 304 break; 305 case DeclaratorChunk::Pointer: 306 if (T->isReferenceType()) { 307 // C++ 8.3.2p4: There shall be no ... pointers to references ... 308 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) 309 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 310 D.setInvalidType(true); 311 T = Context.IntTy; 312 } 313 314 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 315 // object or incomplete types shall not be restrict-qualified." 316 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && 317 !T->isIncompleteOrObjectType()) { 318 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 319 << T; 320 DeclType.Ptr.TypeQuals &= ~QualType::Restrict; 321 } 322 323 // Apply the pointer typequals to the pointer object. 324 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); 325 break; 326 case DeclaratorChunk::Reference: { 327 // Whether we should suppress the creation of the reference. 328 bool SuppressReference = false; 329 if (T->isReferenceType()) { 330 // C++ [dcl.ref]p4: There shall be no references to references. 331 // 332 // According to C++ DR 106, references to references are only 333 // diagnosed when they are written directly (e.g., "int & &"), 334 // but not when they happen via a typedef: 335 // 336 // typedef int& intref; 337 // typedef intref& intref2; 338 // 339 // Parser::ParserDeclaratorInternal diagnoses the case where 340 // references are written directly; here, we handle the 341 // collapsing of references-to-references as described in C++ 342 // DR 106 and amended by C++ DR 540. 343 SuppressReference = true; 344 } 345 346 // C++ [dcl.ref]p1: 347 // A declarator that specifies the type “reference to cv void” 348 // is ill-formed. 349 if (T->isVoidType()) { 350 Diag(DeclType.Loc, diag::err_reference_to_void); 351 D.setInvalidType(true); 352 T = Context.IntTy; 353 } 354 355 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 356 // object or incomplete types shall not be restrict-qualified." 357 if (DeclType.Ref.HasRestrict && 358 !T->isIncompleteOrObjectType()) { 359 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 360 << T; 361 DeclType.Ref.HasRestrict = false; 362 } 363 364 if (!SuppressReference) 365 T = Context.getReferenceType(T); 366 367 // Handle restrict on references. 368 if (DeclType.Ref.HasRestrict) 369 T.addRestrict(); 370 break; 371 } 372 case DeclaratorChunk::Array: { 373 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 374 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 375 ArrayType::ArraySizeModifier ASM; 376 if (ATI.isStar) 377 ASM = ArrayType::Star; 378 else if (ATI.hasStatic) 379 ASM = ArrayType::Static; 380 else 381 ASM = ArrayType::Normal; 382 383 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 384 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 385 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T, 386 diag::err_illegal_decl_array_incomplete_type)) { 387 T = Context.IntTy; 388 D.setInvalidType(true); 389 } else if (T->isFunctionType()) { 390 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions) 391 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 392 T = Context.getPointerType(T); 393 D.setInvalidType(true); 394 } else if (const ReferenceType *RT = T->getAsReferenceType()) { 395 // C++ 8.3.2p4: There shall be no ... arrays of references ... 396 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references) 397 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 398 T = RT->getPointeeType(); 399 D.setInvalidType(true); 400 } else if (const RecordType *EltTy = T->getAsRecordType()) { 401 // If the element type is a struct or union that contains a variadic 402 // array, accept it as a GNU extension: C99 6.7.2.1p2. 403 if (EltTy->getDecl()->hasFlexibleArrayMember()) 404 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T; 405 } else if (T->isObjCInterfaceType()) { 406 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T; 407 } 408 409 // C99 6.7.5.2p1: The size expression shall have integer type. 410 if (ArraySize && !ArraySize->getType()->isIntegerType()) { 411 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) 412 << ArraySize->getType() << ArraySize->getSourceRange(); 413 D.setInvalidType(true); 414 ArraySize->Destroy(Context); 415 ATI.NumElts = ArraySize = 0; 416 } 417 llvm::APSInt ConstVal(32); 418 if (!ArraySize) { 419 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); 420 } else if (ArraySize->isValueDependent()) { 421 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals); 422 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 423 !T->isConstantSizeType()) { 424 // Per C99, a variable array is an array with either a non-constant 425 // size or an element type that has a non-constant-size 426 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); 427 } else { 428 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 429 // have a value greater than zero. 430 if (ConstVal.isSigned()) { 431 if (ConstVal.isNegative()) { 432 Diag(ArraySize->getLocStart(), 433 diag::err_typecheck_negative_array_size) 434 << ArraySize->getSourceRange(); 435 D.setInvalidType(true); 436 } else if (ConstVal == 0) { 437 // GCC accepts zero sized static arrays. 438 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) 439 << ArraySize->getSourceRange(); 440 } 441 } 442 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); 443 } 444 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 445 if (!getLangOptions().C99) { 446 if (ArraySize && !ArraySize->isValueDependent() && 447 !ArraySize->isIntegerConstantExpr(Context)) 448 Diag(D.getIdentifierLoc(), diag::ext_vla); 449 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0) 450 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage); 451 } 452 break; 453 } 454 case DeclaratorChunk::Function: { 455 // If the function declarator has a prototype (i.e. it is not () and 456 // does not have a K&R-style identifier list), then the arguments are part 457 // of the type, otherwise the argument list is (). 458 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 459 460 // C99 6.7.5.3p1: The return type may not be a function or array type. 461 if (T->isArrayType() || T->isFunctionType()) { 462 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T; 463 T = Context.IntTy; 464 D.setInvalidType(true); 465 } 466 467 if (FTI.NumArgs == 0) { 468 if (getLangOptions().CPlusPlus) { 469 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the 470 // function takes no arguments. 471 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals); 472 } else { 473 // Simple void foo(), where the incoming T is the result type. 474 T = Context.getFunctionTypeNoProto(T); 475 } 476 } else if (FTI.ArgInfo[0].Param == 0) { 477 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. 478 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 479 } else { 480 // Otherwise, we have a function with an argument list that is 481 // potentially variadic. 482 llvm::SmallVector<QualType, 16> ArgTys; 483 484 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 485 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; 486 QualType ArgTy = Param->getType(); 487 assert(!ArgTy.isNull() && "Couldn't parse type?"); 488 // 489 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 490 // This matches the conversion that is done in 491 // Sema::ActOnParamDeclarator(). Without this conversion, the 492 // argument type in the function prototype *will not* match the 493 // type in ParmVarDecl (which makes the code generator unhappy). 494 // 495 // FIXME: We still apparently need the conversion in 496 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since 497 // it should be driving off the type being created here. 498 // 499 // FIXME: If a source translation tool needs to see the original type, 500 // then we need to consider storing both types somewhere... 501 // 502 if (ArgTy->isArrayType()) { 503 ArgTy = Context.getArrayDecayedType(ArgTy); 504 } else if (ArgTy->isFunctionType()) 505 ArgTy = Context.getPointerType(ArgTy); 506 507 // Look for 'void'. void is allowed only as a single argument to a 508 // function with no other parameters (C99 6.7.5.3p10). We record 509 // int(void) as a FunctionTypeProto with an empty argument list. 510 else if (ArgTy->isVoidType()) { 511 // If this is something like 'float(int, void)', reject it. 'void' 512 // is an incomplete type (C99 6.2.5p19) and function decls cannot 513 // have arguments of incomplete type. 514 if (FTI.NumArgs != 1 || FTI.isVariadic) { 515 Diag(DeclType.Loc, diag::err_void_only_param); 516 ArgTy = Context.IntTy; 517 Param->setType(ArgTy); 518 } else if (FTI.ArgInfo[i].Ident) { 519 // Reject, but continue to parse 'int(void abc)'. 520 Diag(FTI.ArgInfo[i].IdentLoc, 521 diag::err_param_with_void_type); 522 ArgTy = Context.IntTy; 523 Param->setType(ArgTy); 524 } else { 525 // Reject, but continue to parse 'float(const void)'. 526 if (ArgTy.getCVRQualifiers()) 527 Diag(DeclType.Loc, diag::err_void_param_qualified); 528 529 // Do not add 'void' to the ArgTys list. 530 break; 531 } 532 } else if (!FTI.hasPrototype) { 533 if (ArgTy->isPromotableIntegerType()) { 534 ArgTy = Context.IntTy; 535 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { 536 if (BTy->getKind() == BuiltinType::Float) 537 ArgTy = Context.DoubleTy; 538 } 539 } 540 541 ArgTys.push_back(ArgTy); 542 } 543 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 544 FTI.isVariadic, FTI.TypeQuals); 545 } 546 break; 547 } 548 case DeclaratorChunk::MemberPointer: 549 // The scope spec must refer to a class, or be dependent. 550 DeclContext *DC = static_cast<DeclContext*>( 551 DeclType.Mem.Scope().getScopeRep()); 552 QualType ClsType; 553 // FIXME: Extend for dependent types when it's actually supported. 554 // See ActOnCXXNestedNameSpecifier. 555 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) { 556 ClsType = Context.getTagDeclType(RD); 557 } else { 558 if (DC) { 559 Diag(DeclType.Mem.Scope().getBeginLoc(), 560 diag::err_illegal_decl_mempointer_in_nonclass) 561 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") 562 << DeclType.Mem.Scope().getRange(); 563 } 564 D.setInvalidType(true); 565 ClsType = Context.IntTy; 566 } 567 568 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member 569 // with reference type, or "cv void." 570 if (T->isReferenceType()) { 571 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference) 572 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 573 D.setInvalidType(true); 574 T = Context.IntTy; 575 } 576 if (T->isVoidType()) { 577 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void) 578 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 579 T = Context.IntTy; 580 } 581 582 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 583 // object or incomplete types shall not be restrict-qualified." 584 if ((DeclType.Mem.TypeQuals & QualType::Restrict) && 585 !T->isIncompleteOrObjectType()) { 586 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) 587 << T; 588 DeclType.Mem.TypeQuals &= ~QualType::Restrict; 589 } 590 591 T = Context.getMemberPointerType(T, ClsType.getTypePtr()). 592 getQualifiedType(DeclType.Mem.TypeQuals); 593 594 break; 595 } 596 597 // See if there are any attributes on this declarator chunk. 598 if (const AttributeList *AL = DeclType.getAttrs()) 599 ProcessTypeAttributeList(T, AL); 600 } 601 602 if (getLangOptions().CPlusPlus && T->isFunctionType()) { 603 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto(); 604 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?"); 605 606 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type 607 // for a nonstatic member function, the function type to which a pointer 608 // to member refers, or the top-level function type of a function typedef 609 // declaration. 610 if (FnTy->getTypeQuals() != 0 && 611 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && 612 ((D.getContext() != Declarator::MemberContext && 613 (!D.getCXXScopeSpec().isSet() || 614 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep()) 615 ->isRecord())) || 616 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { 617 if (D.isFunctionDeclarator()) 618 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); 619 else 620 Diag(D.getIdentifierLoc(), 621 diag::err_invalid_qualified_typedef_function_type_use); 622 623 // Strip the cv-quals from the type. 624 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), 625 FnTy->getNumArgs(), FnTy->isVariadic(), 0); 626 } 627 } 628 629 // If there were any type attributes applied to the decl itself (not the 630 // type, apply the type attribute to the type!) 631 if (const AttributeList *Attrs = D.getAttributes()) 632 ProcessTypeAttributeList(T, Attrs); 633 634 return T; 635} 636 637/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition 638/// declarator 639QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { 640 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); 641 QualType T = MDecl->getResultType(); 642 llvm::SmallVector<QualType, 16> ArgTys; 643 644 // Add the first two invisible argument types for self and _cmd. 645 if (MDecl->isInstanceMethod()) { 646 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); 647 selfTy = Context.getPointerType(selfTy); 648 ArgTys.push_back(selfTy); 649 } 650 else 651 ArgTys.push_back(Context.getObjCIdType()); 652 ArgTys.push_back(Context.getObjCSelType()); 653 654 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { 655 ParmVarDecl *PDecl = MDecl->getParamDecl(i); 656 QualType ArgTy = PDecl->getType(); 657 assert(!ArgTy.isNull() && "Couldn't parse type?"); 658 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 659 // This matches the conversion that is done in 660 // Sema::ActOnParamDeclarator(). 661 if (ArgTy->isArrayType()) 662 ArgTy = Context.getArrayDecayedType(ArgTy); 663 else if (ArgTy->isFunctionType()) 664 ArgTy = Context.getPointerType(ArgTy); 665 ArgTys.push_back(ArgTy); 666 } 667 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 668 MDecl->isVariadic(), 0); 669 return T; 670} 671 672/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that 673/// may be similar (C++ 4.4), replaces T1 and T2 with the type that 674/// they point to and return true. If T1 and T2 aren't pointer types 675/// or pointer-to-member types, or if they are not similar at this 676/// level, returns false and leaves T1 and T2 unchanged. Top-level 677/// qualifiers on T1 and T2 are ignored. This function will typically 678/// be called in a loop that successively "unwraps" pointer and 679/// pointer-to-member types to compare them at each level. 680bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) 681{ 682 const PointerType *T1PtrType = T1->getAsPointerType(), 683 *T2PtrType = T2->getAsPointerType(); 684 if (T1PtrType && T2PtrType) { 685 T1 = T1PtrType->getPointeeType(); 686 T2 = T2PtrType->getPointeeType(); 687 return true; 688 } 689 690 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(), 691 *T2MPType = T2->getAsMemberPointerType(); 692 if (T1MPType && T2MPType && 693 Context.getCanonicalType(T1MPType->getClass()) == 694 Context.getCanonicalType(T2MPType->getClass())) { 695 T1 = T1MPType->getPointeeType(); 696 T2 = T2MPType->getPointeeType(); 697 return true; 698 } 699 return false; 700} 701 702Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 703 // C99 6.7.6: Type names have no identifier. This is already validated by 704 // the parser. 705 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 706 707 QualType T = GetTypeForDeclarator(D, S); 708 709 assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); 710 711 // Check that there are no default arguments (C++ only). 712 if (getLangOptions().CPlusPlus) 713 CheckExtraCXXDefaultArguments(D); 714 715 // In this context, we *do not* check D.getInvalidType(). If the declarator 716 // type was invalid, GetTypeForDeclarator() still returns a "valid" type, 717 // though it will not reflect the user specified type. 718 return T.getAsOpaquePtr(); 719} 720 721 722 723//===----------------------------------------------------------------------===// 724// Type Attribute Processing 725//===----------------------------------------------------------------------===// 726 727/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 728/// specified type. The attribute contains 1 argument, the id of the address 729/// space for the type. 730static void HandleAddressSpaceTypeAttribute(QualType &Type, 731 const AttributeList &Attr, Sema &S){ 732 // If this type is already address space qualified, reject it. 733 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 734 // for two or more different address spaces." 735 if (Type.getAddressSpace()) { 736 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); 737 return; 738 } 739 740 // Check the attribute arguments. 741 if (Attr.getNumArgs() != 1) { 742 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; 743 return; 744 } 745 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); 746 llvm::APSInt addrSpace(32); 747 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { 748 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) 749 << ASArgExpr->getSourceRange(); 750 return; 751 } 752 753 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 754 Type = S.Context.getASQualType(Type, ASIdx); 755} 756 757void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { 758 // Scan through and apply attributes to this type where it makes sense. Some 759 // attributes (such as __address_space__, __vector_size__, etc) apply to the 760 // type, but others can be present in the type specifiers even though they 761 // apply to the decl. Here we apply type attributes and ignore the rest. 762 for (; AL; AL = AL->getNext()) { 763 // If this is an attribute we can handle, do so now, otherwise, add it to 764 // the LeftOverAttrs list for rechaining. 765 switch (AL->getKind()) { 766 default: break; 767 case AttributeList::AT_address_space: 768 HandleAddressSpaceTypeAttribute(Result, *AL, *this); 769 break; 770 } 771 } 772} 773 774/// @brief If the type T is incomplete and cannot be completed, 775/// produce a suitable diagnostic. 776/// 777/// This routine checks whether the type @p T is complete in any 778/// context where a complete type is required. If @p T is a complete 779/// type, returns false. If @p T is incomplete, issues the diagnostic 780/// @p diag (giving it the type @p T) and returns true. 781/// 782/// @param Loc The location in the source that the incomplete type 783/// diagnostic should refer to. 784/// 785/// @param T The type that this routine is examining for completeness. 786/// 787/// @param diag The diagnostic value (e.g., 788/// @c diag::err_typecheck_decl_incomplete_type) that will be used 789/// for the error message if @p T is incomplete. 790/// 791/// @param Range1 An optional range in the source code that will be a 792/// part of the "incomplete type" error message. 793/// 794/// @param Range2 An optional range in the source code that will be a 795/// part of the "incomplete type" error message. 796/// 797/// @param PrintType If non-NULL, the type that should be printed 798/// instead of @p T. This parameter should be used when the type that 799/// we're checking for incompleteness isn't the type that should be 800/// displayed to the user, e.g., when T is a type and PrintType is a 801/// pointer to T. 802/// 803/// @returns @c true if @p T is incomplete and a diagnostic was emitted, 804/// @c false otherwise. 805/// 806/// @todo When Clang gets proper support for C++ templates, this 807/// routine will also be able perform template instantiation when @p T 808/// is a class template specialization. 809bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag, 810 SourceRange Range1, SourceRange Range2, 811 QualType PrintType) { 812 // If we have a complete type, we're done. 813 if (!T->isIncompleteType()) 814 return false; 815 816 if (PrintType.isNull()) 817 PrintType = T; 818 819 // We have an incomplete type. Produce a diagnostic. 820 Diag(Loc, diag) << PrintType << Range1 << Range2; 821 822 // If the type was a forward declaration of a class/struct/union 823 // type, produce 824 const TagType *Tag = 0; 825 if (const RecordType *Record = T->getAsRecordType()) 826 Tag = Record; 827 else if (const EnumType *Enum = T->getAsEnumType()) 828 Tag = Enum; 829 830 if (Tag && !Tag->getDecl()->isInvalidDecl()) 831 Diag(Tag->getDecl()->getLocation(), 832 Tag->isBeingDefined() ? diag::note_type_being_defined 833 : diag::note_forward_declaration) 834 << QualType(Tag, 0); 835 836 return true; 837} 838