Chris Lattner | 6b6b537 | 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" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 21 | #include <llvm/ADT/StringExtras.h> |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Helper functions |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 28 | static const FunctionType *getFunctionType(Decl *d) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 29 | QualType Ty; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | if (ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
| 31 | Ty = decl->getType(); |
| 32 | else if (FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
| 33 | Ty = decl->getType(); |
| 34 | else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
| 35 | Ty = decl->getUnderlyingType(); |
| 36 | else |
| 37 | return 0; |
| 38 | |
| 39 | if (Ty->isFunctionPointerType()) |
| 40 | Ty = Ty->getAsPointerType()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 41 | |
| 42 | return Ty->getAsFunctionType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 45 | // FIXME: We should provide an abstraction around a method or function |
| 46 | // to provide the following bits of information. |
| 47 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 48 | /// isFunctionOrMethod - Return true if the given decl has function |
| 49 | /// type (function or function-typed variable) or an Objective-C |
| 50 | /// method. |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 51 | static bool isFunctionOrMethod(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 52 | return getFunctionType(d) || isa<ObjCMethodDecl>(d); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 55 | /// hasFunctionProto - Return true if the given decl has a argument |
| 56 | /// information. This decl should have already passed |
| 57 | /// isFunctionOrMethod. |
| 58 | static bool hasFunctionProto(Decl *d) { |
| 59 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 60 | return isa<FunctionTypeProto>(FnTy); |
| 61 | } else { |
| 62 | assert(isa<ObjCMethodDecl>(d)); |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 68 | /// arguments. It is an error to call this on a K&R function (use |
| 69 | /// hasFunctionProto first). |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 70 | static unsigned getFunctionOrMethodNumArgs(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 71 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 72 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 73 | return proto->getNumArgs(); |
| 74 | } else { |
| 75 | return cast<ObjCMethodDecl>(d)->getNumParams(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 80 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 81 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 82 | return proto->getArgType(Idx); |
| 83 | } else { |
| 84 | return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static bool isFunctionOrMethodVariadic(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 89 | if (const FunctionType *FnTy = getFunctionType(d)) { |
| 90 | const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 91 | return proto->isVariadic(); |
| 92 | } else { |
| 93 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 94 | } |
| 95 | } |
| 96 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 97 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 98 | const PointerType *PT = T->getAsPointerType(); |
| 99 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 100 | return false; |
| 101 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 102 | const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 103 | if (!ClsT) |
| 104 | return false; |
| 105 | |
| 106 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 107 | |
| 108 | // FIXME: Should we walk the chain of classes? |
| 109 | return ClsName == &Ctx.Idents.get("NSString") || |
| 110 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 111 | } |
| 112 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 113 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
| 114 | const PointerType *PT = T->getAsPointerType(); |
| 115 | if (!PT) |
| 116 | return false; |
| 117 | |
| 118 | const RecordType *RT = PT->getPointeeType()->getAsRecordType(); |
| 119 | if (!RT) |
| 120 | return false; |
| 121 | |
| 122 | const RecordDecl *RD = RT->getDecl(); |
| 123 | if (RD->getTagKind() != TagDecl::TK_struct) |
| 124 | return false; |
| 125 | |
| 126 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 127 | } |
| 128 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 130 | // Attribute Implementations |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 133 | // FIXME: All this manual attribute parsing code is gross. At the |
| 134 | // least add some helper functions to check most argument patterns (# |
| 135 | // and types of args). |
| 136 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 137 | static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr, |
| 138 | Sema &S) { |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 139 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 140 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 141 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 142 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 145 | QualType curType = tDecl->getUnderlyingType(); |
| 146 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 147 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 148 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 151 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 152 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 153 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 154 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 155 | << "ext_vector_type" << sizeExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 159 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 160 | if (!curType->isIntegerType() && !curType->isRealFloatingType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 161 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | // unlike gcc's vector_size attribute, the size is specified as the |
| 165 | // number of elements, not the number of bytes. |
| 166 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 167 | |
| 168 | if (vectorSize == 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 169 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 170 | << sizeExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | // Instantiate/Install the vector type, the number of elements is > 0. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 174 | tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 175 | // Remember this typedef decl, we will need it later for diagnostics. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 176 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 179 | |
| 180 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 181 | /// integral and float scalars, although arrays, pointers, and function |
| 182 | /// return values are allowed in conjunction with this construct. Aggregates |
| 183 | /// with this attribute are invalid, even if they are of the same size as a |
| 184 | /// corresponding scalar. |
| 185 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 186 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 187 | /// formed, this routine will return a new vector type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 188 | static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 189 | QualType CurType; |
| 190 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 191 | CurType = VD->getType(); |
| 192 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 193 | CurType = TD->getUnderlyingType(); |
| 194 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 195 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 196 | << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | // Check the attribute arugments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 201 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 202 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 203 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 205 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 206 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 207 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 208 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 209 | << "vector_size" << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 210 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 211 | } |
| 212 | // navigate to the base type - we need to provide for vector pointers, |
| 213 | // vector arrays, and functions returning vectors. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 214 | if (CurType->isPointerType() || CurType->isArrayType() || |
| 215 | CurType->isFunctionType()) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 216 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
| 217 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 218 | do { |
| 219 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 220 | canonType = PT->getPointeeType().getTypePtr(); |
| 221 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 222 | canonType = AT->getElementType().getTypePtr(); |
| 223 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 224 | canonType = FT->getResultType().getTypePtr(); |
| 225 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 226 | canonType->isFunctionType()); |
| 227 | */ |
| 228 | } |
| 229 | // the base type must be integer or float. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 230 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 231 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 232 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 233 | } |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 234 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 235 | // vecSize is specified in bytes - convert to bits. |
| 236 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 237 | |
| 238 | // the vector size needs to be an integral multiple of the type size. |
| 239 | if (vectorSize % typeSize) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 240 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 241 | << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 242 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 243 | } |
| 244 | if (vectorSize == 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 245 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 246 | << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 247 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 249 | |
| 250 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 251 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 252 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 253 | |
| 254 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 255 | VD->setType(CurType); |
| 256 | else |
| 257 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 260 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 261 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 262 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 263 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 268 | TD->addAttr(new PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 269 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 270 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 271 | // has no effect. |
| 272 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 273 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 274 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 275 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 276 | else |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 277 | FD->addAttr(new PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 278 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 279 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 282 | static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 283 | // check the attribute arguments. |
| 284 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 285 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
| 288 | |
| 289 | // The IBOutlet attribute only applies to instance variables of Objective-C |
| 290 | // classes. |
| 291 | if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d)) |
| 292 | ID->addAttr(new IBOutletAttr()); |
| 293 | else |
| 294 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar); |
| 295 | } |
| 296 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 297 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 298 | // GCC ignores the nonnull attribute on K&R style function |
| 299 | // prototypes, so we ignore it as well |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 300 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 301 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 302 | << "nonnull" << "function"; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 306 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 307 | |
| 308 | // The nonnull attribute only applies to pointers. |
| 309 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
| 310 | |
| 311 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 312 | E=Attr.arg_end(); I!=E; ++I) { |
| 313 | |
| 314 | |
| 315 | // The argument must be an integer constant expression. |
Ted Kremenek | f5e8834 | 2008-12-04 19:38:33 +0000 | [diff] [blame] | 316 | Expr *Ex = static_cast<Expr *>(*I); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 317 | llvm::APSInt ArgNum(32); |
| 318 | if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 319 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 320 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | |
| 324 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 325 | |
| 326 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 327 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 328 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 329 | return; |
| 330 | } |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 331 | |
| 332 | --x; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 333 | |
| 334 | // Is the function argument a pointer type? |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 335 | QualType T = getFunctionOrMethodArgType(d, x); |
| 336 | if (!T->isPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 337 | // FIXME: Should also highlight argument in decl. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 338 | S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only) |
| 339 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 340 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | NonNullArgs.push_back(x); |
| 344 | } |
| 345 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 346 | // If no arguments were specified to __attribute__((nonnull)) then all |
| 347 | // pointer arguments have a nonnull attribute. |
| 348 | if (NonNullArgs.empty()) { |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 349 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
| 350 | QualType T = getFunctionOrMethodArgType(d, I); |
| 351 | if (T->isPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 352 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 353 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 354 | |
| 355 | if (NonNullArgs.empty()) { |
| 356 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 357 | return; |
| 358 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 359 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 360 | |
| 361 | unsigned* start = &NonNullArgs[0]; |
| 362 | unsigned size = NonNullArgs.size(); |
| 363 | std::sort(start, start + size); |
| 364 | d->addAttr(new NonNullAttr(start, size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 367 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 368 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 369 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 370 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 374 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 375 | Arg = Arg->IgnoreParenCasts(); |
| 376 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 377 | |
| 378 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 379 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 380 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
| 384 | const char *Alias = Str->getStrData(); |
| 385 | unsigned AliasLen = Str->getByteLength(); |
| 386 | |
| 387 | // FIXME: check if target symbol exists in current file |
| 388 | |
| 389 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 390 | } |
| 391 | |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 392 | static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, |
| 393 | Sema &S) { |
| 394 | // check the attribute arguments. |
| 395 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 396 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | |
| 400 | d->addAttr(new AlwaysInlineAttr()); |
| 401 | } |
| 402 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 403 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 404 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 405 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 406 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 407 | return; |
| 408 | } |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 409 | |
| 410 | if (!isFunctionOrMethod(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 411 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 412 | << "noreturn" << "function"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | d->addAttr(new NoReturnAttr()); |
| 417 | } |
| 418 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 419 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 420 | // check the attribute arguments. |
| 421 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 422 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 426 | if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 427 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 428 | << "unused" << "variable and function"; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | d->addAttr(new UnusedAttr()); |
| 433 | } |
| 434 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 435 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 436 | // check the attribute arguments. |
| 437 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 438 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 439 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 444 | if (Attr.getNumArgs() > 0) { |
| 445 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 446 | llvm::APSInt Idx(32); |
| 447 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 448 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 449 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | priority = Idx.getZExtValue(); |
| 453 | } |
| 454 | |
| 455 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 456 | if (!Fn) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 457 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 458 | << "constructor" << "function"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | d->addAttr(new ConstructorAttr(priority)); |
| 463 | } |
| 464 | |
| 465 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 466 | // check the attribute arguments. |
| 467 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 468 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 469 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 474 | if (Attr.getNumArgs() > 0) { |
| 475 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 476 | llvm::APSInt Idx(32); |
| 477 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 478 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 479 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | priority = Idx.getZExtValue(); |
| 483 | } |
| 484 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 485 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 486 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 487 | << "destructor" << "function"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | d->addAttr(new DestructorAttr(priority)); |
| 492 | } |
| 493 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 494 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 495 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 496 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 497 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | d->addAttr(new DeprecatedAttr()); |
| 502 | } |
| 503 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 504 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 505 | // check the attribute arguments. |
| 506 | if (Attr.getNumArgs() != 0) { |
| 507 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | d->addAttr(new UnavailableAttr()); |
| 512 | } |
| 513 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 514 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 515 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 516 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 517 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 521 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 522 | Arg = Arg->IgnoreParenCasts(); |
| 523 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 524 | |
| 525 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 526 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 527 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | |
| 531 | const char *TypeStr = Str->getStrData(); |
| 532 | unsigned TypeLen = Str->getByteLength(); |
| 533 | VisibilityAttr::VisibilityTypes type; |
| 534 | |
| 535 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 536 | type = VisibilityAttr::DefaultVisibility; |
| 537 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 538 | type = VisibilityAttr::HiddenVisibility; |
| 539 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 540 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 541 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 542 | type = VisibilityAttr::ProtectedVisibility; |
| 543 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 544 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | |
| 548 | d->addAttr(new VisibilityAttr(type)); |
| 549 | } |
| 550 | |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 551 | static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 6e14a8f | 2008-08-24 16:33:25 +0000 | [diff] [blame] | 552 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 553 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 554 | << "objc_gc" << 1; |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 559 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 563 | |
| 564 | ObjCGCAttr::GCAttrTypes type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 565 | if (Attr.getParameterName()->isStr("weak")) { |
Fariborz Jahanian | 24b93f2 | 2008-11-20 19:35:51 +0000 | [diff] [blame] | 566 | if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d)) |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 567 | S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field); |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 568 | type = ObjCGCAttr::Weak; |
Fariborz Jahanian | 2682d8b | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 569 | } |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 570 | else if (Attr.getParameterName()->isStr("strong")) |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 571 | type = ObjCGCAttr::Strong; |
| 572 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 573 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 574 | << "objc_gc" << Attr.getParameterName(); |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
| 578 | d->addAttr(new ObjCGCAttr(type)); |
| 579 | } |
| 580 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 581 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 582 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 583 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 584 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
| 588 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 589 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 590 | return; |
| 591 | } |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 592 | |
| 593 | BlocksAttr::BlocksAttrTypes type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 594 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 595 | type = BlocksAttr::ByRef; |
| 596 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 597 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 598 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | |
| 602 | d->addAttr(new BlocksAttr(type)); |
| 603 | } |
| 604 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 605 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 606 | // check the attribute arguments. |
| 607 | if (Attr.getNumArgs() > 2) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 608 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 609 | << "0, 1 or 2"; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
| 613 | int sentinel = 0; |
| 614 | if (Attr.getNumArgs() > 0) { |
| 615 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 616 | llvm::APSInt Idx(32); |
| 617 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 618 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 619 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | sentinel = Idx.getZExtValue(); |
| 623 | |
| 624 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 625 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 626 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | int nullPos = 0; |
| 632 | if (Attr.getNumArgs() > 1) { |
| 633 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 634 | llvm::APSInt Idx(32); |
| 635 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 636 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 637 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | nullPos = Idx.getZExtValue(); |
| 641 | |
| 642 | if (nullPos > 1 || nullPos < 0) { |
| 643 | // FIXME: This error message could be improved, it would be nice |
| 644 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 645 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 646 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
| 652 | QualType FT = FD->getType(); |
| 653 | if (!FT->getAsFunctionTypeProto()->isVariadic()) { |
| 654 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 655 | return; |
| 656 | } |
| 657 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 658 | if (!MD->isVariadic()) { |
| 659 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 660 | return; |
| 661 | } |
| 662 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 663 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 664 | << "sentinel" << "function or method"; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | // FIXME: Actually create the attribute. |
| 669 | } |
| 670 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 671 | static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 672 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 673 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 674 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
| 678 | d->addAttr(new WeakAttr()); |
| 679 | } |
| 680 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 681 | static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 682 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 683 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 684 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 685 | return; |
| 686 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 687 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 688 | // Attribute can be applied only to functions or variables. |
| 689 | if (isa<VarDecl>(d)) { |
| 690 | d->addAttr(new DLLImportAttr()); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | FunctionDecl *FD = dyn_cast<FunctionDecl>(d); |
| 695 | if (!FD) { |
| 696 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 697 | << "dllimport" << "function or variable"; |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 702 | // Warning is emitted. |
| 703 | if (FD->isInline()) { |
| 704 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | // The attribute is also overridden by a subsequent declaration as dllexport. |
| 709 | // Warning is emitted. |
| 710 | for (AttributeList *nextAttr = Attr.getNext(); nextAttr; |
| 711 | nextAttr = nextAttr->getNext()) { |
| 712 | if (nextAttr->getKind() == AttributeList::AT_dllexport) { |
| 713 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 714 | return; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (d->getAttr<DLLExportAttr>()) { |
| 719 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 720 | return; |
| 721 | } |
| 722 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 723 | d->addAttr(new DLLImportAttr()); |
| 724 | } |
| 725 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 726 | static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 727 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 728 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 729 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 730 | return; |
| 731 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 732 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 733 | // Attribute can be applied only to functions or variables. |
| 734 | if (isa<VarDecl>(d)) { |
| 735 | d->addAttr(new DLLExportAttr()); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | FunctionDecl *FD = dyn_cast<FunctionDecl>(d); |
| 740 | if (!FD) { |
| 741 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 742 | << "dllexport" << "function or variable"; |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | // Currently, the dllexport attribute is ignored for inlined functions, |
| 747 | // unless the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 748 | if (FD->isInline()) { |
| 749 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 750 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport"; |
| 751 | return; |
| 752 | } |
| 753 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 754 | d->addAttr(new DLLExportAttr()); |
| 755 | } |
| 756 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 757 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 758 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 759 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 760 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 761 | return; |
| 762 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 763 | |
| 764 | // Attribute can be applied only to functions. |
| 765 | if (!isa<FunctionDecl>(d)) { |
| 766 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 767 | << "stdcall" << "function"; |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | // stdcall and fastcall attributes are mutually incompatible. |
| 772 | if (d->getAttr<FastCallAttr>()) { |
| 773 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 774 | << "stdcall" << "fastcall"; |
| 775 | return; |
| 776 | } |
| 777 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 778 | d->addAttr(new StdCallAttr()); |
| 779 | } |
| 780 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 781 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 782 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 783 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 784 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 785 | return; |
| 786 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 787 | |
| 788 | if (!isa<FunctionDecl>(d)) { |
| 789 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 790 | << "fastcall" << "function"; |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | // stdcall and fastcall attributes are mutually incompatible. |
| 795 | if (d->getAttr<StdCallAttr>()) { |
| 796 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 797 | << "fastcall" << "stdcall"; |
| 798 | return; |
| 799 | } |
| 800 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 801 | d->addAttr(new FastCallAttr()); |
| 802 | } |
| 803 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 804 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 805 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 806 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 807 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | |
| 811 | d->addAttr(new NoThrowAttr()); |
| 812 | } |
| 813 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 814 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 815 | // check the attribute arguments. |
| 816 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 817 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 818 | return; |
| 819 | } |
| 820 | |
| 821 | d->addAttr(new ConstAttr()); |
| 822 | } |
| 823 | |
| 824 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 825 | // check the attribute arguments. |
| 826 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 827 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | |
| 831 | d->addAttr(new PureAttr()); |
| 832 | } |
| 833 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 834 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 835 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 836 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 838 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 839 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 840 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 841 | return; |
| 842 | } |
| 843 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 844 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 845 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 849 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 850 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 851 | << "format" << "function"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | |
| 855 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 856 | // this is needed in order to be compatible with GCC |
| 857 | // the index must start in 1 and the limit is numargs+1 |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 858 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 859 | unsigned FirstIdx = 1; |
| 860 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 861 | const char *Format = Attr.getParameterName()->getName(); |
| 862 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 863 | |
| 864 | // Normalize the argument, __foo__ becomes foo. |
| 865 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 866 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 867 | Format += 2; |
| 868 | FormatLen -= 4; |
| 869 | } |
| 870 | |
| 871 | bool Supported = false; |
| 872 | bool is_NSString = false; |
| 873 | bool is_strftime = false; |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 874 | bool is_CFString = false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 875 | |
| 876 | switch (FormatLen) { |
| 877 | default: break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 878 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 879 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 880 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 881 | case 8: |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 882 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 883 | (is_NSString = !memcmp(Format, "NSString", 8)) || |
| 884 | (is_CFString = !memcmp(Format, "CFString", 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
| 887 | |
| 888 | if (!Supported) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 889 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 890 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | |
| 894 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 895 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 896 | llvm::APSInt Idx(32); |
| 897 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 898 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 899 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
| 903 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 904 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 905 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 906 | return; |
| 907 | } |
| 908 | |
| 909 | // FIXME: Do we need to bounds check? |
| 910 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 911 | |
| 912 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 913 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 914 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 915 | if (is_CFString) { |
| 916 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 917 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 918 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | } else if (is_NSString) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 922 | // FIXME: do we need to check if the type is NSString*? What are |
| 923 | // the semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 924 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 925 | // FIXME: Should highlight the actual expression that has the |
| 926 | // wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 927 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 928 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | } else if (!Ty->isPointerType() || |
| 932 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 933 | // FIXME: Should highlight the actual expression that has the |
| 934 | // wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 935 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 936 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
| 940 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 941 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 942 | llvm::APSInt FirstArg(32); |
| 943 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 944 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 945 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 946 | return; |
| 947 | } |
| 948 | |
| 949 | // check if the function is variadic if the 3rd argument non-zero |
| 950 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 951 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 952 | ++NumArgs; // +1 for ... |
| 953 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 954 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | } |
| 958 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 959 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 960 | // variable the input is just the current time + the format string. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 961 | if (is_strftime) { |
| 962 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 963 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 964 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 968 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 969 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 970 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | |
| 974 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 975 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 976 | } |
| 977 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 978 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 979 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 980 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 981 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 982 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 986 | // FIXME: This shouldn't be restricted to typedefs |
| 987 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
| 988 | if (!TD || !TD->getUnderlyingType()->isUnionType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 989 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 990 | << "transparent_union" << "union"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 994 | RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 995 | |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 996 | // FIXME: Should we do a check for RD->isDefinition()? |
| 997 | |
| 998 | // FIXME: This isn't supposed to be restricted to pointers, but otherwise |
| 999 | // we might silently generate incorrect code; see following code |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1000 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1001 | FieldEnd = RD->field_end(); |
| 1002 | Field != FieldEnd; ++Field) { |
| 1003 | if (!Field->getType()->isPointerType()) { |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1004 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer); |
| 1005 | return; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // FIXME: This is a complete hack; we should be properly propagating |
| 1010 | // transparent_union through Sema. That said, this is close enough to |
| 1011 | // correctly compile all the common cases of transparent_union without |
| 1012 | // errors or warnings |
| 1013 | QualType NewTy = S.Context.VoidPtrTy; |
| 1014 | NewTy.addConst(); |
| 1015 | TD->setUnderlyingType(NewTy); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1018 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1019 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1020 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1021 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1024 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1025 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 1026 | |
| 1027 | // Make sure that there is a string literal as the annotation's single |
| 1028 | // argument. |
| 1029 | if (!SE) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1030 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
| 1033 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 1034 | SE->getByteLength()))); |
| 1035 | } |
| 1036 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1037 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1038 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1039 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1040 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1045 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1046 | // FIXME: This should be the target specific maximum alignment. |
| 1047 | // (For now we just use 128 bits which is the maximum on X86. |
| 1048 | Align = 128; |
| 1049 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1050 | } |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1051 | |
| 1052 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1053 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1054 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1055 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1056 | << "aligned" << alignmentExpr->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1060 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1062 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 1063 | /// primitive type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1064 | /// |
| 1065 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 1066 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 1067 | /// 'G' be HImode, not an intermediate pointer. |
| 1068 | /// |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1069 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1070 | // This attribute isn't documented, but glibc uses it. It changes |
| 1071 | // the width of an int or unsigned int to the specified size. |
| 1072 | |
| 1073 | // Check that there aren't any arguments |
| 1074 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1075 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1080 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1081 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1082 | return; |
| 1083 | } |
| 1084 | const char *Str = Name->getName(); |
| 1085 | unsigned Len = Name->getLength(); |
| 1086 | |
| 1087 | // Normalize the attribute name, __foo__ becomes foo. |
| 1088 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 1089 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 1090 | Str += 2; |
| 1091 | Len -= 4; |
| 1092 | } |
| 1093 | |
| 1094 | unsigned DestWidth = 0; |
| 1095 | bool IntegerMode = true; |
| 1096 | switch (Len) { |
| 1097 | case 2: |
| 1098 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 1099 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 1100 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 1101 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 1102 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 1103 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 1104 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 1105 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 1106 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 1107 | break; |
| 1108 | case 4: |
| 1109 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1110 | // pointer on PIC16 and other embedded platforms. |
| 1111 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1112 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1113 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1114 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1115 | break; |
| 1116 | case 7: |
| 1117 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1118 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | } |
| 1121 | |
| 1122 | QualType OldTy; |
| 1123 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1124 | OldTy = TD->getUnderlyingType(); |
| 1125 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1126 | OldTy = VD->getType(); |
| 1127 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1128 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 1129 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | // FIXME: Need proper fixed-width types |
| 1134 | QualType NewTy; |
| 1135 | switch (DestWidth) { |
| 1136 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1137 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1138 | return; |
| 1139 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | case 8: |
| 1143 | assert(IntegerMode); |
| 1144 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1145 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1146 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1147 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | case 16: |
| 1150 | assert(IntegerMode); |
| 1151 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1152 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1153 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1154 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | case 32: |
| 1157 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1158 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1159 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1160 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1161 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1162 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | case 64: |
| 1165 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1166 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1167 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1168 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1169 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1170 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | if (!OldTy->getAsBuiltinType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1175 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1176 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 1177 | !(!IntegerMode && OldTy->isFloatingType())) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1178 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | // Install the new type. |
| 1182 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1183 | TD->setUnderlyingType(NewTy); |
| 1184 | else |
| 1185 | cast<ValueDecl>(D)->setType(NewTy); |
| 1186 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1187 | |
| 1188 | //===----------------------------------------------------------------------===// |
| 1189 | // Top Level Sema Entry Points |
| 1190 | //===----------------------------------------------------------------------===// |
| 1191 | |
Sebastian Redl | a89d82c | 2008-12-21 19:24:58 +0000 | [diff] [blame] | 1192 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1193 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 1194 | /// silently ignore it. |
| 1195 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1196 | switch (Attr.getKind()) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1197 | case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1198 | case AttributeList::AT_address_space: |
| 1199 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 1200 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1201 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 1202 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1203 | case AttributeList::AT_always_inline: |
| 1204 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1205 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 1206 | case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break; |
| 1207 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 1208 | case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break; |
| 1209 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 1210 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1211 | case AttributeList::AT_ext_vector_type: |
| 1212 | HandleExtVectorTypeAttr(D, Attr, S); |
| 1213 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1214 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 1215 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1216 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1217 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
| 1218 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 1219 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 1220 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 1221 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1222 | case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1223 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 1224 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1225 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
| 1226 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1227 | case AttributeList::AT_transparent_union: |
| 1228 | HandleTransparentUnionAttr(D, Attr, S); |
| 1229 | break; |
Anders Carlsson | aa0d25b | 2008-08-23 23:22:21 +0000 | [diff] [blame] | 1230 | case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1231 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1232 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1233 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 1234 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1235 | default: |
| 1236 | #if 0 |
| 1237 | // TODO: when we have the full set of attributes, warn about unknown ones. |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1238 | S.Diag(Attr->getLoc(), diag::warn_attribute_ignored) << Attr->getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1239 | #endif |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 1245 | /// attribute list to the specified decl, ignoring any type attributes. |
| 1246 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 1247 | while (AttrList) { |
| 1248 | ProcessDeclAttribute(D, *AttrList, *this); |
| 1249 | AttrList = AttrList->getNext(); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1254 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 1255 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 1256 | /// specified in many different places, and we need to find and apply them all. |
| 1257 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 1258 | // Apply decl attributes from the DeclSpec if present. |
| 1259 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 1260 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1262 | // Walk the declarator structure, applying decl attributes that were in a type |
| 1263 | // position to the decl itself. This handles cases like: |
| 1264 | // int *__attr__(x)** D; |
| 1265 | // when X is a decl attribute. |
| 1266 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 1267 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 1268 | ProcessDeclAttributeList(D, Attrs); |
| 1269 | |
| 1270 | // Finally, apply any attributes on the decl itself. |
| 1271 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 1272 | ProcessDeclAttributeList(D, Attrs); |
| 1273 | } |
| 1274 | |