Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for non-trivial attributes and |
| 11 | // pragmas. |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame^] | 15 | #include "clang/Sema/Sema.h" |
| 16 | #include "clang/Sema/Lookup.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 23 | // Pragma 'pack' and 'options align' |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 27 | struct PackStackEntry { |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 28 | // We just use a sentinel to represent when the stack is set to mac68k |
| 29 | // alignment. |
| 30 | static const unsigned kMac68kAlignmentSentinel = ~0U; |
| 31 | |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 32 | unsigned Alignment; |
| 33 | IdentifierInfo *Name; |
| 34 | }; |
| 35 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 36 | /// PragmaPackStack - Simple class to wrap the stack used by #pragma |
| 37 | /// pack. |
| 38 | class PragmaPackStack { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 39 | typedef std::vector<PackStackEntry> stack_ty; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 40 | |
| 41 | /// Alignment - The current user specified alignment. |
| 42 | unsigned Alignment; |
| 43 | |
| 44 | /// Stack - Entries in the #pragma pack stack, consisting of saved |
| 45 | /// alignments and optional names. |
| 46 | stack_ty Stack; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 49 | PragmaPackStack() : Alignment(0) {} |
| 50 | |
| 51 | void setAlignment(unsigned A) { Alignment = A; } |
| 52 | unsigned getAlignment() { return Alignment; } |
| 53 | |
| 54 | /// push - Push the current alignment onto the stack, optionally |
| 55 | /// using the given \arg Name for the record, if non-zero. |
| 56 | void push(IdentifierInfo *Name) { |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 57 | PackStackEntry PSE = { Alignment, Name }; |
| 58 | Stack.push_back(PSE); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// pop - Pop a record from the stack and restore the current |
| 62 | /// alignment to the previous value. If \arg Name is non-zero then |
| 63 | /// the first such named record is popped, otherwise the top record |
| 64 | /// is popped. Returns true if the pop succeeded. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 65 | bool pop(IdentifierInfo *Name, bool IsReset); |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 66 | }; |
| 67 | } // end anonymous namespace. |
| 68 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 69 | bool PragmaPackStack::pop(IdentifierInfo *Name, bool IsReset) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 70 | // If name is empty just pop top. |
| 71 | if (!Name) { |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 72 | // An empty stack is a special case... |
| 73 | if (Stack.empty()) { |
| 74 | // If this isn't a reset, it is always an error. |
| 75 | if (!IsReset) |
| 76 | return false; |
| 77 | |
| 78 | // Otherwise, it is an error only if some alignment has been set. |
| 79 | if (!Alignment) |
| 80 | return false; |
| 81 | |
| 82 | // Otherwise, reset to the default alignment. |
| 83 | Alignment = 0; |
| 84 | } else { |
| 85 | Alignment = Stack.back().Alignment; |
| 86 | Stack.pop_back(); |
| 87 | } |
| 88 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 89 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 92 | // Otherwise, find the named record. |
| 93 | for (unsigned i = Stack.size(); i != 0; ) { |
| 94 | --i; |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 95 | if (Stack[i].Name == Name) { |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 96 | // Found it, pop up to and including this record. |
Daniel Dunbar | ae2232b | 2010-05-27 02:25:27 +0000 | [diff] [blame] | 97 | Alignment = Stack[i].Alignment; |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 98 | Stack.erase(Stack.begin() + i, Stack.end()); |
| 99 | return true; |
| 100 | } |
| 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /// FreePackedContext - Deallocate and null out PackContext. |
| 108 | void Sema::FreePackedContext() { |
| 109 | delete static_cast<PragmaPackStack*>(PackContext); |
| 110 | PackContext = 0; |
| 111 | } |
| 112 | |
Daniel Dunbar | 9f21f89 | 2010-05-27 01:53:40 +0000 | [diff] [blame] | 113 | void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { |
| 114 | // If there is no pack context, we don't need any attributes. |
| 115 | if (!PackContext) |
| 116 | return; |
| 117 | |
| 118 | PragmaPackStack *Stack = static_cast<PragmaPackStack*>(PackContext); |
| 119 | |
| 120 | // Otherwise, check to see if we need a max field alignment attribute. |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 121 | if (unsigned Alignment = Stack->getAlignment()) { |
| 122 | if (Alignment == PackStackEntry::kMac68kAlignmentSentinel) |
| 123 | RD->addAttr(::new (Context) AlignMac68kAttr()); |
| 124 | else |
| 125 | RD->addAttr(::new (Context) MaxFieldAlignmentAttr(Alignment * 8)); |
| 126 | } |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 129 | void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, |
| 130 | SourceLocation PragmaLoc, |
| 131 | SourceLocation KindLoc) { |
| 132 | if (PackContext == 0) |
| 133 | PackContext = new PragmaPackStack(); |
| 134 | |
| 135 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
| 136 | |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 137 | // Reset just pops the top of the stack, or resets the current alignment to |
| 138 | // default. |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 139 | if (Kind == Action::POAK_Reset) { |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 140 | if (!Context->pop(0, /*IsReset=*/true)) { |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 141 | Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) |
| 142 | << "stack empty"; |
| 143 | } |
| 144 | return; |
| 145 | } |
| 146 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 147 | switch (Kind) { |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 148 | // For all targets we support native and natural are the same. |
| 149 | // |
| 150 | // FIXME: This is not true on Darwin/PPC. |
| 151 | case POAK_Native: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 152 | case POAK_Power: |
Daniel Dunbar | d6b305d | 2010-05-28 20:08:00 +0000 | [diff] [blame] | 153 | case POAK_Natural: |
Daniel Dunbar | 450f793 | 2010-05-28 19:43:33 +0000 | [diff] [blame] | 154 | Context->push(0); |
| 155 | Context->setAlignment(0); |
| 156 | break; |
| 157 | |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 158 | // Note that '#pragma options align=packed' is not equivalent to attribute |
| 159 | // packed, it has a different precedence relative to attribute aligned. |
| 160 | case POAK_Packed: |
| 161 | Context->push(0); |
| 162 | Context->setAlignment(1); |
| 163 | break; |
| 164 | |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 165 | case POAK_Mac68k: |
| 166 | // Check if the target supports this. |
| 167 | if (!PP.getTargetInfo().hasAlignMac68kSupport()) { |
| 168 | Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); |
| 169 | return; |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 170 | } |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 171 | Context->push(0); |
| 172 | Context->setAlignment(PackStackEntry::kMac68kAlignmentSentinel); |
Daniel Dunbar | 613fd67 | 2010-05-27 00:35:16 +0000 | [diff] [blame] | 173 | break; |
| 174 | |
Daniel Dunbar | ea75a82 | 2010-05-27 00:04:40 +0000 | [diff] [blame] | 175 | default: |
| 176 | Diag(PragmaLoc, diag::warn_pragma_options_align_unsupported_option) |
| 177 | << KindLoc; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | void Sema::ActOnPragmaPack(PragmaPackKind Kind, IdentifierInfo *Name, |
| 183 | ExprTy *alignment, SourceLocation PragmaLoc, |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 184 | SourceLocation LParenLoc, SourceLocation RParenLoc) { |
| 185 | Expr *Alignment = static_cast<Expr *>(alignment); |
| 186 | |
| 187 | // If specified then alignment must be a "small" power of two. |
| 188 | unsigned AlignmentVal = 0; |
| 189 | if (Alignment) { |
| 190 | llvm::APSInt Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 192 | // pack(0) is like pack(), which just works out since that is what |
| 193 | // we use 0 for in PackAttr. |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 194 | if (Alignment->isTypeDependent() || |
| 195 | Alignment->isValueDependent() || |
| 196 | !Alignment->isIntegerConstantExpr(Val, Context) || |
Daniel Dunbar | 79cd116 | 2009-03-06 20:45:54 +0000 | [diff] [blame] | 197 | !(Val == 0 || Val.isPowerOf2()) || |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 198 | Val.getZExtValue() > 16) { |
| 199 | Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 200 | return; // Ignore |
| 201 | } |
| 202 | |
| 203 | AlignmentVal = (unsigned) Val.getZExtValue(); |
| 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 206 | if (PackContext == 0) |
| 207 | PackContext = new PragmaPackStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 209 | PragmaPackStack *Context = static_cast<PragmaPackStack*>(PackContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 211 | switch (Kind) { |
| 212 | case Action::PPK_Default: // pack([n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 213 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | case Action::PPK_Show: // pack(show) |
| 217 | // Show the current alignment, making sure to show the right value |
| 218 | // for the default. |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 219 | AlignmentVal = Context->getAlignment(); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 220 | // FIXME: This should come from the target. |
| 221 | if (AlignmentVal == 0) |
| 222 | AlignmentVal = 8; |
Daniel Dunbar | c6082fe | 2010-05-27 05:45:51 +0000 | [diff] [blame] | 223 | if (AlignmentVal == PackStackEntry::kMac68kAlignmentSentinel) |
| 224 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; |
| 225 | else |
| 226 | Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 227 | break; |
| 228 | |
| 229 | case Action::PPK_Push: // pack(push [, id] [, [n]) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 230 | Context->push(Name); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 231 | // Set the new alignment if specified. |
| 232 | if (Alignment) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 234 | break; |
| 235 | |
| 236 | case Action::PPK_Pop: // pack(pop [, id] [, n]) |
| 237 | // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: |
| 238 | // "#pragma pack(pop, identifier, n) is undefined" |
| 239 | if (Alignment && Name) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifer_and_alignment); |
| 241 | |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 242 | // Do the pop. |
Daniel Dunbar | ddc6ff6 | 2010-07-16 04:54:16 +0000 | [diff] [blame] | 243 | if (!Context->pop(Name, /*IsReset=*/false)) { |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 244 | // If a name was specified then failure indicates the name |
| 245 | // wasn't found. Otherwise failure indicates the stack was |
| 246 | // empty. |
| 247 | Diag(PragmaLoc, diag::warn_pragma_pack_pop_failed) |
| 248 | << (Name ? "no record matching name" : "stack empty"); |
| 249 | |
| 250 | // FIXME: Warn about popping named records as MSVC does. |
| 251 | } else { |
| 252 | // Pop succeeded, set the new alignment if specified. |
| 253 | if (Alignment) |
Chris Lattner | 574aa40 | 2009-02-17 01:09:29 +0000 | [diff] [blame] | 254 | Context->setAlignment(AlignmentVal); |
Chris Lattner | 5a0c351 | 2009-02-17 00:57:29 +0000 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | |
| 258 | default: |
| 259 | assert(0 && "Invalid #pragma pack kind."); |
| 260 | } |
| 261 | } |
| 262 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 263 | void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers, |
| 264 | Scope *curScope, |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 265 | SourceLocation PragmaLoc, |
| 266 | SourceLocation LParenLoc, |
| 267 | SourceLocation RParenLoc) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 269 | for (unsigned i = 0; i < NumIdentifiers; ++i) { |
| 270 | const Token &Tok = Identifiers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 272 | LookupResult Lookup(*this, Name, Tok.getLocation(), LookupOrdinaryName); |
| 273 | LookupParsedName(Lookup, curScope, NULL, true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 274 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 275 | if (Lookup.empty()) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 276 | Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) |
| 277 | << Name << SourceRange(Tok.getLocation()); |
| 278 | continue; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 279 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 281 | VarDecl *VD = Lookup.getAsSingle<VarDecl>(); |
| 282 | if (!VD || !VD->hasLocalStorage()) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 283 | Diag(PragmaLoc, diag::warn_pragma_unused_expected_localvar) |
| 284 | << Name << SourceRange(Tok.getLocation()); |
| 285 | continue; |
| 286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 288 | VD->addAttr(::new (Context) UnusedAttr()); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 291 | |
| 292 | typedef std::vector<VisibilityAttr::VisibilityTypes> VisStack; |
| 293 | |
| 294 | void Sema::AddPushedVisibilityAttribute(Decl *D) { |
| 295 | if (!VisContext) |
| 296 | return; |
| 297 | |
| 298 | if (D->hasAttr<VisibilityAttr>()) |
| 299 | return; |
| 300 | |
| 301 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 302 | VisibilityAttr::VisibilityTypes type = Stack->back(); |
| 303 | |
| 304 | D->addAttr(::new (Context) VisibilityAttr(type, true)); |
| 305 | } |
| 306 | |
| 307 | /// FreeVisContext - Deallocate and null out VisContext. |
| 308 | void Sema::FreeVisContext() { |
| 309 | delete static_cast<VisStack*>(VisContext); |
| 310 | VisContext = 0; |
| 311 | } |
| 312 | |
| 313 | void Sema::ActOnPragmaVisibility(bool IsPush, const IdentifierInfo* VisType, |
| 314 | SourceLocation PragmaLoc) { |
| 315 | if (IsPush) { |
| 316 | // Compute visibility to use. |
| 317 | VisibilityAttr::VisibilityTypes type; |
| 318 | if (VisType->isStr("default")) |
| 319 | type = VisibilityAttr::DefaultVisibility; |
| 320 | else if (VisType->isStr("hidden")) |
| 321 | type = VisibilityAttr::HiddenVisibility; |
| 322 | else if (VisType->isStr("internal")) |
| 323 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 324 | else if (VisType->isStr("protected")) |
| 325 | type = VisibilityAttr::ProtectedVisibility; |
| 326 | else { |
| 327 | Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << |
| 328 | VisType->getName(); |
| 329 | return; |
| 330 | } |
| 331 | PushPragmaVisibility(type); |
| 332 | } else { |
| 333 | PopPragmaVisibility(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void Sema::PushPragmaVisibility(VisibilityAttr::VisibilityTypes type) { |
| 338 | // Put visibility on stack. |
| 339 | if (!VisContext) |
| 340 | VisContext = new VisStack; |
| 341 | |
| 342 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 343 | Stack->push_back(type); |
| 344 | } |
| 345 | |
| 346 | void Sema::PopPragmaVisibility() { |
| 347 | // Pop visibility from stack, if there is one on the stack. |
| 348 | if (VisContext) { |
| 349 | VisStack *Stack = static_cast<VisStack*>(VisContext); |
| 350 | |
| 351 | Stack->pop_back(); |
| 352 | // To simplify the implementation, never keep around an empty stack. |
| 353 | if (Stack->empty()) |
| 354 | FreeVisContext(); |
| 355 | } |
| 356 | // FIXME: Add diag for pop without push. |
| 357 | } |