blob: 764724aeca032f0830966787e294fe379c2cdd01 [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"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000021#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Chris Lattner6b6b5372008-06-26 18:38:35 +000028static const FunctionTypeProto *getFunctionProto(Decl *d) {
29 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
31 Ty = decl->getType();
32 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
33 Ty = decl->getType();
34 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
35 Ty = decl->getUnderlyingType();
36 else
37 return 0;
38
39 if (Ty->isFunctionPointerType())
40 Ty = Ty->getAsPointerType()->getPointeeType();
41
42 if (const FunctionType *FnTy = Ty->getAsFunctionType())
43 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
44
45 return 0;
46}
47
48static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000049 const PointerType *PT = T->getAsPointerType();
50 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000051 return false;
52
Chris Lattnerb77792e2008-07-26 22:17:49 +000053 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000054 if (!ClsT)
55 return false;
56
57 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
58
59 // FIXME: Should we walk the chain of classes?
60 return ClsName == &Ctx.Idents.get("NSString") ||
61 ClsName == &Ctx.Idents.get("NSMutableString");
62}
63
Daniel Dunbar085e8f72008-09-26 03:32:58 +000064static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
65 const PointerType *PT = T->getAsPointerType();
66 if (!PT)
67 return false;
68
69 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
70 if (!RT)
71 return false;
72
73 const RecordDecl *RD = RT->getDecl();
74 if (RD->getTagKind() != TagDecl::TK_struct)
75 return false;
76
77 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
78}
79
Chris Lattnere5c5ee12008-06-29 00:16:31 +000080//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +000081// Attribute Implementations
82//===----------------------------------------------------------------------===//
83
Daniel Dunbar3068ae02008-07-31 22:40:48 +000084// FIXME: All this manual attribute parsing code is gross. At the
85// least add some helper functions to check most argument patterns (#
86// and types of args).
87
Chris Lattner803d0802008-06-29 00:43:07 +000088static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
89 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +000090 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
91 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000092 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +000093 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +000094 }
95
Chris Lattner6b6b5372008-06-26 18:38:35 +000096 QualType curType = tDecl->getUnderlyingType();
97 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +000098 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +000099 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
100 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000101 return;
102 }
Chris Lattner545dd342008-06-28 23:36:30 +0000103 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000104 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000105 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
107 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000108 return;
109 }
110 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
111 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000112 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000113 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000114 curType.getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000115 return;
116 }
117 // unlike gcc's vector_size attribute, the size is specified as the
118 // number of elements, not the number of bytes.
119 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
120
121 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000122 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
123 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000124 return;
125 }
126 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000127 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000128 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000129 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130}
131
Chris Lattner065c5a82008-06-28 23:48:25 +0000132
133/// HandleVectorSizeAttribute - this attribute is only applicable to
134/// integral and float scalars, although arrays, pointers, and function
135/// return values are allowed in conjunction with this construct. Aggregates
136/// with this attribute are invalid, even if they are of the same size as a
137/// corresponding scalar.
138/// The raw attribute should contain precisely 1 argument, the vector size
139/// for the variable, measured in bytes. If curType and rawAttr are well
140/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000141static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000142 QualType CurType;
143 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
144 CurType = VD->getType();
145 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
146 CurType = TD->getUnderlyingType();
147 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000148 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
149 std::string("vector_size"),
150 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000151 return;
152 }
153
154 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000155 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
157 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000158 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000159 }
Chris Lattner545dd342008-06-28 23:36:30 +0000160 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000161 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000162 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
164 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000165 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000166 }
167 // navigate to the base type - we need to provide for vector pointers,
168 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (CurType->isPointerType() || CurType->isArrayType() ||
170 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000171 assert(0 && "HandleVector(): Complex type construction unimplemented");
172 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
173 do {
174 if (PointerType *PT = dyn_cast<PointerType>(canonType))
175 canonType = PT->getPointeeType().getTypePtr();
176 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
177 canonType = AT->getElementType().getTypePtr();
178 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
179 canonType = FT->getResultType().getTypePtr();
180 } while (canonType->isPointerType() || canonType->isArrayType() ||
181 canonType->isFunctionType());
182 */
183 }
184 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000185 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000186 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000187 CurType.getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000188 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000189 }
Chris Lattner803d0802008-06-29 00:43:07 +0000190 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000191 // vecSize is specified in bytes - convert to bits.
192 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
193
194 // the vector size needs to be an integral multiple of the type size.
195 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000196 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
197 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000198 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000199 }
200 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000201 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
202 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000203 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000205
206 // Success! Instantiate the vector type, the number of elements is > 0, and
207 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000208 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000209
210 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
211 VD->setType(CurType);
212 else
213 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000214}
215
Chris Lattner803d0802008-06-29 00:43:07 +0000216static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000217 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000218 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
220 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000221 return;
222 }
223
224 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000225 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
227 // If the alignment is less than or equal to 8 bits, the packed attribute
228 // has no effect.
229 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000230 S.Context.getTypeAlign(FD->getType()) <= 8)
231 S.Diag(Attr.getLoc(),
232 diag::warn_attribute_ignored_for_field_of_type,
233 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000234 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000235 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000236 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000237 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
238 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000239}
240
Ted Kremenek96329d42008-07-15 22:26:48 +0000241static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
242 // check the attribute arguments.
243 if (Attr.getNumArgs() > 0) {
244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
245 std::string("0"));
246 return;
247 }
248
249 // The IBOutlet attribute only applies to instance variables of Objective-C
250 // classes.
251 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
252 ID->addAttr(new IBOutletAttr());
253 else
254 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
255}
256
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000257static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
258
259 // GCC ignores the nonnull attribute on K&R style function
260 // prototypes, so we ignore it as well
261 const FunctionTypeProto *proto = getFunctionProto(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000262 if (!proto) {
263 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
264 "nonnull", "function");
265 return;
266 }
267
268 unsigned NumArgs = proto->getNumArgs();
269
270 // The nonnull attribute only applies to pointers.
271 llvm::SmallVector<unsigned, 10> NonNullArgs;
272
273 for (AttributeList::arg_iterator I=Attr.arg_begin(),
274 E=Attr.arg_end(); I!=E; ++I) {
275
276
277 // The argument must be an integer constant expression.
278 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
279 llvm::APSInt ArgNum(32);
280 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
282 "nonnull", Ex->getSourceRange());
283 return;
284 }
285
286 unsigned x = (unsigned) ArgNum.getZExtValue();
287
288 if (x < 1 || x > NumArgs) {
289 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Ted Kremenek6e1eb872008-07-22 16:56:21 +0000290 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000291 return;
292 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000293
294 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000295
296 // Is the function argument a pointer type?
Chris Lattnerb77792e2008-07-26 22:17:49 +0000297 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000298 // FIXME: Should also highlight argument in decl.
299 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
300 "nonnull", Ex->getSourceRange());
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000301 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 }
303
304 NonNullArgs.push_back(x);
305 }
306
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000307 // If no arguments were specified to __attribute__((nonnull)) then all
308 // pointer arguments have a nonnull attribute.
309 if (NonNullArgs.empty()) {
310 unsigned idx = 0;
311
312 for (FunctionTypeProto::arg_type_iterator
313 I=proto->arg_type_begin(), E=proto->arg_type_end(); I!=E; ++I, ++idx)
314 if ((*I)->isPointerType())
315 NonNullArgs.push_back(idx);
316
317 if (NonNullArgs.empty()) {
318 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
319 return;
320 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000321 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000322
323 unsigned* start = &NonNullArgs[0];
324 unsigned size = NonNullArgs.size();
325 std::sort(start, start + size);
326 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327}
328
Chris Lattner803d0802008-06-29 00:43:07 +0000329static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000330 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000331 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
333 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000334 return;
335 }
336
Chris Lattner545dd342008-06-28 23:36:30 +0000337 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000338 Arg = Arg->IgnoreParenCasts();
339 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
340
341 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000342 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
343 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000344 return;
345 }
346
347 const char *Alias = Str->getStrData();
348 unsigned AliasLen = Str->getByteLength();
349
350 // FIXME: check if target symbol exists in current file
351
352 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
353}
354
Chris Lattner803d0802008-06-29 00:43:07 +0000355static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000356 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000357 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000358 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
359 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000360 return;
361 }
362
363 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000364 if (!Fn) {
Chris Lattner803d0802008-06-29 00:43:07 +0000365 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
366 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000367 return;
368 }
369
370 d->addAttr(new NoReturnAttr());
371}
372
Ted Kremenek73798892008-07-25 04:39:19 +0000373static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
374 // check the attribute arguments.
375 if (Attr.getNumArgs() != 0) {
376 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
377 std::string("0"));
378 return;
379 }
380
Ted Kremenek356b63a2008-08-07 01:02:05 +0000381 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenek73798892008-07-25 04:39:19 +0000382 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek356b63a2008-08-07 01:02:05 +0000383 "unused", "variable and function");
Ted Kremenek73798892008-07-25 04:39:19 +0000384 return;
385 }
386
387 d->addAttr(new UnusedAttr());
388}
389
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000390static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
391 // check the attribute arguments.
392 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
394 return;
395 }
396
397 int priority = 65535; // FIXME: Do not hardcode such constants.
398 if (Attr.getNumArgs() > 0) {
399 Expr *E = static_cast<Expr *>(Attr.getArg(0));
400 llvm::APSInt Idx(32);
401 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
402 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
403 "constructor", "1", E->getSourceRange());
404 return;
405 }
406 priority = Idx.getZExtValue();
407 }
408
409 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
410 if (!Fn) {
411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
412 "constructor", "function");
413 return;
414 }
415
416 d->addAttr(new ConstructorAttr(priority));
417}
418
419static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
420 // check the attribute arguments.
421 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
423 return;
424 }
425
426 int priority = 65535; // FIXME: Do not hardcode such constants.
427 if (Attr.getNumArgs() > 0) {
428 Expr *E = static_cast<Expr *>(Attr.getArg(0));
429 llvm::APSInt Idx(32);
430 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
432 "destructor", "1", E->getSourceRange());
433 return;
434 }
435 priority = Idx.getZExtValue();
436 }
437
Anders Carlsson6782fc62008-08-22 22:10:48 +0000438 if (!isa<FunctionDecl>(d)) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
440 "destructor", "function");
441 return;
442 }
443
444 d->addAttr(new DestructorAttr(priority));
445}
446
Chris Lattner803d0802008-06-29 00:43:07 +0000447static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000448 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000449 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
451 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000452 return;
453 }
454
455 d->addAttr(new DeprecatedAttr());
456}
457
Chris Lattner803d0802008-06-29 00:43:07 +0000458static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000459 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000460 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
462 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000463 return;
464 }
465
Chris Lattner545dd342008-06-28 23:36:30 +0000466 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000467 Arg = Arg->IgnoreParenCasts();
468 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
469
470 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
472 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000473 return;
474 }
475
476 const char *TypeStr = Str->getStrData();
477 unsigned TypeLen = Str->getByteLength();
478 VisibilityAttr::VisibilityTypes type;
479
480 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
481 type = VisibilityAttr::DefaultVisibility;
482 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
483 type = VisibilityAttr::HiddenVisibility;
484 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
485 type = VisibilityAttr::HiddenVisibility; // FIXME
486 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
487 type = VisibilityAttr::ProtectedVisibility;
488 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000489 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
490 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000491 return;
492 }
493
494 d->addAttr(new VisibilityAttr(type));
495}
496
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000497static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000498 if (!Attr.getParameterName()) {
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
500 "objc_gc", std::string("1"));
501 return;
502 }
503
504 if (Attr.getNumArgs() != 0) {
505 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
506 std::string("1"));
507 return;
508 }
509
510 const char *TypeStr = Attr.getParameterName()->getName();
511 unsigned TypeLen = Attr.getParameterName()->getLength();
512
513 ObjCGCAttr::GCAttrTypes type;
514
515 if (TypeLen == 4 && !memcmp(TypeStr, "weak", 4))
516 type = ObjCGCAttr::Weak;
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000517 else if (TypeLen == 6 && !memcmp(TypeStr, "strong", 6))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000518 type = ObjCGCAttr::Strong;
519 else {
520 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
521 "objc_gc", TypeStr);
522 return;
523 }
524
525 d->addAttr(new ObjCGCAttr(type));
526}
527
Steve Naroff9eae5762008-09-18 16:44:58 +0000528static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
529 if (!Attr.getParameterName()) {
530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
531 "blocks", std::string("1"));
532 return;
533 }
534
535 if (Attr.getNumArgs() != 0) {
536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
537 std::string("1"));
538 return;
539 }
540 const char *TypeStr = Attr.getParameterName()->getName();
541 unsigned TypeLen = Attr.getParameterName()->getLength();
542
543 BlocksAttr::BlocksAttrTypes type;
544
545 if (TypeLen == 5 && !memcmp(TypeStr, "byref", 5))
546 type = BlocksAttr::ByRef;
547 else {
548 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
549 "blocks", TypeStr);
550 return;
551 }
552
553 d->addAttr(new BlocksAttr(type));
554}
555
Chris Lattner803d0802008-06-29 00:43:07 +0000556static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000558 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
560 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000561 return;
562 }
563
564 d->addAttr(new WeakAttr());
565}
566
Chris Lattner803d0802008-06-29 00:43:07 +0000567static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000568 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000569 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000570 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
571 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000572 return;
573 }
574
575 d->addAttr(new DLLImportAttr());
576}
577
Chris Lattner803d0802008-06-29 00:43:07 +0000578static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000579 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000580 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
582 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000583 return;
584 }
585
586 d->addAttr(new DLLExportAttr());
587}
588
Chris Lattner803d0802008-06-29 00:43:07 +0000589static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000590 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000591 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000592 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
593 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000594 return;
595 }
596
597 d->addAttr(new StdCallAttr());
598}
599
Chris Lattner803d0802008-06-29 00:43:07 +0000600static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000601 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000602 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
604 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000605 return;
606 }
607
608 d->addAttr(new FastCallAttr());
609}
610
Chris Lattner803d0802008-06-29 00:43:07 +0000611static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000612 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000613 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
615 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 return;
617 }
618
619 d->addAttr(new NoThrowAttr());
620}
621
622/// Handle __attribute__((format(type,idx,firstarg))) attributes
623/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000624static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000625
Chris Lattner545dd342008-06-28 23:36:30 +0000626 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000627 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000628 "format", std::string("1"));
629 return;
630 }
631
Chris Lattner545dd342008-06-28 23:36:30 +0000632 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
634 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000635 return;
636 }
637
638 // GCC ignores the format attribute on K&R style function
639 // prototypes, so we ignore it as well
640 const FunctionTypeProto *proto = getFunctionProto(d);
641
642 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000643 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
644 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000645 return;
646 }
647
648 // FIXME: in C++ the implicit 'this' function parameter also counts.
649 // this is needed in order to be compatible with GCC
650 // the index must start in 1 and the limit is numargs+1
651 unsigned NumArgs = proto->getNumArgs();
652 unsigned FirstIdx = 1;
653
Chris Lattner545dd342008-06-28 23:36:30 +0000654 const char *Format = Attr.getParameterName()->getName();
655 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000656
657 // Normalize the argument, __foo__ becomes foo.
658 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
659 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
660 Format += 2;
661 FormatLen -= 4;
662 }
663
664 bool Supported = false;
665 bool is_NSString = false;
666 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000667 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000668
669 switch (FormatLen) {
670 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000671 case 5: Supported = !memcmp(Format, "scanf", 5); break;
672 case 6: Supported = !memcmp(Format, "printf", 6); break;
673 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000674 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000675 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
676 (is_NSString = !memcmp(Format, "NSString", 8)) ||
677 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000678 break;
679 }
680
681 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000682 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000683 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000684 return;
685 }
686
687 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000688 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000689 llvm::APSInt Idx(32);
690 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
691 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000692 "format", std::string("2"), IdxExpr->getSourceRange());
693 return;
694 }
695
696 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000697 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000698 "format", std::string("2"), IdxExpr->getSourceRange());
699 return;
700 }
701
702 // FIXME: Do we need to bounds check?
703 unsigned ArgIdx = Idx.getZExtValue() - 1;
704
705 // make sure the format string is really a string
706 QualType Ty = proto->getArgType(ArgIdx);
707
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000708 if (is_CFString) {
709 if (!isCFStringType(Ty, S.Context)) {
710 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
711 "a CFString", IdxExpr->getSourceRange());
712 return;
713 }
714 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 // FIXME: do we need to check if the type is NSString*? What are
716 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000717 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000718 // FIXME: Should highlight the actual expression that has the
719 // wrong type.
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000720 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
721 "an NSString", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000722 return;
723 }
724 } else if (!Ty->isPointerType() ||
725 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
726 // FIXME: Should highlight the actual expression that has the
727 // wrong type.
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000728 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
729 "a string type", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000730 return;
731 }
732
733 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000734 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000735 llvm::APSInt FirstArg(32);
736 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000738 "format", std::string("3"), FirstArgExpr->getSourceRange());
739 return;
740 }
741
742 // check if the function is variadic if the 3rd argument non-zero
743 if (FirstArg != 0) {
744 if (proto->isVariadic()) {
745 ++NumArgs; // +1 for ...
746 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000747 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000748 return;
749 }
750 }
751
752 // strftime requires FirstArg to be 0 because it doesn't read from any variable
753 // the input is just the current time + the format string
754 if (is_strftime) {
755 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000756 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000757 FirstArgExpr->getSourceRange());
758 return;
759 }
760 // if 0 it disables parameter checking (to use with e.g. va_list)
761 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000763 "format", std::string("3"), FirstArgExpr->getSourceRange());
764 return;
765 }
766
767 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
768 Idx.getZExtValue(), FirstArg.getZExtValue()));
769}
770
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000771static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
772 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000773 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000774 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776 std::string("0"));
777 return;
778 }
779
Eli Friedmanbc887452008-09-02 05:19:23 +0000780 // FIXME: This shouldn't be restricted to typedefs
781 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
782 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000783 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000784 "transparent_union", "union");
785 return;
786 }
787
Eli Friedmanbc887452008-09-02 05:19:23 +0000788 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000789
Eli Friedmanbc887452008-09-02 05:19:23 +0000790 // FIXME: Should we do a check for RD->isDefinition()?
791
792 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
793 // we might silently generate incorrect code; see following code
794 for (int i = 0; i < RD->getNumMembers(); i++) {
795 if (!RD->getMember(i)->getType()->isPointerType()) {
796 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
797 return;
798 }
799 }
800
801 // FIXME: This is a complete hack; we should be properly propagating
802 // transparent_union through Sema. That said, this is close enough to
803 // correctly compile all the common cases of transparent_union without
804 // errors or warnings
805 QualType NewTy = S.Context.VoidPtrTy;
806 NewTy.addConst();
807 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000808}
809
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000810static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000811 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000812 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
814 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000815 return;
816 }
Chris Lattner545dd342008-06-28 23:36:30 +0000817 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000818 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
819
820 // Make sure that there is a string literal as the annotation's single
821 // argument.
822 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000823 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000824 return;
825 }
826 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
827 SE->getByteLength())));
828}
829
Chris Lattner803d0802008-06-29 00:43:07 +0000830static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000831 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000832 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
834 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000835 return;
836 }
837
838 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000839 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000840 // FIXME: This should be the target specific maximum alignment.
841 // (For now we just use 128 bits which is the maximum on X86.
842 Align = 128;
843 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000845
846 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
847 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000848 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
849 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
850 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000851 return;
852 }
853 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000854}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000855
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000856/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000857/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000858///
859/// Despite what would be logical, the mode attribute is a decl attribute,
860/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
861/// 'G' be HImode, not an intermediate pointer.
862///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000863static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000864 // This attribute isn't documented, but glibc uses it. It changes
865 // the width of an int or unsigned int to the specified size.
866
867 // Check that there aren't any arguments
868 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000869 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
870 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000871 return;
872 }
873
874 IdentifierInfo *Name = Attr.getParameterName();
875 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000876 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000877 return;
878 }
879 const char *Str = Name->getName();
880 unsigned Len = Name->getLength();
881
882 // Normalize the attribute name, __foo__ becomes foo.
883 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
884 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
885 Str += 2;
886 Len -= 4;
887 }
888
889 unsigned DestWidth = 0;
890 bool IntegerMode = true;
891 switch (Len) {
892 case 2:
893 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
894 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
895 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
896 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
897 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
898 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
899 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
900 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
901 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
902 break;
903 case 4:
904 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
905 // pointer on PIC16 and other embedded platforms.
906 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000907 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000908 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000909 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000910 break;
911 case 7:
912 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000913 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000914 break;
915 }
916
917 QualType OldTy;
918 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
919 OldTy = TD->getUnderlyingType();
920 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
921 OldTy = VD->getType();
922 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000923 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
924 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000925 return;
926 }
927
928 // FIXME: Need proper fixed-width types
929 QualType NewTy;
930 switch (DestWidth) {
931 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000932 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000933 return;
934 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000935 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000936 return;
937 case 8:
938 assert(IntegerMode);
939 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000940 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000941 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000942 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000943 break;
944 case 16:
945 assert(IntegerMode);
946 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000947 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000948 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000949 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000950 break;
951 case 32:
952 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000953 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000954 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000955 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000956 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000957 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000958 break;
959 case 64:
960 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000961 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000962 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000963 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000964 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000965 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000966 break;
967 }
968
969 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000970 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000971 else if (!(IntegerMode && OldTy->isIntegerType()) &&
972 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000973 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000974 }
975
976 // Install the new type.
977 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
978 TD->setUnderlyingType(NewTy);
979 else
980 cast<ValueDecl>(D)->setType(NewTy);
981}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000982
983//===----------------------------------------------------------------------===//
984// Top Level Sema Entry Points
985//===----------------------------------------------------------------------===//
986
Chris Lattner803d0802008-06-29 00:43:07 +0000987/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
988/// the attribute applies to decls. If the attribute is a type attribute, just
989/// silently ignore it.
990static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
991 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000992 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000993 case AttributeList::AT_address_space:
994 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
995 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000996 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
997 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
998 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
999 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1000 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1001 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1002 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1003 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001004 case AttributeList::AT_ext_vector_type:
1005 HandleExtVectorTypeAttr(D, Attr, S);
1006 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001007 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1008 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001009 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001010 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1011 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1012 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1013 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1014 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1015 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1016 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001017 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1018 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001019 case AttributeList::AT_transparent_union:
1020 HandleTransparentUnionAttr(D, Attr, S);
1021 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001022 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001023 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001024 default:
1025#if 0
1026 // TODO: when we have the full set of attributes, warn about unknown ones.
1027 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
1028 Attr->getName()->getName());
1029#endif
1030 break;
1031 }
1032}
1033
1034/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1035/// attribute list to the specified decl, ignoring any type attributes.
1036void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1037 while (AttrList) {
1038 ProcessDeclAttribute(D, *AttrList, *this);
1039 AttrList = AttrList->getNext();
1040 }
1041}
1042
1043
Chris Lattner0744e5f2008-06-29 00:23:49 +00001044/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1045/// it, apply them to D. This is a bit tricky because PD can have attributes
1046/// specified in many different places, and we need to find and apply them all.
1047void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1048 // Apply decl attributes from the DeclSpec if present.
1049 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1050 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001051
Chris Lattner0744e5f2008-06-29 00:23:49 +00001052 // Walk the declarator structure, applying decl attributes that were in a type
1053 // position to the decl itself. This handles cases like:
1054 // int *__attr__(x)** D;
1055 // when X is a decl attribute.
1056 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1057 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1058 ProcessDeclAttributeList(D, Attrs);
1059
1060 // Finally, apply any attributes on the decl itself.
1061 if (const AttributeList *Attrs = PD.getAttributes())
1062 ProcessDeclAttributeList(D, Attrs);
1063}
1064