blob: f8217b362f214a0ef0340805aca54b32c4a4c8e4 [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"
Chris Lattner6b6b5372008-06-26 18:38:35 +000017using namespace clang;
18
Chris Lattnere5c5ee12008-06-29 00:16:31 +000019//===----------------------------------------------------------------------===//
20// Helper functions
21//===----------------------------------------------------------------------===//
22
Chris Lattner6b6b5372008-06-26 18:38:35 +000023static const FunctionTypeProto *getFunctionProto(Decl *d) {
24 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000025 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
26 Ty = decl->getType();
27 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
28 Ty = decl->getType();
29 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
30 Ty = decl->getUnderlyingType();
31 else
32 return 0;
33
34 if (Ty->isFunctionPointerType())
35 Ty = Ty->getAsPointerType()->getPointeeType();
36
37 if (const FunctionType *FnTy = Ty->getAsFunctionType())
38 return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType());
39
40 return 0;
41}
42
43static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
44 if (!T->isPointerType())
45 return false;
46
47 T = T->getAsPointerType()->getPointeeType().getCanonicalType();
48 ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
49
50 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
Chris Lattner803d0802008-06-29 00:43:07 +000064static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
65 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +000066 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
67 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000068 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +000069 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +000070 }
71
Chris Lattner6b6b5372008-06-26 18:38:35 +000072 QualType curType = tDecl->getUnderlyingType();
73 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +000074 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +000075 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
76 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +000077 return;
78 }
Chris Lattner545dd342008-06-28 23:36:30 +000079 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +000080 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +000081 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
82 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
83 "ext_vector_type", sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +000084 return;
85 }
86 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
87 // in conjunction with complex types (pointers, arrays, functions, etc.).
88 Type *canonType = curType.getCanonicalType().getTypePtr();
89 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner803d0802008-06-29 00:43:07 +000090 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
91 curType.getCanonicalType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +000092 return;
93 }
94 // unlike gcc's vector_size attribute, the size is specified as the
95 // number of elements, not the number of bytes.
96 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
97
98 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +000099 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
100 sizeExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000101 return;
102 }
103 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000104 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000105 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000106 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000107}
108
Chris Lattner065c5a82008-06-28 23:48:25 +0000109
110/// HandleVectorSizeAttribute - this attribute is only applicable to
111/// integral and float scalars, although arrays, pointers, and function
112/// return values are allowed in conjunction with this construct. Aggregates
113/// with this attribute are invalid, even if they are of the same size as a
114/// corresponding scalar.
115/// The raw attribute should contain precisely 1 argument, the vector size
116/// for the variable, measured in bytes. If curType and rawAttr are well
117/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000118static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000119 QualType CurType;
120 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
121 CurType = VD->getType();
122 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
123 CurType = TD->getUnderlyingType();
124 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000125 S.Diag(D->getLocation(), diag::err_attr_wrong_decl,
126 std::string("vector_size"),
127 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattner065c5a82008-06-28 23:48:25 +0000128 return;
129 }
130
131 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000132 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
134 std::string("1"));
Chris Lattner065c5a82008-06-28 23:48:25 +0000135 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136 }
Chris Lattner545dd342008-06-28 23:36:30 +0000137 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000139 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
141 "vector_size", sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000142 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 }
144 // navigate to the base type - we need to provide for vector pointers,
145 // vector arrays, and functions returning vectors.
Chris Lattner065c5a82008-06-28 23:48:25 +0000146 Type *canonType = CurType.getCanonicalType().getTypePtr();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000147
148 if (canonType->isPointerType() || canonType->isArrayType() ||
149 canonType->isFunctionType()) {
150 assert(0 && "HandleVector(): Complex type construction unimplemented");
151 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
152 do {
153 if (PointerType *PT = dyn_cast<PointerType>(canonType))
154 canonType = PT->getPointeeType().getTypePtr();
155 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
156 canonType = AT->getElementType().getTypePtr();
157 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
158 canonType = FT->getResultType().getTypePtr();
159 } while (canonType->isPointerType() || canonType->isArrayType() ||
160 canonType->isFunctionType());
161 */
162 }
163 // the base type must be integer or float.
164 if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) {
Chris Lattner803d0802008-06-29 00:43:07 +0000165 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type,
166 CurType.getCanonicalType().getAsString());
Chris Lattner065c5a82008-06-28 23:48:25 +0000167 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000168 }
Chris Lattner803d0802008-06-29 00:43:07 +0000169 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 // vecSize is specified in bytes - convert to bits.
171 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
172
173 // the vector size needs to be an integral multiple of the type size.
174 if (vectorSize % typeSize) {
Chris Lattner803d0802008-06-29 00:43:07 +0000175 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size,
176 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000177 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 }
179 if (vectorSize == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size,
181 sizeExpr->getSourceRange());
Chris Lattner065c5a82008-06-28 23:48:25 +0000182 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000184
185 // Success! Instantiate the vector type, the number of elements is > 0, and
186 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000187 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000188
189 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
190 VD->setType(CurType);
191 else
192 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193}
194
Chris Lattner803d0802008-06-29 00:43:07 +0000195static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000196 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000197 if (Attr.getNumArgs() > 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000198 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
199 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000200 return;
201 }
202
203 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner49e2d342008-06-28 23:50:44 +0000204 TD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000205 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
206 // If the alignment is less than or equal to 8 bits, the packed attribute
207 // has no effect.
208 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000209 S.Context.getTypeAlign(FD->getType()) <= 8)
210 S.Diag(Attr.getLoc(),
211 diag::warn_attribute_ignored_for_field_of_type,
212 Attr.getName()->getName(), FD->getType().getAsString());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 else
Chris Lattner49e2d342008-06-28 23:50:44 +0000214 FD->addAttr(new PackedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 } else
Chris Lattner803d0802008-06-29 00:43:07 +0000216 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
217 Attr.getName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000218}
219
Ted Kremenek96329d42008-07-15 22:26:48 +0000220static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
221 // check the attribute arguments.
222 if (Attr.getNumArgs() > 0) {
223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
224 std::string("0"));
225 return;
226 }
227
228 // The IBOutlet attribute only applies to instance variables of Objective-C
229 // classes.
230 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
231 ID->addAttr(new IBOutletAttr());
232 else
233 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
234}
235
Chris Lattner803d0802008-06-29 00:43:07 +0000236static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000237 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000238 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
240 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000241 return;
242 }
243
Chris Lattner545dd342008-06-28 23:36:30 +0000244 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000245 Arg = Arg->IgnoreParenCasts();
246 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
247
248 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
250 "alias", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000251 return;
252 }
253
254 const char *Alias = Str->getStrData();
255 unsigned AliasLen = Str->getByteLength();
256
257 // FIXME: check if target symbol exists in current file
258
259 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
260}
261
Chris Lattner803d0802008-06-29 00:43:07 +0000262static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000263 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000264 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000265 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
266 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000267 return;
268 }
269
270 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000271 if (!Fn) {
Chris Lattner803d0802008-06-29 00:43:07 +0000272 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
273 "noreturn", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000274 return;
275 }
276
277 d->addAttr(new NoReturnAttr());
278}
279
Chris Lattner803d0802008-06-29 00:43:07 +0000280static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000281 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000282 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
284 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000285 return;
286 }
287
288 d->addAttr(new DeprecatedAttr());
289}
290
Chris Lattner803d0802008-06-29 00:43:07 +0000291static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000292 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000293 if (Attr.getNumArgs() != 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000294 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
295 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000296 return;
297 }
298
Chris Lattner545dd342008-06-28 23:36:30 +0000299 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000300 Arg = Arg->IgnoreParenCasts();
301 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
302
303 if (Str == 0 || Str->isWide()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
305 "visibility", std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000306 return;
307 }
308
309 const char *TypeStr = Str->getStrData();
310 unsigned TypeLen = Str->getByteLength();
311 VisibilityAttr::VisibilityTypes type;
312
313 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
314 type = VisibilityAttr::DefaultVisibility;
315 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
316 type = VisibilityAttr::HiddenVisibility;
317 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
318 type = VisibilityAttr::HiddenVisibility; // FIXME
319 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
320 type = VisibilityAttr::ProtectedVisibility;
321 else {
Chris Lattner803d0802008-06-29 00:43:07 +0000322 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
323 "visibility", TypeStr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000324 return;
325 }
326
327 d->addAttr(new VisibilityAttr(type));
328}
329
Chris Lattner803d0802008-06-29 00:43:07 +0000330static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000331 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000332 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000333 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
334 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000335 return;
336 }
337
338 d->addAttr(new WeakAttr());
339}
340
Chris Lattner803d0802008-06-29 00:43:07 +0000341static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000342 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000343 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
345 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000346 return;
347 }
348
349 d->addAttr(new DLLImportAttr());
350}
351
Chris Lattner803d0802008-06-29 00:43:07 +0000352static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000353 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000354 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
356 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000357 return;
358 }
359
360 d->addAttr(new DLLExportAttr());
361}
362
Chris Lattner803d0802008-06-29 00:43:07 +0000363static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000364 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000365 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000366 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
367 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000368 return;
369 }
370
371 d->addAttr(new StdCallAttr());
372}
373
Chris Lattner803d0802008-06-29 00:43:07 +0000374static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000375 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000376 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
378 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000379 return;
380 }
381
382 d->addAttr(new FastCallAttr());
383}
384
Chris Lattner803d0802008-06-29 00:43:07 +0000385static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000386 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000387 if (Attr.getNumArgs() != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
389 std::string("0"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000390 return;
391 }
392
393 d->addAttr(new NoThrowAttr());
394}
395
396/// Handle __attribute__((format(type,idx,firstarg))) attributes
397/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +0000398static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000399
Chris Lattner545dd342008-06-28 23:36:30 +0000400 if (!Attr.getParameterName()) {
Chris Lattner803d0802008-06-29 00:43:07 +0000401 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000402 "format", std::string("1"));
403 return;
404 }
405
Chris Lattner545dd342008-06-28 23:36:30 +0000406 if (Attr.getNumArgs() != 2) {
Chris Lattner803d0802008-06-29 00:43:07 +0000407 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
408 std::string("3"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000409 return;
410 }
411
412 // GCC ignores the format attribute on K&R style function
413 // prototypes, so we ignore it as well
414 const FunctionTypeProto *proto = getFunctionProto(d);
415
416 if (!proto) {
Chris Lattner803d0802008-06-29 00:43:07 +0000417 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
418 "format", "function");
Chris Lattner6b6b5372008-06-26 18:38:35 +0000419 return;
420 }
421
422 // FIXME: in C++ the implicit 'this' function parameter also counts.
423 // this is needed in order to be compatible with GCC
424 // the index must start in 1 and the limit is numargs+1
425 unsigned NumArgs = proto->getNumArgs();
426 unsigned FirstIdx = 1;
427
Chris Lattner545dd342008-06-28 23:36:30 +0000428 const char *Format = Attr.getParameterName()->getName();
429 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000430
431 // Normalize the argument, __foo__ becomes foo.
432 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
433 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
434 Format += 2;
435 FormatLen -= 4;
436 }
437
438 bool Supported = false;
439 bool is_NSString = false;
440 bool is_strftime = false;
441
442 switch (FormatLen) {
443 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +0000444 case 5: Supported = !memcmp(Format, "scanf", 5); break;
445 case 6: Supported = !memcmp(Format, "printf", 6); break;
446 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000447 case 8:
448 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
449 (is_NSString = !memcmp(Format, "NSString", 8));
450 break;
451 }
452
453 if (!Supported) {
Chris Lattner803d0802008-06-29 00:43:07 +0000454 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
Chris Lattner545dd342008-06-28 23:36:30 +0000455 "format", Attr.getParameterName()->getName());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000456 return;
457 }
458
459 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000460 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +0000461 llvm::APSInt Idx(32);
462 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000464 "format", std::string("2"), IdxExpr->getSourceRange());
465 return;
466 }
467
468 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000470 "format", std::string("2"), IdxExpr->getSourceRange());
471 return;
472 }
473
474 // FIXME: Do we need to bounds check?
475 unsigned ArgIdx = Idx.getZExtValue() - 1;
476
477 // make sure the format string is really a string
478 QualType Ty = proto->getArgType(ArgIdx);
479
480 if (is_NSString) {
481 // FIXME: do we need to check if the type is NSString*? What are
482 // the semantics?
Chris Lattner803d0802008-06-29 00:43:07 +0000483 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000484 // FIXME: Should highlight the actual expression that has the
485 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000486 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString,
487 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000488 return;
489 }
490 } else if (!Ty->isPointerType() ||
491 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
492 // FIXME: Should highlight the actual expression that has the
493 // wrong type.
Chris Lattner803d0802008-06-29 00:43:07 +0000494 S.Diag(Attr.getLoc(), diag::err_format_attribute_not_string,
495 IdxExpr->getSourceRange());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000496 return;
497 }
498
499 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +0000500 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +0000501 llvm::APSInt FirstArg(32);
502 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
503 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000504 "format", std::string("3"), FirstArgExpr->getSourceRange());
505 return;
506 }
507
508 // check if the function is variadic if the 3rd argument non-zero
509 if (FirstArg != 0) {
510 if (proto->isVariadic()) {
511 ++NumArgs; // +1 for ...
512 } else {
Chris Lattner803d0802008-06-29 00:43:07 +0000513 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000514 return;
515 }
516 }
517
518 // strftime requires FirstArg to be 0 because it doesn't read from any variable
519 // the input is just the current time + the format string
520 if (is_strftime) {
521 if (FirstArg != 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000522 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000523 FirstArgExpr->getSourceRange());
524 return;
525 }
526 // if 0 it disables parameter checking (to use with e.g. va_list)
527 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner803d0802008-06-29 00:43:07 +0000528 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000529 "format", std::string("3"), FirstArgExpr->getSourceRange());
530 return;
531 }
532
533 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
534 Idx.getZExtValue(), FirstArg.getZExtValue()));
535}
536
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000537static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
538 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000539 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000540 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000541 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000542 std::string("0"));
543 return;
544 }
545
546 TypeDecl *decl = dyn_cast<TypeDecl>(d);
547
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000548 if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
549 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
Chris Lattner6b6b5372008-06-26 18:38:35 +0000550 "transparent_union", "union");
551 return;
552 }
553
554 //QualType QTy = Context.getTypeDeclType(decl);
555 //const RecordType *Ty = QTy->getAsUnionType();
556
557// FIXME
558// Ty->addAttr(new TransparentUnionAttr());
559}
560
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000561static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000562 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000563 if (Attr.getNumArgs() != 1) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000564 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
565 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000566 return;
567 }
Chris Lattner545dd342008-06-28 23:36:30 +0000568 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000569 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
570
571 // Make sure that there is a string literal as the annotation's single
572 // argument.
573 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000574 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000575 return;
576 }
577 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
578 SE->getByteLength())));
579}
580
Chris Lattner803d0802008-06-29 00:43:07 +0000581static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000582 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000583 if (Attr.getNumArgs() > 1) {
Chris Lattner803d0802008-06-29 00:43:07 +0000584 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
585 std::string("1"));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000586 return;
587 }
588
589 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +0000590 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000591 // FIXME: This should be the target specific maximum alignment.
592 // (For now we just use 128 bits which is the maximum on X86.
593 Align = 128;
594 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000595 }
Chris Lattner49e2d342008-06-28 23:50:44 +0000596
597 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
598 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000599 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int,
601 "aligned", alignmentExpr->getSourceRange());
Chris Lattner49e2d342008-06-28 23:50:44 +0000602 return;
603 }
604 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000605}
Chris Lattnerfbf13472008-06-27 22:18:37 +0000606
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000607/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +0000608/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +0000609///
610/// Despite what would be logical, the mode attribute is a decl attribute,
611/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
612/// 'G' be HImode, not an intermediate pointer.
613///
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000614static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +0000615 // This attribute isn't documented, but glibc uses it. It changes
616 // the width of an int or unsigned int to the specified size.
617
618 // Check that there aren't any arguments
619 if (Attr.getNumArgs() != 0) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
621 std::string("0"));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000622 return;
623 }
624
625 IdentifierInfo *Name = Attr.getParameterName();
626 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000627 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000628 return;
629 }
630 const char *Str = Name->getName();
631 unsigned Len = Name->getLength();
632
633 // Normalize the attribute name, __foo__ becomes foo.
634 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
635 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
636 Str += 2;
637 Len -= 4;
638 }
639
640 unsigned DestWidth = 0;
641 bool IntegerMode = true;
642 switch (Len) {
643 case 2:
644 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
645 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
646 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
647 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
648 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
649 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
650 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
651 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
652 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
653 break;
654 case 4:
655 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
656 // pointer on PIC16 and other embedded platforms.
657 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000658 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000659 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000660 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +0000661 break;
662 case 7:
663 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000664 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000665 break;
666 }
667
668 QualType OldTy;
669 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
670 OldTy = TD->getUnderlyingType();
671 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
672 OldTy = VD->getType();
673 else {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000674 S.Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode",
675 SourceRange(Attr.getLoc(), Attr.getLoc()));
Chris Lattnerfbf13472008-06-27 22:18:37 +0000676 return;
677 }
678
679 // FIXME: Need proper fixed-width types
680 QualType NewTy;
681 switch (DestWidth) {
682 case 0:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000683 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000684 return;
685 default:
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000686 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName());
Chris Lattnerfbf13472008-06-27 22:18:37 +0000687 return;
688 case 8:
689 assert(IntegerMode);
690 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000691 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000692 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000693 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000694 break;
695 case 16:
696 assert(IntegerMode);
697 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000698 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000699 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000700 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000701 break;
702 case 32:
703 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000704 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000705 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000706 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000707 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000708 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000709 break;
710 case 64:
711 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000712 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000713 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000714 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000715 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000716 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +0000717 break;
718 }
719
720 if (!OldTy->getAsBuiltinType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000721 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000722 else if (!(IntegerMode && OldTy->isIntegerType()) &&
723 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +0000724 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerfbf13472008-06-27 22:18:37 +0000725 }
726
727 // Install the new type.
728 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
729 TD->setUnderlyingType(NewTy);
730 else
731 cast<ValueDecl>(D)->setType(NewTy);
732}
Chris Lattner0744e5f2008-06-29 00:23:49 +0000733
734//===----------------------------------------------------------------------===//
735// Top Level Sema Entry Points
736//===----------------------------------------------------------------------===//
737
Chris Lattner803d0802008-06-29 00:43:07 +0000738/// HandleDeclAttribute - Apply the specific attribute to the specified decl if
739/// the attribute applies to decls. If the attribute is a type attribute, just
740/// silently ignore it.
741static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
742 switch (Attr.getKind()) {
743 case AttributeList::AT_address_space:
744 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
745 break;
746 case AttributeList::AT_ext_vector_type:
747 HandleExtVectorTypeAttr(D, Attr, S);
748 break;
749 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
750 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
751 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
752 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
753 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
754 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
755 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
756 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
757 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
758 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
759 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
760 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
761 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
762 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
763 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
764 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Ted Kremenek96329d42008-07-15 22:26:48 +0000765 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +0000766 case AttributeList::AT_transparent_union:
767 HandleTransparentUnionAttr(D, Attr, S);
768 break;
769 default:
770#if 0
771 // TODO: when we have the full set of attributes, warn about unknown ones.
772 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored,
773 Attr->getName()->getName());
774#endif
775 break;
776 }
777}
778
779/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
780/// attribute list to the specified decl, ignoring any type attributes.
781void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
782 while (AttrList) {
783 ProcessDeclAttribute(D, *AttrList, *this);
784 AttrList = AttrList->getNext();
785 }
786}
787
788
Chris Lattner0744e5f2008-06-29 00:23:49 +0000789/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
790/// it, apply them to D. This is a bit tricky because PD can have attributes
791/// specified in many different places, and we need to find and apply them all.
792void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
793 // Apply decl attributes from the DeclSpec if present.
794 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
795 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +0000796
Chris Lattner0744e5f2008-06-29 00:23:49 +0000797 // Walk the declarator structure, applying decl attributes that were in a type
798 // position to the decl itself. This handles cases like:
799 // int *__attr__(x)** D;
800 // when X is a decl attribute.
801 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
802 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
803 ProcessDeclAttributeList(D, Attrs);
804
805 // Finally, apply any attributes on the decl itself.
806 if (const AttributeList *Attrs = PD.getAttributes())
807 ProcessDeclAttributeList(D, Attrs);
808}
809