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