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