blob: ab697eeed53bc24f76322f4bfb8cb0d92dc28959 [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 McCall2d887082010-08-25 22:03:47 +000016#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000017#include "clang/Basic/TargetInfo.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.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
Sean Huntcf807c42010-08-18 23:23:40 +0000150 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getLoc(), S.Context));
Charles Davis5a0164d2010-02-10 23:06:52 +0000151}
152
Charles Davisf0122fe2010-02-16 18:27:26 +0000153static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
154 // check the attribute arguments.
155 if (Attr.getNumArgs() != 0) {
156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
157 return;
158 }
159
160 // Attribute can be applied only to functions or variables.
161 if (isa<VarDecl>(D)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000162 D->addAttr(::new (S.Context) DLLImportAttr(Attr.getLoc(), S.Context));
Charles Davisf0122fe2010-02-16 18:27:26 +0000163 return;
164 }
165
166 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
167 if (!FD) {
Ted Kremenek240670c2010-02-21 05:12:56 +0000168 // Apparently Visual C++ thinks it is okay to not emit a warning
169 // in this case, so only emit a warning when -fms-extensions is not
170 // specified.
171 if (!S.getLangOptions().Microsoft)
172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
173 << Attr.getName() << 2 /*variable and function*/;
Charles Davisf0122fe2010-02-16 18:27:26 +0000174 return;
175 }
176
177 // Currently, the dllimport attribute is ignored for inlined functions.
178 // Warning is emitted.
179 if (FD->isInlineSpecified()) {
180 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
181 return;
182 }
183
184 // The attribute is also overridden by a subsequent declaration as dllexport.
185 // Warning is emitted.
186 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
187 nextAttr = nextAttr->getNext()) {
188 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
189 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
190 return;
191 }
192 }
193
194 if (D->getAttr<DLLExportAttr>()) {
195 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
196 return;
197 }
198
Sean Huntcf807c42010-08-18 23:23:40 +0000199 D->addAttr(::new (S.Context) DLLImportAttr(Attr.getLoc(), S.Context));
Charles Davisf0122fe2010-02-16 18:27:26 +0000200}
201
202static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
203 // check the attribute arguments.
204 if (Attr.getNumArgs() != 0) {
205 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
206 return;
207 }
208
209 // Attribute can be applied only to functions or variables.
210 if (isa<VarDecl>(D)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000211 D->addAttr(::new (S.Context) DLLExportAttr(Attr.getLoc(), S.Context));
Charles Davisf0122fe2010-02-16 18:27:26 +0000212 return;
213 }
214
215 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
216 if (!FD) {
217 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
218 << Attr.getName() << 2 /*variable and function*/;
219 return;
220 }
221
222 // Currently, the dllexport attribute is ignored for inlined functions, unless
223 // the -fkeep-inline-functions flag has been used. Warning is emitted;
224 if (FD->isInlineSpecified()) {
225 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
226 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
227 return;
228 }
229
Sean Huntcf807c42010-08-18 23:23:40 +0000230 D->addAttr(::new (S.Context) DLLExportAttr(Attr.getLoc(), S.Context));
Charles Davisf0122fe2010-02-16 18:27:26 +0000231}
232
Charles Davis5a0164d2010-02-10 23:06:52 +0000233namespace {
234 class X86AttributesSema : public TargetAttributesSema {
235 public:
236 X86AttributesSema() { }
237 bool ProcessDeclAttribute(Scope *scope, Decl *D,
238 const AttributeList &Attr, Sema &S) const {
Charles Davisf0122fe2010-02-16 18:27:26 +0000239 const llvm::Triple &Triple(S.Context.Target.getTriple());
240 if (Triple.getOS() == llvm::Triple::Win32 ||
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +0000241 Triple.getOS() == llvm::Triple::MinGW32) {
Charles Davisf0122fe2010-02-16 18:27:26 +0000242 switch (Attr.getKind()) {
243 case AttributeList::AT_dllimport: HandleDLLImportAttr(D, Attr, S);
244 return true;
245 case AttributeList::AT_dllexport: HandleDLLExportAttr(D, Attr, S);
246 return true;
247 default: break;
248 }
249 }
Charles Davisbeaf5ed2010-02-18 04:39:19 +0000250 if (Attr.getName()->getName() == "force_align_arg_pointer" ||
251 Attr.getName()->getName() == "__force_align_arg_pointer__") {
Charles Davis5a0164d2010-02-10 23:06:52 +0000252 HandleX86ForceAlignArgPointerAttr(D, Attr, S);
253 return true;
254 }
255 return false;
256 }
257 };
258}
259
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000260const TargetAttributesSema &Sema::getTargetAttributesSema() const {
261 if (TheTargetAttributesSema)
262 return *TheTargetAttributesSema;
263
264 const llvm::Triple &Triple(Context.Target.getTriple());
265 switch (Triple.getArch()) {
266 default:
267 return *(TheTargetAttributesSema = new TargetAttributesSema);
268
269 case llvm::Triple::msp430:
270 return *(TheTargetAttributesSema = new MSP430AttributesSema);
Wesley Peck276fdf42010-12-19 19:57:51 +0000271 case llvm::Triple::mblaze:
272 return *(TheTargetAttributesSema = new MBlazeAttributesSema);
Charles Davis5a0164d2010-02-10 23:06:52 +0000273 case llvm::Triple::x86:
274 return *(TheTargetAttributesSema = new X86AttributesSema);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000275 }
276}