SemaType.cpp revision 99dbc963bb0d81c7806f867a258d15c72d257267
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(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_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_class: 99 case DeclSpec::TST_enum: 100 case DeclSpec::TST_union: 101 case DeclSpec::TST_struct: { 102 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 103 assert(D && "Didn't get a decl for a class/enum/union/struct?"); 104 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 105 DS.getTypeSpecSign() == 0 && 106 "Can't handle qualifiers on typedef names yet!"); 107 // TypeQuals handled by caller. 108 Result = Context.getTypeDeclType(cast<TypeDecl>(D)); 109 break; 110 } 111 case DeclSpec::TST_typedef: { 112 Decl *D = static_cast<Decl *>(DS.getTypeRep()); 113 assert(D && "Didn't get a decl for a typedef?"); 114 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && 115 DS.getTypeSpecSign() == 0 && 116 "Can't handle qualifiers on typedef names yet!"); 117 118 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so 119 // we have this "hack" for now... 120 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { 121 if (DS.getProtocolQualifiers() == 0) { 122 Result = Context.getObjCInterfaceType(ObjCIntDecl); 123 break; 124 } 125 126 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; 127 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, 128 reinterpret_cast<ObjCProtocolDecl**>(PPDecl), 129 DS.getNumProtocolQualifiers()); 130 break; 131 } 132 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { 133 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) 134 && DS.getProtocolQualifiers()) { 135 // id<protocol-list> 136 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; 137 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(), 138 reinterpret_cast<ObjCProtocolDecl**>(PPDecl), 139 DS.getNumProtocolQualifiers()); 140 break; 141 } 142 } 143 // TypeQuals handled by caller. 144 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D)); 145 break; 146 } 147 case DeclSpec::TST_typeofType: 148 Result = QualType::getFromOpaquePtr(DS.getTypeRep()); 149 assert(!Result.isNull() && "Didn't get a type for typeof?"); 150 // TypeQuals handled by caller. 151 Result = Context.getTypeOfType(Result); 152 break; 153 case DeclSpec::TST_typeofExpr: { 154 Expr *E = static_cast<Expr *>(DS.getTypeRep()); 155 assert(E && "Didn't get an expression for typeof?"); 156 // TypeQuals handled by caller. 157 Result = Context.getTypeOfExpr(E); 158 break; 159 } 160 } 161 162 // Handle complex types. 163 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) 164 Result = Context.getComplexType(Result); 165 166 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && 167 "FIXME: imaginary types not supported yet!"); 168 169 // See if there are any attributes on the declspec that apply to the type (as 170 // opposed to the decl). 171 if (const AttributeList *AL = DS.getAttributes()) 172 ProcessTypeAttributes(Result, AL); 173 174 // Apply const/volatile/restrict qualifiers to T. 175 if (unsigned TypeQuals = DS.getTypeQualifiers()) { 176 177 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object 178 // or incomplete types shall not be restrict-qualified." C++ also allows 179 // restrict-qualified references. 180 if (TypeQuals & QualType::Restrict) { 181 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) { 182 QualType EltTy = PT->getPointeeType(); 183 184 // If we have a pointer or reference, the pointee must have an object or 185 // incomplete type. 186 if (!EltTy->isIncompleteOrObjectType()) { 187 Diag(DS.getRestrictSpecLoc(), 188 diag::err_typecheck_invalid_restrict_invalid_pointee, 189 EltTy.getAsString(), DS.getSourceRange()); 190 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 191 } 192 } else { 193 Diag(DS.getRestrictSpecLoc(), 194 diag::err_typecheck_invalid_restrict_not_pointer, 195 Result.getAsString(), DS.getSourceRange()); 196 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier. 197 } 198 } 199 200 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification 201 // of a function type includes any type qualifiers, the behavior is 202 // undefined." 203 if (Result->isFunctionType() && TypeQuals) { 204 // Get some location to point at, either the C or V location. 205 SourceLocation Loc; 206 if (TypeQuals & QualType::Const) 207 Loc = DS.getConstSpecLoc(); 208 else { 209 assert((TypeQuals & QualType::Volatile) && 210 "Has CV quals but not C or V?"); 211 Loc = DS.getVolatileSpecLoc(); 212 } 213 Diag(Loc, diag::warn_typecheck_function_qualifiers, 214 Result.getAsString(), DS.getSourceRange()); 215 } 216 217 Result = Result.getQualifiedType(TypeQuals); 218 } 219 return Result; 220} 221 222/// GetTypeForDeclarator - Convert the type for the specified declarator to Type 223/// instances. 224QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { 225 // long long is a C99 feature. 226 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && 227 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) 228 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); 229 230 QualType T = ConvertDeclSpecToType(D.getDeclSpec()); 231 232 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos 233 // are ordered from the identifier out, which is opposite of what we want :). 234 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { 235 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); 236 switch (DeclType.Kind) { 237 default: assert(0 && "Unknown decltype!"); 238 case DeclaratorChunk::Pointer: 239 if (T->isReferenceType()) { 240 // C++ 8.3.2p4: There shall be no ... pointers to references ... 241 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference, 242 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 243 D.setInvalidType(true); 244 T = Context.IntTy; 245 } 246 247 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 248 // object or incomplete types shall not be restrict-qualified." 249 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && 250 !T->isIncompleteOrObjectType()) { 251 Diag(DeclType.Loc, 252 diag::err_typecheck_invalid_restrict_invalid_pointee, 253 T.getAsString()); 254 DeclType.Ptr.TypeQuals &= QualType::Restrict; 255 } 256 257 // Apply the pointer typequals to the pointer object. 258 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); 259 260 // See if there are any attributes on the pointer that apply to it. 261 if (const AttributeList *AL = DeclType.Ptr.AttrList) 262 ProcessTypeAttributes(T, AL); 263 264 break; 265 case DeclaratorChunk::Reference: 266 if (const ReferenceType *RT = T->getAsReferenceType()) { 267 // C++ 8.3.2p4: There shall be no references to references. 268 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference, 269 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 270 D.setInvalidType(true); 271 T = RT->getPointeeType(); 272 } 273 274 // Enforce C99 6.7.3p2: "Types other than pointer types derived from 275 // object or incomplete types shall not be restrict-qualified." 276 if (DeclType.Ref.HasRestrict && 277 !T->isIncompleteOrObjectType()) { 278 Diag(DeclType.Loc, 279 diag::err_typecheck_invalid_restrict_invalid_pointee, 280 T.getAsString()); 281 DeclType.Ref.HasRestrict = false; 282 } 283 284 T = Context.getReferenceType(T); 285 286 // Handle restrict on references. 287 if (DeclType.Ref.HasRestrict) 288 T.addRestrict(); 289 290 // See if there are any attributes on the pointer that apply to it. 291 if (const AttributeList *AL = DeclType.Ref.AttrList) 292 ProcessTypeAttributes(T, AL); 293 break; 294 case DeclaratorChunk::Array: { 295 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; 296 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); 297 ArrayType::ArraySizeModifier ASM; 298 if (ATI.isStar) 299 ASM = ArrayType::Star; 300 else if (ATI.hasStatic) 301 ASM = ArrayType::Static; 302 else 303 ASM = ArrayType::Normal; 304 305 // C99 6.7.5.2p1: If the element type is an incomplete or function type, 306 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) 307 if (T->isIncompleteType()) { 308 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, 309 T.getAsString()); 310 T = Context.IntTy; 311 D.setInvalidType(true); 312 } else if (T->isFunctionType()) { 313 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, 314 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 315 T = Context.getPointerType(T); 316 D.setInvalidType(true); 317 } else if (const ReferenceType *RT = T->getAsReferenceType()) { 318 // C++ 8.3.2p4: There shall be no ... arrays of references ... 319 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, 320 D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); 321 T = RT->getPointeeType(); 322 D.setInvalidType(true); 323 } else if (const RecordType *EltTy = T->getAsRecordType()) { 324 // If the element type is a struct or union that contains a variadic 325 // array, reject it: C99 6.7.2.1p2. 326 if (EltTy->getDecl()->hasFlexibleArrayMember()) { 327 Diag(DeclType.Loc, diag::err_flexible_array_in_array, 328 T.getAsString()); 329 T = Context.IntTy; 330 D.setInvalidType(true); 331 } 332 } 333 // C99 6.7.5.2p1: The size expression shall have integer type. 334 if (ArraySize && !ArraySize->getType()->isIntegerType()) { 335 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, 336 ArraySize->getType().getAsString(), ArraySize->getSourceRange()); 337 D.setInvalidType(true); 338 delete ArraySize; 339 ATI.NumElts = ArraySize = 0; 340 } 341 llvm::APSInt ConstVal(32); 342 if (!ArraySize) { 343 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); 344 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || 345 !T->isConstantSizeType()) { 346 // Per C99, a variable array is an array with either a non-constant 347 // size or an element type that has a non-constant-size 348 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); 349 } else { 350 // C99 6.7.5.2p1: If the expression is a constant expression, it shall 351 // have a value greater than zero. 352 if (ConstVal.isSigned()) { 353 if (ConstVal.isNegative()) { 354 Diag(ArraySize->getLocStart(), 355 diag::err_typecheck_negative_array_size, 356 ArraySize->getSourceRange()); 357 D.setInvalidType(true); 358 } else if (ConstVal == 0) { 359 // GCC accepts zero sized static arrays. 360 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, 361 ArraySize->getSourceRange()); 362 } 363 } 364 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); 365 } 366 // If this is not C99, extwarn about VLA's and C99 array size modifiers. 367 if (!getLangOptions().C99 && 368 (ASM != ArrayType::Normal || 369 (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) 370 Diag(D.getIdentifierLoc(), diag::ext_vla); 371 break; 372 } 373 case DeclaratorChunk::Function: 374 // If the function declarator has a prototype (i.e. it is not () and 375 // does not have a K&R-style identifier list), then the arguments are part 376 // of the type, otherwise the argument list is (). 377 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; 378 379 // C99 6.7.5.3p1: The return type may not be a function or array type. 380 if (T->isArrayType() || T->isFunctionType()) { 381 Diag(DeclType.Loc, diag::err_func_returning_array_function, 382 T.getAsString()); 383 T = Context.IntTy; 384 D.setInvalidType(true); 385 } 386 387 if (!FTI.hasPrototype) { 388 // Simple void foo(), where the incoming T is the result type. 389 T = Context.getFunctionTypeNoProto(T); 390 391 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. 392 if (FTI.NumArgs != 0) 393 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); 394 395 } else { 396 // Otherwise, we have a function with an argument list that is 397 // potentially variadic. 398 llvm::SmallVector<QualType, 16> ArgTys; 399 400 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { 401 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param; 402 QualType ArgTy = Param->getType(); 403 assert(!ArgTy.isNull() && "Couldn't parse type?"); 404 // 405 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 406 // This matches the conversion that is done in 407 // Sema::ActOnParamDeclarator(). Without this conversion, the 408 // argument type in the function prototype *will not* match the 409 // type in ParmVarDecl (which makes the code generator unhappy). 410 // 411 // FIXME: We still apparently need the conversion in 412 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since 413 // it should be driving off the type being created here. 414 // 415 // FIXME: If a source translation tool needs to see the original type, 416 // then we need to consider storing both types somewhere... 417 // 418 if (ArgTy->isArrayType()) { 419 ArgTy = Context.getArrayDecayedType(ArgTy); 420 } else if (ArgTy->isFunctionType()) 421 ArgTy = Context.getPointerType(ArgTy); 422 423 // Look for 'void'. void is allowed only as a single argument to a 424 // function with no other parameters (C99 6.7.5.3p10). We record 425 // int(void) as a FunctionTypeProto with an empty argument list. 426 else if (ArgTy->isVoidType()) { 427 // If this is something like 'float(int, void)', reject it. 'void' 428 // is an incomplete type (C99 6.2.5p19) and function decls cannot 429 // have arguments of incomplete type. 430 if (FTI.NumArgs != 1 || FTI.isVariadic) { 431 Diag(DeclType.Loc, diag::err_void_only_param); 432 ArgTy = Context.IntTy; 433 Param->setType(ArgTy); 434 } else if (FTI.ArgInfo[i].Ident) { 435 // Reject, but continue to parse 'int(void abc)'. 436 Diag(FTI.ArgInfo[i].IdentLoc, 437 diag::err_param_with_void_type); 438 ArgTy = Context.IntTy; 439 Param->setType(ArgTy); 440 } else { 441 // Reject, but continue to parse 'float(const void)'. 442 if (ArgTy.getCVRQualifiers()) 443 Diag(DeclType.Loc, diag::err_void_param_qualified); 444 445 // Do not add 'void' to the ArgTys list. 446 break; 447 } 448 } 449 450 ArgTys.push_back(ArgTy); 451 } 452 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 453 FTI.isVariadic); 454 } 455 break; 456 } 457 } 458 459 return T; 460} 461 462/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition 463/// declarator 464QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { 465 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); 466 QualType T = MDecl->getResultType(); 467 llvm::SmallVector<QualType, 16> ArgTys; 468 469 // Add the first two invisible argument types for self and _cmd. 470 if (MDecl->isInstance()) { 471 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); 472 selfTy = Context.getPointerType(selfTy); 473 ArgTys.push_back(selfTy); 474 } 475 else 476 ArgTys.push_back(Context.getObjCIdType()); 477 ArgTys.push_back(Context.getObjCSelType()); 478 479 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { 480 ParmVarDecl *PDecl = MDecl->getParamDecl(i); 481 QualType ArgTy = PDecl->getType(); 482 assert(!ArgTy.isNull() && "Couldn't parse type?"); 483 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). 484 // This matches the conversion that is done in 485 // Sema::ActOnParamDeclarator(). 486 if (ArgTy->isArrayType()) 487 ArgTy = Context.getArrayDecayedType(ArgTy); 488 else if (ArgTy->isFunctionType()) 489 ArgTy = Context.getPointerType(ArgTy); 490 ArgTys.push_back(ArgTy); 491 } 492 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), 493 MDecl->isVariadic()); 494 return T; 495} 496 497Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { 498 // C99 6.7.6: Type names have no identifier. This is already validated by 499 // the parser. 500 assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); 501 502 QualType T = GetTypeForDeclarator(D, S); 503 504 assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); 505 506 // Check that there are no default arguments (C++ only). 507 if (getLangOptions().CPlusPlus) 508 CheckExtraCXXDefaultArguments(D); 509 510 // In this context, we *do not* check D.getInvalidType(). If the declarator 511 // type was invalid, GetTypeForDeclarator() still returns a "valid" type, 512 // though it will not reflect the user specified type. 513 return T.getAsOpaquePtr(); 514} 515 516void Sema::ProcessTypeAttributes(QualType &Result, const AttributeList *AL) { 517 // Scan through and apply attributes to this type where it makes sense. Some 518 // attributes (such as __address_space__, __vector_size__, etc) apply to the 519 // type, but others can be present in the type specifiers even though they 520 // apply to the decl. Here we apply type attributes and ignore the rest. 521 for (; AL; AL = AL->getNext()) { 522 // If this is an attribute we can handle, do so now, otherwise, add it to 523 // the LeftOverAttrs list for rechaining. 524 switch (AL->getKind()) { 525 default: break; 526 case AttributeList::AT_address_space: 527 Result = HandleAddressSpaceTypeAttribute(Result, AL); 528 continue; 529 } 530 } 531} 532 533/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the 534/// specified type. 535QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type, 536 const AttributeList *Attr) { 537 // If this type is already address space qualified, reject it. 538 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers 539 // for two or more different address spaces." 540 if (Type.getAddressSpace()) { 541 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers); 542 return Type; 543 } 544 545 // Check the attribute arguments. 546 if (Attr->getNumArgs() != 1) { 547 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments, 548 std::string("1")); 549 return Type; 550 } 551 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0)); 552 llvm::APSInt addrSpace(32); 553 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) { 554 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int, 555 ASArgExpr->getSourceRange()); 556 return Type; 557 } 558 559 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 560 return Context.getASQualType(Type, ASIdx); 561} 562 563/// HandleModeTypeAttribute - Process a mode attribute on the 564/// specified type. 565QualType Sema::HandleModeTypeAttribute(QualType Type, 566 AttributeList *Attr) { 567 // This attribute isn't documented, but glibc uses it. It changes 568 // the width of an int or unsigned int to the specified size. 569 570 // Check that there aren't any arguments 571 if (Attr->getNumArgs() != 0) { 572 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments, 573 std::string("0")); 574 return Type; 575 } 576 577 IdentifierInfo * Name = Attr->getParameterName(); 578 if (!Name) { 579 Diag(Attr->getLoc(), diag::err_attribute_missing_parameter_name); 580 return Type; 581 } 582 const char *Str = Name->getName(); 583 unsigned Len = Name->getLength(); 584 585 // Normalize the attribute name, __foo__ becomes foo. 586 if (Len > 4 && Str[0] == '_' && Str[1] == '_' && 587 Str[Len - 2] == '_' && Str[Len - 1] == '_') { 588 Str += 2; 589 Len -= 4; 590 } 591 592 unsigned DestWidth = 0; 593 bool IntegerMode = true; 594 595 switch (Len) { 596 case 2: 597 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } 598 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } 599 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } 600 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } 601 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } 602 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } 603 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } 604 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } 605 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } 606 break; 607 case 4: 608 if (!memcmp(Str, "word", 4)) { 609 // FIXME: glibc uses this to define register_t; this is 610 // narrover than a pointer on PIC16 and other embedded 611 // platforms 612 DestWidth = Context.getTypeSize(Context.VoidPtrTy); 613 break; 614 } 615 if (!memcmp(Str, "byte", 4)) { 616 DestWidth = Context.getTypeSize(Context.CharTy); 617 break; 618 } 619 break; 620 case 7: 621 if (!memcmp(Str, "pointer", 7)) { 622 DestWidth = Context.getTypeSize(Context.VoidPtrTy); 623 IntegerMode = true; 624 break; 625 } 626 break; 627 } 628 629 // FIXME: Need proper fixed-width types 630 QualType RetTy; 631 switch (DestWidth) { 632 case 0: 633 Diag(Attr->getLoc(), diag::err_unknown_machine_mode, 634 std::string(Str, Len)); 635 return Type; 636 case 8: 637 assert(IntegerMode); 638 if (Type->isSignedIntegerType()) 639 RetTy = Context.SignedCharTy; 640 else 641 RetTy = Context.UnsignedCharTy; 642 break; 643 case 16: 644 assert(IntegerMode); 645 if (Type->isSignedIntegerType()) 646 RetTy = Context.ShortTy; 647 else 648 RetTy = Context.UnsignedShortTy; 649 break; 650 case 32: 651 if (!IntegerMode) 652 RetTy = Context.FloatTy; 653 else if (Type->isSignedIntegerType()) 654 RetTy = Context.IntTy; 655 else 656 RetTy = Context.UnsignedIntTy; 657 break; 658 case 64: 659 if (!IntegerMode) 660 RetTy = Context.DoubleTy; 661 else if (Type->isSignedIntegerType()) 662 RetTy = Context.LongLongTy; 663 else 664 RetTy = Context.UnsignedLongLongTy; 665 break; 666 default: 667 Diag(Attr->getLoc(), diag::err_unsupported_machine_mode, 668 std::string(Str, Len)); 669 return Type; 670 } 671 672 if (!Type->getAsBuiltinType()) 673 Diag(Attr->getLoc(), diag::err_mode_not_primitive); 674 else if (!(IntegerMode && Type->isIntegerType()) && 675 !(!IntegerMode && Type->isFloatingType())) { 676 Diag(Attr->getLoc(), diag::err_mode_wrong_type); 677 } 678 679 return RetTy; 680} 681 682 683