blob: 81283a85606b196b5e7bb53f0452fa1a37fca587 [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"
Daniel Dunbare0ad2152008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerdc789562008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Ted Kremenek26151d12008-07-22 16:56:21 +000021#include <llvm/ADT/StringExtras.h>
Chris Lattner6953a072008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner2024f0a2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Daniel Dunbar0ea20472008-10-19 02:04:16 +000028static const FunctionType *getFunctionType(Decl *d) {
Chris Lattner6953a072008-06-26 18:38:35 +000029 QualType Ty;
Chris Lattner6953a072008-06-26 18:38:35 +000030 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
31 Ty = decl->getType();
32 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
33 Ty = decl->getType();
34 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
35 Ty = decl->getUnderlyingType();
36 else
37 return 0;
38
39 if (Ty->isFunctionPointerType())
40 Ty = Ty->getAsPointerType()->getPointeeType();
Daniel Dunbar0ea20472008-10-19 02:04:16 +000041
42 return Ty->getAsFunctionType();
Chris Lattner6953a072008-06-26 18:38:35 +000043}
44
Daniel Dunbard1d847c2008-09-26 04:12:28 +000045// FIXME: We should provide an abstraction around a method or function
46// to provide the following bits of information.
47
Daniel Dunbar0ea20472008-10-19 02:04:16 +000048/// isFunctionOrMethod - Return true if the given decl has function
49/// type (function or function-typed variable) or an Objective-C
50/// method.
Daniel Dunbard1d847c2008-09-26 04:12:28 +000051static bool isFunctionOrMethod(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000052 return getFunctionType(d) || isa<ObjCMethodDecl>(d);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000053}
54
Daniel Dunbar0ea20472008-10-19 02:04:16 +000055/// hasFunctionProto - Return true if the given decl has a argument
56/// information. This decl should have already passed
57/// isFunctionOrMethod.
58static bool hasFunctionProto(Decl *d) {
59 if (const FunctionType *FnTy = getFunctionType(d)) {
60 return isa<FunctionTypeProto>(FnTy);
61 } else {
62 assert(isa<ObjCMethodDecl>(d));
63 return true;
64 }
65}
66
67/// getFunctionOrMethodNumArgs - Return number of function or method
68/// arguments. It is an error to call this on a K&R function (use
69/// hasFunctionProto first).
Daniel Dunbard1d847c2008-09-26 04:12:28 +000070static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000071 if (const FunctionType *FnTy = getFunctionType(d)) {
72 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000073 return proto->getNumArgs();
74 } else {
75 return cast<ObjCMethodDecl>(d)->getNumParams();
76 }
77}
78
79static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000080 if (const FunctionType *FnTy = getFunctionType(d)) {
81 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000082 return proto->getArgType(Idx);
83 } else {
84 return cast<ObjCMethodDecl>(d)->getParamDecl(Idx)->getType();
85 }
86}
87
88static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbar0ea20472008-10-19 02:04:16 +000089 if (const FunctionType *FnTy = getFunctionType(d)) {
90 const FunctionTypeProto *proto = cast<FunctionTypeProto>(FnTy);
Daniel Dunbard1d847c2008-09-26 04:12:28 +000091 return proto->isVariadic();
92 } else {
93 return cast<ObjCMethodDecl>(d)->isVariadic();
94 }
95}
96
Chris Lattner6953a072008-06-26 18:38:35 +000097static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000098 const PointerType *PT = T->getAsPointerType();
99 if (!PT)
Chris Lattner6953a072008-06-26 18:38:35 +0000100 return false;
101
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000102 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6953a072008-06-26 18:38:35 +0000103 if (!ClsT)
104 return false;
105
106 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
107
108 // FIXME: Should we walk the chain of classes?
109 return ClsName == &Ctx.Idents.get("NSString") ||
110 ClsName == &Ctx.Idents.get("NSMutableString");
111}
112
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000113static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
114 const PointerType *PT = T->getAsPointerType();
115 if (!PT)
116 return false;
117
118 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
119 if (!RT)
120 return false;
121
122 const RecordDecl *RD = RT->getDecl();
123 if (RD->getTagKind() != TagDecl::TK_struct)
124 return false;
125
126 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
127}
128
Chris Lattner2024f0a2008-06-29 00:16:31 +0000129//===----------------------------------------------------------------------===//
Chris Lattner2024f0a2008-06-29 00:16:31 +0000130// Attribute Implementations
131//===----------------------------------------------------------------------===//
132
Daniel Dunbar8716a012008-07-31 22:40:48 +0000133// FIXME: All this manual attribute parsing code is gross. At the
134// least add some helper functions to check most argument patterns (#
135// and types of args).
136
Chris Lattner703c52d2008-06-29 00:43:07 +0000137static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
138 Sema &S) {
Chris Lattner1c151132008-06-28 23:36:30 +0000139 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
140 if (tDecl == 0) {
Chris Lattner703c52d2008-06-29 00:43:07 +0000141 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner1c151132008-06-28 23:36:30 +0000142 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000143 }
144
Chris Lattner6953a072008-06-26 18:38:35 +0000145 QualType curType = tDecl->getUnderlyingType();
146 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000147 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000149 return;
150 }
Chris Lattner1c151132008-06-28 23:36:30 +0000151 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000152 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000153 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000154 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
155 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000156 return;
157 }
158 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
159 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000160 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000161 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6953a072008-06-26 18:38:35 +0000162 return;
163 }
164 // unlike gcc's vector_size attribute, the size is specified as the
165 // number of elements, not the number of bytes.
166 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
167
168 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000169 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
170 << sizeExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000171 return;
172 }
173 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner703c52d2008-06-29 00:43:07 +0000174 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6953a072008-06-26 18:38:35 +0000175 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner703c52d2008-06-29 00:43:07 +0000176 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6953a072008-06-26 18:38:35 +0000177}
178
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000179
180/// HandleVectorSizeAttribute - this attribute is only applicable to
181/// integral and float scalars, although arrays, pointers, and function
182/// return values are allowed in conjunction with this construct. Aggregates
183/// with this attribute are invalid, even if they are of the same size as a
184/// corresponding scalar.
185/// The raw attribute should contain precisely 1 argument, the vector size
186/// for the variable, measured in bytes. If curType and rawAttr are well
187/// formed, this routine will return a new vector type.
Chris Lattner703c52d2008-06-29 00:43:07 +0000188static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000189 QualType CurType;
190 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
191 CurType = VD->getType();
192 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
193 CurType = TD->getUnderlyingType();
194 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000195 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
196 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000197 return;
198 }
199
200 // Check the attribute arugments.
Chris Lattner1c151132008-06-28 23:36:30 +0000201 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000203 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000204 }
Chris Lattner1c151132008-06-28 23:36:30 +0000205 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000206 llvm::APSInt vecSize(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000207 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
209 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000210 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000211 }
212 // navigate to the base type - we need to provide for vector pointers,
213 // vector arrays, and functions returning vectors.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000214 if (CurType->isPointerType() || CurType->isArrayType() ||
215 CurType->isFunctionType()) {
Chris Lattner6953a072008-06-26 18:38:35 +0000216 assert(0 && "HandleVector(): Complex type construction unimplemented");
217 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
218 do {
219 if (PointerType *PT = dyn_cast<PointerType>(canonType))
220 canonType = PT->getPointeeType().getTypePtr();
221 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
222 canonType = AT->getElementType().getTypePtr();
223 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
224 canonType = FT->getResultType().getTypePtr();
225 } while (canonType->isPointerType() || canonType->isArrayType() ||
226 canonType->isFunctionType());
227 */
228 }
229 // the base type must be integer or float.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000230 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000232 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000233 }
Chris Lattner703c52d2008-06-29 00:43:07 +0000234 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6953a072008-06-26 18:38:35 +0000235 // vecSize is specified in bytes - convert to bits.
236 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
237
238 // the vector size needs to be an integral multiple of the type size.
239 if (vectorSize % typeSize) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
241 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000242 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000243 }
244 if (vectorSize == 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000245 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
246 << sizeExpr->getSourceRange();
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000247 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000248 }
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000249
250 // Success! Instantiate the vector type, the number of elements is > 0, and
251 // not required to be a power of 2, unlike GCC.
Chris Lattner703c52d2008-06-29 00:43:07 +0000252 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner8ed14aa2008-06-28 23:48:25 +0000253
254 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
255 VD->setType(CurType);
256 else
257 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6953a072008-06-26 18:38:35 +0000258}
259
Chris Lattner703c52d2008-06-29 00:43:07 +0000260static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000261 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000262 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000264 return;
265 }
266
267 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000268 TD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000269 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
270 // If the alignment is less than or equal to 8 bits, the packed attribute
271 // has no effect.
272 if (!FD->getType()->isIncompleteType() &&
Chris Lattner703c52d2008-06-29 00:43:07 +0000273 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000274 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000275 << Attr.getName() << FD->getType();
Chris Lattner6953a072008-06-26 18:38:35 +0000276 else
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000277 FD->addAttr(new PackedAttr(1));
Chris Lattner6953a072008-06-26 18:38:35 +0000278 } else
Chris Lattner65cae292008-11-19 08:23:25 +0000279 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000280}
281
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000282static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
283 // check the attribute arguments.
284 if (Attr.getNumArgs() > 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000285 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek4e5bb122008-07-15 22:26:48 +0000286 return;
287 }
288
289 // The IBOutlet attribute only applies to instance variables of Objective-C
290 // classes.
291 if (ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(d))
292 ID->addAttr(new IBOutletAttr());
293 else
294 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet_non_ivar);
295}
296
Ted Kremenekc0af2542008-07-21 21:53:04 +0000297static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000298 // GCC ignores the nonnull attribute on K&R style function
299 // prototypes, so we ignore it as well
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000300 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000301 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
302 << "nonnull" << "function";
Ted Kremenekc0af2542008-07-21 21:53:04 +0000303 return;
304 }
305
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000306 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000307
308 // The nonnull attribute only applies to pointers.
309 llvm::SmallVector<unsigned, 10> NonNullArgs;
310
311 for (AttributeList::arg_iterator I=Attr.arg_begin(),
312 E=Attr.arg_end(); I!=E; ++I) {
313
314
315 // The argument must be an integer constant expression.
Ted Kremenek73dc0652008-12-04 19:38:33 +0000316 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekc0af2542008-07-21 21:53:04 +0000317 llvm::APSInt ArgNum(32);
318 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
320 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000321 return;
322 }
323
324 unsigned x = (unsigned) ArgNum.getZExtValue();
325
326 if (x < 1 || x > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner68f621c2008-11-19 07:22:31 +0000328 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekc0af2542008-07-21 21:53:04 +0000329 return;
330 }
Ted Kremenek178cee22008-07-21 22:09:15 +0000331
332 --x;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000333
334 // Is the function argument a pointer type?
Ted Kremenekba57bd92008-11-18 06:52:58 +0000335 QualType T = getFunctionOrMethodArgType(d, x);
336 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekc0af2542008-07-21 21:53:04 +0000337 // FIXME: Should also highlight argument in decl.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000338 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
339 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7af441b2008-09-01 19:57:52 +0000340 continue;
Ted Kremenekc0af2542008-07-21 21:53:04 +0000341 }
342
343 NonNullArgs.push_back(x);
344 }
345
Ted Kremenek7af441b2008-09-01 19:57:52 +0000346 // If no arguments were specified to __attribute__((nonnull)) then all
347 // pointer arguments have a nonnull attribute.
348 if (NonNullArgs.empty()) {
Ted Kremenekba57bd92008-11-18 06:52:58 +0000349 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
350 QualType T = getFunctionOrMethodArgType(d, I);
351 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000352 NonNullArgs.push_back(I);
Ted Kremenekba57bd92008-11-18 06:52:58 +0000353 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000354
355 if (NonNullArgs.empty()) {
356 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
357 return;
358 }
Ted Kremenekc0af2542008-07-21 21:53:04 +0000359 }
Ted Kremenek7af441b2008-09-01 19:57:52 +0000360
361 unsigned* start = &NonNullArgs[0];
362 unsigned size = NonNullArgs.size();
363 std::sort(start, start + size);
364 d->addAttr(new NonNullAttr(start, size));
Ted Kremenekc0af2542008-07-21 21:53:04 +0000365}
366
Chris Lattner703c52d2008-06-29 00:43:07 +0000367static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000368 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000369 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000371 return;
372 }
373
Chris Lattner1c151132008-06-28 23:36:30 +0000374 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000375 Arg = Arg->IgnoreParenCasts();
376 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
377
378 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000379 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000380 << "alias" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000381 return;
382 }
383
384 const char *Alias = Str->getStrData();
385 unsigned AliasLen = Str->getByteLength();
386
387 // FIXME: check if target symbol exists in current file
388
389 d->addAttr(new AliasAttr(std::string(Alias, AliasLen)));
390}
391
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000392static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
393 Sema &S) {
394 // check the attribute arguments.
395 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000396 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar0a2da712008-10-28 00:17:57 +0000397 return;
398 }
399
400 d->addAttr(new AlwaysInlineAttr());
401}
402
Chris Lattner703c52d2008-06-29 00:43:07 +0000403static void HandleNoReturnAttr(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 Lattner65cae292008-11-19 08:23:25 +0000406 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000407 return;
408 }
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000409
410 if (!isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
412 << "noreturn" << "function";
Chris Lattner6953a072008-06-26 18:38:35 +0000413 return;
414 }
415
416 d->addAttr(new NoReturnAttr());
417}
418
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000419static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
420 // check the attribute arguments.
421 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000423 return;
424 }
425
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000426 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
428 << "unused" << "variable and function";
Ted Kremenekce13e1e2008-07-25 04:39:19 +0000429 return;
430 }
431
432 d->addAttr(new UnusedAttr());
433}
434
Daniel Dunbar8716a012008-07-31 22:40:48 +0000435static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
439 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000440 return;
441 }
442
443 int priority = 65535; // FIXME: Do not hardcode such constants.
444 if (Attr.getNumArgs() > 0) {
445 Expr *E = static_cast<Expr *>(Attr.getArg(0));
446 llvm::APSInt Idx(32);
447 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000448 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000449 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000450 return;
451 }
452 priority = Idx.getZExtValue();
453 }
454
455 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
456 if (!Fn) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000457 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
458 << "constructor" << "function";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000459 return;
460 }
461
462 d->addAttr(new ConstructorAttr(priority));
463}
464
465static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
466 // check the attribute arguments.
467 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
469 << "0 or 1";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000470 return;
471 }
472
473 int priority = 65535; // FIXME: Do not hardcode such constants.
474 if (Attr.getNumArgs() > 0) {
475 Expr *E = static_cast<Expr *>(Attr.getArg(0));
476 llvm::APSInt Idx(32);
477 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000478 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000479 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar8716a012008-07-31 22:40:48 +0000480 return;
481 }
482 priority = Idx.getZExtValue();
483 }
484
Anders Carlsson03e49652008-08-22 22:10:48 +0000485 if (!isa<FunctionDecl>(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000486 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
487 << "destructor" << "function";
Daniel Dunbar8716a012008-07-31 22:40:48 +0000488 return;
489 }
490
491 d->addAttr(new DestructorAttr(priority));
492}
493
Chris Lattner703c52d2008-06-29 00:43:07 +0000494static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000495 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000496 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000497 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000498 return;
499 }
500
501 d->addAttr(new DeprecatedAttr());
502}
503
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000504static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
505 // check the attribute arguments.
506 if (Attr.getNumArgs() != 0) {
507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
508 return;
509 }
510
511 d->addAttr(new UnavailableAttr());
512}
513
Chris Lattner703c52d2008-06-29 00:43:07 +0000514static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000515 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000516 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000518 return;
519 }
520
Chris Lattner1c151132008-06-28 23:36:30 +0000521 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000522 Arg = Arg->IgnoreParenCasts();
523 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
524
525 if (Str == 0 || Str->isWide()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000527 << "visibility" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000528 return;
529 }
530
531 const char *TypeStr = Str->getStrData();
532 unsigned TypeLen = Str->getByteLength();
533 VisibilityAttr::VisibilityTypes type;
534
535 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
536 type = VisibilityAttr::DefaultVisibility;
537 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
538 type = VisibilityAttr::HiddenVisibility;
539 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
540 type = VisibilityAttr::HiddenVisibility; // FIXME
541 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
542 type = VisibilityAttr::ProtectedVisibility;
543 else {
Chris Lattnerb1753422008-11-23 21:45:46 +0000544 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6953a072008-06-26 18:38:35 +0000545 return;
546 }
547
548 d->addAttr(new VisibilityAttr(type));
549}
550
Anders Carlssond83141a52008-08-23 23:22:21 +0000551static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssone0f00b32008-08-24 16:33:25 +0000552 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000553 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000554 << "objc_gc" << 1;
Anders Carlssond83141a52008-08-23 23:22:21 +0000555 return;
556 }
557
558 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Anders Carlssond83141a52008-08-23 23:22:21 +0000560 return;
561 }
562
Anders Carlssond83141a52008-08-23 23:22:21 +0000563
564 ObjCGCAttr::GCAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000565 if (Attr.getParameterName()->isStr("weak")) {
Fariborz Jahanian7563a322008-11-20 19:35:51 +0000566 if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d))
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000567 S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
Anders Carlssond83141a52008-08-23 23:22:21 +0000568 type = ObjCGCAttr::Weak;
Fariborz Jahanianf0ca65f2008-11-20 00:15:42 +0000569 }
Chris Lattner05fb7c82008-11-20 04:42:34 +0000570 else if (Attr.getParameterName()->isStr("strong"))
Anders Carlssond83141a52008-08-23 23:22:21 +0000571 type = ObjCGCAttr::Strong;
572 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000573 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000574 << "objc_gc" << Attr.getParameterName();
Anders Carlssond83141a52008-08-23 23:22:21 +0000575 return;
576 }
577
578 d->addAttr(new ObjCGCAttr(type));
579}
580
Steve Naroffe1cecba2008-09-18 16:44:58 +0000581static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
582 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000584 << "blocks" << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000585 return;
586 }
587
588 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroffe1cecba2008-09-18 16:44:58 +0000590 return;
591 }
Steve Naroffe1cecba2008-09-18 16:44:58 +0000592
593 BlocksAttr::BlocksAttrTypes type;
Chris Lattner05fb7c82008-11-20 04:42:34 +0000594 if (Attr.getParameterName()->isStr("byref"))
Steve Naroffe1cecba2008-09-18 16:44:58 +0000595 type = BlocksAttr::ByRef;
596 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000597 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner65cae292008-11-19 08:23:25 +0000598 << "blocks" << Attr.getParameterName();
Steve Naroffe1cecba2008-09-18 16:44:58 +0000599 return;
600 }
601
602 d->addAttr(new BlocksAttr(type));
603}
604
Anders Carlsson244d99b2008-10-05 18:05:59 +0000605static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
606 // check the attribute arguments.
607 if (Attr.getNumArgs() > 2) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000608 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
609 << "0, 1 or 2";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000610 return;
611 }
612
613 int sentinel = 0;
614 if (Attr.getNumArgs() > 0) {
615 Expr *E = static_cast<Expr *>(Attr.getArg(0));
616 llvm::APSInt Idx(32);
617 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000619 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000620 return;
621 }
622 sentinel = Idx.getZExtValue();
623
624 if (sentinel < 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000625 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
626 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000627 return;
628 }
629 }
630
631 int nullPos = 0;
632 if (Attr.getNumArgs() > 1) {
633 Expr *E = static_cast<Expr *>(Attr.getArg(1));
634 llvm::APSInt Idx(32);
635 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000637 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000638 return;
639 }
640 nullPos = Idx.getZExtValue();
641
642 if (nullPos > 1 || nullPos < 0) {
643 // FIXME: This error message could be improved, it would be nice
644 // to say what the bounds actually are.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000645 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
646 << E->getSourceRange();
Anders Carlsson244d99b2008-10-05 18:05:59 +0000647 return;
648 }
649 }
650
651 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
652 QualType FT = FD->getType();
653 if (!FT->getAsFunctionTypeProto()->isVariadic()) {
654 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
655 return;
656 }
657 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
658 if (!MD->isVariadic()) {
659 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic);
660 return;
661 }
662 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
664 << "sentinel" << "function or method";
Anders Carlsson244d99b2008-10-05 18:05:59 +0000665 return;
666 }
667
668 // FIXME: Actually create the attribute.
669}
670
Chris Lattner703c52d2008-06-29 00:43:07 +0000671static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000672 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000673 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000674 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000675 return;
676 }
677
678 d->addAttr(new WeakAttr());
679}
680
Chris Lattner703c52d2008-06-29 00:43:07 +0000681static void HandleDLLImportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000682 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000683 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000685 return;
686 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000687
Chris Lattner6953a072008-06-26 18:38:35 +0000688 d->addAttr(new DLLImportAttr());
689}
690
Chris Lattner703c52d2008-06-29 00:43:07 +0000691static void HandleDLLExportAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000692 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000693 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000695 return;
696 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000697
Chris Lattner6953a072008-06-26 18:38:35 +0000698 d->addAttr(new DLLExportAttr());
699}
700
Chris Lattner703c52d2008-06-29 00:43:07 +0000701static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000702 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000703 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000704 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000705 return;
706 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000707
708 // Attribute can be applied only to functions.
709 if (!isa<FunctionDecl>(d)) {
710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
711 << "stdcall" << "function";
712 return;
713 }
714
715 // stdcall and fastcall attributes are mutually incompatible.
716 if (d->getAttr<FastCallAttr>()) {
717 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
718 << "stdcall" << "fastcall";
719 return;
720 }
721
Chris Lattner6953a072008-06-26 18:38:35 +0000722 d->addAttr(new StdCallAttr());
723}
724
Chris Lattner703c52d2008-06-29 00:43:07 +0000725static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov79626942008-12-23 22:24:07 +0000726 // Attribute has no arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000727 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000729 return;
730 }
Anton Korobeynikov79626942008-12-23 22:24:07 +0000731
732 if (!isa<FunctionDecl>(d)) {
733 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
734 << "fastcall" << "function";
735 return;
736 }
737
738 // stdcall and fastcall attributes are mutually incompatible.
739 if (d->getAttr<StdCallAttr>()) {
740 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
741 << "fastcall" << "stdcall";
742 return;
743 }
744
Chris Lattner6953a072008-06-26 18:38:35 +0000745 d->addAttr(new FastCallAttr());
746}
747
Chris Lattner703c52d2008-06-29 00:43:07 +0000748static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000749 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000750 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000752 return;
753 }
754
755 d->addAttr(new NoThrowAttr());
756}
757
Anders Carlssondd6791c2008-10-05 23:32:53 +0000758static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
759 // check the attribute arguments.
760 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000761 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000762 return;
763 }
764
765 d->addAttr(new ConstAttr());
766}
767
768static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
769 // check the attribute arguments.
770 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000772 return;
773 }
774
775 d->addAttr(new PureAttr());
776}
777
Chris Lattner6953a072008-06-26 18:38:35 +0000778/// Handle __attribute__((format(type,idx,firstarg))) attributes
779/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner703c52d2008-06-29 00:43:07 +0000780static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000781
Chris Lattner1c151132008-06-28 23:36:30 +0000782 if (!Attr.getParameterName()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000783 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner65cae292008-11-19 08:23:25 +0000784 << "format" << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000785 return;
786 }
787
Chris Lattner1c151132008-06-28 23:36:30 +0000788 if (Attr.getNumArgs() != 2) {
Chris Lattner65cae292008-11-19 08:23:25 +0000789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6953a072008-06-26 18:38:35 +0000790 return;
791 }
792
Daniel Dunbar0ea20472008-10-19 02:04:16 +0000793 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
795 << "format" << "function";
Chris Lattner6953a072008-06-26 18:38:35 +0000796 return;
797 }
798
799 // FIXME: in C++ the implicit 'this' function parameter also counts.
800 // this is needed in order to be compatible with GCC
801 // the index must start in 1 and the limit is numargs+1
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000802 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6953a072008-06-26 18:38:35 +0000803 unsigned FirstIdx = 1;
804
Chris Lattner1c151132008-06-28 23:36:30 +0000805 const char *Format = Attr.getParameterName()->getName();
806 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6953a072008-06-26 18:38:35 +0000807
808 // Normalize the argument, __foo__ becomes foo.
809 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
810 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
811 Format += 2;
812 FormatLen -= 4;
813 }
814
815 bool Supported = false;
816 bool is_NSString = false;
817 bool is_strftime = false;
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000818 bool is_CFString = false;
Chris Lattner6953a072008-06-26 18:38:35 +0000819
820 switch (FormatLen) {
821 default: break;
Chris Lattner703c52d2008-06-29 00:43:07 +0000822 case 5: Supported = !memcmp(Format, "scanf", 5); break;
823 case 6: Supported = !memcmp(Format, "printf", 6); break;
824 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6953a072008-06-26 18:38:35 +0000825 case 8:
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000826 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
827 (is_NSString = !memcmp(Format, "NSString", 8)) ||
828 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6953a072008-06-26 18:38:35 +0000829 break;
830 }
831
832 if (!Supported) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000833 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
834 << "format" << Attr.getParameterName()->getName();
Chris Lattner6953a072008-06-26 18:38:35 +0000835 return;
836 }
837
838 // checks for the 2nd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000839 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner703c52d2008-06-29 00:43:07 +0000840 llvm::APSInt Idx(32);
841 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000842 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000843 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000844 return;
845 }
846
847 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000848 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +0000849 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000850 return;
851 }
852
853 // FIXME: Do we need to bounds check?
854 unsigned ArgIdx = Idx.getZExtValue() - 1;
855
856 // make sure the format string is really a string
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000857 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6953a072008-06-26 18:38:35 +0000858
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000859 if (is_CFString) {
860 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000861 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
862 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbarbddb14a2008-09-26 03:32:58 +0000863 return;
864 }
865 } else if (is_NSString) {
Chris Lattner6953a072008-06-26 18:38:35 +0000866 // FIXME: do we need to check if the type is NSString*? What are
867 // the semantics?
Chris Lattner703c52d2008-06-29 00:43:07 +0000868 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner6953a072008-06-26 18:38:35 +0000869 // FIXME: Should highlight the actual expression that has the
870 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000871 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
872 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000873 return;
874 }
875 } else if (!Ty->isPointerType() ||
876 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
877 // FIXME: Should highlight the actual expression that has the
878 // wrong type.
Chris Lattner8ba580c2008-11-19 05:08:23 +0000879 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
880 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000881 return;
882 }
883
884 // check the 3rd argument
Chris Lattner1c151132008-06-28 23:36:30 +0000885 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner703c52d2008-06-29 00:43:07 +0000886 llvm::APSInt FirstArg(32);
887 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000888 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner65cae292008-11-19 08:23:25 +0000889 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000890 return;
891 }
892
893 // check if the function is variadic if the 3rd argument non-zero
894 if (FirstArg != 0) {
Daniel Dunbard1d847c2008-09-26 04:12:28 +0000895 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6953a072008-06-26 18:38:35 +0000896 ++NumArgs; // +1 for ...
897 } else {
Chris Lattner703c52d2008-06-29 00:43:07 +0000898 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6953a072008-06-26 18:38:35 +0000899 return;
900 }
901 }
902
Chris Lattner65cae292008-11-19 08:23:25 +0000903 // strftime requires FirstArg to be 0 because it doesn't read from any
904 // variable the input is just the current time + the format string.
Chris Lattner6953a072008-06-26 18:38:35 +0000905 if (is_strftime) {
906 if (FirstArg != 0) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000907 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
908 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000909 return;
910 }
911 // if 0 it disables parameter checking (to use with e.g. va_list)
912 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000913 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner65cae292008-11-19 08:23:25 +0000914 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6953a072008-06-26 18:38:35 +0000915 return;
916 }
917
918 d->addAttr(new FormatAttr(std::string(Format, FormatLen),
919 Idx.getZExtValue(), FirstArg.getZExtValue()));
920}
921
Chris Lattnerf6690152008-06-29 00:28:59 +0000922static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
923 Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000924 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000925 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6953a072008-06-26 18:38:35 +0000927 return;
928 }
929
Eli Friedman283b3622008-09-02 05:19:23 +0000930 // FIXME: This shouldn't be restricted to typedefs
931 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
932 if (!TD || !TD->getUnderlyingType()->isUnionType()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000933 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
934 << "transparent_union" << "union";
Chris Lattner6953a072008-06-26 18:38:35 +0000935 return;
936 }
937
Eli Friedman283b3622008-09-02 05:19:23 +0000938 RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
Chris Lattner6953a072008-06-26 18:38:35 +0000939
Eli Friedman283b3622008-09-02 05:19:23 +0000940 // FIXME: Should we do a check for RD->isDefinition()?
941
942 // FIXME: This isn't supposed to be restricted to pointers, but otherwise
943 // we might silently generate incorrect code; see following code
Douglas Gregor8acb7272008-12-11 16:49:14 +0000944 for (RecordDecl::field_iterator Field = RD->field_begin(),
945 FieldEnd = RD->field_end();
946 Field != FieldEnd; ++Field) {
947 if (!Field->getType()->isPointerType()) {
Eli Friedman283b3622008-09-02 05:19:23 +0000948 S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
949 return;
950 }
951 }
952
953 // FIXME: This is a complete hack; we should be properly propagating
954 // transparent_union through Sema. That said, this is close enough to
955 // correctly compile all the common cases of transparent_union without
956 // errors or warnings
957 QualType NewTy = S.Context.VoidPtrTy;
958 NewTy.addConst();
959 TD->setUnderlyingType(NewTy);
Chris Lattner6953a072008-06-26 18:38:35 +0000960}
961
Chris Lattnerf6690152008-06-29 00:28:59 +0000962static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000963 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000964 if (Attr.getNumArgs() != 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000966 return;
967 }
Chris Lattner1c151132008-06-28 23:36:30 +0000968 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6953a072008-06-26 18:38:35 +0000969 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
970
971 // Make sure that there is a string literal as the annotation's single
972 // argument.
973 if (!SE) {
Chris Lattnerf6690152008-06-29 00:28:59 +0000974 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6953a072008-06-26 18:38:35 +0000975 return;
976 }
977 d->addAttr(new AnnotateAttr(std::string(SE->getStrData(),
978 SE->getByteLength())));
979}
980
Chris Lattner703c52d2008-06-29 00:43:07 +0000981static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6953a072008-06-26 18:38:35 +0000982 // check the attribute arguments.
Chris Lattner1c151132008-06-28 23:36:30 +0000983 if (Attr.getNumArgs() > 1) {
Chris Lattner65cae292008-11-19 08:23:25 +0000984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6953a072008-06-26 18:38:35 +0000985 return;
986 }
987
988 unsigned Align = 0;
Chris Lattner1c151132008-06-28 23:36:30 +0000989 if (Attr.getNumArgs() == 0) {
Chris Lattner6953a072008-06-26 18:38:35 +0000990 // FIXME: This should be the target specific maximum alignment.
991 // (For now we just use 128 bits which is the maximum on X86.
992 Align = 128;
993 return;
Chris Lattner6953a072008-06-26 18:38:35 +0000994 }
Chris Lattnerb0011ca2008-06-28 23:50:44 +0000995
996 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
997 llvm::APSInt Alignment(32);
Chris Lattner703c52d2008-06-29 00:43:07 +0000998 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1000 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattnerb0011ca2008-06-28 23:50:44 +00001001 return;
1002 }
1003 d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6953a072008-06-26 18:38:35 +00001004}
Chris Lattnerdc789562008-06-27 22:18:37 +00001005
Chris Lattnerf6690152008-06-29 00:28:59 +00001006/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner8ed14aa2008-06-28 23:48:25 +00001007/// primitive type.
Chris Lattnerdc789562008-06-27 22:18:37 +00001008///
1009/// Despite what would be logical, the mode attribute is a decl attribute,
1010/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1011/// 'G' be HImode, not an intermediate pointer.
1012///
Chris Lattnerf6690152008-06-29 00:28:59 +00001013static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerdc789562008-06-27 22:18:37 +00001014 // This attribute isn't documented, but glibc uses it. It changes
1015 // the width of an int or unsigned int to the specified size.
1016
1017 // Check that there aren't any arguments
1018 if (Attr.getNumArgs() != 0) {
Chris Lattner65cae292008-11-19 08:23:25 +00001019 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerdc789562008-06-27 22:18:37 +00001020 return;
1021 }
1022
1023 IdentifierInfo *Name = Attr.getParameterName();
1024 if (!Name) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001025 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerdc789562008-06-27 22:18:37 +00001026 return;
1027 }
1028 const char *Str = Name->getName();
1029 unsigned Len = Name->getLength();
1030
1031 // Normalize the attribute name, __foo__ becomes foo.
1032 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1033 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1034 Str += 2;
1035 Len -= 4;
1036 }
1037
1038 unsigned DestWidth = 0;
1039 bool IntegerMode = true;
1040 switch (Len) {
1041 case 2:
1042 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
1043 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
1044 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
1045 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
1046 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
1047 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
1048 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
1049 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
1050 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
1051 break;
1052 case 4:
1053 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1054 // pointer on PIC16 and other embedded platforms.
1055 if (!memcmp(Str, "word", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001056 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001057 if (!memcmp(Str, "byte", 4))
Chris Lattnerf6690152008-06-29 00:28:59 +00001058 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerdc789562008-06-27 22:18:37 +00001059 break;
1060 case 7:
1061 if (!memcmp(Str, "pointer", 7))
Chris Lattnerf6690152008-06-29 00:28:59 +00001062 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerdc789562008-06-27 22:18:37 +00001063 break;
1064 }
1065
1066 QualType OldTy;
1067 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1068 OldTy = TD->getUnderlyingType();
1069 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1070 OldTy = VD->getType();
1071 else {
Chris Lattner8ba580c2008-11-19 05:08:23 +00001072 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1073 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerdc789562008-06-27 22:18:37 +00001074 return;
1075 }
1076
1077 // FIXME: Need proper fixed-width types
1078 QualType NewTy;
1079 switch (DestWidth) {
1080 case 0:
Chris Lattner65cae292008-11-19 08:23:25 +00001081 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001082 return;
1083 default:
Chris Lattner65cae292008-11-19 08:23:25 +00001084 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerdc789562008-06-27 22:18:37 +00001085 return;
1086 case 8:
1087 assert(IntegerMode);
1088 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001089 NewTy = S.Context.SignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001090 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001091 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001092 break;
1093 case 16:
1094 assert(IntegerMode);
1095 if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001096 NewTy = S.Context.ShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001097 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001098 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001099 break;
1100 case 32:
1101 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001102 NewTy = S.Context.FloatTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001103 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001104 NewTy = S.Context.IntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001105 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001106 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001107 break;
1108 case 64:
1109 if (!IntegerMode)
Chris Lattnerf6690152008-06-29 00:28:59 +00001110 NewTy = S.Context.DoubleTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001111 else if (OldTy->isSignedIntegerType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001112 NewTy = S.Context.LongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001113 else
Chris Lattnerf6690152008-06-29 00:28:59 +00001114 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerdc789562008-06-27 22:18:37 +00001115 break;
1116 }
1117
1118 if (!OldTy->getAsBuiltinType())
Chris Lattnerf6690152008-06-29 00:28:59 +00001119 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
Chris Lattnerdc789562008-06-27 22:18:37 +00001120 else if (!(IntegerMode && OldTy->isIntegerType()) &&
1121 !(!IntegerMode && OldTy->isFloatingType())) {
Chris Lattnerf6690152008-06-29 00:28:59 +00001122 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
Chris Lattnerdc789562008-06-27 22:18:37 +00001123 }
1124
1125 // Install the new type.
1126 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1127 TD->setUnderlyingType(NewTy);
1128 else
1129 cast<ValueDecl>(D)->setType(NewTy);
1130}
Chris Lattnera72440d2008-06-29 00:23:49 +00001131
1132//===----------------------------------------------------------------------===//
1133// Top Level Sema Entry Points
1134//===----------------------------------------------------------------------===//
1135
Sebastian Redl6e67d9b2008-12-21 19:24:58 +00001136/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner703c52d2008-06-29 00:43:07 +00001137/// the attribute applies to decls. If the attribute is a type attribute, just
1138/// silently ignore it.
1139static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1140 switch (Attr.getKind()) {
Daniel Dunbar8716a012008-07-31 22:40:48 +00001141 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001142 case AttributeList::AT_address_space:
1143 // Ignore this, this is a type attribute, handled by ProcessTypeAttributes.
1144 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001145 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1146 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbar0a2da712008-10-28 00:17:57 +00001147 case AttributeList::AT_always_inline:
1148 HandleAlwaysInlineAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001149 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1150 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1151 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1152 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1153 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1154 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001155 case AttributeList::AT_ext_vector_type:
1156 HandleExtVectorTypeAttr(D, Attr, S);
1157 break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001158 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1159 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001160 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001161 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1162 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1163 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1164 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1165 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +00001166 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar8716a012008-07-31 22:40:48 +00001167 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1168 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001169 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
1170 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001171 case AttributeList::AT_transparent_union:
1172 HandleTransparentUnionAttr(D, Attr, S);
1173 break;
Anders Carlssond83141a52008-08-23 23:22:21 +00001174 case AttributeList::AT_objc_gc: HandleObjCGCAttr (D, Attr, S); break;
Steve Naroffe1cecba2008-09-18 16:44:58 +00001175 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson244d99b2008-10-05 18:05:59 +00001176 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001177 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1178 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Chris Lattner703c52d2008-06-29 00:43:07 +00001179 default:
1180#if 0
1181 // TODO: when we have the full set of attributes, warn about unknown ones.
Chris Lattnerb1753422008-11-23 21:45:46 +00001182 S.Diag(Attr->getLoc(), diag::warn_attribute_ignored) << Attr->getName();
Chris Lattner703c52d2008-06-29 00:43:07 +00001183#endif
1184 break;
1185 }
1186}
1187
1188/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1189/// attribute list to the specified decl, ignoring any type attributes.
1190void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1191 while (AttrList) {
1192 ProcessDeclAttribute(D, *AttrList, *this);
1193 AttrList = AttrList->getNext();
1194 }
1195}
1196
1197
Chris Lattnera72440d2008-06-29 00:23:49 +00001198/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1199/// it, apply them to D. This is a bit tricky because PD can have attributes
1200/// specified in many different places, and we need to find and apply them all.
1201void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1202 // Apply decl attributes from the DeclSpec if present.
1203 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1204 ProcessDeclAttributeList(D, Attrs);
Chris Lattner703c52d2008-06-29 00:43:07 +00001205
Chris Lattnera72440d2008-06-29 00:23:49 +00001206 // Walk the declarator structure, applying decl attributes that were in a type
1207 // position to the decl itself. This handles cases like:
1208 // int *__attr__(x)** D;
1209 // when X is a decl attribute.
1210 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1211 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1212 ProcessDeclAttributeList(D, Attrs);
1213
1214 // Finally, apply any attributes on the decl itself.
1215 if (const AttributeList *Attrs = PD.getAttributes())
1216 ProcessDeclAttributeList(D, Attrs);
1217}
1218