blob: 7a50356fedbba0761192fabdb1b1621a0bd0611a [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
Daniel Dunbarea75a822010-05-27 00:04:40 +0000132void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
133 SourceLocation PragmaLoc,
134 SourceLocation KindLoc) {
135 if (PackContext == 0)
136 PackContext = new PragmaPackStack();
137
138 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
139
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000140 // Reset just pops the top of the stack, or resets the current alignment to
141 // default.
John McCallf312b1e2010-08-26 23:41:50 +0000142 if (Kind == Sema::POAK_Reset) {
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000143 if (!Context->pop(0, /*IsReset=*/true)) {
Daniel Dunbarea75a822010-05-27 00:04:40 +0000144 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
145 << "stack empty";
146 }
147 return;
148 }
149
Daniel Dunbarea75a822010-05-27 00:04:40 +0000150 switch (Kind) {
Daniel Dunbar638e7cf2010-05-27 18:42:09 +0000151 // For all targets we support native and natural are the same.
152 //
153 // FIXME: This is not true on Darwin/PPC.
154 case POAK_Native:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000155 case POAK_Power:
Daniel Dunbard6b305d2010-05-28 20:08:00 +0000156 case POAK_Natural:
Daniel Dunbar450f7932010-05-28 19:43:33 +0000157 Context->push(0);
158 Context->setAlignment(0);
159 break;
160
Daniel Dunbar6f739142010-05-27 18:42:17 +0000161 // Note that '#pragma options align=packed' is not equivalent to attribute
162 // packed, it has a different precedence relative to attribute aligned.
163 case POAK_Packed:
164 Context->push(0);
165 Context->setAlignment(1);
166 break;
167
Daniel Dunbar613fd672010-05-27 00:35:16 +0000168 case POAK_Mac68k:
169 // Check if the target supports this.
170 if (!PP.getTargetInfo().hasAlignMac68kSupport()) {
171 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
172 return;
Daniel Dunbar613fd672010-05-27 00:35:16 +0000173 }
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000174 Context->push(0);
175 Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel);
Daniel Dunbar613fd672010-05-27 00:35:16 +0000176 break;
177
Daniel Dunbarea75a822010-05-27 00:04:40 +0000178 default:
179 Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option)
180 << KindLoc;
181 break;
182 }
183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name,
186 ExprTy *alignment, SourceLocation PragmaLoc,
Chris Lattner5a0c3512009-02-17 00:57:29 +0000187 SourceLocation LParenLoc, SourceLocation RParenLoc) {
188 Expr *Alignment = static_cast<Expr *>(alignment);
189
190 // If specified then alignment must be a "small" power of two.
191 unsigned AlignmentVal = 0;
192 if (Alignment) {
193 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000195 // pack(0) is like pack(), which just works out since that is what
196 // we use 0 for in PackAttr.
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000197 if (Alignment->isTypeDependent() ||
198 Alignment->isValueDependent() ||
199 !Alignment->isIntegerConstantExpr(Val, Context) ||
Daniel Dunbar79cd1162009-03-06 20:45:54 +0000200 !(Val == 0 || Val.isPowerOf2()) ||
Chris Lattner5a0c3512009-02-17 00:57:29 +0000201 Val.getZExtValue() > 16) {
202 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000203 return; // Ignore
204 }
205
206 AlignmentVal = (unsigned) Val.getZExtValue();
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner574aa402009-02-17 01:09:29 +0000209 if (PackContext == 0)
210 PackContext = new PragmaPackStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner574aa402009-02-17 01:09:29 +0000212 PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner5a0c3512009-02-17 00:57:29 +0000214 switch (Kind) {
John McCallf312b1e2010-08-26 23:41:50 +0000215 case Sema::PPK_Default: // pack([n])
Chris Lattner574aa402009-02-17 01:09:29 +0000216 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000217 break;
218
John McCallf312b1e2010-08-26 23:41:50 +0000219 case Sema::PPK_Show: // pack(show)
Chris Lattner5a0c3512009-02-17 00:57:29 +0000220 // Show the current alignment, making sure to show the right value
221 // for the default.
Chris Lattner574aa402009-02-17 01:09:29 +0000222 AlignmentVal = Context->getAlignment();
Chris Lattner5a0c3512009-02-17 00:57:29 +0000223 // FIXME: This should come from the target.
224 if (AlignmentVal == 0)
225 AlignmentVal = 8;
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000226 if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel)
227 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
228 else
229 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
Chris Lattner5a0c3512009-02-17 00:57:29 +0000230 break;
231
John McCallf312b1e2010-08-26 23:41:50 +0000232 case Sema::PPK_Push: // pack(push [, id] [, [n])
Chris Lattner574aa402009-02-17 01:09:29 +0000233 Context->push(Name);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000234 // Set the new alignment if specified.
235 if (Alignment)
Mike Stump1eb44332009-09-09 15:08:12 +0000236 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000237 break;
238
John McCallf312b1e2010-08-26 23:41:50 +0000239 case Sema::PPK_Pop: // pack(pop [, id] [, n])
Chris Lattner5a0c3512009-02-17 00:57:29 +0000240 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
241 // "#pragma pack(pop, identifier, n) is undefined"
242 if (Alignment && Name)
Mike Stump1eb44332009-09-09 15:08:12 +0000243 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment);
244
Chris Lattner5a0c3512009-02-17 00:57:29 +0000245 // Do the pop.
Daniel Dunbarddc6ff62010-07-16 04:54:16 +0000246 if (!Context->pop(Name, /*IsReset=*/false)) {
Chris Lattner5a0c3512009-02-17 00:57:29 +0000247 // If a name was specified then failure indicates the name
248 // wasn't found. Otherwise failure indicates the stack was
249 // empty.
250 Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed)
251 << (Name ? "no record matching name" : "stack empty");
252
253 // FIXME: Warn about popping named records as MSVC does.
254 } else {
255 // Pop succeeded, set the new alignment if specified.
256 if (Alignment)
Chris Lattner574aa402009-02-17 01:09:29 +0000257 Context->setAlignment(AlignmentVal);
Chris Lattner5a0c3512009-02-17 00:57:29 +0000258 }
259 break;
260
261 default:
262 assert(0 && "Invalid #pragma pack kind.");
263 }
264}
265
Fariborz Jahanian62c92582011-04-25 18:49:15 +0000266void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
267 MSStructPragmaOn = (Kind == PMSST_ON);
268}
269
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000270void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
271 SourceLocation PragmaLoc) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000272
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000273 IdentifierInfo *Name = IdTok.getIdentifierInfo();
274 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
275 LookupParsedName(Lookup, curScope, NULL, true);
Ted Kremenek4726d032009-03-23 22:28:25 +0000276
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000277 if (Lookup.empty()) {
278 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
279 << Name << SourceRange(IdTok.getLocation());
280 return;
Ted Kremenek4726d032009-03-23 22:28:25 +0000281 }
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000282
283 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
Argyrios Kyrtzidis2a5c45b2011-01-27 18:16:48 +0000284 if (!VD) {
285 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000286 << Name << SourceRange(IdTok.getLocation());
287 return;
288 }
289
290 // Warn if this was used before being marked unused.
291 if (VD->isUsed())
292 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
293
294 VD->addAttr(::new (Context) UnusedAttr(IdTok.getLocation(), Context));
Ted Kremenek4726d032009-03-23 22:28:25 +0000295}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000296
John McCall90f14502010-12-10 02:59:44 +0000297typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
298enum { NoVisibility = (unsigned) -1 };
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000299
300void Sema::AddPushedVisibilityAttribute(Decl *D) {
301 if (!VisContext)
302 return;
303
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000304 if (isa<NamedDecl>(D) && cast<NamedDecl>(D)->getExplicitVisibility())
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000305 return;
306
307 VisStack *Stack = static_cast<VisStack*>(VisContext);
John McCall90f14502010-12-10 02:59:44 +0000308 unsigned rawType = Stack->back().first;
309 if (rawType == NoVisibility) return;
310
311 VisibilityAttr::VisibilityType type
312 = (VisibilityAttr::VisibilityType) rawType;
Sean Huntcf807c42010-08-18 23:23:40 +0000313 SourceLocation loc = Stack->back().second;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000314
Sean Huntcf807c42010-08-18 23:23:40 +0000315 D->addAttr(::new (Context) VisibilityAttr(loc, Context, type));
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000316}
317
318/// FreeVisContext - Deallocate and null out VisContext.
319void Sema::FreeVisContext() {
320 delete static_cast<VisStack*>(VisContext);
321 VisContext = 0;
322}
323
John McCall90f14502010-12-10 02:59:44 +0000324static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
John McCallea318642010-08-26 09:15:37 +0000325 // Put visibility on stack.
326 if (!S.VisContext)
327 S.VisContext = new VisStack;
328
329 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
330 Stack->push_back(std::make_pair(type, loc));
331}
332
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000333void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType,
334 SourceLocation PragmaLoc) {
335 if (IsPush) {
336 // Compute visibility to use.
Sean Huntcf807c42010-08-18 23:23:40 +0000337 VisibilityAttr::VisibilityType type;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000338 if (VisType->isStr("default"))
Sean Huntcf807c42010-08-18 23:23:40 +0000339 type = VisibilityAttr::Default;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000340 else if (VisType->isStr("hidden"))
Sean Huntcf807c42010-08-18 23:23:40 +0000341 type = VisibilityAttr::Hidden;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000342 else if (VisType->isStr("internal"))
Sean Huntcf807c42010-08-18 23:23:40 +0000343 type = VisibilityAttr::Hidden; // FIXME
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000344 else if (VisType->isStr("protected"))
Sean Huntcf807c42010-08-18 23:23:40 +0000345 type = VisibilityAttr::Protected;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000346 else {
347 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) <<
348 VisType->getName();
349 return;
350 }
John McCallea318642010-08-26 09:15:37 +0000351 PushPragmaVisibility(*this, type, PragmaLoc);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000352 } else {
353 PopPragmaVisibility();
354 }
355}
356
Peter Collingbourne321b8172011-02-14 01:42:35 +0000357void Sema::ActOnPragmaFPContract(tok::OnOffSwitch OOS) {
358 switch (OOS) {
359 case tok::OOS_ON:
360 FPFeatures.fp_contract = 1;
361 break;
362 case tok::OOS_OFF:
363 FPFeatures.fp_contract = 0;
364 break;
365 case tok::OOS_DEFAULT:
366 FPFeatures.fp_contract = getLangOptions().DefaultFPContract;
367 break;
368 }
369}
370
John McCall90f14502010-12-10 02:59:44 +0000371void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr) {
372 // Visibility calculations will consider the namespace's visibility.
373 // Here we just want to note that we're in a visibility context
374 // which overrides any enclosing #pragma context, but doesn't itself
375 // contribute visibility.
376 PushPragmaVisibility(*this, NoVisibility, SourceLocation());
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000377}
378
379void Sema::PopPragmaVisibility() {
380 // Pop visibility from stack, if there is one on the stack.
381 if (VisContext) {
382 VisStack *Stack = static_cast<VisStack*>(VisContext);
383
384 Stack->pop_back();
385 // To simplify the implementation, never keep around an empty stack.
386 if (Stack->empty())
387 FreeVisContext();
388 }
389 // FIXME: Add diag for pop without push.
390}