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