blob: b4e3dd99ac7747194e07e533072965f37b60aed3 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- 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 Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Daniel Dunbard3f2c102008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000040
41 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000042}
43
Daniel Dunbar35682492008-09-26 04:12:28 +000044// FIXME: We should provide an abstraction around a method or function
45// to provide the following bits of information.
46
Daniel Dunbard3f2c102008-10-19 02:04:16 +000047/// isFunctionOrMethod - Return true if the given decl has function
48/// type (function or function-typed variable) or an Objective-C
49/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000050static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000051 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000052}
53
Daniel Dunbard3f2c102008-10-19 02:04:16 +000054/// hasFunctionProto - Return true if the given decl has a argument
55/// information. This decl should have already passed
56/// isFunctionOrMethod.
57static bool hasFunctionProto(Decl *d) {
58 if (const FunctionType *FnTy = getFunctionType(d)) {
59 return isa<FunctionTypeProto>(FnTy);
60 } else {
61 assert(isa<ObjCMethodDecl>(d));
62 return true;
63 }
64}
65
66/// getFunctionOrMethodNumArgs - Return number of function or method
67/// arguments. It is an error to call this on a K&R function (use
68/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070 if (const FunctionType *FnTy = getFunctionType(d)) {
71 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000072 return proto->getNumArgs();
73 } else {
74 return cast<ObjCMethodDecl>(d)->getNumParams();
75 }
76}
77
78static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000079 if (const FunctionType *FnTy = getFunctionType(d)) {
80 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000081 return proto->getArgType(Idx);
82 } else {
83 return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
84 }
85}
86
87static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +000088 if (const FunctionType *FnTy = getFunctionType(d)) {
89 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +000090 return proto->isVariadic();
91 } else {
92 return cast<ObjCMethodDecl>(d)->isVariadic();
93 }
94}
95
Chris Lattner6b6b5372008-06-26 18:38:35 +000096static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000097 const PointerType *PT = T->getAsPointerType();
98 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000099 return false;
100
Chris Lattnerb77792e2008-07-26 22:17:49 +0000101 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000102 if (!ClsT)
103 return false;
104
105 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
106
107 // FIXME: Should we walk the chain of classes?
108 return ClsName == &Ctx.Idents.get("NSString") ||
109 ClsName == &Ctx.Idents.get("NSMutableString");
110}
111
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000112static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
113 const PointerType *PT = T->getAsPointerType();
114 if (!PT)
115 return false;
116
117 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
118 if (!RT)
119 return false;
120
121 const RecordDecl *RD = RT->getDecl();
122 if (RD->getTagKind() != TagDecl::TK_struct)
123 return false;
124
125 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
126}
127
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000128//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000129// Attribute Implementations
130//===----------------------------------------------------------------------===//
131
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000132// FIXME: All this manual attribute parsing code is gross. At the
133// least add some helper functions to check most argument patterns (#
134// and types of args).
135
Chris Lattner803d0802008-06-29 00:43:07 +0000136static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
137 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000138 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
139 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000140 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000141 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000142 }
143
Chris Lattner6b6b5372008-06-26 18:38:35 +0000144 QualType curType = tDecl->getUnderlyingType();
145 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000146 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000148 return;
149 }
Chris Lattner545dd342008-06-28 23:36:30 +0000150 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000151 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000152 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000153 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
154 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000155 return;
156 }
157 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
158 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000159 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000160 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000161 return;
162 }
163 // unlike gcc's vector_size attribute, the size is specified as the
164 // number of elements, not the number of bytes.
165 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
166
167 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
169 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return;
171 }
172 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000173 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000175 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176}
177
Chris Lattner065c5a82008-06-28 23:48:25 +0000178
179/// HandleVectorSizeAttribute - this attribute is only applicable to
180/// integral and float scalars, although arrays, pointers, and function
181/// return values are allowed in conjunction with this construct. Aggregates
182/// with this attribute are invalid, even if they are of the same size as a
183/// corresponding scalar.
184/// The raw attribute should contain precisely 1 argument, the vector size
185/// for the variable, measured in bytes. If curType and rawAttr are well
186/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000187static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000188 QualType CurType;
189 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
190 CurType = VD->getType();
191 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
192 CurType = TD->getUnderlyingType();
193 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000194 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
195 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000196 return;
197 }
198
199 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000200 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000202 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner545dd342008-06-28 23:36:30 +0000204 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000206 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
208 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000209 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 }
211 // navigate to the base type - we need to provide for vector pointers,
212 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000213 if (CurType->isPointerType() || CurType->isArrayType() ||
214 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 assert(0 && "HandleVector(): Complex type construction unimplemented");
216 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
217 do {
218 if (PointerType *PT = dyn_cast<PointerType>(canonType))
219 canonType = PT->getPointeeType().getTypePtr();
220 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
221 canonType = AT->getElementType().getTypePtr();
222 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
223 canonType = FT->getResultType().getTypePtr();
224 } while (canonType->isPointerType() || canonType->isArrayType() ||
225 canonType->isFunctionType());
226 */
227 }
228 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000229 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000231 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000232 }
Chris Lattner803d0802008-06-29 00:43:07 +0000233 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 // vecSize is specified in bytes - convert to bits.
235 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
236
237 // the vector size needs to be an integral multiple of the type size.
238 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
240 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000241 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000242 }
243 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000244 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
245 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000246 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000247 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000248
249 // Success! Instantiate the vector type, the number of elements is > 0, and
250 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000251 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000252
253 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
254 VD->setType(CurType);
255 else
256 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000257}
258
Chris Lattner803d0802008-06-29 00:43:07 +0000259static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000261 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 return;
264 }
265
266 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000267 TD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000268 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
269 // If the alignment is less than or equal to 8 bits, the packed attribute
270 // has no effect.
271 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000272 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000274 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000275 else
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000276 FD->addAttr(new PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000277 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000279}
280
Ted Kremenek96329d42008-07-15 22:26:48 +0000281static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
282 // check the attribute arguments.
283 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000285 return;
286 }
287
288 // The IBOutlet attribute only applies to instance variables of Objective-C
289 // classes.
290 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
291 ID->addAttr(new IBOutletAttr());
292 else
293 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
294}
295
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000296static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000297 // GCC ignores the nonnull attribute on K&R style function
298 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000299 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
301 << "nonnull" << "function";
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 return;
303 }
304
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000305 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000306
307 // The nonnull attribute only applies to pointers.
308 llvm::SmallVector<unsigned, 10> NonNullArgs;
309
310 for (AttributeList::arg_iterator I=Attr.arg_begin(),
311 E=Attr.arg_end(); I!=E; ++I) {
312
313
314 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000315 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000316 llvm::APSInt ArgNum(32);
317 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
319 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000320 return;
321 }
322
323 unsigned x = (unsigned) ArgNum.getZExtValue();
324
325 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000327 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000328 return;
329 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000330
331 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332
333 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000334 QualType T = getFunctionOrMethodArgType(d, x);
335 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000339 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 }
341
342 NonNullArgs.push_back(x);
343 }
344
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000345 // If no arguments were specified to __attribute__((nonnull)) then all
346 // pointer arguments have a nonnull attribute.
347 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000348 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
349 QualType T = getFunctionOrMethodArgType(d, I);
350 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000351 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000352 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000353
354 if (NonNullArgs.empty()) {
355 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
356 return;
357 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000358 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000359
360 unsigned* start = &NonNullArgs[0];
361 unsigned size = NonNullArgs.size();
362 std::sort(start, start + size);
363 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000364}
365
Chris Lattner803d0802008-06-29 00:43:07 +0000366static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000367 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000368 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000370 return;
371 }
372
Chris Lattner545dd342008-06-28 23:36:30 +0000373 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000374 Arg = Arg->IgnoreParenCasts();
375 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
376
377 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000379 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000380 return;
381 }
382
383 const char *Alias = Str->getStrData();
384 unsigned AliasLen = Str->getByteLength();
385
386 // FIXME: check if target symbol exists in current file
387
388 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
389}
390
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000391static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
392 Sema &S) {
393 // check the attribute arguments.
394 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000396 return;
397 }
398
399 d->addAttr(new AlwaysInlineAttr());
400}
401
Chris Lattner803d0802008-06-29 00:43:07 +0000402static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000403 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000404 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000406 return;
407 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000408
409 if (!isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
411 << "noreturn" << "function";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000412 return;
413 }
414
415 d->addAttr(new NoReturnAttr());
416}
417
Ted Kremenek73798892008-07-25 04:39:19 +0000418static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
419 // check the attribute arguments.
420 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000422 return;
423 }
424
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000425 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
427 << "unused" << "variable and function";
Ted Kremenek73798892008-07-25 04:39:19 +0000428 return;
429 }
430
431 d->addAttr(new UnusedAttr());
432}
433
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000434static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
435 // check the attribute arguments.
436 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
438 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000439 return;
440 }
441
442 int priority = 65535; // FIXME: Do not hardcode such constants.
443 if (Attr.getNumArgs() > 0) {
444 Expr *E = static_cast<Expr *>(Attr.getArg(0));
445 llvm::APSInt Idx(32);
446 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000447 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000448 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000449 return;
450 }
451 priority = Idx.getZExtValue();
452 }
453
454 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
455 if (!Fn) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
457 << "constructor" << "function";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000458 return;
459 }
460
461 d->addAttr(new ConstructorAttr(priority));
462}
463
464static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
465 // check the attribute arguments.
466 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
468 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000469 return;
470 }
471
472 int priority = 65535; // FIXME: Do not hardcode such constants.
473 if (Attr.getNumArgs() > 0) {
474 Expr *E = static_cast<Expr *>(Attr.getArg(0));
475 llvm::APSInt Idx(32);
476 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000478 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000479 return;
480 }
481 priority = Idx.getZExtValue();
482 }
483
Anders Carlsson6782fc62008-08-22 22:10:48 +0000484 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000485 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
486 << "destructor" << "function";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000487 return;
488 }
489
490 d->addAttr(new DestructorAttr(priority));
491}
492
Chris Lattner803d0802008-06-29 00:43:07 +0000493static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000494 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000495 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000497 return;
498 }
499
500 d->addAttr(new DeprecatedAttr());
501}
502
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000503static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
504 // check the attribute arguments.
505 if (Attr.getNumArgs() != 0) {
506 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
507 return;
508 }
509
510 d->addAttr(new UnavailableAttr());
511}
512
Chris Lattner803d0802008-06-29 00:43:07 +0000513static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000514 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000515 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000517 return;
518 }
519
Chris Lattner545dd342008-06-28 23:36:30 +0000520 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000521 Arg = Arg->IgnoreParenCasts();
522 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
523
524 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000526 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000527 return;
528 }
529
530 const char *TypeStr = Str->getStrData();
531 unsigned TypeLen = Str->getByteLength();
532 VisibilityAttr::VisibilityTypes type;
533
534 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
535 type = VisibilityAttr::DefaultVisibility;
536 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
537 type = VisibilityAttr::HiddenVisibility;
538 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
539 type = VisibilityAttr::HiddenVisibility; // FIXME
540 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
541 type = VisibilityAttr::ProtectedVisibility;
542 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000543 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000544 return;
545 }
546
547 d->addAttr(new VisibilityAttr(type));
548}
549
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000550static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000551 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000553 << "objc_gc" << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000554 return;
555 }
556
557 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000558 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000559 return;
560 }
561
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000562
563 ObjCGCAttr::GCAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000564 if (Attr.getParameterName()->isStr("weak")) {
Fariborz Jahanian24b93f22008-11-20 19:35:51 +0000565 if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d))
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000566 S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000567 type = ObjCGCAttr::Weak;
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000568 }
Chris Lattner92e62b02008-11-20 04:42:34 +0000569 else if (Attr.getParameterName()->isStr("strong"))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000570 type = ObjCGCAttr::Strong;
571 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 << "objc_gc" << Attr.getParameterName();
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000574 return;
575 }
576
577 d->addAttr(new ObjCGCAttr(type));
578}
579
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000580static void HandleObjCNSObject(Decl *d, const AttributeList &Attr, Sema &S) {
581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
583 return;
584 }
585 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(d)) {
586 QualType T = TD->getUnderlyingType();
587 if (!T->isPointerType() ||
588 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
589 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
590 return;
591 }
592 }
593 d->addAttr(new ObjCNSObjectAttr);
594}
595
Douglas Gregorf9201e02009-02-11 23:02:49 +0000596static void
597HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
598 if (Attr.getNumArgs() != 0) {
599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
600 return;
601 }
602
603 if (!isa<FunctionDecl>(D)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
605 return;
606 }
607
608 D->addAttr(new OverloadableAttr);
609}
610
Steve Naroff9eae5762008-09-18 16:44:58 +0000611static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
612 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000614 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000615 return;
616 }
617
618 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000620 return;
621 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000622
623 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000624 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000625 type = BlocksAttr::ByRef;
626 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000627 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000629 return;
630 }
631
632 d->addAttr(new BlocksAttr(type));
633}
634
Anders Carlsson77091822008-10-05 18:05:59 +0000635static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
639 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000640 return;
641 }
642
643 int sentinel = 0;
644 if (Attr.getNumArgs() > 0) {
645 Expr *E = static_cast<Expr *>(Attr.getArg(0));
646 llvm::APSInt Idx(32);
647 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000650 return;
651 }
652 sentinel = Idx.getZExtValue();
653
654 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000655 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
656 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000657 return;
658 }
659 }
660
661 int nullPos = 0;
662 if (Attr.getNumArgs() > 1) {
663 Expr *E = static_cast<Expr *>(Attr.getArg(1));
664 llvm::APSInt Idx(32);
665 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000667 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000668 return;
669 }
670 nullPos = Idx.getZExtValue();
671
672 if (nullPos > 1 || nullPos < 0) {
673 // FIXME: This error message could be improved, it would be nice
674 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
676 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000677 return;
678 }
679 }
680
681 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
682 QualType FT = FD->getType();
683 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
684 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
685 return;
686 }
687 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
688 if (!MD->isVariadic()) {
689 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
690 return;
691 }
692 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000693 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
694 << "sentinel" << "function or method";
Anders Carlsson77091822008-10-05 18:05:59 +0000695 return;
696 }
697
698 // FIXME: Actually create the attribute.
699}
700
Chris Lattner803d0802008-06-29 00:43:07 +0000701static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000702 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000703 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000704 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000705 return;
706 }
707
708 d->addAttr(new WeakAttr());
709}
710
Chris Lattner803d0802008-06-29 00:43:07 +0000711static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000712 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000713 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000714 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 return;
716 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000717
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000718 // Attribute can be applied only to functions or variables.
719 if (isa<VarDecl>(d)) {
720 d->addAttr(new DLLImportAttr());
721 return;
722 }
723
724 FunctionDecl *FD = dyn_cast<FunctionDecl>(d);
725 if (!FD) {
726 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
727 << "dllimport" << "function or variable";
728 return;
729 }
730
731 // Currently, the dllimport attribute is ignored for inlined functions.
732 // Warning is emitted.
733 if (FD->isInline()) {
734 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
735 return;
736 }
737
738 // The attribute is also overridden by a subsequent declaration as dllexport.
739 // Warning is emitted.
740 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
741 nextAttr = nextAttr->getNext()) {
742 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
743 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
744 return;
745 }
746 }
747
748 if (d->getAttr<DLLExportAttr>()) {
749 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
750 return;
751 }
752
Chris Lattner6b6b5372008-06-26 18:38:35 +0000753 d->addAttr(new DLLImportAttr());
754}
755
Chris Lattner803d0802008-06-29 00:43:07 +0000756static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000757 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000758 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000760 return;
761 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000762
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000763 // Attribute can be applied only to functions or variables.
764 if (isa<VarDecl>(d)) {
765 d->addAttr(new DLLExportAttr());
766 return;
767 }
768
769 FunctionDecl *FD = dyn_cast<FunctionDecl>(d);
770 if (!FD) {
771 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
772 << "dllexport" << "function or variable";
773 return;
774 }
775
776 // Currently, the dllexport attribute is ignored for inlined functions,
777 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
778 if (FD->isInline()) {
779 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
780 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
781 return;
782 }
783
Chris Lattner6b6b5372008-06-26 18:38:35 +0000784 d->addAttr(new DLLExportAttr());
785}
786
Chris Lattner803d0802008-06-29 00:43:07 +0000787static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000788 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000789 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000791 return;
792 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000793
794 // Attribute can be applied only to functions.
795 if (!isa<FunctionDecl>(d)) {
796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
797 << "stdcall" << "function";
798 return;
799 }
800
801 // stdcall and fastcall attributes are mutually incompatible.
802 if (d->getAttr<FastCallAttr>()) {
803 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
804 << "stdcall" << "fastcall";
805 return;
806 }
807
Chris Lattner6b6b5372008-06-26 18:38:35 +0000808 d->addAttr(new StdCallAttr());
809}
810
Chris Lattner803d0802008-06-29 00:43:07 +0000811static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000812 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000813 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000814 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000815 return;
816 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000817
818 if (!isa<FunctionDecl>(d)) {
819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
820 << "fastcall" << "function";
821 return;
822 }
823
824 // stdcall and fastcall attributes are mutually incompatible.
825 if (d->getAttr<StdCallAttr>()) {
826 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
827 << "fastcall" << "stdcall";
828 return;
829 }
830
Chris Lattner6b6b5372008-06-26 18:38:35 +0000831 d->addAttr(new FastCallAttr());
832}
833
Chris Lattner803d0802008-06-29 00:43:07 +0000834static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000835 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000836 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000838 return;
839 }
840
841 d->addAttr(new NoThrowAttr());
842}
843
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000844static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
845 // check the attribute arguments.
846 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000848 return;
849 }
850
851 d->addAttr(new ConstAttr());
852}
853
854static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
855 // check the attribute arguments.
856 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000857 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000858 return;
859 }
860
861 d->addAttr(new PureAttr());
862}
863
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000864static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000865 // Match gcc which ignores cleanup attrs when compiling C++.
866 if (S.getLangOptions().CPlusPlus)
867 return;
868
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000869 if (!Attr.getParameterName()) {
870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
871 return;
872 }
873
874 if (Attr.getNumArgs() != 0) {
875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
876 return;
877 }
878
879 VarDecl *VD = dyn_cast<VarDecl>(d);
880
881 if (!VD || !VD->hasLocalStorage()) {
882 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
883 return;
884 }
885
886 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000887 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
888 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000889 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000890 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000891 Attr.getParameterName();
892 return;
893 }
894
895 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
896 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000897 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000898 Attr.getParameterName();
899 return;
900 }
901
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000902 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +0000903 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000904 Attr.getParameterName();
905 return;
906 }
907
Anders Carlsson89941c12009-02-07 23:16:50 +0000908 // We're currently more strict than GCC about what function types we accept.
909 // If this ever proves to be a problem it should be easy to fix.
910 QualType Ty = S.Context.getPointerType(VD->getType());
911 QualType ParamTy = FD->getParamDecl(0)->getType();
912 if (Ty != ParamTy) {
913 S.Diag(Attr.getLoc(),
914 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
915 Attr.getParameterName() << ParamTy << Ty;
916 return;
917 }
918
Anders Carlssonf6e35d02009-01-31 01:16:18 +0000919 d->addAttr(new CleanupAttr(FD));
920}
921
Chris Lattner6b6b5372008-06-26 18:38:35 +0000922/// Handle __attribute__((format(type,idx,firstarg))) attributes
923/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000924static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000925
Chris Lattner545dd342008-06-28 23:36:30 +0000926 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000928 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000929 return;
930 }
931
Chris Lattner545dd342008-06-28 23:36:30 +0000932 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000934 return;
935 }
936
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000937 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
939 << "format" << "function";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000940 return;
941 }
942
943 // FIXME: in C++ the implicit 'this' function parameter also counts.
944 // this is needed in order to be compatible with GCC
945 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +0000946 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000947 unsigned FirstIdx = 1;
948
Chris Lattner545dd342008-06-28 23:36:30 +0000949 const char *Format = Attr.getParameterName()->getName();
950 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000951
952 // Normalize the argument, __foo__ becomes foo.
953 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
954 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
955 Format += 2;
956 FormatLen -= 4;
957 }
958
959 bool Supported = false;
960 bool is_NSString = false;
961 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000962 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000963
964 switch (FormatLen) {
965 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000966 case 5: Supported = !memcmp(Format, "scanf", 5); break;
967 case 6: Supported = !memcmp(Format, "printf", 6); break;
968 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000969 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000970 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
971 (is_NSString = !memcmp(Format, "NSString", 8)) ||
972 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973 break;
974 }
975
976 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000977 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
978 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000979 return;
980 }
981
982 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000983 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000984 llvm::APSInt Idx(32);
985 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000988 return;
989 }
990
991 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +0000993 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000994 return;
995 }
996
997 // FIXME: Do we need to bounds check?
998 unsigned ArgIdx = Idx.getZExtValue() - 1;
999
1000 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001001 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001002
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001003 if (is_CFString) {
1004 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001005 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1006 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001007 return;
1008 }
1009 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001010 // FIXME: do we need to check if the type is NSString*? What are
1011 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001012 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001013 // FIXME: Should highlight the actual expression that has the
1014 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001015 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1016 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001017 return;
1018 }
1019 } else if (!Ty->isPointerType() ||
1020 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
1021 // FIXME: Should highlight the actual expression that has the
1022 // wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001023 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1024 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001025 return;
1026 }
1027
1028 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001029 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001030 llvm::APSInt FirstArg(32);
1031 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001032 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001033 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034 return;
1035 }
1036
1037 // check if the function is variadic if the 3rd argument non-zero
1038 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001039 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001040 ++NumArgs; // +1 for ...
1041 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001042 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001043 return;
1044 }
1045 }
1046
Chris Lattner3c73c412008-11-19 08:23:25 +00001047 // strftime requires FirstArg to be 0 because it doesn't read from any
1048 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001049 if (is_strftime) {
1050 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001051 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1052 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001053 return;
1054 }
1055 // if 0 it disables parameter checking (to use with e.g. va_list)
1056 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001057 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001058 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001059 return;
1060 }
1061
1062 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
1063 Idx.getZExtValue(), FirstArg.getZExtValue()));
1064}
1065
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001066static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1067 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001068 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001069 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001071 return;
1072 }
1073
Eli Friedmanbc887452008-09-02 05:19:23 +00001074 // FIXME: This shouldn't be restricted to typedefs
1075 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1076 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001077 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1078 << "transparent_union" << "union";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001079 return;
1080 }
1081
Eli Friedmanbc887452008-09-02 05:19:23 +00001082 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001083
Eli Friedmanbc887452008-09-02 05:19:23 +00001084 // FIXME: Should we do a check for RD->isDefinition()?
1085
1086 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1087 // we might silently generate incorrect code; see following code
Douglas Gregor44b43212008-12-11 16:49:14 +00001088 for (RecordDecl::field_iterator Field = RD->field_begin(),
1089 FieldEnd = RD->field_end();
1090 Field != FieldEnd; ++Field) {
1091 if (!Field->getType()->isPointerType()) {
Eli Friedmanbc887452008-09-02 05:19:23 +00001092 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1093 return;
1094 }
1095 }
1096
1097 // FIXME: This is a complete hack; we should be properly propagating
1098 // transparent_union through Sema. That said, this is close enough to
1099 // correctly compile all the common cases of transparent_union without
1100 // errors or warnings
1101 QualType NewTy = S.Context.VoidPtrTy;
1102 NewTy.addConst();
1103 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001104}
1105
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001106static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001107 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001108 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001110 return;
1111 }
Chris Lattner545dd342008-06-28 23:36:30 +00001112 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001113 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1114
1115 // Make sure that there is a string literal as the annotation's single
1116 // argument.
1117 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001118 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001119 return;
1120 }
1121 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
1122 SE->getByteLength())));
1123}
1124
Chris Lattner803d0802008-06-29 00:43:07 +00001125static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001126 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001127 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001129 return;
1130 }
1131
1132 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001133 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001134 // FIXME: This should be the target specific maximum alignment.
1135 // (For now we just use 128 bits which is the maximum on X86.
1136 Align = 128;
1137 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001139
1140 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1141 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001142 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1144 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001145 return;
1146 }
1147 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001148}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001149
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001150/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001151/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001152///
1153/// Despite what would be logical, the mode attribute is a decl attribute,
1154/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1155/// 'G' be HImode, not an intermediate pointer.
1156///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001157static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001158 // This attribute isn't documented, but glibc uses it. It changes
1159 // the width of an int or unsigned int to the specified size.
1160
1161 // Check that there aren't any arguments
1162 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001163 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001164 return;
1165 }
1166
1167 IdentifierInfo *Name = Attr.getParameterName();
1168 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001169 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001170 return;
1171 }
1172 const char *Str = Name->getName();
1173 unsigned Len = Name->getLength();
1174
1175 // Normalize the attribute name, __foo__ becomes foo.
1176 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1177 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1178 Str += 2;
1179 Len -= 4;
1180 }
1181
1182 unsigned DestWidth = 0;
1183 bool IntegerMode = true;
1184 switch (Len) {
1185 case 2:
1186 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1187 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1188 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1189 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1190 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1191 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1192 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1193 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1194 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1195 break;
1196 case 4:
1197 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1198 // pointer on PIC16 and other embedded platforms.
1199 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001200 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001201 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001202 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001203 break;
1204 case 7:
1205 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001206 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001207 break;
1208 }
1209
1210 QualType OldTy;
1211 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1212 OldTy = TD->getUnderlyingType();
1213 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1214 OldTy = VD->getType();
1215 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001216 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1217 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001218 return;
1219 }
1220
1221 // FIXME: Need proper fixed-width types
1222 QualType NewTy;
1223 switch (DestWidth) {
1224 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001225 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001226 return;
1227 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001228 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001229 return;
1230 case 8:
1231 assert(IntegerMode);
1232 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001233 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001234 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001235 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001236 break;
1237 case 16:
1238 assert(IntegerMode);
1239 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001240 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001241 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001242 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001243 break;
1244 case 32:
1245 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001246 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001247 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001248 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001249 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001250 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001251 break;
1252 case 64:
1253 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001254 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001255 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001256 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001257 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001258 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001259 break;
1260 }
1261
1262 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001263 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001264 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1265 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001266 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001267 }
1268
1269 // Install the new type.
1270 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1271 TD->setUnderlyingType(NewTy);
1272 else
1273 cast<ValueDecl>(D)->setType(NewTy);
1274}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001275
1276//===----------------------------------------------------------------------===//
1277// Top Level Sema Entry Points
1278//===----------------------------------------------------------------------===//
1279
Sebastian Redla89d82c2008-12-21 19:24:58 +00001280/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001281/// the attribute applies to decls. If the attribute is a type attribute, just
1282/// silently ignore it.
1283static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1284 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001285 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001286 case AttributeList::AT_address_space:
1287 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1288 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001289 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1290 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001291 case AttributeList::AT_always_inline:
1292 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001293 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1294 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1295 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1296 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1297 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1298 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001299 case AttributeList::AT_ext_vector_type:
1300 HandleExtVectorTypeAttr(D, Attr, S);
1301 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001302 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1303 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001304 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001305 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1306 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1307 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1308 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1309 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001310 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001311 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1312 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001313 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1314 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001315 case AttributeList::AT_transparent_union:
1316 HandleTransparentUnionAttr(D, Attr, S);
1317 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001318 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001319 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001320 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001321 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001322 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001323 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1324 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001325 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001326 default:
1327#if 0
1328 // TODO: when we have the full set of attributes, warn about unknown ones.
Chris Lattner08631c52008-11-23 21:45:46 +00001329 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored) << Attr->getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001330#endif
1331 break;
1332 }
1333}
1334
1335/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1336/// attribute list to the specified decl, ignoring any type attributes.
1337void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1338 while (AttrList) {
1339 ProcessDeclAttribute(D, *AttrList, *this);
1340 AttrList = AttrList->getNext();
1341 }
1342}
1343
1344
Chris Lattner0744e5f2008-06-29 00:23:49 +00001345/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1346/// it, apply them to D. This is a bit tricky because PD can have attributes
1347/// specified in many different places, and we need to find and apply them all.
1348void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1349 // Apply decl attributes from the DeclSpec if present.
1350 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1351 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001352
Chris Lattner0744e5f2008-06-29 00:23:49 +00001353 // Walk the declarator structure, applying decl attributes that were in a type
1354 // position to the decl itself. This handles cases like:
1355 // int *__attr__(x)** D;
1356 // when X is a decl attribute.
1357 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1358 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1359 ProcessDeclAttributeList(D, Attrs);
1360
1361 // Finally, apply any attributes on the decl itself.
1362 if (const AttributeList *Attrs = PD.getAttributes())
1363 ProcessDeclAttributeList(D, Attrs);
1364}
1365