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 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 220 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 221 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 222 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 223 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 224 | std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 228 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 229 | Arg = Arg->IgnoreParenCasts(); |
| 230 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 231 | |
| 232 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 233 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 234 | "alias", std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
| 238 | const char *Alias = Str->getStrData(); |
| 239 | unsigned AliasLen = Str->getByteLength(); |
| 240 | |
| 241 | // FIXME: check if target symbol exists in current file |
| 242 | |
| 243 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 244 | } |
| 245 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 246 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 247 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 248 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 249 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 250 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
| 254 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 255 | if (!Fn) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 256 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 257 | "noreturn", "function"); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | d->addAttr(new NoReturnAttr()); |
| 262 | } |
| 263 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 264 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 265 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 266 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 268 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | d->addAttr(new DeprecatedAttr()); |
| 273 | } |
| 274 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 275 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 276 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 277 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 278 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 279 | std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 283 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 284 | Arg = Arg->IgnoreParenCasts(); |
| 285 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 286 | |
| 287 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 288 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
| 289 | "visibility", std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
| 293 | const char *TypeStr = Str->getStrData(); |
| 294 | unsigned TypeLen = Str->getByteLength(); |
| 295 | VisibilityAttr::VisibilityTypes type; |
| 296 | |
| 297 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 298 | type = VisibilityAttr::DefaultVisibility; |
| 299 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 300 | type = VisibilityAttr::HiddenVisibility; |
| 301 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 302 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 303 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 304 | type = VisibilityAttr::ProtectedVisibility; |
| 305 | else { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 306 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 307 | "visibility", TypeStr); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
| 311 | d->addAttr(new VisibilityAttr(type)); |
| 312 | } |
| 313 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 314 | static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 315 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 316 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 317 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 318 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
| 322 | d->addAttr(new WeakAttr()); |
| 323 | } |
| 324 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 325 | static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 326 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 327 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 328 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 329 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
| 333 | d->addAttr(new DLLImportAttr()); |
| 334 | } |
| 335 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 336 | static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 337 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 338 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 339 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 340 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
| 344 | d->addAttr(new DLLExportAttr()); |
| 345 | } |
| 346 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 347 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 348 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 349 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 350 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 351 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
| 355 | d->addAttr(new StdCallAttr()); |
| 356 | } |
| 357 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 358 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 359 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 360 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 361 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 362 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
| 366 | d->addAttr(new FastCallAttr()); |
| 367 | } |
| 368 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 369 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 370 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 371 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 372 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 373 | std::string("0")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | d->addAttr(new NoThrowAttr()); |
| 378 | } |
| 379 | |
| 380 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 381 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 382 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 384 | if (!Attr.getParameterName()) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 385 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 386 | "format", std::string("1")); |
| 387 | return; |
| 388 | } |
| 389 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 390 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 391 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 392 | std::string("3")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
| 396 | // GCC ignores the format attribute on K&R style function |
| 397 | // prototypes, so we ignore it as well |
| 398 | const FunctionTypeProto *proto = getFunctionProto(d); |
| 399 | |
| 400 | if (!proto) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 401 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
| 402 | "format", "function"); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
| 406 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 407 | // this is needed in order to be compatible with GCC |
| 408 | // the index must start in 1 and the limit is numargs+1 |
| 409 | unsigned NumArgs = proto->getNumArgs(); |
| 410 | unsigned FirstIdx = 1; |
| 411 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 412 | const char *Format = Attr.getParameterName()->getName(); |
| 413 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 414 | |
| 415 | // Normalize the argument, __foo__ becomes foo. |
| 416 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 417 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 418 | Format += 2; |
| 419 | FormatLen -= 4; |
| 420 | } |
| 421 | |
| 422 | bool Supported = false; |
| 423 | bool is_NSString = false; |
| 424 | bool is_strftime = false; |
| 425 | |
| 426 | switch (FormatLen) { |
| 427 | default: break; |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 428 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 429 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 430 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 431 | case 8: |
| 432 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 433 | (is_NSString = !memcmp(Format, "NSString", 8)); |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | if (!Supported) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 438 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 439 | "format", Attr.getParameterName()->getName()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | // checks for the 2nd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 444 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 445 | llvm::APSInt Idx(32); |
| 446 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| 447 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 448 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 453 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 454 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | // FIXME: Do we need to bounds check? |
| 459 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 460 | |
| 461 | // make sure the format string is really a string |
| 462 | QualType Ty = proto->getArgType(ArgIdx); |
| 463 | |
| 464 | if (is_NSString) { |
| 465 | // FIXME: do we need to check if the type is NSString*? What are |
| 466 | // the semantics? |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 467 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 468 | // FIXME: Should highlight the actual expression that has the |
| 469 | // wrong type. |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 470 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString, |
| 471 | IdxExpr->getSourceRange()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | } else if (!Ty->isPointerType() || |
| 475 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 476 | // FIXME: Should highlight the actual expression that has the |
| 477 | // wrong type. |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 478 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string, |
| 479 | IdxExpr->getSourceRange()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | // check the 3rd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 484 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 485 | llvm::APSInt FirstArg(32); |
| 486 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
| 487 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 488 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | // check if the function is variadic if the 3rd argument non-zero |
| 493 | if (FirstArg != 0) { |
| 494 | if (proto->isVariadic()) { |
| 495 | ++NumArgs; // +1 for ... |
| 496 | } else { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 497 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 503 | // the input is just the current time + the format string |
| 504 | if (is_strftime) { |
| 505 | if (FirstArg != 0) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 506 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 507 | FirstArgExpr->getSourceRange()); |
| 508 | return; |
| 509 | } |
| 510 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 511 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 512 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 513 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 518 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 519 | } |
| 520 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 521 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 522 | Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 523 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 524 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 525 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 526 | std::string("0")); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | TypeDecl *decl = dyn_cast<TypeDecl>(d); |
| 531 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 532 | if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) { |
| 533 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 534 | "transparent_union", "union"); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | //QualType QTy = Context.getTypeDeclType(decl); |
| 539 | //const RecordType *Ty = QTy->getAsUnionType(); |
| 540 | |
| 541 | // FIXME |
| 542 | // Ty->addAttr(new TransparentUnionAttr()); |
| 543 | } |
| 544 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 545 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 546 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 547 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 548 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 549 | std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 550 | return; |
| 551 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 552 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 553 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 554 | |
| 555 | // Make sure that there is a string literal as the annotation's single |
| 556 | // argument. |
| 557 | if (!SE) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 558 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 562 | SE->getByteLength()))); |
| 563 | } |
| 564 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 565 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 566 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 567 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 568 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 569 | std::string("1")); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | |
| 573 | unsigned Align = 0; |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 574 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 575 | // FIXME: This should be the target specific maximum alignment. |
| 576 | // (For now we just use 128 bits which is the maximum on X86. |
| 577 | Align = 128; |
| 578 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 579 | } |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 580 | |
| 581 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 582 | llvm::APSInt Alignment(32); |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 583 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
| 584 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 585 | "aligned", alignmentExpr->getSourceRange()); |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 589 | } |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 590 | |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 591 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 592 | /// primitive type. |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 593 | /// |
| 594 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 595 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 596 | /// 'G' be HImode, not an intermediate pointer. |
| 597 | /// |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 598 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 599 | // This attribute isn't documented, but glibc uses it. It changes |
| 600 | // the width of an int or unsigned int to the specified size. |
| 601 | |
| 602 | // Check that there aren't any arguments |
| 603 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 604 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 605 | std::string("0")); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | IdentifierInfo *Name = Attr.getParameterName(); |
| 610 | if (!Name) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 611 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | const char *Str = Name->getName(); |
| 615 | unsigned Len = Name->getLength(); |
| 616 | |
| 617 | // Normalize the attribute name, __foo__ becomes foo. |
| 618 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 619 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 620 | Str += 2; |
| 621 | Len -= 4; |
| 622 | } |
| 623 | |
| 624 | unsigned DestWidth = 0; |
| 625 | bool IntegerMode = true; |
| 626 | switch (Len) { |
| 627 | case 2: |
| 628 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 629 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 630 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 631 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 632 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 633 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 634 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 635 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 636 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 637 | break; |
| 638 | case 4: |
| 639 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 640 | // pointer on PIC16 and other embedded platforms. |
| 641 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 642 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 643 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 644 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 645 | break; |
| 646 | case 7: |
| 647 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 648 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | |
| 652 | QualType OldTy; |
| 653 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 654 | OldTy = TD->getUnderlyingType(); |
| 655 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 656 | OldTy = VD->getType(); |
| 657 | else { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 658 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode", |
| 659 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | // FIXME: Need proper fixed-width types |
| 664 | QualType NewTy; |
| 665 | switch (DestWidth) { |
| 666 | case 0: |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 667 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName()); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 668 | return; |
| 669 | default: |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 670 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName()); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 671 | return; |
| 672 | case 8: |
| 673 | assert(IntegerMode); |
| 674 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 675 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 676 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 677 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 678 | break; |
| 679 | case 16: |
| 680 | assert(IntegerMode); |
| 681 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 682 | NewTy = S.Context.ShortTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 683 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 684 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 685 | break; |
| 686 | case 32: |
| 687 | if (!IntegerMode) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 688 | NewTy = S.Context.FloatTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 689 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 690 | NewTy = S.Context.IntTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 691 | else |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 692 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 693 | break; |
| 694 | case 64: |
| 695 | if (!IntegerMode) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 696 | NewTy = S.Context.DoubleTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 697 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 698 | NewTy = S.Context.LongLongTy; |
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.UnsignedLongLongTy; |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (!OldTy->getAsBuiltinType()) |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 705 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 706 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 707 | !(!IntegerMode && OldTy->isFloatingType())) { |
Chris Lattner | f669015 | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 708 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | // Install the new type. |
| 712 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 713 | TD->setUnderlyingType(NewTy); |
| 714 | else |
| 715 | cast<ValueDecl>(D)->setType(NewTy); |
| 716 | } |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 717 | |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | // Top Level Sema Entry Points |
| 720 | //===----------------------------------------------------------------------===// |
| 721 | |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 722 | /// HandleDeclAttribute - Apply the specific attribute to the specified decl if |
| 723 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 724 | /// silently ignore it. |
| 725 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 726 | switch (Attr.getKind()) { |
| 727 | case AttributeList::AT_address_space: |
| 728 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 729 | break; |
| 730 | case AttributeList::AT_ext_vector_type: |
| 731 | HandleExtVectorTypeAttr(D, Attr, S); |
| 732 | break; |
| 733 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
| 734 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 735 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 736 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 737 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
| 738 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
| 739 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
| 740 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 741 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 742 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
| 743 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 744 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
| 745 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 746 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 747 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 748 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 749 | case AttributeList::AT_transparent_union: |
| 750 | HandleTransparentUnionAttr(D, Attr, S); |
| 751 | break; |
| 752 | default: |
| 753 | #if 0 |
| 754 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 755 | S.Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 756 | Attr->getName()->getName()); |
| 757 | #endif |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 763 | /// attribute list to the specified decl, ignoring any type attributes. |
| 764 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 765 | while (AttrList) { |
| 766 | ProcessDeclAttribute(D, *AttrList, *this); |
| 767 | AttrList = AttrList->getNext(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 772 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 773 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 774 | /// specified in many different places, and we need to find and apply them all. |
| 775 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 776 | // Apply decl attributes from the DeclSpec if present. |
| 777 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 778 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 703c52d | 2008-06-29 00:43:07 +0000 | [diff] [blame^] | 779 | |
Chris Lattner | a72440d | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 780 | // Walk the declarator structure, applying decl attributes that were in a type |
| 781 | // position to the decl itself. This handles cases like: |
| 782 | // int *__attr__(x)** D; |
| 783 | // when X is a decl attribute. |
| 784 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 785 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 786 | ProcessDeclAttributeList(D, Attrs); |
| 787 | |
| 788 | // Finally, apply any attributes on the decl itself. |
| 789 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 790 | ProcessDeclAttributeList(D, Attrs); |
| 791 | } |
| 792 | |