blob: 2f7701227da649f552cf3c49f24de81f6a66e470 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===-- TargetAttributesSema.cpp - Encapsulate target attributes-*- C++ -*-===//
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 contains semantic analysis implementation for target-specific
11// attributes.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
John McCall384aff82010-08-25 07:42:41 +000016#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000019#include "llvm/ADT/Triple.h"
20
21using namespace clang;
22
23TargetAttributesSema::~TargetAttributesSema() {}
24bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D,
25 const AttributeList &Attr, Sema &S) const {
26 return false;
27}
28
29static void HandleMSP430InterruptAttr(Decl *d,
30 const AttributeList &Attr, Sema &S) {
31 // Check the attribute arguments.
32 if (Attr.getNumArgs() != 1) {
33 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
34 return;
35 }
36
37 // FIXME: Check for decl - it should be void ()(void).
38
39 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
40 llvm::APSInt NumParams(32);
41 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
42 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
43 << "interrupt" << NumParamsExpr->getSourceRange();
44 return;
45 }
46
47 unsigned Num = NumParams.getLimitedValue(255);
48 if ((Num & 1) || Num > 30) {
49 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
50 << "interrupt" << (int)NumParams.getSExtValue()
51 << NumParamsExpr->getSourceRange();
52 return;
53 }
54
Sean Huntcf807c42010-08-18 23:23:40 +000055 d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
56 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000057 }
58
59namespace {
60 class MSP430AttributesSema : public TargetAttributesSema {
61 public:
62 MSP430AttributesSema() { }
63 bool ProcessDeclAttribute(Scope *scope, Decl *D,
64 const AttributeList &Attr, Sema &S) const {
65 if (Attr.getName()->getName() == "interrupt") {
66 HandleMSP430InterruptAttr(D, Attr, S);
67 return true;
68 }
69 return false;
70 }
71 };
72}
73
Wesley Peck276fdf42010-12-19 19:57:51 +000074static void HandleMBlazeInterruptHandlerAttr(Decl *d, const AttributeList &Attr,
75 Sema &S) {
76 // Check the attribute arguments.
77 if (Attr.getNumArgs() != 0) {
78 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
79 return;
80 }
81
82 // FIXME: Check for decl - it should be void ()(void).
83
84 d->addAttr(::new (S.Context) MBlazeInterruptHandlerAttr(Attr.getLoc(),
85 S.Context));
86 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
87}
88
89static void HandleMBlazeSaveVolatilesAttr(Decl *d, const AttributeList &Attr,
90 Sema &S) {
91 // Check the attribute arguments.
92 if (Attr.getNumArgs() != 0) {
93 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
94 return;
95 }
96
97 // FIXME: Check for decl - it should be void ()(void).
98
99 d->addAttr(::new (S.Context) MBlazeSaveVolatilesAttr(Attr.getLoc(),
100 S.Context));
101 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
102}
103
104
105namespace {
106 class MBlazeAttributesSema : public TargetAttributesSema {
107 public:
108 MBlazeAttributesSema() { }
109 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
110 Sema &S) const {
111 if (Attr.getName()->getName() == "interrupt_handler") {
112 HandleMBlazeInterruptHandlerAttr(D, Attr, S);
113 return true;
114 } else if (Attr.getName()->getName() == "save_volatiles") {
115 HandleMBlazeSaveVolatilesAttr(D, Attr, S);
116 return true;
117 }
118 return false;
119 }
120 };
121}
122
Charles Davis5a0164d2010-02-10 23:06:52 +0000123static void HandleX86ForceAlignArgPointerAttr(Decl *D,
124 const AttributeList& Attr,
125 Sema &S) {
126 // Check the attribute arguments.
127 if (Attr.getNumArgs() != 0) {
128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
129 return;
130 }
131
Charles Davisab442162010-02-17 00:44:47 +0000132 // If we try to apply it to a function pointer, don't warn, but don't
133 // do anything, either. It doesn't matter anyway, because there's nothing
134 // special about calling a force_align_arg_pointer function.
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000135 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Charles Davisab442162010-02-17 00:44:47 +0000136 if (VD && VD->getType()->isFunctionPointerType())
Charles Davis5a0164d2010-02-10 23:06:52 +0000137 return;
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000138 // Also don't warn on function pointer typedefs.
Richard Smith162e1c12011-04-15 14:24:37 +0000139 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Charles Davise01c0632010-02-18 04:56:59 +0000140 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
141 TD->getUnderlyingType()->isFunctionType()))
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000142 return;
Charles Davis5a0164d2010-02-10 23:06:52 +0000143 // Attribute can only be applied to function types.
Charles Davis9c00be52010-02-10 23:26:12 +0000144 if (!isa<FunctionDecl>(D)) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000145 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
146 << Attr.getName() << /* function */0;
147 return;
148 }
149
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000150 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
151 S.Context));
Charles Davis5a0164d2010-02-10 23:06:52 +0000152}
153
Michael Han51d8c522013-01-24 16:46:58 +0000154DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
155 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000156 if (D->hasAttr<DLLExportAttr>()) {
157 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
Rafael Espindola599f1b72012-05-13 03:25:18 +0000158 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000159 }
160
161 if (D->hasAttr<DLLImportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000162 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000163
Michael Han51d8c522013-01-24 16:46:58 +0000164 return ::new (Context) DLLImportAttr(Range, Context,
165 AttrSpellingListIndex);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000166}
167
Charles Davisf0122fe2010-02-16 18:27:26 +0000168static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
169 // check the attribute arguments.
170 if (Attr.getNumArgs() != 0) {
171 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
172 return;
173 }
174
175 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000176 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000177 if (!FD && !isa<VarDecl>(D)) {
Ted Kremenek240670c2010-02-21 05:12:56 +0000178 // Apparently Visual C++ thinks it is okay to not emit a warning
179 // in this case, so only emit a warning when -fms-extensions is not
180 // specified.
David Blaikie4e4d0842012-03-11 07:00:24 +0000181 if (!S.getLangOpts().MicrosoftExt)
Ted Kremenek240670c2010-02-21 05:12:56 +0000182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
183 << Attr.getName() << 2 /*variable and function*/;
Charles Davisf0122fe2010-02-16 18:27:26 +0000184 return;
185 }
186
187 // Currently, the dllimport attribute is ignored for inlined functions.
188 // Warning is emitted.
Rafael Espindola98ae8342012-05-10 02:50:16 +0000189 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000190 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
191 return;
192 }
193
Michael Han51d8c522013-01-24 16:46:58 +0000194 unsigned Index = Attr.getAttributeSpellingListIndex();
195 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000196 if (NewAttr)
197 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000198}
199
Michael Han51d8c522013-01-24 16:46:58 +0000200DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
201 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000202 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
203 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
204 D->dropAttr<DLLImportAttr>();
Charles Davisf0122fe2010-02-16 18:27:26 +0000205 }
206
Rafael Espindola98ae8342012-05-10 02:50:16 +0000207 if (D->hasAttr<DLLExportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000208 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000209
Michael Han51d8c522013-01-24 16:46:58 +0000210 return ::new (Context) DLLExportAttr(Range, Context,
211 AttrSpellingListIndex);
Charles Davisf0122fe2010-02-16 18:27:26 +0000212}
213
214static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
215 // check the attribute arguments.
216 if (Attr.getNumArgs() != 0) {
217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
218 return;
219 }
220
221 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000222 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000223 if (!FD && !isa<VarDecl>(D)) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
225 << Attr.getName() << 2 /*variable and function*/;
226 return;
227 }
228
229 // Currently, the dllexport attribute is ignored for inlined functions, unless
230 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000231 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000232 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
234 return;
235 }
236
Michael Han51d8c522013-01-24 16:46:58 +0000237 unsigned Index = Attr.getAttributeSpellingListIndex();
238 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000239 if (NewAttr)
240 D->addAttr(NewAttr);
Charles Davisf0122fe2010-02-16 18:27:26 +0000241}
242
Charles Davis5a0164d2010-02-10 23:06:52 +0000243namespace {
244 class X86AttributesSema : public TargetAttributesSema {
245 public:
246 X86AttributesSema() { }
247 bool ProcessDeclAttribute(Scope *scope, Decl *D,
248 const AttributeList &Attr, Sema &S) const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000249 const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
Charles Davisf0122fe2010-02-16 18:27:26 +0000250 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +0000251 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000252 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +0000253 case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000254 return true;
Sean Hunt8e083e72012-06-19 23:57:03 +0000255 case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000256 return true;
257 default: break;
258 }
259 }
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000260 if (Triple.getArch() != llvm::Triple::x86_64 &&
Benjamin Kramerf2cee5c2011-09-30 20:32:22 +0000261 (Attr.getName()->getName() == "force_align_arg_pointer" ||
262 Attr.getName()->getName() == "__force_align_arg_pointer__")) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000263 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
264 return true;
265 }
266 return false;
267 }
268 };
269}
270
Reed Kotler7dfd1822013-01-16 17:10:28 +0000271static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
272 // check the attribute arguments.
273 if (Attr.hasParameterOrArguments()) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
275 return;
276 }
277 // Attribute can only be applied to function types.
278 if (!isa<FunctionDecl>(D)) {
279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
280 << Attr.getName() << /* function */0;
281 return;
282 }
Michael Han51d8c522013-01-24 16:46:58 +0000283 D->addAttr(::new (S.Context) Mips16Attr(Attr.getRange(), S.Context,
284 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000285}
286
287static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
288 // check the attribute arguments.
289 if (Attr.hasParameterOrArguments()) {
290 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
291 return;
292 }
293 // Attribute can only be applied to function types.
294 if (!isa<FunctionDecl>(D)) {
295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
296 << Attr.getName() << /* function */0;
297 return;
298 }
Michael Han51d8c522013-01-24 16:46:58 +0000299 D->addAttr(::new (S.Context)
300 NoMips16Attr(Attr.getRange(), S.Context,
301 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000302}
303
304namespace {
305 class MipsAttributesSema : public TargetAttributesSema {
306 public:
307 MipsAttributesSema() { }
308 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
309 Sema &S) const {
310 if (Attr.getName()->getName() == "mips16") {
311 HandleMips16Attr(D, Attr, S);
312 return true;
313 } else if (Attr.getName()->getName() == "nomips16") {
314 HandleNoMips16Attr(D, Attr, S);
315 return true;
316 }
317 return false;
318 }
319 };
320}
321
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000322const TargetAttributesSema &Sema::getTargetAttributesSema() const {
323 if (TheTargetAttributesSema)
324 return *TheTargetAttributesSema;
325
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000326 const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000327 switch (Triple.getArch()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000328 case llvm::Triple::msp430:
329 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Wesley Peck276fdf42010-12-19 19:57:51 +0000330 case llvm::Triple::mblaze:
331 return *(TheTargetAttributesSema = new MBlazeAttributesSema);
Charles Davis5a0164d2010-02-10 23:06:52 +0000332 case llvm::Triple::x86:
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000333 case llvm::Triple::x86_64:
Charles Davis5a0164d2010-02-10 23:06:52 +0000334 return *(TheTargetAttributesSema = new X86AttributesSema);
Reed Kotler7dfd1822013-01-16 17:10:28 +0000335 case llvm::Triple::mips:
336 case llvm::Triple::mipsel:
337 return *(TheTargetAttributesSema = new MipsAttributesSema);
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000338 default:
339 return *(TheTargetAttributesSema = new TargetAttributesSema);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000340 }
341}