blob: 1c498925b71291f9868692791e2eec96872f2f71 [file] [log] [blame]
Chris Lattner5a0c3512009-02-17 00:57:29 +00001//===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
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//
Chris Lattner574aa402009-02-17 01:09:29 +000010// This file implements semantic analysis for non-trivial attributes and
11// pragmas.
Chris Lattner5a0c3512009-02-17 00:57:29 +000012//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
Sean Huntcf807c42010-08-18 23:23:40 +000017#include "clang/AST/Attr.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000018#include "clang/AST/Expr.h"
Daniel Dunbar613fd672010-05-27 00:35:16 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/Lex/Preprocessor.h"
Chris Lattner5a0c3512009-02-17 00:57:29 +000021using namespace clang;
22
Chris Lattner574aa402009-02-17 01:09:29 +000023//===----------------------------------------------------------------------===//
Daniel Dunbarea75a822010-05-27 00:04:40 +000024// Pragma 'pack' and 'options align'
Chris Lattner574aa402009-02-17 01:09:29 +000025//===----------------------------------------------------------------------===//
26
27namespace {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000028 struct PackStackEntry {
Daniel Dunbarc6082fe2010-05-27 05:45:51 +000029 // We just use a sentinel to represent when the stack is set to mac68k
30 // alignment.
31 static const unsigned kMac68kAlignmentSentinel = ~0U;
32
Daniel Dunbarae2232b2010-05-27 02:25:27 +000033 unsigned Alignment;
34 IdentifierInfo *Name;
35 };
36
Chris Lattner574aa402009-02-17 01:09:29 +000037 /// PragmaPackStack - Simple class to wrap the stack used by #pragma
38 /// pack.
39 class PragmaPackStack {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000040 typedef std::vector<PackStackEntry> stack_ty;
Chris Lattner574aa402009-02-17 01:09:29 +000041
42 /// Alignment - The current user specified alignment.
43 unsigned Alignment;
44
45 /// Stack - Entries in the #pragma pack stack, consisting of saved
46 /// alignments and optional names.
47 stack_ty Stack;
Mike Stump1eb44332009-09-09 15:08:12 +000048
49 public:
Chris Lattner574aa402009-02-17 01:09:29 +000050 PragmaPackStack() : Alignment(0) {}
51
52 void setAlignment(unsigned A) { Alignment = A; }
53 unsigned getAlignment() { return Alignment; }
54
55 /// push - Push the current alignment onto the stack, optionally
56 /// using the given \arg Name for the record, if non-zero.
57 void push(IdentifierInfo *Name) {
Daniel Dunbarae2232b2010-05-27 02:25:27 +000058 PackStackEntry PSE = { Alignment, Name };
59 Stack.push_back(PSE);
Chris Lattner574aa402009-02-17 01:09:29 +000060 }
61
62 /// pop - Pop a record from the stack and restore the current
63 /// alignment to the previous value. If \arg Name is non-zero then
64 /// the first such named record is popped, otherwise the top record
65 /// is popped. Returns true if the pop succeeded.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000066 bool pop(IdentifierInfo *Name, bool IsReset);
Chris Lattner574aa402009-02-17 01:09:29 +000067 };
68} // end anonymous namespace.
69
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000070bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) {
Chris Lattner574aa402009-02-17 01:09:29 +000071 // If name is empty just pop top.
72 if (!Name) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +000073 // An empty stack is a special case...
74 if (Stack.empty()) {
75 // If this isn't a reset, it is always an error.
76 if (!IsReset)
77 return false;
78
79 // Otherwise, it is an error only if some alignment has been set.
80 if (!Alignment)
81 return false;
82
83 // Otherwise, reset to the default alignment.
84 Alignment = 0;
85 } else {
86 Alignment = Stack.back().Alignment;
87 Stack.pop_back();
88 }
89
Chris Lattner574aa402009-02-17 01:09:29 +000090 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000091 }
92
Chris Lattner574aa402009-02-17 01:09:29 +000093 // Otherwise, find the named record.
94 for (unsigned i = Stack.size(); i != 0; ) {
95 --i;
Daniel Dunbarae2232b2010-05-27 02:25:27 +000096 if (Stack[i].Name == Name) {
Chris Lattner574aa402009-02-17 01:09:29 +000097 // Found it, pop up to and including this record.
Daniel Dunbarae2232b2010-05-27 02:25:27 +000098 Alignment = Stack[i].Alignment;
Chris Lattner574aa402009-02-17 01:09:29 +000099 Stack.erase(Stack.begin() + i, Stack.end());
100 return true;
101 }
102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner574aa402009-02-17 01:09:29 +0000104 return false;
105}
106
107
108/// FreePackedContext - Deallocate and null out PackContext.
109void Sema::FreePackedContext() {
110 delete static_cast<PragmaPackStack*>(PackContext);
111 PackContext = 0;
112}
113
Daniel Dunbar9f21f892010-05-27 01:53:40 +0000114void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
115 // If there is no pack context, we don't need any attributes.
116 if (!PackContext)
117 return;
118
119 PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext);
120
121 // Otherwise, check to see if we need a max field alignment attribute.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000122 if (unsigned Alignment = Stack->getAlignment()) {
123 if (Alignment == PackStackEntry::kMac68kAlignmentSentinel)
Sean Huntcf807c42010-08-18 23:23:40 +0000124 RD->addAttr(::new (Context) AlignMac68kAttr(SourceLocation(), Context));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000125 else
Sean Huntcf807c42010-08-18 23:23:40 +0000126 RD->addAttr(::new (Context) MaxFieldAlignmentAttr(SourceLocation(),
127 Context,
128 Alignment * 8));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000129 }
Chris Lattner574aa402009-02-17 01:09:29 +0000130}
131
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000132void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
133 if (!MSStructPragmaOn)
134 return;
135 RD->addAttr(::new (Context) MsStructAttr(SourceLocation(), Context));
136}
137
Daniel Dunbarea75a822010-05-27 00:04:40 +0000138void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
139 SourceLocation PragmaLoc,
140 SourceLocation KindLoc) {
141 if (PackContext == 0)
142 PackContext = new PragmaPackStack();
143
144 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
145
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000146 // Reset just pops the top of the stack, or resets the current alignment to
147 // default.
John McCallf312b1e2010-08-26 23:41:50 +0000148 if (Kind == Sema::POAK_Reset) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000149 if (!Context->pop(0, /*IsReset=*/true)) {
Daniel Dunbarea75a822010-05-27 00:04:40 +0000150 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
151 << "stack empty";
152 }
153 return;
154 }
155
Daniel Dunbarea75a822010-05-27 00:04:40 +0000156 switch (Kind) {
Daniel Dunbar638e7cf2010-05-27 18:42:09 +0000157 // For all targets we support native and natural are the same.
158 //
159 // FIXME: This is not true on Darwin/PPC.
160 case POAK_Native:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000161 case POAK_Power:
Daniel Dunbard6b305d2010-05-28 20:08:00 +0000162 case POAK_Natural:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000163 Context->push(0);
164 Context->setAlignment(0);
165 break;
166
Daniel Dunbar6f739142010-05-27 18:42:17 +0000167 // Note that '#pragma options align=packed' is not equivalent to attribute
168 // packed, it has a different precedence relative to attribute aligned.
169 case POAK_Packed:
170 Context->push(0);
171 Context->setAlignment(1);
172 break;
173
Daniel Dunbar613fd672010-05-27 00:35:16 +0000174 case POAK_Mac68k:
175 // Check if the target supports this.
176 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
177 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
178 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000179 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000180 Context->push(0);
181 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000182 break;
183
Daniel Dunbarea75a822010-05-27 00:04:40 +0000184 default:
185 Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
186 << KindLoc;
187 break;
188 }
189}
190
Mike Stump1eb44332009-09-09 15:08:12 +0000191void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
Richard Trieuf81e5a92011-09-09 02:00:50 +0000192 Expr *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000193 SourceLocation LParenLoc, SourceLocation RParenLoc) {
194 Expr *Alignment = static_cast<Expr *>(alignment);
195
196 // If specified then alignment must be a "small" power of two.
197 unsigned AlignmentVal = 0;
198 if (Alignment) {
199 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000201 // pack(0) is like pack(), which just works out since that is what
202 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000203 if (Alignment->isTypeDependent() ||
204 Alignment->isValueDependent() ||
205 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000206 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000207 Val.getZExtValue() > 16) {
208 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000209 return; // Ignore
210 }
211
212 AlignmentVal = (unsigned) Val.getZExtValue();
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Chris Lattner574aa402009-02-17 01:09:29 +0000215 if (PackContext == 0)
216 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Chris Lattner574aa402009-02-17 01:09:29 +0000218 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattner5a0c3512009-02-17 00:57:29 +0000220 switch (Kind) {
John McCallf312b1e2010-08-26 23:41:50 +0000221 case Sema::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000222 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000223 break;
224
John McCallf312b1e2010-08-26 23:41:50 +0000225 case Sema::PPK_Show: // pack(show)
Chris Lattner5a0c3512009-02-17 00:57:29 +0000226 // Show the current alignment, making sure to show the right value
227 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000228 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000229 // FIXME: This should come from the target.
230 if (AlignmentVal == 0)
231 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000232 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
233 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
234 else
235 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000236 break;
237
John McCallf312b1e2010-08-26 23:41:50 +0000238 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000239 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000240 // Set the new alignment if specified.
241 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000242 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000243 break;
244
John McCallf312b1e2010-08-26 23:41:50 +0000245 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner5a0c3512009-02-17 00:57:29 +0000246 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
247 // "#pragma pack(pop, identifier, n) is undefined"
248 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000249 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
250
Chris Lattner5a0c3512009-02-17 00:57:29 +0000251 // Do the pop.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000252 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000253 // If a name was specified then failure indicates the name
254 // wasn't found. Otherwise failure indicates the stack was
255 // empty.
256 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
257 << (Name ? "no record matching name" : "stack empty");
258
259 // FIXME: Warn about popping named records as MSVC does.
260 } else {
261 // Pop succeeded, set the new alignment if specified.
262 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000263 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000264 }
265 break;
266
267 default:
268 assert(0 && "Invalid #pragma pack kind.");
269 }
270}
271
Fariborz Jahanian62c92582011-04-25 18:49:15 +0000272void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
273 MSStructPragmaOn = (Kind == PMSST_ON);
274}
275
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000276void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
277 SourceLocation PragmaLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000278
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000279 IdentifierInfo *Name = IdTok.getIdentifierInfo();
280 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
281 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000282
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000283 if (Lookup.empty()) {
284 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
285 << Name << SourceRange(IdTok.getLocation());
286 return;
Ted Kremenek4726d032009-03-23 22:28:25 +0000287 }
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000288
289 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidis2a5c45b2011-01-27 18:16:48 +0000290 if (!VD) {
291 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000292 << Name << SourceRange(IdTok.getLocation());
293 return;
294 }
295
296 // Warn if this was used before being marked unused.
297 if (VD->isUsed())
298 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
299
300 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
Ted Kremenek4726d032009-03-23 22:28:25 +0000301}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000302
John McCall90f14502010-12-10 02:59:44 +0000303typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
304enum { NoVisibility = (unsigned) -1 };
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000305
306void Sema::AddPushedVisibilityAttribute(Decl *D) {
307 if (!VisContext)
308 return;
309
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000310 if (isa<NamedDecl>(D) && cast<NamedDecl>(D)->getExplicitVisibility())
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000311 return;
312
313 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall90f14502010-12-10 02:59:44 +0000314 unsigned rawType = Stack->back().first;
315 if (rawType == NoVisibility) return;
316
317 VisibilityAttr::VisibilityType type
318 = (VisibilityAttr::VisibilityType) rawType;
Sean Huntcf807c42010-08-18 23:23:40 +0000319 SourceLocation loc = Stack->back().second;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000320
Sean Huntcf807c42010-08-18 23:23:40 +0000321 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000322}
323
324/// FreeVisContext - Deallocate and null out VisContext.
325void Sema::FreeVisContext() {
326 delete static_cast<VisStack*>(VisContext);
327 VisContext = 0;
328}
329
John McCall90f14502010-12-10 02:59:44 +0000330static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallea318642010-08-26 09:15:37 +0000331 // Put visibility on stack.
332 if (!S.VisContext)
333 S.VisContext = new VisStack;
334
335 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
336 Stack->push_back(std::make_pair(type, loc));
337}
338
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000339void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType,
340 SourceLocation PragmaLoc) {
341 if (IsPush) {
342 // Compute visibility to use.
Sean Huntcf807c42010-08-18 23:23:40 +0000343 VisibilityAttr::VisibilityType type;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000344 if (VisType->isStr("default"))
Sean Huntcf807c42010-08-18 23:23:40 +0000345 type = VisibilityAttr::Default;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000346 else if (VisType->isStr("hidden"))
Sean Huntcf807c42010-08-18 23:23:40 +0000347 type = VisibilityAttr::Hidden;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000348 else if (VisType->isStr("internal"))
Sean Huntcf807c42010-08-18 23:23:40 +0000349 type = VisibilityAttr::Hidden; // FIXME
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000350 else if (VisType->isStr("protected"))
Sean Huntcf807c42010-08-18 23:23:40 +0000351 type = VisibilityAttr::Protected;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000352 else {
353 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
354 VisType->getName();
355 return;
356 }
John McCallea318642010-08-26 09:15:37 +0000357 PushPragmaVisibility(*this, type, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000358 } else {
359 PopPragmaVisibility();
360 }
361}
362
Peter Collingbourne321b8172011-02-14 01:42:35 +0000363void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
364 switch (OOS) {
365 case tok::OOS_ON:
366 FPFeatures.fp_contract = 1;
367 break;
368 case tok::OOS_OFF:
369 FPFeatures.fp_contract = 0;
370 break;
371 case tok::OOS_DEFAULT:
372 FPFeatures.fp_contract = getLangOptions().DefaultFPContract;
373 break;
374 }
375}
376
John McCall90f14502010-12-10 02:59:44 +0000377void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr) {
378 // Visibility calculations will consider the namespace's visibility.
379 // Here we just want to note that we're in a visibility context
380 // which overrides any enclosing #pragma context, but doesn't itself
381 // contribute visibility.
382 PushPragmaVisibility(*this, NoVisibility, SourceLocation());
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000383}
384
385void Sema::PopPragmaVisibility() {
386 // Pop visibility from stack, if there is one on the stack.
387 if (VisContext) {
388 VisStack *Stack = static_cast<VisStack*>(VisContext);
389
390 Stack->pop_back();
391 // To simplify the implementation, never keep around an empty stack.
392 if (Stack->empty())
393 FreeVisContext();
394 }
395 // FIXME: Add diag for pop without push.
396}