blob: 3f9da3bebe0ceb782f2205047497dff1388ffe89 [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
349 VarDecl *VD = dyn_cast<VarDecl>(d);
350
351 if (!VD) {
352 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
353 "unused", "variable");
354 return;
355 }
356
357 d->addAttr(new UnusedAttr());
358}
359
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000360static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
361 // check the attribute arguments.
362 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
363 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
364 return;
365 }
366
367 int priority = 65535; // FIXME: Do not hardcode such constants.
368 if (Attr.getNumArgs() > 0) {
369 Expr *E = static_cast<Expr *>(Attr.getArg(0));
370 llvm::APSInt Idx(32);
371 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
372 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
373 "constructor", "1", E->getSourceRange());
374 return;
375 }
376 priority = Idx.getZExtValue();
377 }
378
379 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
380 if (!Fn) {
381 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
382 "constructor", "function");
383 return;
384 }
385
386 d->addAttr(new ConstructorAttr(priority));
387}
388
389static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
390 // check the attribute arguments.
391 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
393 return;
394 }
395
396 int priority = 65535; // FIXME: Do not hardcode such constants.
397 if (Attr.getNumArgs() > 0) {
398 Expr *E = static_cast<Expr *>(Attr.getArg(0));
399 llvm::APSInt Idx(32);
400 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
401 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
402 "destructor", "1", E->getSourceRange());
403 return;
404 }
405 priority = Idx.getZExtValue();
406 }
407
408 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
409 if (!Fn) {
410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
411 "destructor", "function");
412 return;
413 }
414
415 d->addAttr(new DestructorAttr(priority));
416}
417
Chris Lattner803d0802008-06-29 00:43:07 +0000418static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000419 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000420 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
422 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000423 return;
424 }
425
426 d->addAttr(new DeprecatedAttr());
427}
428
Chris Lattner803d0802008-06-29 00:43:07 +0000429static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000430 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000431 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
433 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000434 return;
435 }
436
Chris Lattner545dd342008-06-28 23:36:30 +0000437 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000438 Arg = Arg->IgnoreParenCasts();
439 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
440
441 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
443 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000444 return;
445 }
446
447 const char *TypeStr = Str->getStrData();
448 unsigned TypeLen = Str->getByteLength();
449 VisibilityAttr::VisibilityTypes type;
450
451 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
452 type = VisibilityAttr::DefaultVisibility;
453 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
454 type = VisibilityAttr::HiddenVisibility;
455 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
456 type = VisibilityAttr::HiddenVisibility; // FIXME
457 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
458 type = VisibilityAttr::ProtectedVisibility;
459 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000460 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
461 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000462 return;
463 }
464
465 d->addAttr(new VisibilityAttr(type));
466}
467
Chris Lattner803d0802008-06-29 00:43:07 +0000468static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000469 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000470 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
472 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000473 return;
474 }
475
476 d->addAttr(new WeakAttr());
477}
478
Chris Lattner803d0802008-06-29 00:43:07 +0000479static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000480 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000481 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
483 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000484 return;
485 }
486
487 d->addAttr(new DLLImportAttr());
488}
489
Chris Lattner803d0802008-06-29 00:43:07 +0000490static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000491 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000492 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
494 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000495 return;
496 }
497
498 d->addAttr(new DLLExportAttr());
499}
500
Chris Lattner803d0802008-06-29 00:43:07 +0000501static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000502 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000503 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
505 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000506 return;
507 }
508
509 d->addAttr(new StdCallAttr());
510}
511
Chris Lattner803d0802008-06-29 00:43:07 +0000512static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000513 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000514 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
516 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000517 return;
518 }
519
520 d->addAttr(new FastCallAttr());
521}
522
Chris Lattner803d0802008-06-29 00:43:07 +0000523static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000524 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000525 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
527 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000528 return;
529 }
530
531 d->addAttr(new NoThrowAttr());
532}
533
534/// Handle __attribute__((format(type,idx,firstarg))) attributes
535/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000536static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000537
Chris Lattner545dd342008-06-28 23:36:30 +0000538 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000540 "format", std::string("1"));
541 return;
542 }
543
Chris Lattner545dd342008-06-28 23:36:30 +0000544 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
546 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000547 return;
548 }
549
550 // GCC ignores the format attribute on K&R style function
551 // prototypes, so we ignore it as well
552 const FunctionTypeProto *proto = getFunctionProto(d);
553
554 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000555 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
556 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000557 return;
558 }
559
560 // FIXME: in C++ the implicit 'this' function parameter also counts.
561 // this is needed in order to be compatible with GCC
562 // the index must start in 1 and the limit is numargs+1
563 unsigned NumArgs = proto->getNumArgs();
564 unsigned FirstIdx = 1;
565
Chris Lattner545dd342008-06-28 23:36:30 +0000566 const char *Format = Attr.getParameterName()->getName();
567 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000568
569 // Normalize the argument, __foo__ becomes foo.
570 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
571 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
572 Format += 2;
573 FormatLen -= 4;
574 }
575
576 bool Supported = false;
577 bool is_NSString = false;
578 bool is_strftime = false;
579
580 switch (FormatLen) {
581 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000582 case 5: Supported = !memcmp(Format, "scanf", 5); break;
583 case 6: Supported = !memcmp(Format, "printf", 6); break;
584 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000585 case 8:
586 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
587 (is_NSString = !memcmp(Format, "NSString", 8));
588 break;
589 }
590
591 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000592 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000593 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000594 return;
595 }
596
597 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000598 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000599 llvm::APSInt Idx(32);
600 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000602 "format", std::string("2"), IdxExpr->getSourceRange());
603 return;
604 }
605
606 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000607 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608 "format", std::string("2"), IdxExpr->getSourceRange());
609 return;
610 }
611
612 // FIXME: Do we need to bounds check?
613 unsigned ArgIdx = Idx.getZExtValue() - 1;
614
615 // make sure the format string is really a string
616 QualType Ty = proto->getArgType(ArgIdx);
617
618 if (is_NSString) {
619 // FIXME: do we need to check if the type is NSString*? What are
620 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000621 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000622 // FIXME: Should highlight the actual expression that has the
623 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000624 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
625 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000626 return;
627 }
628 } else if (!Ty->isPointerType() ||
629 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
630 // FIXME: Should highlight the actual expression that has the
631 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000632 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
633 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000634 return;
635 }
636
637 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000638 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000639 llvm::APSInt FirstArg(32);
640 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
641 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000642 "format", std::string("3"), FirstArgExpr->getSourceRange());
643 return;
644 }
645
646 // check if the function is variadic if the 3rd argument non-zero
647 if (FirstArg != 0) {
648 if (proto->isVariadic()) {
649 ++NumArgs; // +1 for ...
650 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000651 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000652 return;
653 }
654 }
655
656 // strftime requires FirstArg to be 0 because it doesn't read from any variable
657 // the input is just the current time + the format string
658 if (is_strftime) {
659 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000660 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 FirstArgExpr->getSourceRange());
662 return;
663 }
664 // if 0 it disables parameter checking (to use with e.g. va_list)
665 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000667 "format", std::string("3"), FirstArgExpr->getSourceRange());
668 return;
669 }
670
671 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
672 Idx.getZExtValue(), FirstArg.getZExtValue()));
673}
674
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000675static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
676 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000677 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000678 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000680 std::string("0"));
681 return;
682 }
683
684 TypeDecl *decl = dyn_cast<TypeDecl>(d);
685
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000686 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688 "transparent_union", "union");
689 return;
690 }
691
692 //QualType QTy = Context.getTypeDeclType(decl);
693 //const RecordType *Ty = QTy->getAsUnionType();
694
695// FIXME
696// Ty->addAttr(new TransparentUnionAttr());
697}
698
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000699static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000700 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000701 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
703 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000704 return;
705 }
Chris Lattner545dd342008-06-28 23:36:30 +0000706 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000707 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
708
709 // Make sure that there is a string literal as the annotation's single
710 // argument.
711 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000712 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000713 return;
714 }
715 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
716 SE->getByteLength())));
717}
718
Chris Lattner803d0802008-06-29 00:43:07 +0000719static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000720 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000721 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
723 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000724 return;
725 }
726
727 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000728 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000729 // FIXME: This should be the target specific maximum alignment.
730 // (For now we just use 128 bits which is the maximum on X86.
731 Align = 128;
732 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000733 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000734
735 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
736 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000737 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
739 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000740 return;
741 }
742 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000743}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000744
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000745/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000746/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000747///
748/// Despite what would be logical, the mode attribute is a decl attribute,
749/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
750/// 'G' be HImode, not an intermediate pointer.
751///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000752static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000753 // This attribute isn't documented, but glibc uses it. It changes
754 // the width of an int or unsigned int to the specified size.
755
756 // Check that there aren't any arguments
757 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
759 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000760 return;
761 }
762
763 IdentifierInfo *Name = Attr.getParameterName();
764 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000765 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000766 return;
767 }
768 const char *Str = Name->getName();
769 unsigned Len = Name->getLength();
770
771 // Normalize the attribute name, __foo__ becomes foo.
772 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
773 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
774 Str += 2;
775 Len -= 4;
776 }
777
778 unsigned DestWidth = 0;
779 bool IntegerMode = true;
780 switch (Len) {
781 case 2:
782 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
783 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
784 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
785 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
786 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
787 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
788 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
789 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
790 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
791 break;
792 case 4:
793 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
794 // pointer on PIC16 and other embedded platforms.
795 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000796 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000797 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000798 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000799 break;
800 case 7:
801 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000802 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000803 break;
804 }
805
806 QualType OldTy;
807 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
808 OldTy = TD->getUnderlyingType();
809 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
810 OldTy = VD->getType();
811 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000812 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
813 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000814 return;
815 }
816
817 // FIXME: Need proper fixed-width types
818 QualType NewTy;
819 switch (DestWidth) {
820 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000821 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000822 return;
823 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000824 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000825 return;
826 case 8:
827 assert(IntegerMode);
828 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000829 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000830 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000831 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000832 break;
833 case 16:
834 assert(IntegerMode);
835 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000836 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000837 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000838 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000839 break;
840 case 32:
841 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000842 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000843 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000844 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000845 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000846 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000847 break;
848 case 64:
849 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000850 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000851 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000852 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000853 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000854 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000855 break;
856 }
857
858 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000859 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000860 else if (!(IntegerMode && OldTy->isIntegerType()) &&
861 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000862 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000863 }
864
865 // Install the new type.
866 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
867 TD->setUnderlyingType(NewTy);
868 else
869 cast<ValueDecl>(D)->setType(NewTy);
870}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000871
872//===----------------------------------------------------------------------===//
873// Top Level Sema Entry Points
874//===----------------------------------------------------------------------===//
875
Chris Lattner803d0802008-06-29 00:43:07 +0000876/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
877/// the attribute applies to decls. If the attribute is a type attribute, just
878/// silently ignore it.
879static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
880 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000881 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000882 case AttributeList::AT_address_space:
883 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
884 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000885 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
886 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
887 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
888 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
889 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
890 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
891 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
892 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000893 case AttributeList::AT_ext_vector_type:
894 HandleExtVectorTypeAttr(D, Attr, S);
895 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000896 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
897 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000898 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000899 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
900 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
901 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
902 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
903 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
904 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
905 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000906 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
907 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000908 case AttributeList::AT_transparent_union:
909 HandleTransparentUnionAttr(D, Attr, S);
910 break;
911 default:
912#if 0
913 // TODO: when we have the full set of attributes, warn about unknown ones.
914 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
915 Attr->getName()->getName());
916#endif
917 break;
918 }
919}
920
921/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
922/// attribute list to the specified decl, ignoring any type attributes.
923void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
924 while (AttrList) {
925 ProcessDeclAttribute(D, *AttrList, *this);
926 AttrList = AttrList->getNext();
927 }
928}
929
930
Chris Lattner0744e5f2008-06-29 00:23:49 +0000931/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
932/// it, apply them to D. This is a bit tricky because PD can have attributes
933/// specified in many different places, and we need to find and apply them all.
934void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
935 // Apply decl attributes from the DeclSpec if present.
936 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
937 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +0000938
Chris Lattner0744e5f2008-06-29 00:23:49 +0000939 // Walk the declarator structure, applying decl attributes that were in a type
940 // position to the decl itself. This handles cases like:
941 // int *__attr__(x)** D;
942 // when X is a decl attribute.
943 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
944 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
945 ProcessDeclAttributeList(D, Attrs);
946
947 // Finally, apply any attributes on the decl itself.
948 if (const AttributeList *Attrs = PD.getAttributes())
949 ProcessDeclAttributeList(D, Attrs);
950}
951