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