blob: c8bc12df9f85102dd2281291e1c5a1dbae35a695 [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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000018#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000019using namespace clang;
20
Chris Lattnere5c5ee12008-06-29 00:16:31 +000021//===----------------------------------------------------------------------===//
22// Helper functions
23//===----------------------------------------------------------------------===//
24
Chris Lattner6b6b5372008-06-26 18:38:35 +000025static const FunctionTypeProto *getFunctionProto(Decl *d) {
26 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000027 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
28 Ty = decl->getType();
29 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
30 Ty = decl->getType();
31 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
32 Ty = decl->getUnderlyingType();
33 else
34 return 0;
35
36 if (Ty->isFunctionPointerType())
37 Ty = Ty->getAsPointerType()->getPointeeType();
38
39 if (const FunctionType *FnTy = Ty->getAsFunctionType())
40 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
41
42 return 0;
43}
44
45static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +000046 const PointerType *PT = T->getAsPointerType();
47 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +000048 return false;
49
Chris Lattnerb77792e2008-07-26 22:17:49 +000050 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000051 if (!ClsT)
52 return false;
53
54 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
55
56 // FIXME: Should we walk the chain of classes?
57 return ClsName == &Ctx.Idents.get("NSString") ||
58 ClsName == &Ctx.Idents.get("NSMutableString");
59}
60
Chris Lattnere5c5ee12008-06-29 00:16:31 +000061//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +000062// Attribute Implementations
63//===----------------------------------------------------------------------===//
64
Daniel Dunbar3068ae02008-07-31 22:40:48 +000065// FIXME: All this manual attribute parsing code is gross. At the
66// least add some helper functions to check most argument patterns (#
67// and types of args).
68
Chris Lattner803d0802008-06-29 00:43:07 +000069static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
70 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +000071 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
72 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000073 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +000074 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +000075 }
76
Chris Lattner6b6b5372008-06-26 18:38:35 +000077 QualType curType = tDecl->getUnderlyingType();
78 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +000079 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +000080 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
81 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +000082 return;
83 }
Chris Lattner545dd342008-06-28 23:36:30 +000084 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +000085 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +000086 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
87 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
88 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +000089 return;
90 }
91 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
92 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +000093 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +000094 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +000095 curType.getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +000096 return;
97 }
98 // unlike gcc's vector_size attribute, the size is specified as the
99 // number of elements, not the number of bytes.
100 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
101
102 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000103 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
104 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000105 return;
106 }
107 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000108 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000109 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000110 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000111}
112
Chris Lattner065c5a82008-06-28 23:48:25 +0000113
114/// HandleVectorSizeAttribute - this attribute is only applicable to
115/// integral and float scalars, although arrays, pointers, and function
116/// return values are allowed in conjunction with this construct. Aggregates
117/// with this attribute are invalid, even if they are of the same size as a
118/// corresponding scalar.
119/// The raw attribute should contain precisely 1 argument, the vector size
120/// for the variable, measured in bytes. If curType and rawAttr are well
121/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000122static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000123 QualType CurType;
124 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
125 CurType = VD->getType();
126 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
127 CurType = TD->getUnderlyingType();
128 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000129 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
130 std::string("vector_size"),
131 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000132 return;
133 }
134
135 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000136 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000137 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
138 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000139 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000140 }
Chris Lattner545dd342008-06-28 23:36:30 +0000141 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000142 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000143 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
144 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
145 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000146 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000147 }
148 // navigate to the base type - we need to provide for vector pointers,
149 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000150 if (CurType->isPointerType() || CurType->isArrayType() ||
151 CurType->isFunctionType()) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000152 assert(0 && "HandleVector(): Complex type construction unimplemented");
153 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
154 do {
155 if (PointerType *PT = dyn_cast<PointerType>(canonType))
156 canonType = PT->getPointeeType().getTypePtr();
157 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
158 canonType = AT->getElementType().getTypePtr();
159 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
160 canonType = FT->getResultType().getTypePtr();
161 } while (canonType->isPointerType() || canonType->isArrayType() ||
162 canonType->isFunctionType());
163 */
164 }
165 // the base type must be integer or float.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000166 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000167 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000168 CurType.getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000169 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 }
Chris Lattner803d0802008-06-29 00:43:07 +0000171 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000172 // vecSize is specified in bytes - convert to bits.
173 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
174
175 // the vector size needs to be an integral multiple of the type size.
176 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000177 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
178 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000179 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000180 }
181 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000182 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
183 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000184 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000185 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000186
187 // Success! Instantiate the vector type, the number of elements is > 0, and
188 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000189 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000190
191 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
192 VD->setType(CurType);
193 else
194 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000195}
196
Chris Lattner803d0802008-06-29 00:43:07 +0000197static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000198 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000199 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
201 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000202 return;
203 }
204
205 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000206 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
208 // If the alignment is less than or equal to 8 bits, the packed attribute
209 // has no effect.
210 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000211 S.Context.getTypeAlign(FD->getType()) <= 8)
212 S.Diag(Attr.getLoc(),
213 diag::warn_attribute_ignored_for_field_of_type,
214 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000216 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000217 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000218 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
219 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000220}
221
Ted Kremenek96329d42008-07-15 22:26:48 +0000222static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
223 // check the attribute arguments.
224 if (Attr.getNumArgs() > 0) {
225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
226 std::string("0"));
227 return;
228 }
229
230 // The IBOutlet attribute only applies to instance variables of Objective-C
231 // classes.
232 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
233 ID->addAttr(new IBOutletAttr());
234 else
235 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
236}
237
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000238static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
239
240 // GCC ignores the nonnull attribute on K&R style function
241 // prototypes, so we ignore it as well
242 const FunctionTypeProto *proto = getFunctionProto(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000243 if (!proto) {
244 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
245 "nonnull", "function");
246 return;
247 }
248
249 unsigned NumArgs = proto->getNumArgs();
250
251 // The nonnull attribute only applies to pointers.
252 llvm::SmallVector<unsigned, 10> NonNullArgs;
253
254 for (AttributeList::arg_iterator I=Attr.arg_begin(),
255 E=Attr.arg_end(); I!=E; ++I) {
256
257
258 // The argument must be an integer constant expression.
259 Expr *Ex = static_cast<Expr *>(Attr.getArg(0));
260 llvm::APSInt ArgNum(32);
261 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
263 "nonnull", Ex->getSourceRange());
264 return;
265 }
266
267 unsigned x = (unsigned) ArgNum.getZExtValue();
268
269 if (x < 1 || x > NumArgs) {
270 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Ted Kremenek6e1eb872008-07-22 16:56:21 +0000271 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000272 return;
273 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000274
275 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000276
277 // Is the function argument a pointer type?
Chris Lattnerb77792e2008-07-26 22:17:49 +0000278 if (!proto->getArgType(x)->isPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000279 // FIXME: Should also highlight argument in decl.
280 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
281 "nonnull", Ex->getSourceRange());
282 return;
283 }
284
285 NonNullArgs.push_back(x);
286 }
287
288 if (!NonNullArgs.empty()) {
289 unsigned* start = &NonNullArgs[0];
290 unsigned size = NonNullArgs.size();
291 std::sort(start, start + size);
292 d->addAttr(new NonNullAttr(start, size));
293 }
294 else
295 d->addAttr(new NonNullAttr());
296}
297
Chris Lattner803d0802008-06-29 00:43:07 +0000298static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000299 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000300 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
302 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000303 return;
304 }
305
Chris Lattner545dd342008-06-28 23:36:30 +0000306 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000307 Arg = Arg->IgnoreParenCasts();
308 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
309
310 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
312 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000313 return;
314 }
315
316 const char *Alias = Str->getStrData();
317 unsigned AliasLen = Str->getByteLength();
318
319 // FIXME: check if target symbol exists in current file
320
321 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
322}
323
Chris Lattner803d0802008-06-29 00:43:07 +0000324static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000325 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000326 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000327 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
328 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000329 return;
330 }
331
332 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000333 if (!Fn) {
Chris Lattner803d0802008-06-29 00:43:07 +0000334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
335 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000336 return;
337 }
338
339 d->addAttr(new NoReturnAttr());
340}
341
Ted Kremenek73798892008-07-25 04:39:19 +0000342static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
343 // check the attribute arguments.
344 if (Attr.getNumArgs() != 0) {
345 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
346 std::string("0"));
347 return;
348 }
349
Ted Kremenek356b63a2008-08-07 01:02:05 +0000350 if (!isa<VarDecl>(d) && !getFunctionProto(d)) {
Ted Kremenek73798892008-07-25 04:39:19 +0000351 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Ted Kremenek356b63a2008-08-07 01:02:05 +0000352 "unused", "variable and function");
Ted Kremenek73798892008-07-25 04:39:19 +0000353 return;
354 }
355
356 d->addAttr(new UnusedAttr());
357}
358
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000359static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
360 // check the attribute arguments.
361 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
363 return;
364 }
365
366 int priority = 65535; // FIXME: Do not hardcode such constants.
367 if (Attr.getNumArgs() > 0) {
368 Expr *E = static_cast<Expr *>(Attr.getArg(0));
369 llvm::APSInt Idx(32);
370 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
371 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
372 "constructor", "1", E->getSourceRange());
373 return;
374 }
375 priority = Idx.getZExtValue();
376 }
377
378 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
379 if (!Fn) {
380 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
381 "constructor", "function");
382 return;
383 }
384
385 d->addAttr(new ConstructorAttr(priority));
386}
387
388static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
389 // check the attribute arguments.
390 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
391 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, "0 or 1");
392 return;
393 }
394
395 int priority = 65535; // FIXME: Do not hardcode such constants.
396 if (Attr.getNumArgs() > 0) {
397 Expr *E = static_cast<Expr *>(Attr.getArg(0));
398 llvm::APSInt Idx(32);
399 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
401 "destructor", "1", E->getSourceRange());
402 return;
403 }
404 priority = Idx.getZExtValue();
405 }
406
407 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
408 if (!Fn) {
409 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
410 "destructor", "function");
411 return;
412 }
413
414 d->addAttr(new DestructorAttr(priority));
415}
416
Chris Lattner803d0802008-06-29 00:43:07 +0000417static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000418 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000419 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
421 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000422 return;
423 }
424
425 d->addAttr(new DeprecatedAttr());
426}
427
Chris Lattner803d0802008-06-29 00:43:07 +0000428static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000429 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000430 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
432 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000433 return;
434 }
435
Chris Lattner545dd342008-06-28 23:36:30 +0000436 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000437 Arg = Arg->IgnoreParenCasts();
438 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
439
440 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000441 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
442 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000443 return;
444 }
445
446 const char *TypeStr = Str->getStrData();
447 unsigned TypeLen = Str->getByteLength();
448 VisibilityAttr::VisibilityTypes type;
449
450 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
451 type = VisibilityAttr::DefaultVisibility;
452 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
453 type = VisibilityAttr::HiddenVisibility;
454 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
455 type = VisibilityAttr::HiddenVisibility; // FIXME
456 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
457 type = VisibilityAttr::ProtectedVisibility;
458 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000459 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
460 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000461 return;
462 }
463
464 d->addAttr(new VisibilityAttr(type));
465}
466
Chris Lattner803d0802008-06-29 00:43:07 +0000467static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000468 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000469 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000470 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
471 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000472 return;
473 }
474
475 d->addAttr(new WeakAttr());
476}
477
Chris Lattner803d0802008-06-29 00:43:07 +0000478static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000479 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000480 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
482 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000483 return;
484 }
485
486 d->addAttr(new DLLImportAttr());
487}
488
Chris Lattner803d0802008-06-29 00:43:07 +0000489static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000490 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000491 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
493 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000494 return;
495 }
496
497 d->addAttr(new DLLExportAttr());
498}
499
Chris Lattner803d0802008-06-29 00:43:07 +0000500static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000501 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000502 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
504 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000505 return;
506 }
507
508 d->addAttr(new StdCallAttr());
509}
510
Chris Lattner803d0802008-06-29 00:43:07 +0000511static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000512 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000513 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
515 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000516 return;
517 }
518
519 d->addAttr(new FastCallAttr());
520}
521
Chris Lattner803d0802008-06-29 00:43:07 +0000522static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000523 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000524 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
526 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000527 return;
528 }
529
530 d->addAttr(new NoThrowAttr());
531}
532
533/// Handle __attribute__((format(type,idx,firstarg))) attributes
534/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000535static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000536
Chris Lattner545dd342008-06-28 23:36:30 +0000537 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000539 "format", std::string("1"));
540 return;
541 }
542
Chris Lattner545dd342008-06-28 23:36:30 +0000543 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
545 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000546 return;
547 }
548
549 // GCC ignores the format attribute on K&R style function
550 // prototypes, so we ignore it as well
551 const FunctionTypeProto *proto = getFunctionProto(d);
552
553 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
555 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000556 return;
557 }
558
559 // FIXME: in C++ the implicit 'this' function parameter also counts.
560 // this is needed in order to be compatible with GCC
561 // the index must start in 1 and the limit is numargs+1
562 unsigned NumArgs = proto->getNumArgs();
563 unsigned FirstIdx = 1;
564
Chris Lattner545dd342008-06-28 23:36:30 +0000565 const char *Format = Attr.getParameterName()->getName();
566 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000567
568 // Normalize the argument, __foo__ becomes foo.
569 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
570 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
571 Format += 2;
572 FormatLen -= 4;
573 }
574
575 bool Supported = false;
576 bool is_NSString = false;
577 bool is_strftime = false;
578
579 switch (FormatLen) {
580 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000581 case 5: Supported = !memcmp(Format, "scanf", 5); break;
582 case 6: Supported = !memcmp(Format, "printf", 6); break;
583 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000584 case 8:
585 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
586 (is_NSString = !memcmp(Format, "NSString", 8));
587 break;
588 }
589
590 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000591 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000592 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000593 return;
594 }
595
596 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000597 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000598 llvm::APSInt Idx(32);
599 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000601 "format", std::string("2"), IdxExpr->getSourceRange());
602 return;
603 }
604
605 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000606 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000607 "format", std::string("2"), IdxExpr->getSourceRange());
608 return;
609 }
610
611 // FIXME: Do we need to bounds check?
612 unsigned ArgIdx = Idx.getZExtValue() - 1;
613
614 // make sure the format string is really a string
615 QualType Ty = proto->getArgType(ArgIdx);
616
617 if (is_NSString) {
618 // FIXME: do we need to check if the type is NSString*? What are
619 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000620 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000621 // FIXME: Should highlight the actual expression that has the
622 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000623 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
624 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000625 return;
626 }
627 } else if (!Ty->isPointerType() ||
628 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
629 // FIXME: Should highlight the actual expression that has the
630 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000631 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
632 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000633 return;
634 }
635
636 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000637 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000638 llvm::APSInt FirstArg(32);
639 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
640 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000641 "format", std::string("3"), FirstArgExpr->getSourceRange());
642 return;
643 }
644
645 // check if the function is variadic if the 3rd argument non-zero
646 if (FirstArg != 0) {
647 if (proto->isVariadic()) {
648 ++NumArgs; // +1 for ...
649 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000650 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000651 return;
652 }
653 }
654
655 // strftime requires FirstArg to be 0 because it doesn't read from any variable
656 // the input is just the current time + the format string
657 if (is_strftime) {
658 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000659 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000660 FirstArgExpr->getSourceRange());
661 return;
662 }
663 // if 0 it disables parameter checking (to use with e.g. va_list)
664 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000666 "format", std::string("3"), FirstArgExpr->getSourceRange());
667 return;
668 }
669
670 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
671 Idx.getZExtValue(), FirstArg.getZExtValue()));
672}
673
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000674static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
675 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000676 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000677 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000678 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000679 std::string("0"));
680 return;
681 }
682
683 TypeDecl *decl = dyn_cast<TypeDecl>(d);
684
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000685 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000687 "transparent_union", "union");
688 return;
689 }
690
691 //QualType QTy = Context.getTypeDeclType(decl);
692 //const RecordType *Ty = QTy->getAsUnionType();
693
694// FIXME
695// Ty->addAttr(new TransparentUnionAttr());
696}
697
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000698static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000700 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000701 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
702 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000703 return;
704 }
Chris Lattner545dd342008-06-28 23:36:30 +0000705 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000706 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
707
708 // Make sure that there is a string literal as the annotation's single
709 // argument.
710 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000711 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000712 return;
713 }
714 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
715 SE->getByteLength())));
716}
717
Chris Lattner803d0802008-06-29 00:43:07 +0000718static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000719 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000720 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
722 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000723 return;
724 }
725
726 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000727 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000728 // FIXME: This should be the target specific maximum alignment.
729 // (For now we just use 128 bits which is the maximum on X86.
730 Align = 128;
731 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000732 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000733
734 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
735 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000736 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
738 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000739 return;
740 }
741 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000742}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000743
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000744/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000745/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000746///
747/// Despite what would be logical, the mode attribute is a decl attribute,
748/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
749/// 'G' be HImode, not an intermediate pointer.
750///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000751static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000752 // This attribute isn't documented, but glibc uses it. It changes
753 // the width of an int or unsigned int to the specified size.
754
755 // Check that there aren't any arguments
756 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
758 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000759 return;
760 }
761
762 IdentifierInfo *Name = Attr.getParameterName();
763 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000764 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000765 return;
766 }
767 const char *Str = Name->getName();
768 unsigned Len = Name->getLength();
769
770 // Normalize the attribute name, __foo__ becomes foo.
771 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
772 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
773 Str += 2;
774 Len -= 4;
775 }
776
777 unsigned DestWidth = 0;
778 bool IntegerMode = true;
779 switch (Len) {
780 case 2:
781 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
782 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
783 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
784 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
785 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
786 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
787 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
788 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
789 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
790 break;
791 case 4:
792 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
793 // pointer on PIC16 and other embedded platforms.
794 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000795 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000796 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000797 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000798 break;
799 case 7:
800 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000801 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000802 break;
803 }
804
805 QualType OldTy;
806 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
807 OldTy = TD->getUnderlyingType();
808 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
809 OldTy = VD->getType();
810 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000811 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
812 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000813 return;
814 }
815
816 // FIXME: Need proper fixed-width types
817 QualType NewTy;
818 switch (DestWidth) {
819 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000820 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000821 return;
822 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000823 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000824 return;
825 case 8:
826 assert(IntegerMode);
827 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000828 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000829 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000830 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000831 break;
832 case 16:
833 assert(IntegerMode);
834 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000835 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000836 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000837 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000838 break;
839 case 32:
840 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000841 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000842 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000843 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000844 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000845 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000846 break;
847 case 64:
848 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000849 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000850 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000851 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000852 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000853 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000854 break;
855 }
856
857 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000858 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000859 else if (!(IntegerMode && OldTy->isIntegerType()) &&
860 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000861 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000862 }
863
864 // Install the new type.
865 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
866 TD->setUnderlyingType(NewTy);
867 else
868 cast<ValueDecl>(D)->setType(NewTy);
869}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000870
871//===----------------------------------------------------------------------===//
872// Top Level Sema Entry Points
873//===----------------------------------------------------------------------===//
874
Chris Lattner803d0802008-06-29 00:43:07 +0000875/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
876/// the attribute applies to decls. If the attribute is a type attribute, just
877/// silently ignore it.
878static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
879 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000880 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000881 case AttributeList::AT_address_space:
882 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
883 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000884 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
885 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
886 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
887 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
888 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
889 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
890 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
891 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000892 case AttributeList::AT_ext_vector_type:
893 HandleExtVectorTypeAttr(D, Attr, S);
894 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000895 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
896 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000897 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000898 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
899 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
900 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
901 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
902 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
903 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
904 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000905 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
906 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000907 case AttributeList::AT_transparent_union:
908 HandleTransparentUnionAttr(D, Attr, S);
909 break;
910 default:
911#if 0
912 // TODO: when we have the full set of attributes, warn about unknown ones.
913 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
914 Attr->getName()->getName());
915#endif
916 break;
917 }
918}
919
920/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
921/// attribute list to the specified decl, ignoring any type attributes.
922void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
923 while (AttrList) {
924 ProcessDeclAttribute(D, *AttrList, *this);
925 AttrList = AttrList->getNext();
926 }
927}
928
929
Chris Lattner0744e5f2008-06-29 00:23:49 +0000930/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
931/// it, apply them to D. This is a bit tricky because PD can have attributes
932/// specified in many different places, and we need to find and apply them all.
933void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
934 // Apply decl attributes from the DeclSpec if present.
935 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
936 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +0000937
Chris Lattner0744e5f2008-06-29 00:23:49 +0000938 // Walk the declarator structure, applying decl attributes that were in a type
939 // position to the decl itself. This handles cases like:
940 // int *__attr__(x)** D;
941 // when X is a decl attribute.
942 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
943 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
944 ProcessDeclAttributeList(D, Attrs);
945
946 // Finally, apply any attributes on the decl itself.
947 if (const AttributeList *Attrs = PD.getAttributes())
948 ProcessDeclAttributeList(D, Attrs);
949}
950