blob: 8ea7319b6e776c303daa7c17c82253fd41dbac85 [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) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +000033 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
34 << Attr.getName() << 1;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000035 return;
36 }
37
38 // FIXME: Check for decl - it should be void ()(void).
39
40 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
41 llvm::APSInt NumParams(32);
42 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
43 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
44 << "interrupt" << NumParamsExpr->getSourceRange();
45 return;
46 }
47
48 unsigned Num = NumParams.getLimitedValue(255);
49 if ((Num & 1) || Num > 30) {
50 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
51 << "interrupt" << (int)NumParams.getSExtValue()
52 << NumParamsExpr->getSourceRange();
53 return;
54 }
55
Sean Huntcf807c42010-08-18 23:23:40 +000056 d->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num));
57 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000058 }
59
60namespace {
61 class MSP430AttributesSema : public TargetAttributesSema {
62 public:
63 MSP430AttributesSema() { }
64 bool ProcessDeclAttribute(Scope *scope, Decl *D,
65 const AttributeList &Attr, Sema &S) const {
66 if (Attr.getName()->getName() == "interrupt") {
67 HandleMSP430InterruptAttr(D, Attr, S);
68 return true;
69 }
70 return false;
71 }
72 };
73}
74
Wesley Peck276fdf42010-12-19 19:57:51 +000075static void HandleMBlazeInterruptHandlerAttr(Decl *d, const AttributeList &Attr,
76 Sema &S) {
77 // Check the attribute arguments.
78 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +000079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
80 << Attr.getName() << 1;
Wesley Peck276fdf42010-12-19 19:57:51 +000081 return;
82 }
83
84 // FIXME: Check for decl - it should be void ()(void).
85
86 d->addAttr(::new (S.Context) MBlazeInterruptHandlerAttr(Attr.getLoc(),
87 S.Context));
88 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
89}
90
91static void HandleMBlazeSaveVolatilesAttr(Decl *d, const AttributeList &Attr,
92 Sema &S) {
93 // Check the attribute arguments.
94 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +000095 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
96 << Attr.getName() << 1;
Wesley Peck276fdf42010-12-19 19:57:51 +000097 return;
98 }
99
100 // FIXME: Check for decl - it should be void ()(void).
101
102 d->addAttr(::new (S.Context) MBlazeSaveVolatilesAttr(Attr.getLoc(),
103 S.Context));
104 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
105}
106
107
108namespace {
109 class MBlazeAttributesSema : public TargetAttributesSema {
110 public:
111 MBlazeAttributesSema() { }
112 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
113 Sema &S) const {
114 if (Attr.getName()->getName() == "interrupt_handler") {
115 HandleMBlazeInterruptHandlerAttr(D, Attr, S);
116 return true;
117 } else if (Attr.getName()->getName() == "save_volatiles") {
118 HandleMBlazeSaveVolatilesAttr(D, Attr, S);
119 return true;
120 }
121 return false;
122 }
123 };
124}
125
Charles Davis5a0164d2010-02-10 23:06:52 +0000126static void HandleX86ForceAlignArgPointerAttr(Decl *D,
127 const AttributeList& Attr,
128 Sema &S) {
129 // Check the attribute arguments.
130 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
132 << Attr.getName() << 0;
Charles Davis5a0164d2010-02-10 23:06:52 +0000133 return;
134 }
135
Charles Davisab442162010-02-17 00:44:47 +0000136 // If we try to apply it to a function pointer, don't warn, but don't
137 // do anything, either. It doesn't matter anyway, because there's nothing
138 // special about calling a force_align_arg_pointer function.
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000139 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Charles Davisab442162010-02-17 00:44:47 +0000140 if (VD && VD->getType()->isFunctionPointerType())
Charles Davis5a0164d2010-02-10 23:06:52 +0000141 return;
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000142 // Also don't warn on function pointer typedefs.
Richard Smith162e1c12011-04-15 14:24:37 +0000143 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Charles Davise01c0632010-02-18 04:56:59 +0000144 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
145 TD->getUnderlyingType()->isFunctionType()))
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000146 return;
Charles Davis5a0164d2010-02-10 23:06:52 +0000147 // Attribute can only be applied to function types.
Charles Davis9c00be52010-02-10 23:26:12 +0000148 if (!isa<FunctionDecl>(D)) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000149 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
150 << Attr.getName() << /* function */0;
151 return;
152 }
153
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000154 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(),
155 S.Context));
Charles Davis5a0164d2010-02-10 23:06:52 +0000156}
157
Michael Han51d8c522013-01-24 16:46:58 +0000158DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
159 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000160 if (D->hasAttr<DLLExportAttr>()) {
161 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport";
Rafael Espindola599f1b72012-05-13 03:25:18 +0000162 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000163 }
164
165 if (D->hasAttr<DLLImportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000166 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000167
Reid Klecknerbeba3e82013-05-20 21:53:29 +0000168 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
169 if (VD->hasDefinition()) {
170 // dllimport cannot be applied to definitions.
171 Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition)
172 << "dllimport";
173 return NULL;
174 }
175 }
176
Michael Han51d8c522013-01-24 16:46:58 +0000177 return ::new (Context) DLLImportAttr(Range, Context,
178 AttrSpellingListIndex);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000179}
180
Charles Davisf0122fe2010-02-16 18:27:26 +0000181static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
182 // check the attribute arguments.
183 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000184 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
185 << Attr.getName() << 0;
Charles Davisf0122fe2010-02-16 18:27:26 +0000186 return;
187 }
188
189 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000190 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000191 if (!FD && !isa<VarDecl>(D)) {
Ted Kremenek240670c2010-02-21 05:12:56 +0000192 // Apparently Visual C++ thinks it is okay to not emit a warning
193 // in this case, so only emit a warning when -fms-extensions is not
194 // specified.
David Blaikie4e4d0842012-03-11 07:00:24 +0000195 if (!S.getLangOpts().MicrosoftExt)
Ted Kremenek240670c2010-02-21 05:12:56 +0000196 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
197 << Attr.getName() << 2 /*variable and function*/;
Charles Davisf0122fe2010-02-16 18:27:26 +0000198 return;
199 }
200
201 // Currently, the dllimport attribute is ignored for inlined functions.
202 // Warning is emitted.
Rafael Espindola98ae8342012-05-10 02:50:16 +0000203 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000204 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
205 return;
206 }
207
Michael Han51d8c522013-01-24 16:46:58 +0000208 unsigned Index = Attr.getAttributeSpellingListIndex();
209 DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000210 if (NewAttr)
211 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000212}
213
Michael Han51d8c522013-01-24 16:46:58 +0000214DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
215 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +0000216 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
217 Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport";
218 D->dropAttr<DLLImportAttr>();
Charles Davisf0122fe2010-02-16 18:27:26 +0000219 }
220
Rafael Espindola98ae8342012-05-10 02:50:16 +0000221 if (D->hasAttr<DLLExportAttr>())
Rafael Espindola599f1b72012-05-13 03:25:18 +0000222 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000223
Michael Han51d8c522013-01-24 16:46:58 +0000224 return ::new (Context) DLLExportAttr(Range, Context,
225 AttrSpellingListIndex);
Charles Davisf0122fe2010-02-16 18:27:26 +0000226}
227
228static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
232 << Attr.getName() << 0;
Charles Davisf0122fe2010-02-16 18:27:26 +0000233 return;
234 }
235
236 // Attribute can be applied only to functions or variables.
Charles Davisf0122fe2010-02-16 18:27:26 +0000237 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola98ae8342012-05-10 02:50:16 +0000238 if (!FD && !isa<VarDecl>(D)) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000239 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
240 << Attr.getName() << 2 /*variable and function*/;
241 return;
242 }
243
244 // Currently, the dllexport attribute is ignored for inlined functions, unless
245 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Rafael Espindola98ae8342012-05-10 02:50:16 +0000246 if (FD && FD->isInlineSpecified()) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000247 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
248 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
249 return;
250 }
251
Michael Han51d8c522013-01-24 16:46:58 +0000252 unsigned Index = Attr.getAttributeSpellingListIndex();
253 DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +0000254 if (NewAttr)
255 D->addAttr(NewAttr);
Charles Davisf0122fe2010-02-16 18:27:26 +0000256}
257
Charles Davis5a0164d2010-02-10 23:06:52 +0000258namespace {
259 class X86AttributesSema : public TargetAttributesSema {
260 public:
261 X86AttributesSema() { }
262 bool ProcessDeclAttribute(Scope *scope, Decl *D,
263 const AttributeList &Attr, Sema &S) const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000264 const llvm::Triple &Triple(S.Context.getTargetInfo().getTriple());
Charles Davisf0122fe2010-02-16 18:27:26 +0000265 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +0000266 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000267 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +0000268 case AttributeList::AT_DLLImport: HandleDLLImportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000269 return true;
Sean Hunt8e083e72012-06-19 23:57:03 +0000270 case AttributeList::AT_DLLExport: HandleDLLExportAttr(D, Attr, S);
Charles Davisf0122fe2010-02-16 18:27:26 +0000271 return true;
272 default: break;
273 }
274 }
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000275 if (Triple.getArch() != llvm::Triple::x86_64 &&
Benjamin Kramerf2cee5c2011-09-30 20:32:22 +0000276 (Attr.getName()->getName() == "force_align_arg_pointer" ||
277 Attr.getName()->getName() == "__force_align_arg_pointer__")) {
Charles Davis5a0164d2010-02-10 23:06:52 +0000278 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
279 return true;
280 }
281 return false;
282 }
283 };
284}
285
Reed Kotler7dfd1822013-01-16 17:10:28 +0000286static void HandleMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
287 // check the attribute arguments.
288 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000289 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
290 << Attr.getName() << 0;
Reed Kotler7dfd1822013-01-16 17:10:28 +0000291 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) Mips16Attr(Attr.getRange(), S.Context,
300 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000301}
302
303static void HandleNoMips16Attr(Decl *D, const AttributeList &Attr, Sema &S) {
304 // check the attribute arguments.
305 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
307 << Attr.getName() << 0;
Reed Kotler7dfd1822013-01-16 17:10:28 +0000308 return;
309 }
310 // Attribute can only be applied to function types.
311 if (!isa<FunctionDecl>(D)) {
312 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
313 << Attr.getName() << /* function */0;
314 return;
315 }
Michael Han51d8c522013-01-24 16:46:58 +0000316 D->addAttr(::new (S.Context)
317 NoMips16Attr(Attr.getRange(), S.Context,
318 Attr.getAttributeSpellingListIndex()));
Reed Kotler7dfd1822013-01-16 17:10:28 +0000319}
320
321namespace {
322 class MipsAttributesSema : public TargetAttributesSema {
323 public:
324 MipsAttributesSema() { }
325 bool ProcessDeclAttribute(Scope *scope, Decl *D, const AttributeList &Attr,
326 Sema &S) const {
327 if (Attr.getName()->getName() == "mips16") {
328 HandleMips16Attr(D, Attr, S);
329 return true;
330 } else if (Attr.getName()->getName() == "nomips16") {
331 HandleNoMips16Attr(D, Attr, S);
332 return true;
333 }
334 return false;
335 }
336 };
337}
338
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000339const TargetAttributesSema &Sema::getTargetAttributesSema() const {
340 if (TheTargetAttributesSema)
341 return *TheTargetAttributesSema;
342
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000343 const llvm::Triple &Triple(Context.getTargetInfo().getTriple());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000344 switch (Triple.getArch()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000345 case llvm::Triple::msp430:
346 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Wesley Peck276fdf42010-12-19 19:57:51 +0000347 case llvm::Triple::mblaze:
348 return *(TheTargetAttributesSema = new MBlazeAttributesSema);
Charles Davis5a0164d2010-02-10 23:06:52 +0000349 case llvm::Triple::x86:
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000350 case llvm::Triple::x86_64:
Charles Davis5a0164d2010-02-10 23:06:52 +0000351 return *(TheTargetAttributesSema = new X86AttributesSema);
Reed Kotler7dfd1822013-01-16 17:10:28 +0000352 case llvm::Triple::mips:
353 case llvm::Triple::mipsel:
354 return *(TheTargetAttributesSema = new MipsAttributesSema);
Eli Friedman9afbfbe2011-09-30 18:53:25 +0000355 default:
356 return *(TheTargetAttributesSema = new TargetAttributesSema);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000357 }
358}