blob: 33985ee1a33f070cac3375e7ca7841f56515fa2e [file] [log] [blame]
Chris Lattner6953a072008-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 Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner2024f0a2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar0ea20472008-10-19 02:04:16 +000027static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6953a072008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6953a072008-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 Dunbar0ea20472008-10-19 02:04:16 +000040
41 return Ty->getAsFunctionType();
Chris Lattner6953a072008-06-26 18:38:35 +000042}
43
Daniel Dunbard1d847c2008-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 Dunbar0ea20472008-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 Dunbard1d847c2008-09-26 04:12:28 +000050static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000051 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000052}
53
Daniel Dunbar0ea20472008-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 Dunbard1d847c2008-09-26 04:12:28 +000069static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000070 if (const FunctionType *FnTy = getFunctionType(d)) {
71 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-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 Dunbar0ea20472008-10-19 02:04:16 +000079 if (const FunctionType *FnTy = getFunctionType(d)) {
80 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-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 Dunbar0ea20472008-10-19 02:04:16 +000088 if (const FunctionType *FnTy = getFunctionType(d)) {
89 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000090 return proto->isVariadic();
91 } else {
92 return cast<ObjCMethodDecl>(d)->isVariadic();
93 }
94}
95
Chris Lattner6953a072008-06-26 18:38:35 +000096static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000097 const PointerType *PT = T->getAsPointerType();
98 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +000099 return false;
100
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000101 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-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 Dunbarbddb14a2008-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 Lattner2024f0a2008-06-29 00:16:31 +0000128//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +0000129// Attribute Implementations
130//===----------------------------------------------------------------------===//
131
Daniel Dunbar8716a012008-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 Lattner703c52d2008-06-29 00:43:07 +0000136static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
137 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +0000138 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
139 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000140 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +0000141 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000142 }
143
Chris Lattner6953a072008-06-26 18:38:35 +0000144 QualType curType = tDecl->getUnderlyingType();
145 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000146 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000148 return;
149 }
Chris Lattner1c151132008-06-28 23:36:30 +0000150 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000151 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000152 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000153 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
154 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6953a072008-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 Lattnerd5a56aa2008-07-26 22:17:49 +0000159 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000160 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6953a072008-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 Lattner8ba580c2008-11-19 05:08:23 +0000168 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
169 << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000170 return;
171 }
172 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000173 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000174 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000175 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000176}
177
Chris Lattner8ed14aa2008-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 Lattner703c52d2008-06-29 00:43:07 +0000187static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-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 Lattner8ba580c2008-11-19 05:08:23 +0000194 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
195 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000196 return;
197 }
198
199 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000200 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000202 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000203 }
Chris Lattner1c151132008-06-28 23:36:30 +0000204 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000205 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000206 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
208 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000209 return;
Chris Lattner6953a072008-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 Lattnerd5a56aa2008-07-26 22:17:49 +0000213 if (CurType->isPointerType() || CurType->isArrayType() ||
214 CurType->isFunctionType()) {
Chris Lattner6953a072008-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 Lattnerd5a56aa2008-07-26 22:17:49 +0000229 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000231 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000232 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000233 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-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 Lattner8ba580c2008-11-19 05:08:23 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
240 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000241 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000242 }
243 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000244 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
245 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000246 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000247 }
Chris Lattner8ed14aa2008-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 Lattner703c52d2008-06-29 00:43:07 +0000251 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-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 Lattner6953a072008-06-26 18:38:35 +0000257}
258
Chris Lattner703c52d2008-06-29 00:43:07 +0000259static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000260 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000261 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000262 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000263 return;
264 }
265
266 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000267 TD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-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 Lattner703c52d2008-06-29 00:43:07 +0000272 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000274 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000275 else
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000276 FD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000277 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000279}
280
Ted Kremenek4e5bb122008-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 Lattner65cae292008-11-19 08:23:25 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-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 Kremenekc0af2542008-07-21 21:53:04 +0000296static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-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 Dunbar0ea20472008-10-19 02:04:16 +0000299 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
301 << "nonnull" << "function";
Ted Kremenekc0af2542008-07-21 21:53:04 +0000302 return;
303 }
304
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000305 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-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 Kremenek73dc0652008-12-04 19:38:33 +0000315 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000316 llvm::APSInt ArgNum(32);
317 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
319 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000320 return;
321 }
322
323 unsigned x = (unsigned) ArgNum.getZExtValue();
324
325 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000327 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000328 return;
329 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000330
331 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000332
333 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000334 QualType T = getFunctionOrMethodArgType(d, x);
335 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000336 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000339 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000340 }
341
342 NonNullArgs.push_back(x);
343 }
344
Ted Kremenek7af441b2008-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 Kremenekba57bd92008-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 Dunbar0ea20472008-10-19 02:04:16 +0000351 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000352 }
Ted Kremenek7af441b2008-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 Kremenekc0af2542008-07-21 21:53:04 +0000358 }
Ted Kremenek7af441b2008-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 Kremenekc0af2542008-07-21 21:53:04 +0000364}
365
Chris Lattner703c52d2008-06-29 00:43:07 +0000366static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000367 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000368 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000370 return;
371 }
372
Chris Lattner1c151132008-06-28 23:36:30 +0000373 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000374 Arg = Arg->IgnoreParenCasts();
375 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
376
377 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000378 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000379 << "alias" << 1;
Chris Lattner6953a072008-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 Dunbar0a2da712008-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 Lattner65cae292008-11-19 08:23:25 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000396 return;
397 }
398
399 d->addAttr(new AlwaysInlineAttr());
400}
401
Chris Lattner703c52d2008-06-29 00:43:07 +0000402static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000403 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000404 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000406 return;
407 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000408
409 if (!isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
411 << "noreturn" << "function";
Chris Lattner6953a072008-06-26 18:38:35 +0000412 return;
413 }
414
415 d->addAttr(new NoReturnAttr());
416}
417
Ted Kremenekce13e1e2008-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 Lattner65cae292008-11-19 08:23:25 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000422 return;
423 }
424
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000425 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000426 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
427 << "unused" << "variable and function";
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000428 return;
429 }
430
431 d->addAttr(new UnusedAttr());
432}
433
Daniel Dunbar8716a012008-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 Lattner8ba580c2008-11-19 05:08:23 +0000437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
438 << "0 or 1";
Daniel Dunbar8716a012008-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 Lattner8ba580c2008-11-19 05:08:23 +0000447 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000448 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-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 Lattner8ba580c2008-11-19 05:08:23 +0000456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
457 << "constructor" << "function";
Daniel Dunbar8716a012008-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 Lattner8ba580c2008-11-19 05:08:23 +0000467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
468 << "0 or 1";
Daniel Dunbar8716a012008-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 Lattner8ba580c2008-11-19 05:08:23 +0000477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000478 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000479 return;
480 }
481 priority = Idx.getZExtValue();
482 }
483
Anders Carlsson03e49652008-08-22 22:10:48 +0000484 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000485 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
486 << "destructor" << "function";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000487 return;
488 }
489
490 d->addAttr(new DestructorAttr(priority));
491}
492
Chris Lattner703c52d2008-06-29 00:43:07 +0000493static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000494 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000495 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000497 return;
498 }
499
500 d->addAttr(new DeprecatedAttr());
501}
502
Fariborz Jahanian8f9b1b22008-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 Lattner703c52d2008-06-29 00:43:07 +0000513static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000514 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000515 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000517 return;
518 }
519
Chris Lattner1c151132008-06-28 23:36:30 +0000520 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000521 Arg = Arg->IgnoreParenCasts();
522 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
523
524 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000526 << "visibility" << 1;
Chris Lattner6953a072008-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 Lattnerb1753422008-11-23 21:45:46 +0000543 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000544 return;
545 }
546
547 d->addAttr(new VisibilityAttr(type));
548}
549
Anders Carlssond83141a52008-08-23 23:22:21 +0000550static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssone0f00b32008-08-24 16:33:25 +0000551 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000553 << "objc_gc" << 1;
Anders Carlssond83141a52008-08-23 23:22:21 +0000554 return;
555 }
556
557 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000558 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Anders Carlssond83141a52008-08-23 23:22:21 +0000559 return;
560 }
561
Anders Carlssond83141a52008-08-23 23:22:21 +0000562
563 ObjCGCAttr::GCAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000564 if (Attr.getParameterName()->isStr("weak")) {
Fariborz Jahanian7563a322008-11-20 19:35:51 +0000565 if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d))
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000566 S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
Anders Carlssond83141a52008-08-23 23:22:21 +0000567 type = ObjCGCAttr::Weak;
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000568 }
Chris Lattner05fb7c82008-11-20 04:42:34 +0000569 else if (Attr.getParameterName()->isStr("strong"))
Anders Carlssond83141a52008-08-23 23:22:21 +0000570 type = ObjCGCAttr::Strong;
571 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000573 << "objc_gc" << Attr.getParameterName();
Anders Carlssond83141a52008-08-23 23:22:21 +0000574 return;
575 }
576
577 d->addAttr(new ObjCGCAttr(type));
578}
579
Fariborz Jahanian82f54962009-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
Steve Naroffe1cecba2008-09-18 16:44:58 +0000596static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
597 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000599 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000600 return;
601 }
602
603 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000604 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000605 return;
606 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000607
608 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000609 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000610 type = BlocksAttr::ByRef;
611 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000612 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000613 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000614 return;
615 }
616
617 d->addAttr(new BlocksAttr(type));
618}
619
Anders Carlsson244d99b2008-10-05 18:05:59 +0000620static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
621 // check the attribute arguments.
622 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
624 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000625 return;
626 }
627
628 int sentinel = 0;
629 if (Attr.getNumArgs() > 0) {
630 Expr *E = static_cast<Expr *>(Attr.getArg(0));
631 llvm::APSInt Idx(32);
632 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000633 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000634 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000635 return;
636 }
637 sentinel = Idx.getZExtValue();
638
639 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000640 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
641 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000642 return;
643 }
644 }
645
646 int nullPos = 0;
647 if (Attr.getNumArgs() > 1) {
648 Expr *E = static_cast<Expr *>(Attr.getArg(1));
649 llvm::APSInt Idx(32);
650 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000651 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000652 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000653 return;
654 }
655 nullPos = Idx.getZExtValue();
656
657 if (nullPos > 1 || nullPos < 0) {
658 // FIXME: This error message could be improved, it would be nice
659 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
661 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000662 return;
663 }
664 }
665
666 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
667 QualType FT = FD->getType();
668 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
669 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
670 return;
671 }
672 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
673 if (!MD->isVariadic()) {
674 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
675 return;
676 }
677 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
679 << "sentinel" << "function or method";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000680 return;
681 }
682
683 // FIXME: Actually create the attribute.
684}
685
Chris Lattner703c52d2008-06-29 00:43:07 +0000686static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000687 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000688 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000690 return;
691 }
692
693 d->addAttr(new WeakAttr());
694}
695
Chris Lattner703c52d2008-06-29 00:43:07 +0000696static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000697 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000698 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000699 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000700 return;
701 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000702
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000703 // Attribute can be applied only to functions or variables.
704 if (isa<VarDecl>(d)) {
705 d->addAttr(new DLLImportAttr());
706 return;
707 }
708
709 FunctionDecl *FD = dyn_cast<FunctionDecl>(d);
710 if (!FD) {
711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
712 << "dllimport" << "function or variable";
713 return;
714 }
715
716 // Currently, the dllimport attribute is ignored for inlined functions.
717 // Warning is emitted.
718 if (FD->isInline()) {
719 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
720 return;
721 }
722
723 // The attribute is also overridden by a subsequent declaration as dllexport.
724 // Warning is emitted.
725 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
726 nextAttr = nextAttr->getNext()) {
727 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
728 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
729 return;
730 }
731 }
732
733 if (d->getAttr<DLLExportAttr>()) {
734 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
735 return;
736 }
737
Chris Lattner6953a072008-06-26 18:38:35 +0000738 d->addAttr(new DLLImportAttr());
739}
740
Chris Lattner703c52d2008-06-29 00:43:07 +0000741static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000742 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000743 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000744 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000745 return;
746 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000747
Anton Korobeynikovb27a8702008-12-26 00:52:02 +0000748 // Attribute can be applied only to functions or variables.
749 if (isa<VarDecl>(d)) {
750 d->addAttr(new DLLExportAttr());
751 return;
752 }
753
754 FunctionDecl *FD = dyn_cast<FunctionDecl>(d);
755 if (!FD) {
756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
757 << "dllexport" << "function or variable";
758 return;
759 }
760
761 // Currently, the dllexport attribute is ignored for inlined functions,
762 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
763 if (FD->isInline()) {
764 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
765 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
766 return;
767 }
768
Chris Lattner6953a072008-06-26 18:38:35 +0000769 d->addAttr(new DLLExportAttr());
770}
771
Chris Lattner703c52d2008-06-29 00:43:07 +0000772static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000773 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000774 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000776 return;
777 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000778
779 // Attribute can be applied only to functions.
780 if (!isa<FunctionDecl>(d)) {
781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
782 << "stdcall" << "function";
783 return;
784 }
785
786 // stdcall and fastcall attributes are mutually incompatible.
787 if (d->getAttr<FastCallAttr>()) {
788 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
789 << "stdcall" << "fastcall";
790 return;
791 }
792
Chris Lattner6953a072008-06-26 18:38:35 +0000793 d->addAttr(new StdCallAttr());
794}
795
Chris Lattner703c52d2008-06-29 00:43:07 +0000796static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000797 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000798 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000800 return;
801 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000802
803 if (!isa<FunctionDecl>(d)) {
804 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
805 << "fastcall" << "function";
806 return;
807 }
808
809 // stdcall and fastcall attributes are mutually incompatible.
810 if (d->getAttr<StdCallAttr>()) {
811 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
812 << "fastcall" << "stdcall";
813 return;
814 }
815
Chris Lattner6953a072008-06-26 18:38:35 +0000816 d->addAttr(new FastCallAttr());
817}
818
Chris Lattner703c52d2008-06-29 00:43:07 +0000819static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000820 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000821 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000823 return;
824 }
825
826 d->addAttr(new NoThrowAttr());
827}
828
Anders Carlssondd6791c2008-10-05 23:32:53 +0000829static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
830 // check the attribute arguments.
831 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000833 return;
834 }
835
836 d->addAttr(new ConstAttr());
837}
838
839static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
840 // check the attribute arguments.
841 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000843 return;
844 }
845
846 d->addAttr(new PureAttr());
847}
848
Anders Carlsson677e38f2009-01-31 01:16:18 +0000849static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
850 if (!Attr.getParameterName()) {
851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
852 return;
853 }
854
855 if (Attr.getNumArgs() != 0) {
856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
857 return;
858 }
859
860 VarDecl *VD = dyn_cast<VarDecl>(d);
861
862 if (!VD || !VD->hasLocalStorage()) {
863 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
864 return;
865 }
866
867 // Look up the function
868 Decl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
869 Sema::LookupOrdinaryName);
870 if (!CleanupDecl) {
871 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
872 Attr.getParameterName();
873 return;
874 }
875
876 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
877 if (!FD) {
878 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
879 Attr.getParameterName();
880 return;
881 }
882
883 // FIXME: This needs to work with C++ overloading.
884 // FIXME: This should verify that the function type is compatible
885 if (FD->getNumParams() != 1) {
886 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_must_take_one_arg)<<
887 Attr.getParameterName();
888 return;
889 }
890
891 d->addAttr(new CleanupAttr(FD));
892}
893
Chris Lattner6953a072008-06-26 18:38:35 +0000894/// Handle __attribute__((format(type,idx,firstarg))) attributes
895/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +0000896static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000897
Chris Lattner1c151132008-06-28 23:36:30 +0000898 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000899 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000900 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000901 return;
902 }
903
Chris Lattner1c151132008-06-28 23:36:30 +0000904 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +0000905 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +0000906 return;
907 }
908
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000909 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000910 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
911 << "format" << "function";
Chris Lattner6953a072008-06-26 18:38:35 +0000912 return;
913 }
914
915 // FIXME: in C++ the implicit 'this' function parameter also counts.
916 // this is needed in order to be compatible with GCC
917 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000918 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +0000919 unsigned FirstIdx = 1;
920
Chris Lattner1c151132008-06-28 23:36:30 +0000921 const char *Format = Attr.getParameterName()->getName();
922 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +0000923
924 // Normalize the argument, __foo__ becomes foo.
925 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
926 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
927 Format += 2;
928 FormatLen -= 4;
929 }
930
931 bool Supported = false;
932 bool is_NSString = false;
933 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000934 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +0000935
936 switch (FormatLen) {
937 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000938 case 5: Supported = !memcmp(Format, "scanf", 5); break;
939 case 6: Supported = !memcmp(Format, "printf", 6); break;
940 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +0000941 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000942 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
943 (is_NSString = !memcmp(Format, "NSString", 8)) ||
944 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +0000945 break;
946 }
947
948 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000949 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
950 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000951 return;
952 }
953
954 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000955 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +0000956 llvm::APSInt Idx(32);
957 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000958 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000959 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000960 return;
961 }
962
963 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +0000965 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000966 return;
967 }
968
969 // FIXME: Do we need to bounds check?
970 unsigned ArgIdx = Idx.getZExtValue() - 1;
971
972 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000973 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +0000974
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000975 if (is_CFString) {
976 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000977 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
978 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000979 return;
980 }
981 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +0000982 // FIXME: do we need to check if the type is NSString*? What are
983 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +0000984 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +0000985 // FIXME: Should highlight the actual expression that has the
986 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000987 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
988 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000989 return;
990 }
991 } else if (!Ty->isPointerType() ||
992 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
993 // FIXME: Should highlight the actual expression that has the
994 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000995 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
996 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000997 return;
998 }
999
1000 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +00001001 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +00001002 llvm::APSInt FirstArg(32);
1003 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +00001005 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001006 return;
1007 }
1008
1009 // check if the function is variadic if the 3rd argument non-zero
1010 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001011 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +00001012 ++NumArgs; // +1 for ...
1013 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +00001014 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +00001015 return;
1016 }
1017 }
1018
Chris Lattner65cae292008-11-19 08:23:25 +00001019 // strftime requires FirstArg to be 0 because it doesn't read from any
1020 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +00001021 if (is_strftime) {
1022 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001023 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1024 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001025 return;
1026 }
1027 // if 0 it disables parameter checking (to use with e.g. va_list)
1028 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001029 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +00001030 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +00001031 return;
1032 }
1033
1034 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
1035 Idx.getZExtValue(), FirstArg.getZExtValue()));
1036}
1037
Chris Lattnerf6690152008-06-29 00:28:59 +00001038static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1039 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001040 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001041 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001042 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +00001043 return;
1044 }
1045
Eli Friedman283b3622008-09-02 05:19:23 +00001046 // FIXME: This shouldn't be restricted to typedefs
1047 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1048 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001049 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1050 << "transparent_union" << "union";
Chris Lattner6953a072008-06-26 18:38:35 +00001051 return;
1052 }
1053
Eli Friedman283b3622008-09-02 05:19:23 +00001054 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6953a072008-06-26 18:38:35 +00001055
Eli Friedman283b3622008-09-02 05:19:23 +00001056 // FIXME: Should we do a check for RD->isDefinition()?
1057
1058 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
1059 // we might silently generate incorrect code; see following code
Douglas Gregor8acb7272008-12-11 16:49:14 +00001060 for (RecordDecl::field_iterator Field = RD->field_begin(),
1061 FieldEnd = RD->field_end();
1062 Field != FieldEnd; ++Field) {
1063 if (!Field->getType()->isPointerType()) {
Eli Friedman283b3622008-09-02 05:19:23 +00001064 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
1065 return;
1066 }
1067 }
1068
1069 // FIXME: This is a complete hack; we should be properly propagating
1070 // transparent_union through Sema. That said, this is close enough to
1071 // correctly compile all the common cases of transparent_union without
1072 // errors or warnings
1073 QualType NewTy = S.Context.VoidPtrTy;
1074 NewTy.addConst();
1075 TD->setUnderlyingType(NewTy);
Chris Lattner6953a072008-06-26 18:38:35 +00001076}
1077
Chris Lattnerf6690152008-06-29 00:28:59 +00001078static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001079 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001080 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001082 return;
1083 }
Chris Lattner1c151132008-06-28 23:36:30 +00001084 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +00001085 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1086
1087 // Make sure that there is a string literal as the annotation's single
1088 // argument.
1089 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +00001091 return;
1092 }
1093 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
1094 SE->getByteLength())));
1095}
1096
Chris Lattner703c52d2008-06-29 00:43:07 +00001097static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +00001098 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +00001099 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +00001100 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +00001101 return;
1102 }
1103
1104 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +00001105 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +00001106 // FIXME: This should be the target specific maximum alignment.
1107 // (For now we just use 128 bits which is the maximum on X86.
1108 Align = 128;
1109 return;
Chris Lattner6953a072008-06-26 18:38:35 +00001110 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001111
1112 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1113 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +00001114 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1116 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001117 return;
1118 }
1119 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001120}
Chris Lattnerdc789562008-06-27 22:18:37 +00001121
Chris Lattnerf6690152008-06-29 00:28:59 +00001122/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001123/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001124///
1125/// Despite what would be logical, the mode attribute is a decl attribute,
1126/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1127/// 'G' be HImode, not an intermediate pointer.
1128///
Chris Lattnerf6690152008-06-29 00:28:59 +00001129static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001130 // This attribute isn't documented, but glibc uses it. It changes
1131 // the width of an int or unsigned int to the specified size.
1132
1133 // Check that there aren't any arguments
1134 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001135 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001136 return;
1137 }
1138
1139 IdentifierInfo *Name = Attr.getParameterName();
1140 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001141 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001142 return;
1143 }
1144 const char *Str = Name->getName();
1145 unsigned Len = Name->getLength();
1146
1147 // Normalize the attribute name, __foo__ becomes foo.
1148 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1149 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1150 Str += 2;
1151 Len -= 4;
1152 }
1153
1154 unsigned DestWidth = 0;
1155 bool IntegerMode = true;
1156 switch (Len) {
1157 case 2:
1158 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1159 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1160 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1161 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1162 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1163 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1164 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1165 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1166 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1167 break;
1168 case 4:
1169 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1170 // pointer on PIC16 and other embedded platforms.
1171 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001172 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001173 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001174 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001175 break;
1176 case 7:
1177 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001178 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001179 break;
1180 }
1181
1182 QualType OldTy;
1183 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1184 OldTy = TD->getUnderlyingType();
1185 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1186 OldTy = VD->getType();
1187 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001188 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1189 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001190 return;
1191 }
1192
1193 // FIXME: Need proper fixed-width types
1194 QualType NewTy;
1195 switch (DestWidth) {
1196 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001197 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001198 return;
1199 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001200 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001201 return;
1202 case 8:
1203 assert(IntegerMode);
1204 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001205 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001206 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001207 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001208 break;
1209 case 16:
1210 assert(IntegerMode);
1211 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001212 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001213 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001214 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001215 break;
1216 case 32:
1217 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001218 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001219 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001220 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001221 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001222 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001223 break;
1224 case 64:
1225 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001226 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001227 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001228 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001229 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001230 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001231 break;
1232 }
1233
1234 if (!OldTy->getAsBuiltinType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001235 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerdc789562008-06-27 22:18:37 +00001236 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1237 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001238 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerdc789562008-06-27 22:18:37 +00001239 }
1240
1241 // Install the new type.
1242 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1243 TD->setUnderlyingType(NewTy);
1244 else
1245 cast<ValueDecl>(D)->setType(NewTy);
1246}
Chris Lattnera72440d2008-06-29 00:23:49 +00001247
1248//===----------------------------------------------------------------------===//
1249// Top Level Sema Entry Points
1250//===----------------------------------------------------------------------===//
1251
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001252/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001253/// the attribute applies to decls. If the attribute is a type attribute, just
1254/// silently ignore it.
1255static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1256 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001257 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001258 case AttributeList::AT_address_space:
1259 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1260 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001261 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1262 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001263 case AttributeList::AT_always_inline:
1264 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001265 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1266 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1267 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1268 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1269 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1270 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001271 case AttributeList::AT_ext_vector_type:
1272 HandleExtVectorTypeAttr(D, Attr, S);
1273 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001274 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1275 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001276 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001277 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1278 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1279 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1280 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1281 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001282 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001283 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1284 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001285 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1286 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001287 case AttributeList::AT_transparent_union:
1288 HandleTransparentUnionAttr(D, Attr, S);
1289 break;
Anders Carlssond83141a52008-08-23 23:22:21 +00001290 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Fariborz Jahanian82f54962009-01-13 23:34:40 +00001291 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001292 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001293 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001294 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1295 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlsson677e38f2009-01-31 01:16:18 +00001296 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001297 default:
1298#if 0
1299 // TODO: when we have the full set of attributes, warn about unknown ones.
Chris Lattnerb1753422008-11-23 21:45:46 +00001300 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored) << Attr->getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001301#endif
1302 break;
1303 }
1304}
1305
1306/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1307/// attribute list to the specified decl, ignoring any type attributes.
1308void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1309 while (AttrList) {
1310 ProcessDeclAttribute(D, *AttrList, *this);
1311 AttrList = AttrList->getNext();
1312 }
1313}
1314
1315
Chris Lattnera72440d2008-06-29 00:23:49 +00001316/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1317/// it, apply them to D. This is a bit tricky because PD can have attributes
1318/// specified in many different places, and we need to find and apply them all.
1319void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1320 // Apply decl attributes from the DeclSpec if present.
1321 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1322 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001323
Chris Lattnera72440d2008-06-29 00:23:49 +00001324 // Walk the declarator structure, applying decl attributes that were in a type
1325 // position to the decl itself. This handles cases like:
1326 // int *__attr__(x)** D;
1327 // when X is a decl attribute.
1328 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1329 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1330 ProcessDeclAttributeList(D, Attrs);
1331
1332 // Finally, apply any attributes on the decl itself.
1333 if (const AttributeList *Attrs = PD.getAttributes())
1334 ProcessDeclAttributeList(D, Attrs);
1335}
1336