blob: 54f6579e36c0c47a58ac77d778f19edb8600c271 [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"
Chris Lattnerfbf13472008-06-27 22:18:37 +000016#include "clang/Basic/TargetInfo.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000017#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000018using namespace clang;
19
Chris Lattnere5c5ee12008-06-29 00:16:31 +000020//===----------------------------------------------------------------------===//
21// Helper functions
22//===----------------------------------------------------------------------===//
23
Chris Lattner6b6b5372008-06-26 18:38:35 +000024static const FunctionTypeProto *getFunctionProto(Decl *d) {
25 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000026 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
27 Ty = decl->getType();
28 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
29 Ty = decl->getType();
30 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
31 Ty = decl->getUnderlyingType();
32 else
33 return 0;
34
35 if (Ty->isFunctionPointerType())
36 Ty = Ty->getAsPointerType()->getPointeeType();
37
38 if (const FunctionType *FnTy = Ty->getAsFunctionType())
39 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
40
41 return 0;
42}
43
44static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000045 const PointerType *PT = T->getAsPointerType();
46 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000047 return false;
48
Chris Lattnerb77792e2008-07-26 22:17:49 +000049 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000050 if (!ClsT)
51 return false;
52
53 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
54
55 // FIXME: Should we walk the chain of classes?
56 return ClsName == &Ctx.Idents.get("NSString") ||
57 ClsName == &Ctx.Idents.get("NSMutableString");
58}
59
Chris Lattnere5c5ee12008-06-29 00:16:31 +000060//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +000061// Attribute Implementations
62//===----------------------------------------------------------------------===//
63
Daniel Dunbar3068ae02008-07-31 22:40:48 +000064// FIXME: All this manual attribute parsing code is gross. At the
65// least add some helper functions to check most argument patterns (#
66// and types of args).
67
Chris Lattner803d0802008-06-29 00:43:07 +000068static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
69 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +000070 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
71 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000072 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +000073 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +000074 }
75
Chris Lattner6b6b5372008-06-26 18:38:35 +000076 QualType curType = tDecl->getUnderlyingType();
77 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +000078 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +000079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
80 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +000081 return;
82 }
Chris Lattner545dd342008-06-28 23:36:30 +000083 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +000084 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +000085 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
86 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
87 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +000088 return;
89 }
90 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
91 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +000092 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +000093 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +000094 curType.getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +000095 return;
96 }
97 // unlike gcc's vector_size attribute, the size is specified as the
98 // number of elements, not the number of bytes.
99 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
100
101 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000102 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
103 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000104 return;
105 }
106 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000107 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000108 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000109 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000110}
111
Chris Lattner065c5a82008-06-28 23:48:25 +0000112
113/// HandleVectorSizeAttribute - this attribute is only applicable to
114/// integral and float scalars, although arrays, pointers, and function
115/// return values are allowed in conjunction with this construct. Aggregates
116/// with this attribute are invalid, even if they are of the same size as a
117/// corresponding scalar.
118/// The raw attribute should contain precisely 1 argument, the vector size
119/// for the variable, measured in bytes. If curType and rawAttr are well
120/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000121static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000122 QualType CurType;
123 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
124 CurType = VD->getType();
125 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
126 CurType = TD->getUnderlyingType();
127 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000128 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
129 std::string("vector_size"),
130 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000131 return;
132 }
133
134 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000135 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000136 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
137 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000138 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000139 }
Chris Lattner545dd342008-06-28 23:36:30 +0000140 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000141 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000142 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
144 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000145 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000146 }
147 // navigate to the base type - we need to provide for vector pointers,
148 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000149 if (CurType->isPointerType() || CurType->isArrayType() ||
150 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000151 assert(0 && "HandleVector(): Complex type construction unimplemented");
152 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
153 do {
154 if (PointerType *PT = dyn_cast<PointerType>(canonType))
155 canonType = PT->getPointeeType().getTypePtr();
156 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
157 canonType = AT->getElementType().getTypePtr();
158 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
159 canonType = FT->getResultType().getTypePtr();
160 } while (canonType->isPointerType() || canonType->isArrayType() ||
161 canonType->isFunctionType());
162 */
163 }
164 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000165 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000166 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000167 CurType.getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000168 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000169 }
Chris Lattner803d0802008-06-29 00:43:07 +0000170 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000171 // vecSize is specified in bytes - convert to bits.
172 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
173
174 // the vector size needs to be an integral multiple of the type size.
175 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000176 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
177 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000178 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 }
180 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000181 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
182 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000183 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000185
186 // Success! Instantiate the vector type, the number of elements is > 0, and
187 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000188 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000189
190 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
191 VD->setType(CurType);
192 else
193 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000194}
195
Chris Lattner803d0802008-06-29 00:43:07 +0000196static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000197 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000198 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
200 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000201 return;
202 }
203
204 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000205 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000206 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
207 // If the alignment is less than or equal to 8 bits, the packed attribute
208 // has no effect.
209 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000210 S.Context.getTypeAlign(FD->getType()) <= 8)
211 S.Diag(Attr.getLoc(),
212 diag::warn_attribute_ignored_for_field_of_type,
213 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000214 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000215 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000216 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000217 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
218 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000219}
220
Ted Kremenek96329d42008-07-15 22:26:48 +0000221static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
222 // check the attribute arguments.
223 if (Attr.getNumArgs() > 0) {
224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
225 std::string("0"));
226 return;
227 }
228
229 // The IBOutlet attribute only applies to instance variables of Objective-C
230 // classes.
231 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
232 ID->addAttr(new IBOutletAttr());
233 else
234 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
235}
236
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000237static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
238
239 // GCC ignores the nonnull attribute on K&R style function
240 // prototypes, so we ignore it as well
241 const FunctionTypeProto *proto = getFunctionProto(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000242 if (!proto) {
243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
244 "nonnull", "function");
245 return;
246 }
247
248 unsigned NumArgs = proto->getNumArgs();
249
250 // The nonnull attribute only applies to pointers.
251 llvm::SmallVector<unsigned, 10> NonNullArgs;
252
253 for (AttributeList::arg_iterator I=Attr.arg_begin(),
254 E=Attr.arg_end(); I!=E; ++I) {
255
256
257 // The argument must be an integer constant expression.
258 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
259 llvm::APSInt ArgNum(32);
260 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
262 "nonnull", Ex->getSourceRange());
263 return;
264 }
265
266 unsigned x = (unsigned) ArgNum.getZExtValue();
267
268 if (x < 1 || x > NumArgs) {
269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Ted Kremenek6e1eb872008-07-22 16:56:21 +0000270 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000271 return;
272 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000273
274 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000275
276 // Is the function argument a pointer type?
Chris Lattnerb77792e2008-07-26 22:17:49 +0000277 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000278 // FIXME: Should also highlight argument in decl.
279 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
280 "nonnull", Ex->getSourceRange());
281 return;
282 }
283
284 NonNullArgs.push_back(x);
285 }
286
287 if (!NonNullArgs.empty()) {
288 unsigned* start = &NonNullArgs[0];
289 unsigned size = NonNullArgs.size();
290 std::sort(start, start + size);
291 d->addAttr(new NonNullAttr(start, size));
292 }
293 else
294 d->addAttr(new NonNullAttr());
295}
296
Chris Lattner803d0802008-06-29 00:43:07 +0000297static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000298 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000299 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
301 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000302 return;
303 }
304
Chris Lattner545dd342008-06-28 23:36:30 +0000305 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000306 Arg = Arg->IgnoreParenCasts();
307 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
308
309 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000310 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
311 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000312 return;
313 }
314
315 const char *Alias = Str->getStrData();
316 unsigned AliasLen = Str->getByteLength();
317
318 // FIXME: check if target symbol exists in current file
319
320 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
321}
322
Chris Lattner803d0802008-06-29 00:43:07 +0000323static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000324 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000325 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
327 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000328 return;
329 }
330
331 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000332 if (!Fn) {
Chris Lattner803d0802008-06-29 00:43:07 +0000333 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
334 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000335 return;
336 }
337
338 d->addAttr(new NoReturnAttr());
339}
340
Ted Kremenek73798892008-07-25 04:39:19 +0000341static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
342 // check the attribute arguments.
343 if (Attr.getNumArgs() != 0) {
344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
345 std::string("0"));
346 return;
347 }
348
Ted Kremenek356b63a2008-08-07 01:02:05 +0000349 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenek73798892008-07-25 04:39:19 +0000350 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek356b63a2008-08-07 01:02:05 +0000351 "unused", "variable and function");
Ted Kremenek73798892008-07-25 04:39:19 +0000352 return;
353 }
354
355 d->addAttr(new UnusedAttr());
356}
357
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000358static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
359 // check the attribute arguments.
360 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
361 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
362 return;
363 }
364
365 int priority = 65535; // FIXME: Do not hardcode such constants.
366 if (Attr.getNumArgs() > 0) {
367 Expr *E = static_cast<Expr *>(Attr.getArg(0));
368 llvm::APSInt Idx(32);
369 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
371 "constructor", "1", E->getSourceRange());
372 return;
373 }
374 priority = Idx.getZExtValue();
375 }
376
377 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
378 if (!Fn) {
379 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
380 "constructor", "function");
381 return;
382 }
383
384 d->addAttr(new ConstructorAttr(priority));
385}
386
387static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
388 // check the attribute arguments.
389 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
391 return;
392 }
393
394 int priority = 65535; // FIXME: Do not hardcode such constants.
395 if (Attr.getNumArgs() > 0) {
396 Expr *E = static_cast<Expr *>(Attr.getArg(0));
397 llvm::APSInt Idx(32);
398 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
399 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
400 "destructor", "1", E->getSourceRange());
401 return;
402 }
403 priority = Idx.getZExtValue();
404 }
405
406 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
407 if (!Fn) {
408 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
409 "destructor", "function");
410 return;
411 }
412
413 d->addAttr(new DestructorAttr(priority));
414}
415
Chris Lattner803d0802008-06-29 00:43:07 +0000416static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000417 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000418 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000419 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
420 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000421 return;
422 }
423
424 d->addAttr(new DeprecatedAttr());
425}
426
Chris Lattner803d0802008-06-29 00:43:07 +0000427static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000428 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000429 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
431 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000432 return;
433 }
434
Chris Lattner545dd342008-06-28 23:36:30 +0000435 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000436 Arg = Arg->IgnoreParenCasts();
437 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
438
439 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
441 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000442 return;
443 }
444
445 const char *TypeStr = Str->getStrData();
446 unsigned TypeLen = Str->getByteLength();
447 VisibilityAttr::VisibilityTypes type;
448
449 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
450 type = VisibilityAttr::DefaultVisibility;
451 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
452 type = VisibilityAttr::HiddenVisibility;
453 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
454 type = VisibilityAttr::HiddenVisibility; // FIXME
455 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
456 type = VisibilityAttr::ProtectedVisibility;
457 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000458 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
459 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000460 return;
461 }
462
463 d->addAttr(new VisibilityAttr(type));
464}
465
Chris Lattner803d0802008-06-29 00:43:07 +0000466static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000467 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000468 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000469 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
470 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000471 return;
472 }
473
474 d->addAttr(new WeakAttr());
475}
476
Chris Lattner803d0802008-06-29 00:43:07 +0000477static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000478 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000479 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
481 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000482 return;
483 }
484
485 d->addAttr(new DLLImportAttr());
486}
487
Chris Lattner803d0802008-06-29 00:43:07 +0000488static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000489 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000490 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
492 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000493 return;
494 }
495
496 d->addAttr(new DLLExportAttr());
497}
498
Chris Lattner803d0802008-06-29 00:43:07 +0000499static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000500 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000501 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
503 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000504 return;
505 }
506
507 d->addAttr(new StdCallAttr());
508}
509
Chris Lattner803d0802008-06-29 00:43:07 +0000510static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000511 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000512 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
514 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000515 return;
516 }
517
518 d->addAttr(new FastCallAttr());
519}
520
Chris Lattner803d0802008-06-29 00:43:07 +0000521static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000522 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000523 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000524 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
525 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000526 return;
527 }
528
529 d->addAttr(new NoThrowAttr());
530}
531
532/// Handle __attribute__((format(type,idx,firstarg))) attributes
533/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000534static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000535
Chris Lattner545dd342008-06-28 23:36:30 +0000536 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000538 "format", std::string("1"));
539 return;
540 }
541
Chris Lattner545dd342008-06-28 23:36:30 +0000542 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
544 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000545 return;
546 }
547
548 // GCC ignores the format attribute on K&R style function
549 // prototypes, so we ignore it as well
550 const FunctionTypeProto *proto = getFunctionProto(d);
551
552 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000553 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
554 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000555 return;
556 }
557
558 // FIXME: in C++ the implicit 'this' function parameter also counts.
559 // this is needed in order to be compatible with GCC
560 // the index must start in 1 and the limit is numargs+1
561 unsigned NumArgs = proto->getNumArgs();
562 unsigned FirstIdx = 1;
563
Chris Lattner545dd342008-06-28 23:36:30 +0000564 const char *Format = Attr.getParameterName()->getName();
565 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566
567 // Normalize the argument, __foo__ becomes foo.
568 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
569 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
570 Format += 2;
571 FormatLen -= 4;
572 }
573
574 bool Supported = false;
575 bool is_NSString = false;
576 bool is_strftime = false;
577
578 switch (FormatLen) {
579 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000580 case 5: Supported = !memcmp(Format, "scanf", 5); break;
581 case 6: Supported = !memcmp(Format, "printf", 6); break;
582 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000583 case 8:
584 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
585 (is_NSString = !memcmp(Format, "NSString", 8));
586 break;
587 }
588
589 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000590 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000591 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000592 return;
593 }
594
595 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000596 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000597 llvm::APSInt Idx(32);
598 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
599 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000600 "format", std::string("2"), IdxExpr->getSourceRange());
601 return;
602 }
603
604 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 "format", std::string("2"), IdxExpr->getSourceRange());
607 return;
608 }
609
610 // FIXME: Do we need to bounds check?
611 unsigned ArgIdx = Idx.getZExtValue() - 1;
612
613 // make sure the format string is really a string
614 QualType Ty = proto->getArgType(ArgIdx);
615
616 if (is_NSString) {
617 // FIXME: do we need to check if the type is NSString*? What are
618 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000619 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620 // FIXME: Should highlight the actual expression that has the
621 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000622 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
623 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000624 return;
625 }
626 } else if (!Ty->isPointerType() ||
627 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
628 // FIXME: Should highlight the actual expression that has the
629 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000630 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
631 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000632 return;
633 }
634
635 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000636 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000637 llvm::APSInt FirstArg(32);
638 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640 "format", std::string("3"), FirstArgExpr->getSourceRange());
641 return;
642 }
643
644 // check if the function is variadic if the 3rd argument non-zero
645 if (FirstArg != 0) {
646 if (proto->isVariadic()) {
647 ++NumArgs; // +1 for ...
648 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000649 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000650 return;
651 }
652 }
653
654 // strftime requires FirstArg to be 0 because it doesn't read from any variable
655 // the input is just the current time + the format string
656 if (is_strftime) {
657 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000658 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 FirstArgExpr->getSourceRange());
660 return;
661 }
662 // if 0 it disables parameter checking (to use with e.g. va_list)
663 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000665 "format", std::string("3"), FirstArgExpr->getSourceRange());
666 return;
667 }
668
669 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
670 Idx.getZExtValue(), FirstArg.getZExtValue()));
671}
672
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000673static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
674 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000676 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000678 std::string("0"));
679 return;
680 }
681
682 TypeDecl *decl = dyn_cast<TypeDecl>(d);
683
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000684 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
685 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000686 "transparent_union", "union");
687 return;
688 }
689
690 //QualType QTy = Context.getTypeDeclType(decl);
691 //const RecordType *Ty = QTy->getAsUnionType();
692
693// FIXME
694// Ty->addAttr(new TransparentUnionAttr());
695}
696
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000697static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000698 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000699 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
701 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000702 return;
703 }
Chris Lattner545dd342008-06-28 23:36:30 +0000704 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000705 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
706
707 // Make sure that there is a string literal as the annotation's single
708 // argument.
709 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000710 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000711 return;
712 }
713 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
714 SE->getByteLength())));
715}
716
Chris Lattner803d0802008-06-29 00:43:07 +0000717static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000718 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000719 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000720 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
721 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000722 return;
723 }
724
725 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000726 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000727 // FIXME: This should be the target specific maximum alignment.
728 // (For now we just use 128 bits which is the maximum on X86.
729 Align = 128;
730 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000731 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000732
733 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
734 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000735 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
736 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
737 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000738 return;
739 }
740 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000741}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000742
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000743/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000744/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000745///
746/// Despite what would be logical, the mode attribute is a decl attribute,
747/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
748/// 'G' be HImode, not an intermediate pointer.
749///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000750static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000751 // This attribute isn't documented, but glibc uses it. It changes
752 // the width of an int or unsigned int to the specified size.
753
754 // Check that there aren't any arguments
755 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000756 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
757 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000758 return;
759 }
760
761 IdentifierInfo *Name = Attr.getParameterName();
762 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000763 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000764 return;
765 }
766 const char *Str = Name->getName();
767 unsigned Len = Name->getLength();
768
769 // Normalize the attribute name, __foo__ becomes foo.
770 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
771 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
772 Str += 2;
773 Len -= 4;
774 }
775
776 unsigned DestWidth = 0;
777 bool IntegerMode = true;
778 switch (Len) {
779 case 2:
780 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
781 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
782 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
783 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
784 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
785 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
786 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
787 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
788 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
789 break;
790 case 4:
791 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
792 // pointer on PIC16 and other embedded platforms.
793 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000794 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000795 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000796 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000797 break;
798 case 7:
799 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000800 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000801 break;
802 }
803
804 QualType OldTy;
805 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
806 OldTy = TD->getUnderlyingType();
807 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
808 OldTy = VD->getType();
809 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000810 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
811 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000812 return;
813 }
814
815 // FIXME: Need proper fixed-width types
816 QualType NewTy;
817 switch (DestWidth) {
818 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000819 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000820 return;
821 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000822 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000823 return;
824 case 8:
825 assert(IntegerMode);
826 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000827 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000828 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000829 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000830 break;
831 case 16:
832 assert(IntegerMode);
833 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000834 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000835 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000836 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000837 break;
838 case 32:
839 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000840 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000841 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000842 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000843 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000844 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000845 break;
846 case 64:
847 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000848 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000849 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000850 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000851 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000852 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000853 break;
854 }
855
856 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000857 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000858 else if (!(IntegerMode && OldTy->isIntegerType()) &&
859 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000860 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000861 }
862
863 // Install the new type.
864 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
865 TD->setUnderlyingType(NewTy);
866 else
867 cast<ValueDecl>(D)->setType(NewTy);
868}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000869
870//===----------------------------------------------------------------------===//
871// Top Level Sema Entry Points
872//===----------------------------------------------------------------------===//
873
Chris Lattner803d0802008-06-29 00:43:07 +0000874/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
875/// the attribute applies to decls. If the attribute is a type attribute, just
876/// silently ignore it.
877static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
878 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000879 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000880 case AttributeList::AT_address_space:
881 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
882 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000883 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
884 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
885 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
886 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
887 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
888 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
889 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
890 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000891 case AttributeList::AT_ext_vector_type:
892 HandleExtVectorTypeAttr(D, Attr, S);
893 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000894 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
895 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000896 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000897 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
898 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
899 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
900 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
901 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
902 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
903 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000904 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
905 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000906 case AttributeList::AT_transparent_union:
907 HandleTransparentUnionAttr(D, Attr, S);
908 break;
909 default:
910#if 0
911 // TODO: when we have the full set of attributes, warn about unknown ones.
912 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
913 Attr->getName()->getName());
914#endif
915 break;
916 }
917}
918
919/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
920/// attribute list to the specified decl, ignoring any type attributes.
921void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
922 while (AttrList) {
923 ProcessDeclAttribute(D, *AttrList, *this);
924 AttrList = AttrList->getNext();
925 }
926}
927
928
Chris Lattner0744e5f2008-06-29 00:23:49 +0000929/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
930/// it, apply them to D. This is a bit tricky because PD can have attributes
931/// specified in many different places, and we need to find and apply them all.
932void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
933 // Apply decl attributes from the DeclSpec if present.
934 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
935 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +0000936
Chris Lattner0744e5f2008-06-29 00:23:49 +0000937 // Walk the declarator structure, applying decl attributes that were in a type
938 // position to the decl itself. This handles cases like:
939 // int *__attr__(x)** D;
940 // when X is a decl attribute.
941 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
942 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
943 ProcessDeclAttributeList(D, Attrs);
944
945 // Finally, apply any attributes on the decl itself.
946 if (const AttributeList *Attrs = PD.getAttributes())
947 ProcessDeclAttributeList(D, Attrs);
948}
949