blob: 2bdb885f53587a685e19ced3a6187c2bd635813b [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
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
51/// isFunctionOrMethod - Return true if the given decl is a (non-K&R)
52/// function or an Objective-C method.
53static bool isFunctionOrMethod(Decl *d) {
54 return getFunctionProto(d) || isa<ObjCMethodDecl>(d);
55
56}
57
58static unsigned getFunctionOrMethodNumArgs(Decl *d) {
59 if (const FunctionTypeProto *proto = getFunctionProto(d)) {
60 return proto->getNumArgs();
61 } else {
62 return cast<ObjCMethodDecl>(d)->getNumParams();
63 }
64}
65
66static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
67 if (const FunctionTypeProto *proto = getFunctionProto(d)) {
68 return proto->getArgType(Idx);
69 } else {
70 return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
71 }
72}
73
74static bool isFunctionOrMethodVariadic(Decl *d) {
75 if (const FunctionTypeProto *proto = getFunctionProto(d)) {
76 return proto->isVariadic();
77 } else {
78 return cast<ObjCMethodDecl>(d)->isVariadic();
79 }
80}
81
Chris Lattner6b6b5372008-06-26 18:38:35 +000082static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000083 const PointerType *PT = T->getAsPointerType();
84 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000085 return false;
86
Chris Lattnerb77792e2008-07-26 22:17:49 +000087 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000088 if (!ClsT)
89 return false;
90
91 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
92
93 // FIXME: Should we walk the chain of classes?
94 return ClsName == &Ctx.Idents.get("NSString") ||
95 ClsName == &Ctx.Idents.get("NSMutableString");
96}
97
Daniel Dunbar085e8f72008-09-26 03:32:58 +000098static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
99 const PointerType *PT = T->getAsPointerType();
100 if (!PT)
101 return false;
102
103 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
104 if (!RT)
105 return false;
106
107 const RecordDecl *RD = RT->getDecl();
108 if (RD->getTagKind() != TagDecl::TK_struct)
109 return false;
110
111 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
112}
113
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000114//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000115// Attribute Implementations
116//===----------------------------------------------------------------------===//
117
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000118// FIXME: All this manual attribute parsing code is gross. At the
119// least add some helper functions to check most argument patterns (#
120// and types of args).
121
Chris Lattner803d0802008-06-29 00:43:07 +0000122static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
123 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000124 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
125 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000126 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000127 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000128 }
129
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 QualType curType = tDecl->getUnderlyingType();
131 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000132 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
134 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000135 return;
136 }
Chris Lattner545dd342008-06-28 23:36:30 +0000137 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000139 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
141 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000142 return;
143 }
144 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
145 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000146 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000147 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000148 curType.getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000149 return;
150 }
151 // unlike gcc's vector_size attribute, the size is specified as the
152 // number of elements, not the number of bytes.
153 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
154
155 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000156 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
157 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000158 return;
159 }
160 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000161 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000162 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000163 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000164}
165
Chris Lattner065c5a82008-06-28 23:48:25 +0000166
167/// HandleVectorSizeAttribute - this attribute is only applicable to
168/// integral and float scalars, although arrays, pointers, and function
169/// return values are allowed in conjunction with this construct. Aggregates
170/// with this attribute are invalid, even if they are of the same size as a
171/// corresponding scalar.
172/// The raw attribute should contain precisely 1 argument, the vector size
173/// for the variable, measured in bytes. If curType and rawAttr are well
174/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000175static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000176 QualType CurType;
177 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
178 CurType = VD->getType();
179 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
180 CurType = TD->getUnderlyingType();
181 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000182 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
183 std::string("vector_size"),
184 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000185 return;
186 }
187
188 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000189 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000190 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
191 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000192 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193 }
Chris Lattner545dd342008-06-28 23:36:30 +0000194 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000195 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000196 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
197 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
198 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000199 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 }
201 // navigate to the base type - we need to provide for vector pointers,
202 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000203 if (CurType->isPointerType() || CurType->isArrayType() ||
204 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 assert(0 && "HandleVector(): Complex type construction unimplemented");
206 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
207 do {
208 if (PointerType *PT = dyn_cast<PointerType>(canonType))
209 canonType = PT->getPointeeType().getTypePtr();
210 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
211 canonType = AT->getElementType().getTypePtr();
212 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
213 canonType = FT->getResultType().getTypePtr();
214 } while (canonType->isPointerType() || canonType->isArrayType() ||
215 canonType->isFunctionType());
216 */
217 }
218 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000219 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000220 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000221 CurType.getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000222 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000223 }
Chris Lattner803d0802008-06-29 00:43:07 +0000224 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000225 // vecSize is specified in bytes - convert to bits.
226 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
227
228 // the vector size needs to be an integral multiple of the type size.
229 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
231 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000232 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000233 }
234 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000235 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
236 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000237 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000238 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000239
240 // Success! Instantiate the vector type, the number of elements is > 0, and
241 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000242 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000243
244 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
245 VD->setType(CurType);
246 else
247 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000248}
249
Chris Lattner803d0802008-06-29 00:43:07 +0000250static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000252 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
254 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000255 return;
256 }
257
258 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000259 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
261 // If the alignment is less than or equal to 8 bits, the packed attribute
262 // has no effect.
263 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000264 S.Context.getTypeAlign(FD->getType()) <= 8)
265 S.Diag(Attr.getLoc(),
266 diag::warn_attribute_ignored_for_field_of_type,
267 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000268 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000269 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000270 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000271 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
272 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000273}
274
Ted Kremenek96329d42008-07-15 22:26:48 +0000275static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
276 // check the attribute arguments.
277 if (Attr.getNumArgs() > 0) {
278 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
279 std::string("0"));
280 return;
281 }
282
283 // The IBOutlet attribute only applies to instance variables of Objective-C
284 // classes.
285 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
286 ID->addAttr(new IBOutletAttr());
287 else
288 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
289}
290
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000291static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
292
293 // GCC ignores the nonnull attribute on K&R style function
294 // prototypes, so we ignore it as well
295 const FunctionTypeProto *proto = getFunctionProto(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000296 if (!proto) {
297 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
298 "nonnull", "function");
299 return;
300 }
301
302 unsigned NumArgs = proto->getNumArgs();
303
304 // The nonnull attribute only applies to pointers.
305 llvm::SmallVector<unsigned, 10> NonNullArgs;
306
307 for (AttributeList::arg_iterator I=Attr.arg_begin(),
308 E=Attr.arg_end(); I!=E; ++I) {
309
310
311 // The argument must be an integer constant expression.
312 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
313 llvm::APSInt ArgNum(32);
314 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
315 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
316 "nonnull", Ex->getSourceRange());
317 return;
318 }
319
320 unsigned x = (unsigned) ArgNum.getZExtValue();
321
322 if (x < 1 || x > NumArgs) {
323 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Ted Kremenek6e1eb872008-07-22 16:56:21 +0000324 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000325 return;
326 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000327
328 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329
330 // Is the function argument a pointer type?
Chris Lattnerb77792e2008-07-26 22:17:49 +0000331 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 // FIXME: Should also highlight argument in decl.
333 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
334 "nonnull", Ex->getSourceRange());
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000335 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336 }
337
338 NonNullArgs.push_back(x);
339 }
340
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000341 // If no arguments were specified to __attribute__((nonnull)) then all
342 // pointer arguments have a nonnull attribute.
343 if (NonNullArgs.empty()) {
344 unsigned idx = 0;
345
346 for (FunctionTypeProto::arg_type_iterator
347 I=proto->arg_type_begin(), E=proto->arg_type_end(); I!=E; ++I, ++idx)
348 if ((*I)->isPointerType())
349 NonNullArgs.push_back(idx);
350
351 if (NonNullArgs.empty()) {
352 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
353 return;
354 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000356
357 unsigned* start = &NonNullArgs[0];
358 unsigned size = NonNullArgs.size();
359 std::sort(start, start + size);
360 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361}
362
Chris Lattner803d0802008-06-29 00:43:07 +0000363static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000364 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000365 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000366 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
367 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000368 return;
369 }
370
Chris Lattner545dd342008-06-28 23:36:30 +0000371 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000372 Arg = Arg->IgnoreParenCasts();
373 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
374
375 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000376 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
377 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000378 return;
379 }
380
381 const char *Alias = Str->getStrData();
382 unsigned AliasLen = Str->getByteLength();
383
384 // FIXME: check if target symbol exists in current file
385
386 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
387}
388
Chris Lattner803d0802008-06-29 00:43:07 +0000389static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000390 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000391 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
393 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000394 return;
395 }
396
Daniel Dunbar35682492008-09-26 04:12:28 +0000397 if (!isa<FunctionDecl>(d) && !isa<ObjCMethodDecl>(d)) {
Chris Lattner803d0802008-06-29 00:43:07 +0000398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
399 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000400 return;
401 }
402
403 d->addAttr(new NoReturnAttr());
404}
405
Ted Kremenek73798892008-07-25 04:39:19 +0000406static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
407 // check the attribute arguments.
408 if (Attr.getNumArgs() != 0) {
409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
410 std::string("0"));
411 return;
412 }
413
Ted Kremenek356b63a2008-08-07 01:02:05 +0000414 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenek73798892008-07-25 04:39:19 +0000415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek356b63a2008-08-07 01:02:05 +0000416 "unused", "variable and function");
Ted Kremenek73798892008-07-25 04:39:19 +0000417 return;
418 }
419
420 d->addAttr(new UnusedAttr());
421}
422
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000423static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
424 // check the attribute arguments.
425 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
426 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
427 return;
428 }
429
430 int priority = 65535; // FIXME: Do not hardcode such constants.
431 if (Attr.getNumArgs() > 0) {
432 Expr *E = static_cast<Expr *>(Attr.getArg(0));
433 llvm::APSInt Idx(32);
434 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
436 "constructor", "1", E->getSourceRange());
437 return;
438 }
439 priority = Idx.getZExtValue();
440 }
441
442 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
443 if (!Fn) {
444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
445 "constructor", "function");
446 return;
447 }
448
449 d->addAttr(new ConstructorAttr(priority));
450}
451
452static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
453 // check the attribute arguments.
454 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
455 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
456 return;
457 }
458
459 int priority = 65535; // FIXME: Do not hardcode such constants.
460 if (Attr.getNumArgs() > 0) {
461 Expr *E = static_cast<Expr *>(Attr.getArg(0));
462 llvm::APSInt Idx(32);
463 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
465 "destructor", "1", E->getSourceRange());
466 return;
467 }
468 priority = Idx.getZExtValue();
469 }
470
Anders Carlsson6782fc62008-08-22 22:10:48 +0000471 if (!isa<FunctionDecl>(d)) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
473 "destructor", "function");
474 return;
475 }
476
477 d->addAttr(new DestructorAttr(priority));
478}
479
Chris Lattner803d0802008-06-29 00:43:07 +0000480static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000481 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000482 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
484 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000485 return;
486 }
487
488 d->addAttr(new DeprecatedAttr());
489}
490
Chris Lattner803d0802008-06-29 00:43:07 +0000491static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000492 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000493 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
495 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000496 return;
497 }
498
Chris Lattner545dd342008-06-28 23:36:30 +0000499 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000500 Arg = Arg->IgnoreParenCasts();
501 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
502
503 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
505 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000506 return;
507 }
508
509 const char *TypeStr = Str->getStrData();
510 unsigned TypeLen = Str->getByteLength();
511 VisibilityAttr::VisibilityTypes type;
512
513 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
514 type = VisibilityAttr::DefaultVisibility;
515 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
516 type = VisibilityAttr::HiddenVisibility;
517 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
518 type = VisibilityAttr::HiddenVisibility; // FIXME
519 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
520 type = VisibilityAttr::ProtectedVisibility;
521 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000522 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
523 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000524 return;
525 }
526
527 d->addAttr(new VisibilityAttr(type));
528}
529
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000530static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000531 if (!Attr.getParameterName()) {
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
533 "objc_gc", std::string("1"));
534 return;
535 }
536
537 if (Attr.getNumArgs() != 0) {
538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
539 std::string("1"));
540 return;
541 }
542
543 const char *TypeStr = Attr.getParameterName()->getName();
544 unsigned TypeLen = Attr.getParameterName()->getLength();
545
546 ObjCGCAttr::GCAttrTypes type;
547
548 if (TypeLen == 4 && !memcmp(TypeStr, "weak", 4))
549 type = ObjCGCAttr::Weak;
Anders Carlsson6e14a8f2008-08-24 16:33:25 +0000550 else if (TypeLen == 6 && !memcmp(TypeStr, "strong", 6))
Anders Carlssonaa0d25b2008-08-23 23:22:21 +0000551 type = ObjCGCAttr::Strong;
552 else {
553 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
554 "objc_gc", TypeStr);
555 return;
556 }
557
558 d->addAttr(new ObjCGCAttr(type));
559}
560
Steve Naroff9eae5762008-09-18 16:44:58 +0000561static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
562 if (!Attr.getParameterName()) {
563 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
564 "blocks", std::string("1"));
565 return;
566 }
567
568 if (Attr.getNumArgs() != 0) {
569 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
570 std::string("1"));
571 return;
572 }
573 const char *TypeStr = Attr.getParameterName()->getName();
574 unsigned TypeLen = Attr.getParameterName()->getLength();
575
576 BlocksAttr::BlocksAttrTypes type;
577
578 if (TypeLen == 5 && !memcmp(TypeStr, "byref", 5))
579 type = BlocksAttr::ByRef;
580 else {
581 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
582 "blocks", TypeStr);
583 return;
584 }
585
586 d->addAttr(new BlocksAttr(type));
587}
588
Chris Lattner803d0802008-06-29 00:43:07 +0000589static void HandleWeakAttr(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 WeakAttr());
598}
599
Chris Lattner803d0802008-06-29 00:43:07 +0000600static void HandleDLLImportAttr(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 DLLImportAttr());
609}
610
Chris Lattner803d0802008-06-29 00:43:07 +0000611static void HandleDLLExportAttr(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 DLLExportAttr());
620}
621
Chris Lattner803d0802008-06-29 00:43:07 +0000622static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000623 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000624 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000625 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
626 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000627 return;
628 }
629
630 d->addAttr(new StdCallAttr());
631}
632
Chris Lattner803d0802008-06-29 00:43:07 +0000633static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000634 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000635 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
637 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000638 return;
639 }
640
641 d->addAttr(new FastCallAttr());
642}
643
Chris Lattner803d0802008-06-29 00:43:07 +0000644static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000645 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000646 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000647 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
648 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000649 return;
650 }
651
652 d->addAttr(new NoThrowAttr());
653}
654
655/// Handle __attribute__((format(type,idx,firstarg))) attributes
656/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000657static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000658
Chris Lattner545dd342008-06-28 23:36:30 +0000659 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000660 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 "format", std::string("1"));
662 return;
663 }
664
Chris Lattner545dd342008-06-28 23:36:30 +0000665 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
667 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000668 return;
669 }
670
671 // GCC ignores the format attribute on K&R style function
672 // prototypes, so we ignore it as well
Daniel Dunbar35682492008-09-26 04:12:28 +0000673 if (!isFunctionOrMethod(d)) {
Chris Lattner803d0802008-06-29 00:43:07 +0000674 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
675 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000676 return;
677 }
678
679 // FIXME: in C++ the implicit 'this' function parameter also counts.
680 // this is needed in order to be compatible with GCC
681 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +0000682 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000683 unsigned FirstIdx = 1;
684
Chris Lattner545dd342008-06-28 23:36:30 +0000685 const char *Format = Attr.getParameterName()->getName();
686 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000687
688 // Normalize the argument, __foo__ becomes foo.
689 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
690 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
691 Format += 2;
692 FormatLen -= 4;
693 }
694
695 bool Supported = false;
696 bool is_NSString = false;
697 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000698 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699
700 switch (FormatLen) {
701 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000702 case 5: Supported = !memcmp(Format, "scanf", 5); break;
703 case 6: Supported = !memcmp(Format, "printf", 6); break;
704 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000705 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000706 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
707 (is_NSString = !memcmp(Format, "NSString", 8)) ||
708 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000709 break;
710 }
711
712 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000713 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000714 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 return;
716 }
717
718 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000719 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000720 llvm::APSInt Idx(32);
721 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
722 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723 "format", std::string("2"), IdxExpr->getSourceRange());
724 return;
725 }
726
727 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000728 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000729 "format", std::string("2"), IdxExpr->getSourceRange());
730 return;
731 }
732
733 // FIXME: Do we need to bounds check?
734 unsigned ArgIdx = Idx.getZExtValue() - 1;
735
736 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +0000737 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000738
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000739 if (is_CFString) {
740 if (!isCFStringType(Ty, S.Context)) {
741 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
742 "a CFString", IdxExpr->getSourceRange());
743 return;
744 }
745 } else if (is_NSString) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000746 // FIXME: do we need to check if the type is NSString*? What are
747 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000748 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000749 // FIXME: Should highlight the actual expression that has the
750 // wrong type.
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000751 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
752 "an NSString", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000753 return;
754 }
755 } else if (!Ty->isPointerType() ||
756 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
757 // FIXME: Should highlight the actual expression that has the
758 // wrong type.
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000759 S.Diag(Attr.getLoc(), diag::err_format_attribute_not,
760 "a string type", IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000761 return;
762 }
763
764 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000765 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000766 llvm::APSInt FirstArg(32);
767 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
768 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000769 "format", std::string("3"), FirstArgExpr->getSourceRange());
770 return;
771 }
772
773 // check if the function is variadic if the 3rd argument non-zero
774 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +0000775 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776 ++NumArgs; // +1 for ...
777 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000778 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000779 return;
780 }
781 }
782
783 // strftime requires FirstArg to be 0 because it doesn't read from any variable
784 // the input is just the current time + the format string
785 if (is_strftime) {
786 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000787 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000788 FirstArgExpr->getSourceRange());
789 return;
790 }
791 // if 0 it disables parameter checking (to use with e.g. va_list)
792 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000794 "format", std::string("3"), FirstArgExpr->getSourceRange());
795 return;
796 }
797
798 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
799 Idx.getZExtValue(), FirstArg.getZExtValue()));
800}
801
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000802static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
803 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000804 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000805 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000807 std::string("0"));
808 return;
809 }
810
Eli Friedmanbc887452008-09-02 05:19:23 +0000811 // FIXME: This shouldn't be restricted to typedefs
812 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
813 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000815 "transparent_union", "union");
816 return;
817 }
818
Eli Friedmanbc887452008-09-02 05:19:23 +0000819 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000820
Eli Friedmanbc887452008-09-02 05:19:23 +0000821 // FIXME: Should we do a check for RD->isDefinition()?
822
823 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
824 // we might silently generate incorrect code; see following code
825 for (int i = 0; i < RD->getNumMembers(); i++) {
826 if (!RD->getMember(i)->getType()->isPointerType()) {
827 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
828 return;
829 }
830 }
831
832 // FIXME: This is a complete hack; we should be properly propagating
833 // transparent_union through Sema. That said, this is close enough to
834 // correctly compile all the common cases of transparent_union without
835 // errors or warnings
836 QualType NewTy = S.Context.VoidPtrTy;
837 NewTy.addConst();
838 TD->setUnderlyingType(NewTy);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000839}
840
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000841static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000842 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000843 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
845 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000846 return;
847 }
Chris Lattner545dd342008-06-28 23:36:30 +0000848 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000849 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
850
851 // Make sure that there is a string literal as the annotation's single
852 // argument.
853 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000854 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000855 return;
856 }
857 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
858 SE->getByteLength())));
859}
860
Chris Lattner803d0802008-06-29 00:43:07 +0000861static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000862 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000863 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
865 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000866 return;
867 }
868
869 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000870 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000871 // FIXME: This should be the target specific maximum alignment.
872 // (For now we just use 128 bits which is the maximum on X86.
873 Align = 128;
874 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000875 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000876
877 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
878 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000879 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
881 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000882 return;
883 }
884 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000886
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000887/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000888/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000889///
890/// Despite what would be logical, the mode attribute is a decl attribute,
891/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
892/// 'G' be HImode, not an intermediate pointer.
893///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000894static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000895 // This attribute isn't documented, but glibc uses it. It changes
896 // the width of an int or unsigned int to the specified size.
897
898 // Check that there aren't any arguments
899 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
901 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000902 return;
903 }
904
905 IdentifierInfo *Name = Attr.getParameterName();
906 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000907 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000908 return;
909 }
910 const char *Str = Name->getName();
911 unsigned Len = Name->getLength();
912
913 // Normalize the attribute name, __foo__ becomes foo.
914 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
915 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
916 Str += 2;
917 Len -= 4;
918 }
919
920 unsigned DestWidth = 0;
921 bool IntegerMode = true;
922 switch (Len) {
923 case 2:
924 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
925 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
926 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
927 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
928 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
929 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
930 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
931 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
932 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
933 break;
934 case 4:
935 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
936 // pointer on PIC16 and other embedded platforms.
937 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000938 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000939 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000940 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000941 break;
942 case 7:
943 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000944 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000945 break;
946 }
947
948 QualType OldTy;
949 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
950 OldTy = TD->getUnderlyingType();
951 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
952 OldTy = VD->getType();
953 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000954 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
955 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000956 return;
957 }
958
959 // FIXME: Need proper fixed-width types
960 QualType NewTy;
961 switch (DestWidth) {
962 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000963 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000964 return;
965 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000966 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000967 return;
968 case 8:
969 assert(IntegerMode);
970 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000971 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000972 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000973 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000974 break;
975 case 16:
976 assert(IntegerMode);
977 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000978 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000979 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000980 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000981 break;
982 case 32:
983 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000984 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000985 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000986 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000987 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000988 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000989 break;
990 case 64:
991 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000992 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000993 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000994 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000995 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000996 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000997 break;
998 }
999
1000 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001001 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001002 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1003 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001004 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001005 }
1006
1007 // Install the new type.
1008 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1009 TD->setUnderlyingType(NewTy);
1010 else
1011 cast<ValueDecl>(D)->setType(NewTy);
1012}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001013
1014//===----------------------------------------------------------------------===//
1015// Top Level Sema Entry Points
1016//===----------------------------------------------------------------------===//
1017
Chris Lattner803d0802008-06-29 00:43:07 +00001018/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
1019/// the attribute applies to decls. If the attribute is a type attribute, just
1020/// silently ignore it.
1021static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1022 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001023 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001024 case AttributeList::AT_address_space:
1025 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1026 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001027 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1028 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
1029 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1030 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1031 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1032 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1033 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1034 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001035 case AttributeList::AT_ext_vector_type:
1036 HandleExtVectorTypeAttr(D, Attr, S);
1037 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001038 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1039 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001040 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001041 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1042 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1043 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1044 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1045 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1046 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1047 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001048 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1049 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001050 case AttributeList::AT_transparent_union:
1051 HandleTransparentUnionAttr(D, Attr, S);
1052 break;
Anders Carlssonaa0d25b2008-08-23 23:22:21 +00001053 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001054 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001055 default:
1056#if 0
1057 // TODO: when we have the full set of attributes, warn about unknown ones.
1058 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
1059 Attr->getName()->getName());
1060#endif
1061 break;
1062 }
1063}
1064
1065/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1066/// attribute list to the specified decl, ignoring any type attributes.
1067void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1068 while (AttrList) {
1069 ProcessDeclAttribute(D, *AttrList, *this);
1070 AttrList = AttrList->getNext();
1071 }
1072}
1073
1074
Chris Lattner0744e5f2008-06-29 00:23:49 +00001075/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1076/// it, apply them to D. This is a bit tricky because PD can have attributes
1077/// specified in many different places, and we need to find and apply them all.
1078void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1079 // Apply decl attributes from the DeclSpec if present.
1080 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1081 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001082
Chris Lattner0744e5f2008-06-29 00:23:49 +00001083 // Walk the declarator structure, applying decl attributes that were in a type
1084 // position to the decl itself. This handles cases like:
1085 // int *__attr__(x)** D;
1086 // when X is a decl attribute.
1087 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1088 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1089 ProcessDeclAttributeList(D, Attrs);
1090
1091 // Finally, apply any attributes on the decl itself.
1092 if (const AttributeList *Attrs = PD.getAttributes())
1093 ProcessDeclAttributeList(D, Attrs);
1094}
1095