Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 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 decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Chris Lattner | 2024f0a | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // Helper functions |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 23 | static const FunctionTypeProto *getFunctionProto(Decl *d) { |
| 24 | QualType Ty; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 25 | if (ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
| 26 | Ty = decl->getType(); |
| 27 | else if (FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
| 28 | Ty = decl->getType(); |
| 29 | else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
| 30 | Ty = decl->getUnderlyingType(); |
| 31 | else |
| 32 | return 0; |
| 33 | |
| 34 | if (Ty->isFunctionPointerType()) |
| 35 | Ty = Ty->getAsPointerType()->getPointeeType(); |
| 36 | |
| 37 | if (const FunctionType *FnTy = Ty->getAsFunctionType()) |
| 38 | return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType()); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
| 44 | if (!T->isPointerType()) |
| 45 | return false; |
| 46 | |
| 47 | T = T->getAsPointerType()->getPointeeType().getCanonicalType(); |
| 48 | ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr()); |
| 49 | |
| 50 | if (!ClsT) |
| 51 | return false; |
| 52 | |
| 53 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 54 | |
| 55 | // FIXME: Should we walk the chain of classes? |
| 56 | return ClsName == &Ctx.Idents.get("NSString") || |
| 57 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 58 | } |
| 59 | |
Chris Lattner | 2024f0a | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2024f0a | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 61 | // Attribute Implementations |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 64 | void Sema::HandleExtVectorTypeAttribute(Decl *d, const AttributeList &Attr) { |
| 65 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 66 | if (tDecl == 0) { |
| 67 | Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
| 68 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 71 | QualType curType = tDecl->getUnderlyingType(); |
| 72 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 73 | if (Attr.getNumArgs() != 1) { |
| 74 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 75 | std::string("1")); |
| 76 | return; |
| 77 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 78 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 79 | llvm::APSInt vecSize(32); |
| 80 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 81 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 82 | "ext_vector_type", sizeExpr->getSourceRange()); |
| 83 | return; |
| 84 | } |
| 85 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 86 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 87 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 88 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 89 | Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 90 | curType.getCanonicalType().getAsString()); |
| 91 | return; |
| 92 | } |
| 93 | // unlike gcc's vector_size attribute, the size is specified as the |
| 94 | // number of elements, not the number of bytes. |
| 95 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 96 | |
| 97 | if (vectorSize == 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 98 | Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 99 | sizeExpr->getSourceRange()); |
| 100 | return; |
| 101 | } |
| 102 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 103 | tDecl->setUnderlyingType(Context.getExtVectorType(curType, vectorSize)); |
| 104 | // Remember this typedef decl, we will need it later for diagnostics. |
| 105 | ExtVectorDecls.push_back(tDecl); |
| 106 | } |
| 107 | |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 108 | |
| 109 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 110 | /// integral and float scalars, although arrays, pointers, and function |
| 111 | /// return values are allowed in conjunction with this construct. Aggregates |
| 112 | /// with this attribute are invalid, even if they are of the same size as a |
| 113 | /// corresponding scalar. |
| 114 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 115 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 116 | /// formed, this routine will return a new vector type. |
| 117 | void Sema::HandleVectorSizeAttribute(Decl *D, const AttributeList &Attr) { |
| 118 | QualType CurType; |
| 119 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 120 | CurType = VD->getType(); |
| 121 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 122 | CurType = TD->getUnderlyingType(); |
| 123 | else { |
| 124 | Diag(D->getLocation(), diag::err_attr_wrong_decl,std::string("vector_size"), |
| 125 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Check the attribute arugments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 130 | if (Attr.getNumArgs() != 1) { |
| 131 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 132 | std::string("1")); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 133 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 135 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 136 | llvm::APSInt vecSize(32); |
| 137 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 138 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 139 | "vector_size", sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 140 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 141 | } |
| 142 | // navigate to the base type - we need to provide for vector pointers, |
| 143 | // vector arrays, and functions returning vectors. |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 144 | Type *canonType = CurType.getCanonicalType().getTypePtr(); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 145 | |
| 146 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 147 | canonType->isFunctionType()) { |
| 148 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
| 149 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 150 | do { |
| 151 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 152 | canonType = PT->getPointeeType().getTypePtr(); |
| 153 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 154 | canonType = AT->getElementType().getTypePtr(); |
| 155 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 156 | canonType = FT->getResultType().getTypePtr(); |
| 157 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 158 | canonType->isFunctionType()); |
| 159 | */ |
| 160 | } |
| 161 | // the base type must be integer or float. |
| 162 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 163 | Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 164 | CurType.getCanonicalType().getAsString()); |
| 165 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 167 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(CurType)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 168 | // vecSize is specified in bytes - convert to bits. |
| 169 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 170 | |
| 171 | // the vector size needs to be an integral multiple of the type size. |
| 172 | if (vectorSize % typeSize) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 173 | Diag(Attr.getLoc(), diag::err_attribute_invalid_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 175 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 176 | } |
| 177 | if (vectorSize == 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 178 | Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 179 | sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 180 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 182 | |
| 183 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 184 | // not required to be a power of 2, unlike GCC. |
| 185 | CurType = Context.getVectorType(CurType, vectorSize/typeSize); |
| 186 | |
| 187 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 188 | VD->setType(CurType); |
| 189 | else |
| 190 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 193 | void Sema::HandlePackedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 194 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 195 | if (Attr.getNumArgs() > 0) { |
| 196 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 197 | std::string("0")); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 202 | TD->addAttr(new PackedAttr()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 203 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 204 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 205 | // has no effect. |
| 206 | if (!FD->getType()->isIncompleteType() && |
| 207 | Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 208 | Diag(Attr.getLoc(), |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 209 | diag::warn_attribute_ignored_for_field_of_type, |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 210 | Attr.getName()->getName(), FD->getType().getAsString()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 211 | else |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 212 | FD->addAttr(new PackedAttr()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 213 | } else |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 214 | Diag(Attr.getLoc(), diag::warn_attribute_ignored, |
| 215 | Attr.getName()->getName()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 218 | void Sema::HandleAliasAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 219 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 220 | if (Attr.getNumArgs() != 1) { |
| 221 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 222 | std::string("1")); |
| 223 | return; |
| 224 | } |
| 225 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 226 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 227 | Arg = Arg->IgnoreParenCasts(); |
| 228 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 229 | |
| 230 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 231 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 232 | "alias", std::string("1")); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const char *Alias = Str->getStrData(); |
| 237 | unsigned AliasLen = Str->getByteLength(); |
| 238 | |
| 239 | // FIXME: check if target symbol exists in current file |
| 240 | |
| 241 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 242 | } |
| 243 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 244 | void Sema::HandleNoReturnAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 245 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 246 | if (Attr.getNumArgs() != 0) { |
| 247 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 248 | std::string("0")); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 253 | if (!Fn) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 254 | Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 255 | "noreturn", "function"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | d->addAttr(new NoReturnAttr()); |
| 260 | } |
| 261 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 262 | void Sema::HandleDeprecatedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 263 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 264 | if (Attr.getNumArgs() != 0) { |
| 265 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 266 | std::string("0")); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | d->addAttr(new DeprecatedAttr()); |
| 271 | } |
| 272 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 273 | void Sema::HandleVisibilityAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 274 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 275 | if (Attr.getNumArgs() != 1) { |
| 276 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 277 | std::string("1")); |
| 278 | return; |
| 279 | } |
| 280 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 281 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 282 | Arg = Arg->IgnoreParenCasts(); |
| 283 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 284 | |
| 285 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 286 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 287 | "visibility", std::string("1")); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | const char *TypeStr = Str->getStrData(); |
| 292 | unsigned TypeLen = Str->getByteLength(); |
| 293 | VisibilityAttr::VisibilityTypes type; |
| 294 | |
| 295 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 296 | type = VisibilityAttr::DefaultVisibility; |
| 297 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 298 | type = VisibilityAttr::HiddenVisibility; |
| 299 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 300 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 301 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 302 | type = VisibilityAttr::ProtectedVisibility; |
| 303 | else { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 304 | Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 305 | "visibility", TypeStr); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | d->addAttr(new VisibilityAttr(type)); |
| 310 | } |
| 311 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 312 | void Sema::HandleWeakAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 313 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 314 | if (Attr.getNumArgs() != 0) { |
| 315 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 316 | std::string("0")); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | d->addAttr(new WeakAttr()); |
| 321 | } |
| 322 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 323 | void Sema::HandleDLLImportAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 324 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 325 | if (Attr.getNumArgs() != 0) { |
| 326 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 327 | std::string("0")); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | d->addAttr(new DLLImportAttr()); |
| 332 | } |
| 333 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 334 | void Sema::HandleDLLExportAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 335 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 336 | if (Attr.getNumArgs() != 0) { |
| 337 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 338 | std::string("0")); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | d->addAttr(new DLLExportAttr()); |
| 343 | } |
| 344 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 345 | void Sema::HandleStdCallAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 346 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 347 | if (Attr.getNumArgs() != 0) { |
| 348 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 349 | std::string("0")); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | d->addAttr(new StdCallAttr()); |
| 354 | } |
| 355 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 356 | void Sema::HandleFastCallAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 357 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 358 | if (Attr.getNumArgs() != 0) { |
| 359 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 360 | std::string("0")); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | d->addAttr(new FastCallAttr()); |
| 365 | } |
| 366 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 367 | void Sema::HandleNothrowAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 368 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 369 | if (Attr.getNumArgs() != 0) { |
| 370 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 371 | std::string("0")); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | d->addAttr(new NoThrowAttr()); |
| 376 | } |
| 377 | |
| 378 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 379 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 380 | void Sema::HandleFormatAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 382 | if (!Attr.getParameterName()) { |
| 383 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 384 | "format", std::string("1")); |
| 385 | return; |
| 386 | } |
| 387 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 388 | if (Attr.getNumArgs() != 2) { |
| 389 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 390 | std::string("3")); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // GCC ignores the format attribute on K&R style function |
| 395 | // prototypes, so we ignore it as well |
| 396 | const FunctionTypeProto *proto = getFunctionProto(d); |
| 397 | |
| 398 | if (!proto) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 399 | Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 400 | "format", "function"); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | |
| 404 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 405 | // this is needed in order to be compatible with GCC |
| 406 | // the index must start in 1 and the limit is numargs+1 |
| 407 | unsigned NumArgs = proto->getNumArgs(); |
| 408 | unsigned FirstIdx = 1; |
| 409 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 410 | const char *Format = Attr.getParameterName()->getName(); |
| 411 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 412 | |
| 413 | // Normalize the argument, __foo__ becomes foo. |
| 414 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 415 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 416 | Format += 2; |
| 417 | FormatLen -= 4; |
| 418 | } |
| 419 | |
| 420 | bool Supported = false; |
| 421 | bool is_NSString = false; |
| 422 | bool is_strftime = false; |
| 423 | |
| 424 | switch (FormatLen) { |
| 425 | default: break; |
| 426 | case 5: |
| 427 | Supported = !memcmp(Format, "scanf", 5); |
| 428 | break; |
| 429 | case 6: |
| 430 | Supported = !memcmp(Format, "printf", 6); |
| 431 | break; |
| 432 | case 7: |
| 433 | Supported = !memcmp(Format, "strfmon", 7); |
| 434 | break; |
| 435 | case 8: |
| 436 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 437 | (is_NSString = !memcmp(Format, "NSString", 8)); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | if (!Supported) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 442 | Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 443 | "format", Attr.getParameterName()->getName()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| 447 | // checks for the 2nd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 448 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 449 | llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType())); |
| 450 | if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 451 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 452 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 457 | Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 458 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | // FIXME: Do we need to bounds check? |
| 463 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 464 | |
| 465 | // make sure the format string is really a string |
| 466 | QualType Ty = proto->getArgType(ArgIdx); |
| 467 | |
| 468 | if (is_NSString) { |
| 469 | // FIXME: do we need to check if the type is NSString*? What are |
| 470 | // the semantics? |
| 471 | if (!isNSStringType(Ty, Context)) { |
| 472 | // FIXME: Should highlight the actual expression that has the |
| 473 | // wrong type. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 474 | Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 475 | IdxExpr->getSourceRange()); |
| 476 | return; |
| 477 | } |
| 478 | } else if (!Ty->isPointerType() || |
| 479 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 480 | // FIXME: Should highlight the actual expression that has the |
| 481 | // wrong type. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 482 | Diag(Attr.getLoc(), diag::err_format_attribute_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 483 | IdxExpr->getSourceRange()); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // check the 3rd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 488 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 489 | llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType())); |
| 490 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 491 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 492 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | // check if the function is variadic if the 3rd argument non-zero |
| 497 | if (FirstArg != 0) { |
| 498 | if (proto->isVariadic()) { |
| 499 | ++NumArgs; // +1 for ... |
| 500 | } else { |
| 501 | Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
| 502 | return; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 507 | // the input is just the current time + the format string |
| 508 | if (is_strftime) { |
| 509 | if (FirstArg != 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 510 | Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 511 | FirstArgExpr->getSourceRange()); |
| 512 | return; |
| 513 | } |
| 514 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 515 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 516 | Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 517 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 522 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 523 | } |
| 524 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 525 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 526 | Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 527 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 528 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 529 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 530 | std::string("0")); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | TypeDecl *decl = dyn_cast<TypeDecl>(d); |
| 535 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 536 | if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) { |
| 537 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 538 | "transparent_union", "union"); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | //QualType QTy = Context.getTypeDeclType(decl); |
| 543 | //const RecordType *Ty = QTy->getAsUnionType(); |
| 544 | |
| 545 | // FIXME |
| 546 | // Ty->addAttr(new TransparentUnionAttr()); |
| 547 | } |
| 548 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 549 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 550 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 551 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 552 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 553 | std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 556 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 557 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 558 | |
| 559 | // Make sure that there is a string literal as the annotation's single |
| 560 | // argument. |
| 561 | if (!SE) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 562 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 566 | SE->getByteLength()))); |
| 567 | } |
| 568 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 569 | void Sema::HandleAlignedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 570 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 571 | if (Attr.getNumArgs() > 1) { |
| 572 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 573 | std::string("1")); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | unsigned Align = 0; |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 578 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 579 | // FIXME: This should be the target specific maximum alignment. |
| 580 | // (For now we just use 128 bits which is the maximum on X86. |
| 581 | Align = 128; |
| 582 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 583 | } |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 584 | |
| 585 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 586 | llvm::APSInt Alignment(32); |
| 587 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, Context)) { |
| 588 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 589 | "aligned", alignmentExpr->getSourceRange()); |
| 590 | return; |
| 591 | } |
| 592 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 593 | } |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 594 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 595 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 596 | /// primitive type. |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 597 | /// |
| 598 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 599 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 600 | /// 'G' be HImode, not an intermediate pointer. |
| 601 | /// |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 602 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 603 | // This attribute isn't documented, but glibc uses it. It changes |
| 604 | // the width of an int or unsigned int to the specified size. |
| 605 | |
| 606 | // Check that there aren't any arguments |
| 607 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 608 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 609 | std::string("0")); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
| 613 | IdentifierInfo *Name = Attr.getParameterName(); |
| 614 | if (!Name) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 615 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | const char *Str = Name->getName(); |
| 619 | unsigned Len = Name->getLength(); |
| 620 | |
| 621 | // Normalize the attribute name, __foo__ becomes foo. |
| 622 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 623 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 624 | Str += 2; |
| 625 | Len -= 4; |
| 626 | } |
| 627 | |
| 628 | unsigned DestWidth = 0; |
| 629 | bool IntegerMode = true; |
| 630 | switch (Len) { |
| 631 | case 2: |
| 632 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 633 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 634 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 635 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 636 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 637 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 638 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 639 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 640 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 641 | break; |
| 642 | case 4: |
| 643 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 644 | // pointer on PIC16 and other embedded platforms. |
| 645 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 646 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 647 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 648 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 649 | break; |
| 650 | case 7: |
| 651 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 652 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | |
| 656 | QualType OldTy; |
| 657 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 658 | OldTy = TD->getUnderlyingType(); |
| 659 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 660 | OldTy = VD->getType(); |
| 661 | else { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 662 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode", |
| 663 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | |
| 667 | // FIXME: Need proper fixed-width types |
| 668 | QualType NewTy; |
| 669 | switch (DestWidth) { |
| 670 | case 0: |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 671 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName()); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 672 | return; |
| 673 | default: |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 674 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName()); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 675 | return; |
| 676 | case 8: |
| 677 | assert(IntegerMode); |
| 678 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 679 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 680 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 681 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 682 | break; |
| 683 | case 16: |
| 684 | assert(IntegerMode); |
| 685 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 686 | NewTy = S.Context.ShortTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 687 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 688 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 689 | break; |
| 690 | case 32: |
| 691 | if (!IntegerMode) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 692 | NewTy = S.Context.FloatTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 693 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 694 | NewTy = S.Context.IntTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 695 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 696 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 697 | break; |
| 698 | case 64: |
| 699 | if (!IntegerMode) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 700 | NewTy = S.Context.DoubleTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 701 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 702 | NewTy = S.Context.LongLongTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 703 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 704 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | |
| 708 | if (!OldTy->getAsBuiltinType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 709 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 710 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 711 | !(!IntegerMode && OldTy->isFloatingType())) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 712 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Install the new type. |
| 716 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 717 | TD->setUnderlyingType(NewTy); |
| 718 | else |
| 719 | cast<ValueDecl>(D)->setType(NewTy); |
| 720 | } |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 721 | |
| 722 | //===----------------------------------------------------------------------===// |
| 723 | // Top Level Sema Entry Points |
| 724 | //===----------------------------------------------------------------------===// |
| 725 | |
| 726 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 727 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 728 | /// specified in many different places, and we need to find and apply them all. |
| 729 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 730 | // Apply decl attributes from the DeclSpec if present. |
| 731 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 732 | ProcessDeclAttributeList(D, Attrs); |
| 733 | |
| 734 | // Walk the declarator structure, applying decl attributes that were in a type |
| 735 | // position to the decl itself. This handles cases like: |
| 736 | // int *__attr__(x)** D; |
| 737 | // when X is a decl attribute. |
| 738 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 739 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 740 | ProcessDeclAttributeList(D, Attrs); |
| 741 | |
| 742 | // Finally, apply any attributes on the decl itself. |
| 743 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 744 | ProcessDeclAttributeList(D, Attrs); |
| 745 | } |
| 746 | |
| 747 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 748 | /// attribute list to the specified decl, ignoring any type attributes. |
| 749 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 750 | while (AttrList) { |
| 751 | ProcessDeclAttribute(D, *AttrList); |
| 752 | AttrList = AttrList->getNext(); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /// HandleDeclAttribute - Apply the specific attribute to the specified decl if |
| 757 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 758 | /// silently ignore it. |
| 759 | void Sema::ProcessDeclAttribute(Decl *D, const AttributeList &Attr) { |
| 760 | switch (Attr.getKind()) { |
| 761 | case AttributeList::AT_address_space: |
| 762 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 763 | break; |
| 764 | case AttributeList::AT_vector_size: HandleVectorSizeAttribute(D, Attr); break; |
| 765 | case AttributeList::AT_ext_vector_type: |
| 766 | HandleExtVectorTypeAttribute(D, Attr); |
| 767 | break; |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 768 | case AttributeList::AT_mode: HandleModeAttr(D, Attr, *this); break; |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 769 | case AttributeList::AT_alias: HandleAliasAttribute(D, Attr); break; |
| 770 | case AttributeList::AT_deprecated: HandleDeprecatedAttribute(D, Attr);break; |
| 771 | case AttributeList::AT_visibility: HandleVisibilityAttribute(D, Attr);break; |
| 772 | case AttributeList::AT_weak: HandleWeakAttribute(D, Attr); break; |
| 773 | case AttributeList::AT_dllimport: HandleDLLImportAttribute(D, Attr); break; |
| 774 | case AttributeList::AT_dllexport: HandleDLLExportAttribute(D, Attr); break; |
| 775 | case AttributeList::AT_nothrow: HandleNothrowAttribute(D, Attr); break; |
| 776 | case AttributeList::AT_stdcall: HandleStdCallAttribute(D, Attr); break; |
| 777 | case AttributeList::AT_fastcall: HandleFastCallAttribute(D, Attr); break; |
| 778 | case AttributeList::AT_aligned: HandleAlignedAttribute(D, Attr); break; |
| 779 | case AttributeList::AT_packed: HandlePackedAttribute(D, Attr); break; |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 780 | case AttributeList::AT_annotate: HandleAnnotateAttr(D, Attr, *this); break; |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 781 | case AttributeList::AT_noreturn: HandleNoReturnAttribute(D, Attr); break; |
| 782 | case AttributeList::AT_format: HandleFormatAttribute(D, Attr); break; |
| 783 | case AttributeList::AT_transparent_union: |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame^] | 784 | HandleTransparentUnionAttr(D, Attr, *this); |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 785 | break; |
| 786 | default: |
| 787 | #if 0 |
| 788 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 789 | Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 790 | Attr->getName()->getName()); |
| 791 | #endif |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | |