SemaType.cpp revision 7aa5475ca2f62df5ba3f3b647ee141a40bf48fd3
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/Basic/Diagnostic.h" 19#include "clang/Parse/DeclSpec.h" 20using namespace clang; 21 22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate 23/// type object. This returns null on error. 24QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) { 25 // FIXME: Should move the logic from DeclSpec::Finish to here for validity 26 // checking. 27 QualType Result; 28 29 switch (DS.getTypeSpecType()) { 30 default: assert(0 && "Unknown TypeSpecType!"); 31 case DeclSpec::TST_void: 32 Result = Context.VoidTy; 33 break; 34 case DeclSpec::TST_char: 35 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 36 Result = Context.CharTy; 37 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) 38 Result = Context.SignedCharTy; 39 else { 40 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 41 "Unknown TSS value"); 42 Result = Context.UnsignedCharTy; 43 } 44 break; 45 case DeclSpec::TST_wchar: 46 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) 47 Result = Context.WCharTy; 48 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { 49 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec, 50 DS.getSpecifierName(DS.getTypeSpecType())); 51 Result = Context.getSignedWCharType(); 52 } else { 53 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && 54 "Unknown TSS value"); 55 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec, 56 DS.getSpecifierName(DS.getTypeSpecType())); 57 Result = Context.getUnsignedWCharType(); 58 } 59 break; 60 case DeclSpec::TST_unspecified: 61 // "<proto1,proto2>" is an objc qualified ID with a missing id. 62 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { 63 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 64 DS.getNumProtocolQualifiers()); 65 break; 66 } 67 68 // Unspecified typespec defaults to int in C90. However, the C90 grammar 69 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, 70 // type-qualifier, or storage-class-specifier. If not, emit an extwarn. 71 // Note that the one exception to this is function definitions, which are 72 // allowed to be completely missing a declspec. This is handled in the 73 // parser already though by it pretending to have seen an 'int' in this 74 // case. 75 if (getLangOptions().ImplicitInt) { 76 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier | 77 DeclSpec::PQ_TypeSpecifier | 78 DeclSpec::PQ_TypeQualifier)) == 0) 79 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec); 80 } else { 81 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: 82 // "At least one type specifier shall be given in the declaration 83 // specifiers in each declaration, and in the specifier-qualifier list in 84 // each struct declaration and type name." 85 if (!DS.hasTypeSpecifier()) 86 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier); 87 } 88 89 // FALL THROUGH. 90 case DeclSpec::TST_int: { 91 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { 92 switch (DS.getTypeSpecWidth()) { 93 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; 94 case DeclSpec::TSW_short: Result = Context.ShortTy; break; 95 case DeclSpec::TSW_long: Result = Context.LongTy; break; 96 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; 97 } 98 } else { 99 switch (DS.getTypeSpecWidth()) { 100 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; 101 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; 102 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; 103 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; 104 } 105 } 106 break; 107 } 108 case DeclSpec::TST_float: Result = Context.FloatTy; break; 109 case DeclSpec::TST_double: 110 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) 111 Result = Context.LongDoubleTy; 112 else 113 Result = Context.DoubleTy; 114 break; 115 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool 116 case DeclSpec::TST_decimal32: // _Decimal32 117 case DeclSpec::TST_decimal64: // _Decimal64 118 case DeclSpec::TST_decimal128: // _Decimal128 119 assert(0 && "FIXME: GNU decimal extensions not supported yet!"); 120 case DeclSpec::TST_class: 121 case DeclSpec::TST_enum: 122 case DeclSpec::TST_union: 123 case DeclSpec::TST_struct: { 124 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 125 assert(D && "Didn't get a decl for a class/enum/union/struct?"); 126 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 127 DS.getTypeSpecSign() == 0 && 128 "Can't handle qualifiers on typedef names yet!"); 129 // TypeQuals handled by caller. 130 Result = Context.getTypeDeclType(cast<TypeDecl>(D)); 131 break; 132 } 133 case DeclSpec::TST_typedef: { 134 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 135 assert(D && "Didn't get a decl for a typedef?"); 136 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 137 DS.getTypeSpecSign() == 0 && 138 "Can't handle qualifiers on typedef names yet!"); 139 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers(); 140 141 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so 142 // we have this "hack" for now... 143 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { 144 if (PQ == 0) { 145 Result = Context.getObjCInterfaceType(ObjCIntDecl); 146 break; 147 } 148 149 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, 150 (ObjCProtocolDecl**)PQ, 151 DS.getNumProtocolQualifiers()); 152 break; 153 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { 154 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) { 155 // id<protocol-list> 156 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ, 157 DS.getNumProtocolQualifiers()); 158 break; 159 } 160 } 161 // TypeQuals handled by caller. 162 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D)); 163 break; 164 } 165 case DeclSpec::TST_typeofType: 166 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 167 assert(!Result.isNull() && "Didn't get a type for typeof?"); 168 // TypeQuals handled by caller. 169 Result = Context.getTypeOfType(Result); 170 break; 171 case DeclSpec::TST_typeofExpr: { 172 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 173 assert(E && "Didn't get an expression for typeof?"); 174 // TypeQuals handled by caller. 175 Result = Context.getTypeOfExpr(E); 176 break; 177 } 178 } 179 180 // Handle complex types. 181 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) 182 Result = Context.getComplexType(Result); 183 184 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && 185 "FIXME: imaginary types not supported yet!"); 186 187 // See if there are any attributes on the declspec that apply to the type (as 188 // opposed to the decl). 189 if (const AttributeList *AL = DS.getAttributes()) 190 ProcessTypeAttributeList(Result, AL); 191 192 // Apply const/volatile/restrict qualifiers to T. 193 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 194 195 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 196 // or incomplete types shall not be restrict-qualified." C++ also allows 197 // restrict-qualified references. 198 if (TypeQuals & QualType::Restrict) { 199 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) { 200 QualType EltTy = PT->getPointeeType(); 201 202 // If we have a pointer or reference, the pointee must have an object or 203 // incomplete type. 204 if (!EltTy->isIncompleteOrObjectType()) { 205 Diag(DS.getRestrictSpecLoc(), 206 diag::err_typecheck_invalid_restrict_invalid_pointee, 207 EltTy.getAsString(), DS.getSourceRange()); 208 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 209 } 210 } else { 211 Diag(DS.getRestrictSpecLoc(), 212 diag::err_typecheck_invalid_restrict_not_pointer, 213 Result.getAsString(), DS.getSourceRange()); 214 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 215 } 216 } 217 218 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 219 // of a function type includes any type qualifiers, the behavior is 220 // undefined." 221 if (Result->isFunctionType() && TypeQuals) { 222 // Get some location to point at, either the C or V location. 223 SourceLocation Loc; 224 if (TypeQuals & QualType::Const) 225 Loc = DS.getConstSpecLoc(); 226 else { 227 assert((TypeQuals & QualType::Volatile) && 228 "Has CV quals but not C or V?"); 229 Loc = DS.getVolatileSpecLoc(); 230 } 231 Diag(Loc, diag::warn_typecheck_function_qualifiers, 232 Result.getAsString(), DS.getSourceRange()); 233 } 234 235 Result = Result.getQualifiedType(TypeQuals); 236 } 237 return Result; 238} 239 240/// GetTypeForDeclarator - Convert the type for the specified declarator to Type 241/// instances. 242QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { 243 // long long is a C99 feature. 244 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && 245 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) 246 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); 247 248 QualType T = ConvertDeclSpecToType(D.getDeclSpec()); 249 250 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos 251 // are ordered from the identifier out, which is opposite of what we want :). 252 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 253 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); 254 switch (DeclType.Kind) { 255 default: assert(0 && "Unknown decltype!"); 256 case DeclaratorChunk::BlockPointer: 257 if (DeclType.Cls.TypeQuals) 258 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type); 259 if (!T.getTypePtr()->isFunctionType()) 260 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type); 261 else 262 T = Context.getBlockPointerType(T); 263 break; 264 case DeclaratorChunk::Pointer: 265 if (T->isReferenceType()) { 266 // C++ 8.3.2p4: There shall be no ... pointers to references ... 267 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference, 268 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 269 D.setInvalidType(true); 270 T = Context.IntTy; 271 } 272 273 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 274 // object or incomplete types shall not be restrict-qualified." 275 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && 276 !T->isIncompleteOrObjectType()) { 277 Diag(DeclType.Loc, 278 diag::err_typecheck_invalid_restrict_invalid_pointee, 279 T.getAsString()); 280 DeclType.Ptr.TypeQuals &= QualType::Restrict; 281 } 282 283 // Apply the pointer typequals to the pointer object. 284 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); 285 break; 286 case DeclaratorChunk::Reference: 287 if (const ReferenceType *RT = T->getAsReferenceType()) { 288 // C++ 8.3.2p4: There shall be no references to references. 289 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference, 290 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 291 D.setInvalidType(true); 292 T = RT->getPointeeType(); 293 } 294 295 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 296 // object or incomplete types shall not be restrict-qualified." 297 if (DeclType.Ref.HasRestrict && 298 !T->isIncompleteOrObjectType()) { 299 Diag(DeclType.Loc, 300 diag::err_typecheck_invalid_restrict_invalid_pointee, 301 T.getAsString()); 302 DeclType.Ref.HasRestrict = false; 303 } 304 305 T = Context.getReferenceType(T); 306 307 // Handle restrict on references. 308 if (DeclType.Ref.HasRestrict) 309 T.addRestrict(); 310 break; 311 case DeclaratorChunk::Array: { 312 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 313 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 314 ArrayType::ArraySizeModifier ASM; 315 if (ATI.isStar) 316 ASM = ArrayType::Star; 317 else if (ATI.hasStatic) 318 ASM = ArrayType::Static; 319 else 320 ASM = ArrayType::Normal; 321 322 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 323 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 324 if (T->isIncompleteType()) { 325 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, 326 T.getAsString()); 327 T = Context.IntTy; 328 D.setInvalidType(true); 329 } else if (T->isFunctionType()) { 330 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, 331 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 332 T = Context.getPointerType(T); 333 D.setInvalidType(true); 334 } else if (const ReferenceType *RT = T->getAsReferenceType()) { 335 // C++ 8.3.2p4: There shall be no ... arrays of references ... 336 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, 337 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 338 T = RT->getPointeeType(); 339 D.setInvalidType(true); 340 } else if (const RecordType *EltTy = T->getAsRecordType()) { 341 // If the element type is a struct or union that contains a variadic 342 // array, reject it: C99 6.7.2.1p2. 343 if (EltTy->getDecl()->hasFlexibleArrayMember()) { 344 Diag(DeclType.Loc, diag::err_flexible_array_in_array, 345 T.getAsString()); 346 T = Context.IntTy; 347 D.setInvalidType(true); 348 } 349 } else if (T->isObjCInterfaceType()) { 350 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces, 351 T.getAsString()); 352 } 353 354 // C99 6.7.5.2p1: The size expression shall have integer type. 355 if (ArraySize && !ArraySize->getType()->isIntegerType()) { 356 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, 357 ArraySize->getType().getAsString(), ArraySize->getSourceRange()); 358 D.setInvalidType(true); 359 delete ArraySize; 360 ATI.NumElts = ArraySize = 0; 361 } 362 llvm::APSInt ConstVal(32); 363 if (!ArraySize) { 364 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); 365 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 366 !T->isConstantSizeType()) { 367 // Per C99, a variable array is an array with either a non-constant 368 // size or an element type that has a non-constant-size 369 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); 370 } else { 371 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 372 // have a value greater than zero. 373 if (ConstVal.isSigned()) { 374 if (ConstVal.isNegative()) { 375 Diag(ArraySize->getLocStart(), 376 diag::err_typecheck_negative_array_size, 377 ArraySize->getSourceRange()); 378 D.setInvalidType(true); 379 } else if (ConstVal == 0) { 380 // GCC accepts zero sized static arrays. 381 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, 382 ArraySize->getSourceRange()); 383 } 384 } 385 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); 386 } 387 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 388 if (!getLangOptions().C99 && 389 (ASM != ArrayType::Normal || 390 (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) 391 Diag(D.getIdentifierLoc(), diag::ext_vla); 392 break; 393 } 394 case DeclaratorChunk::Function: 395 // If the function declarator has a prototype (i.e. it is not () and 396 // does not have a K&R-style identifier list), then the arguments are part 397 // of the type, otherwise the argument list is (). 398 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 399 400 // C99 6.7.5.3p1: The return type may not be a function or array type. 401 if (T->isArrayType() || T->isFunctionType()) { 402 Diag(DeclType.Loc, diag::err_func_returning_array_function, 403 T.getAsString()); 404 T = Context.IntTy; 405 D.setInvalidType(true); 406 } 407 408 if (FTI.NumArgs == 0) { 409 // Simple void foo(), where the incoming T is the result type. 410 T = Context.getFunctionTypeNoProto(T); 411 } else if (FTI.ArgInfo[0].Param == 0) { 412 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. 413 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 414 } else { 415 // Otherwise, we have a function with an argument list that is 416 // potentially variadic. 417 llvm::SmallVector<QualType, 16> ArgTys; 418 419 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 420 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; 421 QualType ArgTy = Param->getType(); 422 assert(!ArgTy.isNull() && "Couldn't parse type?"); 423 // 424 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 425 // This matches the conversion that is done in 426 // Sema::ActOnParamDeclarator(). Without this conversion, the 427 // argument type in the function prototype *will not* match the 428 // type in ParmVarDecl (which makes the code generator unhappy). 429 // 430 // FIXME: We still apparently need the conversion in 431 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since 432 // it should be driving off the type being created here. 433 // 434 // FIXME: If a source translation tool needs to see the original type, 435 // then we need to consider storing both types somewhere... 436 // 437 if (ArgTy->isArrayType()) { 438 ArgTy = Context.getArrayDecayedType(ArgTy); 439 } else if (ArgTy->isFunctionType()) 440 ArgTy = Context.getPointerType(ArgTy); 441 442 // Look for 'void'. void is allowed only as a single argument to a 443 // function with no other parameters (C99 6.7.5.3p10). We record 444 // int(void) as a FunctionTypeProto with an empty argument list. 445 else if (ArgTy->isVoidType()) { 446 // If this is something like 'float(int, void)', reject it. 'void' 447 // is an incomplete type (C99 6.2.5p19) and function decls cannot 448 // have arguments of incomplete type. 449 if (FTI.NumArgs != 1 || FTI.isVariadic) { 450 Diag(DeclType.Loc, diag::err_void_only_param); 451 ArgTy = Context.IntTy; 452 Param->setType(ArgTy); 453 } else if (FTI.ArgInfo[i].Ident) { 454 // Reject, but continue to parse 'int(void abc)'. 455 Diag(FTI.ArgInfo[i].IdentLoc, 456 diag::err_param_with_void_type); 457 ArgTy = Context.IntTy; 458 Param->setType(ArgTy); 459 } else { 460 // Reject, but continue to parse 'float(const void)'. 461 if (ArgTy.getCVRQualifiers()) 462 Diag(DeclType.Loc, diag::err_void_param_qualified); 463 464 // Do not add 'void' to the ArgTys list. 465 break; 466 } 467 } else if (!FTI.hasPrototype) { 468 if (ArgTy->isPromotableIntegerType()) { 469 ArgTy = Context.IntTy; 470 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) { 471 if (BTy->getKind() == BuiltinType::Float) 472 ArgTy = Context.DoubleTy; 473 } 474 } 475 476 ArgTys.push_back(ArgTy); 477 } 478 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 479 FTI.isVariadic); 480 } 481 break; 482 } 483 484 // See if there are any attributes on this declarator chunk. 485 if (const AttributeList *AL = DeclType.getAttrs()) 486 ProcessTypeAttributeList(T, AL); 487 } 488 489 // If there were any type attributes applied to the decl itself (not the 490 // type, apply the type attribute to the type!) 491 if (const AttributeList *Attrs = D.getAttributes()) 492 ProcessTypeAttributeList(T, Attrs); 493 494 return T; 495} 496 497/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition 498/// declarator 499QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { 500 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); 501 QualType T = MDecl->getResultType(); 502 llvm::SmallVector<QualType, 16> ArgTys; 503 504 // Add the first two invisible argument types for self and _cmd. 505 if (MDecl->isInstance()) { 506 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); 507 selfTy = Context.getPointerType(selfTy); 508 ArgTys.push_back(selfTy); 509 } 510 else 511 ArgTys.push_back(Context.getObjCIdType()); 512 ArgTys.push_back(Context.getObjCSelType()); 513 514 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { 515 ParmVarDecl *PDecl = MDecl->getParamDecl(i); 516 QualType ArgTy = PDecl->getType(); 517 assert(!ArgTy.isNull() && "Couldn't parse type?"); 518 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 519 // This matches the conversion that is done in 520 // Sema::ActOnParamDeclarator(). 521 if (ArgTy->isArrayType()) 522 ArgTy = Context.getArrayDecayedType(ArgTy); 523 else if (ArgTy->isFunctionType()) 524 ArgTy = Context.getPointerType(ArgTy); 525 ArgTys.push_back(ArgTy); 526 } 527 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 528 MDecl->isVariadic()); 529 return T; 530} 531 532Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 533 // C99 6.7.6: Type names have no identifier. This is already validated by 534 // the parser. 535 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 536 537 QualType T = GetTypeForDeclarator(D, S); 538 539 assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); 540 541 // Check that there are no default arguments (C++ only). 542 if (getLangOptions().CPlusPlus) 543 CheckExtraCXXDefaultArguments(D); 544 545 // In this context, we *do not* check D.getInvalidType(). If the declarator 546 // type was invalid, GetTypeForDeclarator() still returns a "valid" type, 547 // though it will not reflect the user specified type. 548 return T.getAsOpaquePtr(); 549} 550 551 552 553//===----------------------------------------------------------------------===// 554// Type Attribute Processing 555//===----------------------------------------------------------------------===// 556 557/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 558/// specified type. The attribute contains 1 argument, the id of the address 559/// space for the type. 560static void HandleAddressSpaceTypeAttribute(QualType &Type, 561 const AttributeList &Attr, Sema &S){ 562 // If this type is already address space qualified, reject it. 563 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 564 // for two or more different address spaces." 565 if (Type.getAddressSpace()) { 566 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); 567 return; 568 } 569 570 // Check the attribute arguments. 571 if (Attr.getNumArgs() != 1) { 572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, 573 std::string("1")); 574 return; 575 } 576 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); 577 llvm::APSInt addrSpace(32); 578 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { 579 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int, 580 ASArgExpr->getSourceRange()); 581 return; 582 } 583 584 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 585 Type = S.Context.getASQualType(Type, ASIdx); 586} 587 588void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) { 589 // Scan through and apply attributes to this type where it makes sense. Some 590 // attributes (such as __address_space__, __vector_size__, etc) apply to the 591 // type, but others can be present in the type specifiers even though they 592 // apply to the decl. Here we apply type attributes and ignore the rest. 593 for (; AL; AL = AL->getNext()) { 594 // If this is an attribute we can handle, do so now, otherwise, add it to 595 // the LeftOverAttrs list for rechaining. 596 switch (AL->getKind()) { 597 default: break; 598 case AttributeList::AT_address_space: 599 HandleAddressSpaceTypeAttribute(Result, *AL, *this); 600 break; 601 } 602 } 603} 604 605 606