blob: eb9888e9a28e91e2a2a9bc458384b11946bfe364 [file] [log] [blame]
Chris Lattner6953a072008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000021#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner2024f0a2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Chris Lattner6953a072008-06-26 18:38:35 +000028static const FunctionTypeProto *getFunctionProto(Decl *d) {
29 QualType Ty;
Chris Lattner6953a072008-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 Lattnerd5a56aa2008-07-26 22:17:49 +000049 const PointerType *PT = T->getAsPointerType();
50 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +000051 return false;
52
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000053 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-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
Chris Lattner2024f0a2008-06-29 00:16:31 +000064//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +000065// Attribute Implementations
66//===----------------------------------------------------------------------===//
67
Daniel Dunbar8716a012008-07-31 22:40:48 +000068// FIXME: All this manual attribute parsing code is gross. At the
69// least add some helper functions to check most argument patterns (#
70// and types of args).
71
Chris Lattner703c52d2008-06-29 00:43:07 +000072static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
73 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +000074 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
75 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +000076 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +000077 return;
Chris Lattner6953a072008-06-26 18:38:35 +000078 }
79
Chris Lattner6953a072008-06-26 18:38:35 +000080 QualType curType = tDecl->getUnderlyingType();
81 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +000082 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +000083 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
84 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +000085 return;
86 }
Chris Lattner1c151132008-06-28 23:36:30 +000087 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +000088 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +000089 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
90 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
91 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +000092 return;
93 }
94 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
95 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000096 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner703c52d2008-06-29 00:43:07 +000097 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000098 curType.getAsString());
Chris Lattner6953a072008-06-26 18:38:35 +000099 return;
100 }
101 // unlike gcc's vector_size attribute, the size is specified as the
102 // number of elements, not the number of bytes.
103 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
104
105 if (vectorSize == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000106 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
107 sizeExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000108 return;
109 }
110 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000111 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000112 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000113 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000114}
115
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000116
117/// HandleVectorSizeAttribute - this attribute is only applicable to
118/// integral and float scalars, although arrays, pointers, and function
119/// return values are allowed in conjunction with this construct. Aggregates
120/// with this attribute are invalid, even if they are of the same size as a
121/// corresponding scalar.
122/// The raw attribute should contain precisely 1 argument, the vector size
123/// for the variable, measured in bytes. If curType and rawAttr are well
124/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000125static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000126 QualType CurType;
127 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
128 CurType = VD->getType();
129 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
130 CurType = TD->getUnderlyingType();
131 else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000132 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
133 std::string("vector_size"),
134 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000135 return;
136 }
137
138 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000139 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000140 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
141 std::string("1"));
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000142 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000143 }
Chris Lattner1c151132008-06-28 23:36:30 +0000144 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000145 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000146 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
147 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
148 "vector_size", sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000149 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000150 }
151 // navigate to the base type - we need to provide for vector pointers,
152 // vector arrays, and functions returning vectors.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000153 if (CurType->isPointerType() || CurType->isArrayType() ||
154 CurType->isFunctionType()) {
Chris Lattner6953a072008-06-26 18:38:35 +0000155 assert(0 && "HandleVector(): Complex type construction unimplemented");
156 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
157 do {
158 if (PointerType *PT = dyn_cast<PointerType>(canonType))
159 canonType = PT->getPointeeType().getTypePtr();
160 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
161 canonType = AT->getElementType().getTypePtr();
162 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
163 canonType = FT->getResultType().getTypePtr();
164 } while (canonType->isPointerType() || canonType->isArrayType() ||
165 canonType->isFunctionType());
166 */
167 }
168 // the base type must be integer or float.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000169 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000171 CurType.getAsString());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000172 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000173 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000174 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000175 // vecSize is specified in bytes - convert to bits.
176 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
177
178 // the vector size needs to be an integral multiple of the type size.
179 if (vectorSize % typeSize) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
181 sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000182 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000183 }
184 if (vectorSize == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000185 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
186 sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000187 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000188 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000189
190 // Success! Instantiate the vector type, the number of elements is > 0, and
191 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000192 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000193
194 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
195 VD->setType(CurType);
196 else
197 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000198}
199
Chris Lattner703c52d2008-06-29 00:43:07 +0000200static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000201 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000202 if (Attr.getNumArgs() > 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
204 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000205 return;
206 }
207
208 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000209 TD->addAttr(new PackedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000210 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
211 // If the alignment is less than or equal to 8 bits, the packed attribute
212 // has no effect.
213 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000214 S.Context.getTypeAlign(FD->getType()) <= 8)
215 S.Diag(Attr.getLoc(),
216 diag::warn_attribute_ignored_for_field_of_type,
217 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6953a072008-06-26 18:38:35 +0000218 else
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000219 FD->addAttr(new PackedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000220 } else
Chris Lattner703c52d2008-06-29 00:43:07 +0000221 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
222 Attr.getName()->getName());
Chris Lattner6953a072008-06-26 18:38:35 +0000223}
224
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000225static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
226 // check the attribute arguments.
227 if (Attr.getNumArgs() > 0) {
228 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
229 std::string("0"));
230 return;
231 }
232
233 // The IBOutlet attribute only applies to instance variables of Objective-C
234 // classes.
235 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
236 ID->addAttr(new IBOutletAttr());
237 else
238 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
239}
240
Ted Kremenekc0af2542008-07-21 21:53:04 +0000241static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
242
243 // GCC ignores the nonnull attribute on K&R style function
244 // prototypes, so we ignore it as well
245 const FunctionTypeProto *proto = getFunctionProto(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000246 if (!proto) {
247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
248 "nonnull", "function");
249 return;
250 }
251
252 unsigned NumArgs = proto->getNumArgs();
253
254 // The nonnull attribute only applies to pointers.
255 llvm::SmallVector<unsigned, 10> NonNullArgs;
256
257 for (AttributeList::arg_iterator I=Attr.arg_begin(),
258 E=Attr.arg_end(); I!=E; ++I) {
259
260
261 // The argument must be an integer constant expression.
262 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
263 llvm::APSInt ArgNum(32);
264 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
266 "nonnull", Ex->getSourceRange());
267 return;
268 }
269
270 unsigned x = (unsigned) ArgNum.getZExtValue();
271
272 if (x < 1 || x > NumArgs) {
273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Ted Kremenek26151d12008-07-22 16:56:21 +0000274 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekc0af2542008-07-21 21:53:04 +0000275 return;
276 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000277
278 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000279
280 // Is the function argument a pointer type?
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000281 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000282 // FIXME: Should also highlight argument in decl.
283 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
284 "nonnull", Ex->getSourceRange());
Ted Kremenek7af441b2008-09-01 19:57:52 +0000285 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000286 }
287
288 NonNullArgs.push_back(x);
289 }
290
Ted Kremenek7af441b2008-09-01 19:57:52 +0000291 // If no arguments were specified to __attribute__((nonnull)) then all
292 // pointer arguments have a nonnull attribute.
293 if (NonNullArgs.empty()) {
294 unsigned idx = 0;
295
296 for (FunctionTypeProto::arg_type_iterator
297 I=proto->arg_type_begin(), E=proto->arg_type_end(); I!=E; ++I, ++idx)
298 if ((*I)->isPointerType())
299 NonNullArgs.push_back(idx);
300
301 if (NonNullArgs.empty()) {
302 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
303 return;
304 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000305 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000306
307 unsigned* start = &NonNullArgs[0];
308 unsigned size = NonNullArgs.size();
309 std::sort(start, start + size);
310 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000311}
312
Chris Lattner703c52d2008-06-29 00:43:07 +0000313static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000314 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000315 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
317 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000318 return;
319 }
320
Chris Lattner1c151132008-06-28 23:36:30 +0000321 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000322 Arg = Arg->IgnoreParenCasts();
323 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
324
325 if (Str == 0 || Str->isWide()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
327 "alias", std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000328 return;
329 }
330
331 const char *Alias = Str->getStrData();
332 unsigned AliasLen = Str->getByteLength();
333
334 // FIXME: check if target symbol exists in current file
335
336 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
337}
338
Chris Lattner703c52d2008-06-29 00:43:07 +0000339static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000340 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000341 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000342 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
343 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000344 return;
345 }
346
347 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6953a072008-06-26 18:38:35 +0000348 if (!Fn) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000349 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
350 "noreturn", "function");
Chris Lattner6953a072008-06-26 18:38:35 +0000351 return;
352 }
353
354 d->addAttr(new NoReturnAttr());
355}
356
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000357static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
358 // check the attribute arguments.
359 if (Attr.getNumArgs() != 0) {
360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
361 std::string("0"));
362 return;
363 }
364
Ted Kremenek4159e8a2008-08-07 01:02:05 +0000365 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000366 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek4159e8a2008-08-07 01:02:05 +0000367 "unused", "variable and function");
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000368 return;
369 }
370
371 d->addAttr(new UnusedAttr());
372}
373
Daniel Dunbar8716a012008-07-31 22:40:48 +0000374static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
375 // check the attribute arguments.
376 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
378 return;
379 }
380
381 int priority = 65535; // FIXME: Do not hardcode such constants.
382 if (Attr.getNumArgs() > 0) {
383 Expr *E = static_cast<Expr *>(Attr.getArg(0));
384 llvm::APSInt Idx(32);
385 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
386 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
387 "constructor", "1", E->getSourceRange());
388 return;
389 }
390 priority = Idx.getZExtValue();
391 }
392
393 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
394 if (!Fn) {
395 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
396 "constructor", "function");
397 return;
398 }
399
400 d->addAttr(new ConstructorAttr(priority));
401}
402
403static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
404 // check the attribute arguments.
405 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
407 return;
408 }
409
410 int priority = 65535; // FIXME: Do not hardcode such constants.
411 if (Attr.getNumArgs() > 0) {
412 Expr *E = static_cast<Expr *>(Attr.getArg(0));
413 llvm::APSInt Idx(32);
414 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
415 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
416 "destructor", "1", E->getSourceRange());
417 return;
418 }
419 priority = Idx.getZExtValue();
420 }
421
Anders Carlsson03e49652008-08-22 22:10:48 +0000422 if (!isa<FunctionDecl>(d)) {
Daniel Dunbar8716a012008-07-31 22:40:48 +0000423 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
424 "destructor", "function");
425 return;
426 }
427
428 d->addAttr(new DestructorAttr(priority));
429}
430
Chris Lattner703c52d2008-06-29 00:43:07 +0000431static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000432 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000433 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
435 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000436 return;
437 }
438
439 d->addAttr(new DeprecatedAttr());
440}
441
Chris Lattner703c52d2008-06-29 00:43:07 +0000442static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000443 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000444 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000445 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
446 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000447 return;
448 }
449
Chris Lattner1c151132008-06-28 23:36:30 +0000450 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000451 Arg = Arg->IgnoreParenCasts();
452 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
453
454 if (Str == 0 || Str->isWide()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000455 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
456 "visibility", std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000457 return;
458 }
459
460 const char *TypeStr = Str->getStrData();
461 unsigned TypeLen = Str->getByteLength();
462 VisibilityAttr::VisibilityTypes type;
463
464 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
465 type = VisibilityAttr::DefaultVisibility;
466 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
467 type = VisibilityAttr::HiddenVisibility;
468 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
469 type = VisibilityAttr::HiddenVisibility; // FIXME
470 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
471 type = VisibilityAttr::ProtectedVisibility;
472 else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000473 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
474 "visibility", TypeStr);
Chris Lattner6953a072008-06-26 18:38:35 +0000475 return;
476 }
477
478 d->addAttr(new VisibilityAttr(type));
479}
480
Anders Carlssond83141a52008-08-23 23:22:21 +0000481static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssone0f00b32008-08-24 16:33:25 +0000482 if (!Attr.getParameterName()) {
Anders Carlssond83141a52008-08-23 23:22:21 +0000483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
484 "objc_gc", std::string("1"));
485 return;
486 }
487
488 if (Attr.getNumArgs() != 0) {
489 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
490 std::string("1"));
491 return;
492 }
493
494 const char *TypeStr = Attr.getParameterName()->getName();
495 unsigned TypeLen = Attr.getParameterName()->getLength();
496
497 ObjCGCAttr::GCAttrTypes type;
498
499 if (TypeLen == 4 && !memcmp(TypeStr, "weak", 4))
500 type = ObjCGCAttr::Weak;
Anders Carlssone0f00b32008-08-24 16:33:25 +0000501 else if (TypeLen == 6 && !memcmp(TypeStr, "strong", 6))
Anders Carlssond83141a52008-08-23 23:22:21 +0000502 type = ObjCGCAttr::Strong;
503 else {
504 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
505 "objc_gc", TypeStr);
506 return;
507 }
508
509 d->addAttr(new ObjCGCAttr(type));
510}
511
Chris Lattner703c52d2008-06-29 00:43:07 +0000512static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000513 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000514 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
516 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000517 return;
518 }
519
520 d->addAttr(new WeakAttr());
521}
522
Chris Lattner703c52d2008-06-29 00:43:07 +0000523static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000524 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000525 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
527 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000528 return;
529 }
530
531 d->addAttr(new DLLImportAttr());
532}
533
Chris Lattner703c52d2008-06-29 00:43:07 +0000534static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000535 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000536 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
538 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000539 return;
540 }
541
542 d->addAttr(new DLLExportAttr());
543}
544
Chris Lattner703c52d2008-06-29 00:43:07 +0000545static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000546 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000547 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000548 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
549 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000550 return;
551 }
552
553 d->addAttr(new StdCallAttr());
554}
555
Chris Lattner703c52d2008-06-29 00:43:07 +0000556static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000557 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000558 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
560 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000561 return;
562 }
563
564 d->addAttr(new FastCallAttr());
565}
566
Chris Lattner703c52d2008-06-29 00:43:07 +0000567static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000568 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000569 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000570 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
571 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000572 return;
573 }
574
575 d->addAttr(new NoThrowAttr());
576}
577
578/// Handle __attribute__((format(type,idx,firstarg))) attributes
579/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +0000580static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000581
Chris Lattner1c151132008-06-28 23:36:30 +0000582 if (!Attr.getParameterName()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6953a072008-06-26 18:38:35 +0000584 "format", std::string("1"));
585 return;
586 }
587
Chris Lattner1c151132008-06-28 23:36:30 +0000588 if (Attr.getNumArgs() != 2) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
590 std::string("3"));
Chris Lattner6953a072008-06-26 18:38:35 +0000591 return;
592 }
593
594 // GCC ignores the format attribute on K&R style function
595 // prototypes, so we ignore it as well
596 const FunctionTypeProto *proto = getFunctionProto(d);
597
598 if (!proto) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
600 "format", "function");
Chris Lattner6953a072008-06-26 18:38:35 +0000601 return;
602 }
603
604 // FIXME: in C++ the implicit 'this' function parameter also counts.
605 // this is needed in order to be compatible with GCC
606 // the index must start in 1 and the limit is numargs+1
607 unsigned NumArgs = proto->getNumArgs();
608 unsigned FirstIdx = 1;
609
Chris Lattner1c151132008-06-28 23:36:30 +0000610 const char *Format = Attr.getParameterName()->getName();
611 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +0000612
613 // Normalize the argument, __foo__ becomes foo.
614 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
615 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
616 Format += 2;
617 FormatLen -= 4;
618 }
619
620 bool Supported = false;
621 bool is_NSString = false;
622 bool is_strftime = false;
623
624 switch (FormatLen) {
625 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000626 case 5: Supported = !memcmp(Format, "scanf", 5); break;
627 case 6: Supported = !memcmp(Format, "printf", 6); break;
628 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +0000629 case 8:
630 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
631 (is_NSString = !memcmp(Format, "NSString", 8));
632 break;
633 }
634
635 if (!Supported) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000636 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner1c151132008-06-28 23:36:30 +0000637 "format", Attr.getParameterName()->getName());
Chris Lattner6953a072008-06-26 18:38:35 +0000638 return;
639 }
640
641 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000642 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +0000643 llvm::APSInt Idx(32);
644 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6953a072008-06-26 18:38:35 +0000646 "format", std::string("2"), IdxExpr->getSourceRange());
647 return;
648 }
649
650 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000651 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6953a072008-06-26 18:38:35 +0000652 "format", std::string("2"), IdxExpr->getSourceRange());
653 return;
654 }
655
656 // FIXME: Do we need to bounds check?
657 unsigned ArgIdx = Idx.getZExtValue() - 1;
658
659 // make sure the format string is really a string
660 QualType Ty = proto->getArgType(ArgIdx);
661
662 if (is_NSString) {
663 // FIXME: do we need to check if the type is NSString*? What are
664 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +0000665 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +0000666 // FIXME: Should highlight the actual expression that has the
667 // wrong type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000668 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
669 IdxExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000670 return;
671 }
672 } else if (!Ty->isPointerType() ||
673 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
674 // FIXME: Should highlight the actual expression that has the
675 // wrong type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000676 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
677 IdxExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000678 return;
679 }
680
681 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000682 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +0000683 llvm::APSInt FirstArg(32);
684 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
685 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6953a072008-06-26 18:38:35 +0000686 "format", std::string("3"), FirstArgExpr->getSourceRange());
687 return;
688 }
689
690 // check if the function is variadic if the 3rd argument non-zero
691 if (FirstArg != 0) {
692 if (proto->isVariadic()) {
693 ++NumArgs; // +1 for ...
694 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000695 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +0000696 return;
697 }
698 }
699
700 // strftime requires FirstArg to be 0 because it doesn't read from any variable
701 // the input is just the current time + the format string
702 if (is_strftime) {
703 if (FirstArg != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000704 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6953a072008-06-26 18:38:35 +0000705 FirstArgExpr->getSourceRange());
706 return;
707 }
708 // if 0 it disables parameter checking (to use with e.g. va_list)
709 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000710 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6953a072008-06-26 18:38:35 +0000711 "format", std::string("3"), FirstArgExpr->getSourceRange());
712 return;
713 }
714
715 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
716 Idx.getZExtValue(), FirstArg.getZExtValue()));
717}
718
Chris Lattnerf6690152008-06-29 00:28:59 +0000719static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
720 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000721 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000722 if (Attr.getNumArgs() != 0) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6953a072008-06-26 18:38:35 +0000724 std::string("0"));
725 return;
726 }
727
728 TypeDecl *decl = dyn_cast<TypeDecl>(d);
729
Chris Lattnerf6690152008-06-29 00:28:59 +0000730 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6953a072008-06-26 18:38:35 +0000732 "transparent_union", "union");
733 return;
734 }
735
736 //QualType QTy = Context.getTypeDeclType(decl);
737 //const RecordType *Ty = QTy->getAsUnionType();
738
739// FIXME
740// Ty->addAttr(new TransparentUnionAttr());
741}
742
Chris Lattnerf6690152008-06-29 00:28:59 +0000743static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000744 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000745 if (Attr.getNumArgs() != 1) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
747 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000748 return;
749 }
Chris Lattner1c151132008-06-28 23:36:30 +0000750 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000751 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
752
753 // Make sure that there is a string literal as the annotation's single
754 // argument.
755 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000756 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +0000757 return;
758 }
759 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
760 SE->getByteLength())));
761}
762
Chris Lattner703c52d2008-06-29 00:43:07 +0000763static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000764 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000765 if (Attr.getNumArgs() > 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
767 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000768 return;
769 }
770
771 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +0000772 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +0000773 // FIXME: This should be the target specific maximum alignment.
774 // (For now we just use 128 bits which is the maximum on X86.
775 Align = 128;
776 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000777 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000778
779 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
780 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000781 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
783 "aligned", alignmentExpr->getSourceRange());
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000784 return;
785 }
786 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +0000787}
Chris Lattnerdc789562008-06-27 22:18:37 +0000788
Chris Lattnerf6690152008-06-29 00:28:59 +0000789/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000790/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +0000791///
792/// Despite what would be logical, the mode attribute is a decl attribute,
793/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
794/// 'G' be HImode, not an intermediate pointer.
795///
Chris Lattnerf6690152008-06-29 00:28:59 +0000796static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +0000797 // This attribute isn't documented, but glibc uses it. It changes
798 // the width of an int or unsigned int to the specified size.
799
800 // Check that there aren't any arguments
801 if (Attr.getNumArgs() != 0) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
803 std::string("0"));
Chris Lattnerdc789562008-06-27 22:18:37 +0000804 return;
805 }
806
807 IdentifierInfo *Name = Attr.getParameterName();
808 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +0000810 return;
811 }
812 const char *Str = Name->getName();
813 unsigned Len = Name->getLength();
814
815 // Normalize the attribute name, __foo__ becomes foo.
816 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
817 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
818 Str += 2;
819 Len -= 4;
820 }
821
822 unsigned DestWidth = 0;
823 bool IntegerMode = true;
824 switch (Len) {
825 case 2:
826 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
827 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
828 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
829 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
830 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
831 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
832 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
833 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
834 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
835 break;
836 case 4:
837 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
838 // pointer on PIC16 and other embedded platforms.
839 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +0000840 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +0000841 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +0000842 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +0000843 break;
844 case 7:
845 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +0000846 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +0000847 break;
848 }
849
850 QualType OldTy;
851 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
852 OldTy = TD->getUnderlyingType();
853 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
854 OldTy = VD->getType();
855 else {
Chris Lattnerf6690152008-06-29 00:28:59 +0000856 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
857 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerdc789562008-06-27 22:18:37 +0000858 return;
859 }
860
861 // FIXME: Need proper fixed-width types
862 QualType NewTy;
863 switch (DestWidth) {
864 case 0:
Chris Lattnerf6690152008-06-29 00:28:59 +0000865 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerdc789562008-06-27 22:18:37 +0000866 return;
867 default:
Chris Lattnerf6690152008-06-29 00:28:59 +0000868 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerdc789562008-06-27 22:18:37 +0000869 return;
870 case 8:
871 assert(IntegerMode);
872 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000873 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000874 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000875 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000876 break;
877 case 16:
878 assert(IntegerMode);
879 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000880 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000881 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000882 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000883 break;
884 case 32:
885 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +0000886 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000887 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000888 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000889 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000890 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000891 break;
892 case 64:
893 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +0000894 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000895 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000896 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000897 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000898 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000899 break;
900 }
901
902 if (!OldTy->getAsBuiltinType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000903 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerdc789562008-06-27 22:18:37 +0000904 else if (!(IntegerMode && OldTy->isIntegerType()) &&
905 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000906 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerdc789562008-06-27 22:18:37 +0000907 }
908
909 // Install the new type.
910 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
911 TD->setUnderlyingType(NewTy);
912 else
913 cast<ValueDecl>(D)->setType(NewTy);
914}
Chris Lattnera72440d2008-06-29 00:23:49 +0000915
916//===----------------------------------------------------------------------===//
917// Top Level Sema Entry Points
918//===----------------------------------------------------------------------===//
919
Chris Lattner703c52d2008-06-29 00:43:07 +0000920/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
921/// the attribute applies to decls. If the attribute is a type attribute, just
922/// silently ignore it.
923static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
924 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +0000925 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000926 case AttributeList::AT_address_space:
927 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
928 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000929 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
930 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
931 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
932 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
933 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
934 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
935 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
936 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000937 case AttributeList::AT_ext_vector_type:
938 HandleExtVectorTypeAttr(D, Attr, S);
939 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000940 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
941 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000942 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +0000943 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
944 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
945 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
946 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
947 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
948 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
949 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000950 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
951 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000952 case AttributeList::AT_transparent_union:
953 HandleTransparentUnionAttr(D, Attr, S);
954 break;
Anders Carlssond83141a52008-08-23 23:22:21 +0000955 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000956 default:
957#if 0
958 // TODO: when we have the full set of attributes, warn about unknown ones.
959 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
960 Attr->getName()->getName());
961#endif
962 break;
963 }
964}
965
966/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
967/// attribute list to the specified decl, ignoring any type attributes.
968void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
969 while (AttrList) {
970 ProcessDeclAttribute(D, *AttrList, *this);
971 AttrList = AttrList->getNext();
972 }
973}
974
975
Chris Lattnera72440d2008-06-29 00:23:49 +0000976/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
977/// it, apply them to D. This is a bit tricky because PD can have attributes
978/// specified in many different places, and we need to find and apply them all.
979void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
980 // Apply decl attributes from the DeclSpec if present.
981 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
982 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +0000983
Chris Lattnera72440d2008-06-29 00:23:49 +0000984 // Walk the declarator structure, applying decl attributes that were in a type
985 // position to the decl itself. This handles cases like:
986 // int *__attr__(x)** D;
987 // when X is a decl attribute.
988 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
989 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
990 ProcessDeclAttributeList(D, Attrs);
991
992 // Finally, apply any attributes on the decl itself.
993 if (const AttributeList *Attrs = PD.getAttributes())
994 ProcessDeclAttributeList(D, Attrs);
995}
996