blob: 04e3f0557b762ac8439cc39020225e837c02d964 [file] [log] [blame]
Chris Lattner6953a072008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000016#include "clang/Basic/TargetInfo.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000017#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000018using namespace clang;
19
Chris Lattner2024f0a2008-06-29 00:16:31 +000020//===----------------------------------------------------------------------===//
21// Helper functions
22//===----------------------------------------------------------------------===//
23
Chris Lattner6953a072008-06-26 18:38:35 +000024static const FunctionTypeProto *getFunctionProto(Decl *d) {
25 QualType Ty;
Chris Lattner6953a072008-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) {
45 if (!T->isPointerType())
46 return false;
47
48 T = T->getAsPointerType()->getPointeeType().getCanonicalType();
49 ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
50
51 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 Lattner2024f0a2008-06-29 00:16:31 +000061//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +000062// Attribute Implementations
63//===----------------------------------------------------------------------===//
64
Chris Lattner703c52d2008-06-29 00:43:07 +000065static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
66 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +000067 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
68 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +000069 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +000070 return;
Chris Lattner6953a072008-06-26 18:38:35 +000071 }
72
Chris Lattner6953a072008-06-26 18:38:35 +000073 QualType curType = tDecl->getUnderlyingType();
74 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +000075 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +000076 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
77 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +000078 return;
79 }
Chris Lattner1c151132008-06-28 23:36:30 +000080 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +000081 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +000082 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
83 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
84 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +000085 return;
86 }
87 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
88 // in conjunction with complex types (pointers, arrays, functions, etc.).
89 Type *canonType = curType.getCanonicalType().getTypePtr();
90 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner703c52d2008-06-29 00:43:07 +000091 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
92 curType.getCanonicalType().getAsString());
Chris Lattner6953a072008-06-26 18:38:35 +000093 return;
94 }
95 // unlike gcc's vector_size attribute, the size is specified as the
96 // number of elements, not the number of bytes.
97 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
98
99 if (vectorSize == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000100 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
101 sizeExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000102 return;
103 }
104 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000105 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000106 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000107 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000108}
109
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000110
111/// HandleVectorSizeAttribute - this attribute is only applicable to
112/// integral and float scalars, although arrays, pointers, and function
113/// return values are allowed in conjunction with this construct. Aggregates
114/// with this attribute are invalid, even if they are of the same size as a
115/// corresponding scalar.
116/// The raw attribute should contain precisely 1 argument, the vector size
117/// for the variable, measured in bytes. If curType and rawAttr are well
118/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000119static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000120 QualType CurType;
121 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
122 CurType = VD->getType();
123 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
124 CurType = TD->getUnderlyingType();
125 else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000126 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
127 std::string("vector_size"),
128 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000129 return;
130 }
131
132 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000133 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000134 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
135 std::string("1"));
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000136 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000137 }
Chris Lattner1c151132008-06-28 23:36:30 +0000138 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000139 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000140 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
141 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
142 "vector_size", sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000143 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000144 }
145 // navigate to the base type - we need to provide for vector pointers,
146 // vector arrays, and functions returning vectors.
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000147 Type *canonType = CurType.getCanonicalType().getTypePtr();
Chris Lattner6953a072008-06-26 18:38:35 +0000148
149 if (canonType->isPointerType() || canonType->isArrayType() ||
150 canonType->isFunctionType()) {
151 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.
165 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000166 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
167 CurType.getCanonicalType().getAsString());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000168 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000169 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000170 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-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 Lattner703c52d2008-06-29 00:43:07 +0000176 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
177 sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000178 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000179 }
180 if (vectorSize == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000181 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
182 sizeExpr->getSourceRange());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000183 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000184 }
Chris Lattner8ed14aa2008-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 Lattner703c52d2008-06-29 00:43:07 +0000188 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-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 Lattner6953a072008-06-26 18:38:35 +0000194}
195
Chris Lattner703c52d2008-06-29 00:43:07 +0000196static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000197 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000198 if (Attr.getNumArgs() > 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
200 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000201 return;
202 }
203
204 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000205 TD->addAttr(new PackedAttr());
Chris Lattner6953a072008-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 Lattner703c52d2008-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 Lattner6953a072008-06-26 18:38:35 +0000214 else
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000215 FD->addAttr(new PackedAttr());
Chris Lattner6953a072008-06-26 18:38:35 +0000216 } else
Chris Lattner703c52d2008-06-29 00:43:07 +0000217 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
218 Attr.getName()->getName());
Chris Lattner6953a072008-06-26 18:38:35 +0000219}
220
Ted Kremenek4e5bb122008-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 Kremenekc0af2542008-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);
242
243 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 Kremenek26151d12008-07-22 16:56:21 +0000271 "nonnull", llvm::utostr_32(I.getArgNum()), Ex->getSourceRange());
Ted Kremenekc0af2542008-07-21 21:53:04 +0000272 return;
273 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000274
275 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000276
277 // Is the function argument a pointer type?
278 if (!proto->getArgType(x).getCanonicalType()->isPointerType()) {
279 // 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 Lattner703c52d2008-06-29 00:43:07 +0000298static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000299 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000300 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
302 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000303 return;
304 }
305
Chris Lattner1c151132008-06-28 23:36:30 +0000306 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000307 Arg = Arg->IgnoreParenCasts();
308 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
309
310 if (Str == 0 || Str->isWide()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
312 "alias", std::string("1"));
Chris Lattner6953a072008-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 Lattner703c52d2008-06-29 00:43:07 +0000324static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000325 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000326 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000327 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
328 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000329 return;
330 }
331
332 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6953a072008-06-26 18:38:35 +0000333 if (!Fn) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000334 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
335 "noreturn", "function");
Chris Lattner6953a072008-06-26 18:38:35 +0000336 return;
337 }
338
339 d->addAttr(new NoReturnAttr());
340}
341
Chris Lattner703c52d2008-06-29 00:43:07 +0000342static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000343 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000344 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000345 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
346 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000347 return;
348 }
349
350 d->addAttr(new DeprecatedAttr());
351}
352
Chris Lattner703c52d2008-06-29 00:43:07 +0000353static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000354 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000355 if (Attr.getNumArgs() != 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000356 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
357 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000358 return;
359 }
360
Chris Lattner1c151132008-06-28 23:36:30 +0000361 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000362 Arg = Arg->IgnoreParenCasts();
363 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
364
365 if (Str == 0 || Str->isWide()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000366 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
367 "visibility", std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000368 return;
369 }
370
371 const char *TypeStr = Str->getStrData();
372 unsigned TypeLen = Str->getByteLength();
373 VisibilityAttr::VisibilityTypes type;
374
375 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
376 type = VisibilityAttr::DefaultVisibility;
377 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
378 type = VisibilityAttr::HiddenVisibility;
379 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
380 type = VisibilityAttr::HiddenVisibility; // FIXME
381 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
382 type = VisibilityAttr::ProtectedVisibility;
383 else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000384 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
385 "visibility", TypeStr);
Chris Lattner6953a072008-06-26 18:38:35 +0000386 return;
387 }
388
389 d->addAttr(new VisibilityAttr(type));
390}
391
Chris Lattner703c52d2008-06-29 00:43:07 +0000392static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000393 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000394 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
396 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000397 return;
398 }
399
400 d->addAttr(new WeakAttr());
401}
402
Chris Lattner703c52d2008-06-29 00:43:07 +0000403static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000404 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000405 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
407 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000408 return;
409 }
410
411 d->addAttr(new DLLImportAttr());
412}
413
Chris Lattner703c52d2008-06-29 00:43:07 +0000414static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000415 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000416 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000417 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
418 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000419 return;
420 }
421
422 d->addAttr(new DLLExportAttr());
423}
424
Chris Lattner703c52d2008-06-29 00:43:07 +0000425static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000426 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000427 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
429 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000430 return;
431 }
432
433 d->addAttr(new StdCallAttr());
434}
435
Chris Lattner703c52d2008-06-29 00:43:07 +0000436static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000437 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000438 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000439 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
440 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000441 return;
442 }
443
444 d->addAttr(new FastCallAttr());
445}
446
Chris Lattner703c52d2008-06-29 00:43:07 +0000447static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000448 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000449 if (Attr.getNumArgs() != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
451 std::string("0"));
Chris Lattner6953a072008-06-26 18:38:35 +0000452 return;
453 }
454
455 d->addAttr(new NoThrowAttr());
456}
457
458/// Handle __attribute__((format(type,idx,firstarg))) attributes
459/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +0000460static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000461
Chris Lattner1c151132008-06-28 23:36:30 +0000462 if (!Attr.getParameterName()) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6953a072008-06-26 18:38:35 +0000464 "format", std::string("1"));
465 return;
466 }
467
Chris Lattner1c151132008-06-28 23:36:30 +0000468 if (Attr.getNumArgs() != 2) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000469 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
470 std::string("3"));
Chris Lattner6953a072008-06-26 18:38:35 +0000471 return;
472 }
473
474 // GCC ignores the format attribute on K&R style function
475 // prototypes, so we ignore it as well
476 const FunctionTypeProto *proto = getFunctionProto(d);
477
478 if (!proto) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
480 "format", "function");
Chris Lattner6953a072008-06-26 18:38:35 +0000481 return;
482 }
483
484 // FIXME: in C++ the implicit 'this' function parameter also counts.
485 // this is needed in order to be compatible with GCC
486 // the index must start in 1 and the limit is numargs+1
487 unsigned NumArgs = proto->getNumArgs();
488 unsigned FirstIdx = 1;
489
Chris Lattner1c151132008-06-28 23:36:30 +0000490 const char *Format = Attr.getParameterName()->getName();
491 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +0000492
493 // Normalize the argument, __foo__ becomes foo.
494 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
495 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
496 Format += 2;
497 FormatLen -= 4;
498 }
499
500 bool Supported = false;
501 bool is_NSString = false;
502 bool is_strftime = false;
503
504 switch (FormatLen) {
505 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000506 case 5: Supported = !memcmp(Format, "scanf", 5); break;
507 case 6: Supported = !memcmp(Format, "printf", 6); break;
508 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +0000509 case 8:
510 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
511 (is_NSString = !memcmp(Format, "NSString", 8));
512 break;
513 }
514
515 if (!Supported) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000516 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner1c151132008-06-28 23:36:30 +0000517 "format", Attr.getParameterName()->getName());
Chris Lattner6953a072008-06-26 18:38:35 +0000518 return;
519 }
520
521 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000522 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +0000523 llvm::APSInt Idx(32);
524 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6953a072008-06-26 18:38:35 +0000526 "format", std::string("2"), IdxExpr->getSourceRange());
527 return;
528 }
529
530 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6953a072008-06-26 18:38:35 +0000532 "format", std::string("2"), IdxExpr->getSourceRange());
533 return;
534 }
535
536 // FIXME: Do we need to bounds check?
537 unsigned ArgIdx = Idx.getZExtValue() - 1;
538
539 // make sure the format string is really a string
540 QualType Ty = proto->getArgType(ArgIdx);
541
542 if (is_NSString) {
543 // FIXME: do we need to check if the type is NSString*? What are
544 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +0000545 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +0000546 // FIXME: Should highlight the actual expression that has the
547 // wrong type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000548 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
549 IdxExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000550 return;
551 }
552 } else if (!Ty->isPointerType() ||
553 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
554 // FIXME: Should highlight the actual expression that has the
555 // wrong type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000556 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
557 IdxExpr->getSourceRange());
Chris Lattner6953a072008-06-26 18:38:35 +0000558 return;
559 }
560
561 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000562 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +0000563 llvm::APSInt FirstArg(32);
564 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6953a072008-06-26 18:38:35 +0000566 "format", std::string("3"), FirstArgExpr->getSourceRange());
567 return;
568 }
569
570 // check if the function is variadic if the 3rd argument non-zero
571 if (FirstArg != 0) {
572 if (proto->isVariadic()) {
573 ++NumArgs; // +1 for ...
574 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000575 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +0000576 return;
577 }
578 }
579
580 // strftime requires FirstArg to be 0 because it doesn't read from any variable
581 // the input is just the current time + the format string
582 if (is_strftime) {
583 if (FirstArg != 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000584 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6953a072008-06-26 18:38:35 +0000585 FirstArgExpr->getSourceRange());
586 return;
587 }
588 // if 0 it disables parameter checking (to use with e.g. va_list)
589 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6953a072008-06-26 18:38:35 +0000591 "format", std::string("3"), FirstArgExpr->getSourceRange());
592 return;
593 }
594
595 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
596 Idx.getZExtValue(), FirstArg.getZExtValue()));
597}
598
Chris Lattnerf6690152008-06-29 00:28:59 +0000599static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
600 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000601 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000602 if (Attr.getNumArgs() != 0) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6953a072008-06-26 18:38:35 +0000604 std::string("0"));
605 return;
606 }
607
608 TypeDecl *decl = dyn_cast<TypeDecl>(d);
609
Chris Lattnerf6690152008-06-29 00:28:59 +0000610 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
611 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6953a072008-06-26 18:38:35 +0000612 "transparent_union", "union");
613 return;
614 }
615
616 //QualType QTy = Context.getTypeDeclType(decl);
617 //const RecordType *Ty = QTy->getAsUnionType();
618
619// FIXME
620// Ty->addAttr(new TransparentUnionAttr());
621}
622
Chris Lattnerf6690152008-06-29 00:28:59 +0000623static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000624 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000625 if (Attr.getNumArgs() != 1) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
627 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000628 return;
629 }
Chris Lattner1c151132008-06-28 23:36:30 +0000630 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000631 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
632
633 // Make sure that there is a string literal as the annotation's single
634 // argument.
635 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000636 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +0000637 return;
638 }
639 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
640 SE->getByteLength())));
641}
642
Chris Lattner703c52d2008-06-29 00:43:07 +0000643static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000644 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000645 if (Attr.getNumArgs() > 1) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
647 std::string("1"));
Chris Lattner6953a072008-06-26 18:38:35 +0000648 return;
649 }
650
651 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +0000652 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +0000653 // FIXME: This should be the target specific maximum alignment.
654 // (For now we just use 128 bits which is the maximum on X86.
655 Align = 128;
656 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000657 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000658
659 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
660 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000661 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
663 "aligned", alignmentExpr->getSourceRange());
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000664 return;
665 }
666 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +0000667}
Chris Lattnerdc789562008-06-27 22:18:37 +0000668
Chris Lattnerf6690152008-06-29 00:28:59 +0000669/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000670/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +0000671///
672/// Despite what would be logical, the mode attribute is a decl attribute,
673/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
674/// 'G' be HImode, not an intermediate pointer.
675///
Chris Lattnerf6690152008-06-29 00:28:59 +0000676static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +0000677 // This attribute isn't documented, but glibc uses it. It changes
678 // the width of an int or unsigned int to the specified size.
679
680 // Check that there aren't any arguments
681 if (Attr.getNumArgs() != 0) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
683 std::string("0"));
Chris Lattnerdc789562008-06-27 22:18:37 +0000684 return;
685 }
686
687 IdentifierInfo *Name = Attr.getParameterName();
688 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +0000690 return;
691 }
692 const char *Str = Name->getName();
693 unsigned Len = Name->getLength();
694
695 // Normalize the attribute name, __foo__ becomes foo.
696 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
697 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
698 Str += 2;
699 Len -= 4;
700 }
701
702 unsigned DestWidth = 0;
703 bool IntegerMode = true;
704 switch (Len) {
705 case 2:
706 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
707 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
708 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
709 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
710 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
711 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
712 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
713 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
714 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
715 break;
716 case 4:
717 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
718 // pointer on PIC16 and other embedded platforms.
719 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +0000720 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +0000721 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +0000722 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +0000723 break;
724 case 7:
725 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +0000726 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +0000727 break;
728 }
729
730 QualType OldTy;
731 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
732 OldTy = TD->getUnderlyingType();
733 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
734 OldTy = VD->getType();
735 else {
Chris Lattnerf6690152008-06-29 00:28:59 +0000736 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
737 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerdc789562008-06-27 22:18:37 +0000738 return;
739 }
740
741 // FIXME: Need proper fixed-width types
742 QualType NewTy;
743 switch (DestWidth) {
744 case 0:
Chris Lattnerf6690152008-06-29 00:28:59 +0000745 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerdc789562008-06-27 22:18:37 +0000746 return;
747 default:
Chris Lattnerf6690152008-06-29 00:28:59 +0000748 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerdc789562008-06-27 22:18:37 +0000749 return;
750 case 8:
751 assert(IntegerMode);
752 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000753 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000754 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000755 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000756 break;
757 case 16:
758 assert(IntegerMode);
759 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000760 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000761 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000762 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000763 break;
764 case 32:
765 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +0000766 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000767 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000768 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000769 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000770 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000771 break;
772 case 64:
773 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +0000774 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000775 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000776 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000777 else
Chris Lattnerf6690152008-06-29 00:28:59 +0000778 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +0000779 break;
780 }
781
782 if (!OldTy->getAsBuiltinType())
Chris Lattnerf6690152008-06-29 00:28:59 +0000783 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerdc789562008-06-27 22:18:37 +0000784 else if (!(IntegerMode && OldTy->isIntegerType()) &&
785 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000786 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerdc789562008-06-27 22:18:37 +0000787 }
788
789 // Install the new type.
790 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
791 TD->setUnderlyingType(NewTy);
792 else
793 cast<ValueDecl>(D)->setType(NewTy);
794}
Chris Lattnera72440d2008-06-29 00:23:49 +0000795
796//===----------------------------------------------------------------------===//
797// Top Level Sema Entry Points
798//===----------------------------------------------------------------------===//
799
Chris Lattner703c52d2008-06-29 00:43:07 +0000800/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
801/// the attribute applies to decls. If the attribute is a type attribute, just
802/// silently ignore it.
803static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
804 switch (Attr.getKind()) {
805 case AttributeList::AT_address_space:
806 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
807 break;
808 case AttributeList::AT_ext_vector_type:
809 HandleExtVectorTypeAttr(D, Attr, S);
810 break;
811 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
812 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
813 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
814 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
815 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
816 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
817 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
818 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
819 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
820 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
821 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
822 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
823 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
824 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
825 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
826 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000827 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
828 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000829 case AttributeList::AT_transparent_union:
830 HandleTransparentUnionAttr(D, Attr, S);
831 break;
832 default:
833#if 0
834 // TODO: when we have the full set of attributes, warn about unknown ones.
835 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
836 Attr->getName()->getName());
837#endif
838 break;
839 }
840}
841
842/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
843/// attribute list to the specified decl, ignoring any type attributes.
844void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
845 while (AttrList) {
846 ProcessDeclAttribute(D, *AttrList, *this);
847 AttrList = AttrList->getNext();
848 }
849}
850
851
Chris Lattnera72440d2008-06-29 00:23:49 +0000852/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
853/// it, apply them to D. This is a bit tricky because PD can have attributes
854/// specified in many different places, and we need to find and apply them all.
855void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
856 // Apply decl attributes from the DeclSpec if present.
857 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
858 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +0000859
Chris Lattnera72440d2008-06-29 00:23:49 +0000860 // Walk the declarator structure, applying decl attributes that were in a type
861 // position to the decl itself. This handles cases like:
862 // int *__attr__(x)** D;
863 // when X is a decl attribute.
864 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
865 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
866 ProcessDeclAttributeList(D, Attrs);
867
868 // Finally, apply any attributes on the decl itself.
869 if (const AttributeList *Attrs = PD.getAttributes())
870 ProcessDeclAttributeList(D, Attrs);
871}
872