Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | e184bae | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTMutationListener.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 26 | #include "clang/Basic/IdentifierTable.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Specifiers.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 33 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 36 | static const VisibilityAttr *GetExplicitVisibility(const Decl *D) { |
| 37 | // If the decl is redeclarable, make sure we use the explicit |
| 38 | // visibility attribute from the most recent declaration. |
| 39 | // |
| 40 | // Note that this isn't necessary for tags, which can't have their |
| 41 | // visibility adjusted. |
| 42 | if (isa<VarDecl>(D)) { |
| 43 | return cast<VarDecl>(D)->getMostRecentDeclaration() |
| 44 | ->getAttr<VisibilityAttr>(); |
| 45 | } else if (isa<FunctionDecl>(D)) { |
| 46 | return cast<FunctionDecl>(D)->getMostRecentDeclaration() |
| 47 | ->getAttr<VisibilityAttr>(); |
| 48 | } else { |
| 49 | return D->getAttr<VisibilityAttr>(); |
| 50 | } |
| 51 | } |
| 52 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 53 | static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) { |
| 54 | switch (A->getVisibility()) { |
| 55 | case VisibilityAttr::Default: |
| 56 | return DefaultVisibility; |
| 57 | case VisibilityAttr::Hidden: |
| 58 | return HiddenVisibility; |
| 59 | case VisibilityAttr::Protected: |
| 60 | return ProtectedVisibility; |
| 61 | } |
| 62 | return DefaultVisibility; |
| 63 | } |
| 64 | |
| 65 | typedef std::pair<Linkage,Visibility> LVPair; |
| 66 | static LVPair merge(LVPair L, LVPair R) { |
| 67 | return LVPair(minLinkage(L.first, R.first), |
| 68 | minVisibility(L.second, R.second)); |
| 69 | } |
| 70 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 71 | /// \brief Get the most restrictive linkage for the types in the given |
| 72 | /// template parameter list. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 73 | static LVPair |
| 74 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
| 75 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 76 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 77 | PEnd = Params->end(); |
| 78 | P != PEnd; ++P) { |
| 79 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) |
| 80 | if (!NTTP->getType()->isDependentType()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 81 | LV = merge(LV, NTTP->getType()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if (TemplateTemplateParmDecl *TTP |
| 86 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 87 | LV = |
| 88 | merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 92 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /// \brief Get the most restrictive linkage for the types and |
| 96 | /// declarations in the given template argument list. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 97 | static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args, |
| 98 | unsigned NumArgs) { |
| 99 | LVPair LV(ExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 100 | |
| 101 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 102 | switch (Args[I].getKind()) { |
| 103 | case TemplateArgument::Null: |
| 104 | case TemplateArgument::Integral: |
| 105 | case TemplateArgument::Expression: |
| 106 | break; |
| 107 | |
| 108 | case TemplateArgument::Type: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 109 | LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 110 | break; |
| 111 | |
| 112 | case TemplateArgument::Declaration: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 113 | // The decl can validly be null as the representation of nullptr |
| 114 | // arguments, valid only in C++0x. |
| 115 | if (Decl *D = Args[I].getAsDecl()) { |
| 116 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 117 | LV = merge(LV, ND->getLinkageAndVisibility()); |
| 118 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 119 | LV = merge(LV, VD->getType()->getLinkageAndVisibility()); |
| 120 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 121 | break; |
| 122 | |
| 123 | case TemplateArgument::Template: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 124 | if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl()) |
| 125 | LV = merge(LV, Template->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case TemplateArgument::Pack: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 129 | LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(), |
| 130 | Args[I].pack_size())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 135 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 136 | } |
| 137 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 138 | static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) { |
| 139 | return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(), |
| 140 | TArgs.flat_size()); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 141 | } |
| 142 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 143 | static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 144 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 145 | "Not a name having namespace scope"); |
| 146 | ASTContext &Context = D->getASTContext(); |
| 147 | |
| 148 | // C++ [basic.link]p3: |
| 149 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 150 | // is the name of |
| 151 | // - an object, reference, function or function template that is |
| 152 | // explicitly declared static; or, |
| 153 | // (This bullet corresponds to C99 6.2.2p3.) |
| 154 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 155 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 156 | if (Var->getStorageClass() == SC_Static) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 157 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 158 | |
| 159 | // - an object or reference that is explicitly declared const |
| 160 | // and neither explicitly declared extern nor previously |
| 161 | // declared to have external linkage; or |
| 162 | // (there is no equivalent in C99) |
| 163 | if (Context.getLangOptions().CPlusPlus && |
Eli Friedman | e9d6554 | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 164 | Var->getType().isConstant(Context) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 165 | Var->getStorageClass() != SC_Extern && |
| 166 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 167 | bool FoundExtern = false; |
| 168 | for (const VarDecl *PrevVar = Var->getPreviousDeclaration(); |
| 169 | PrevVar && !FoundExtern; |
| 170 | PrevVar = PrevVar->getPreviousDeclaration()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 171 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 172 | FoundExtern = true; |
| 173 | |
| 174 | if (!FoundExtern) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 175 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 176 | } |
| 177 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 178 | // C++ [temp]p4: |
| 179 | // A non-member function template can have internal linkage; any |
| 180 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 181 | const FunctionDecl *Function = 0; |
| 182 | if (const FunctionTemplateDecl *FunTmpl |
| 183 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 184 | Function = FunTmpl->getTemplatedDecl(); |
| 185 | else |
| 186 | Function = cast<FunctionDecl>(D); |
| 187 | |
| 188 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 189 | if (Function->getStorageClass() == SC_Static) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 190 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 191 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 192 | // - a data member of an anonymous union. |
| 193 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 194 | return LVPair(InternalLinkage, DefaultVisibility); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 195 | } |
| 196 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 197 | if (D->isInAnonymousNamespace()) |
| 198 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
| 199 | |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 200 | const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D); |
| 201 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 202 | // Set up the defaults. |
| 203 | |
| 204 | // C99 6.2.2p5: |
| 205 | // If the declaration of an identifier for an object has file |
| 206 | // scope and no storage-class specifier, its linkage is |
| 207 | // external. |
| 208 | LVPair LV(ExternalLinkage, DefaultVisibility); |
| 209 | |
| 210 | // We ignore -fvisibility on non-definitions and explicit |
| 211 | // instantiation declarations. |
| 212 | bool ConsiderDashFVisibility = true; |
| 213 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 214 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 216 | // A name having namespace scope has external linkage if it is the |
| 217 | // name of |
| 218 | // |
| 219 | // - an object or reference, unless it has internal linkage; or |
| 220 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 221 | // Modify the variable's LV by the LV of its type unless this is |
| 222 | // C or extern "C". This follows from [basic.link]p9: |
| 223 | // A type without linkage shall not be used as the type of a |
| 224 | // variable or function with external linkage unless |
| 225 | // - the entity has C language linkage, or |
| 226 | // - the entity is declared within an unnamed namespace, or |
| 227 | // - the entity is not used or is defined in the same |
| 228 | // translation unit. |
| 229 | // and [basic.link]p10: |
| 230 | // ...the types specified by all declarations referring to a |
| 231 | // given variable or function shall be identical... |
| 232 | // C does not have an equivalent rule. |
| 233 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 234 | // Ignore this if we've got an explicit attribute; the user |
| 235 | // probably knows what they're doing. |
| 236 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 237 | // Note that we don't want to make the variable non-external |
| 238 | // because of this, but unique-external linkage suits us. |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 239 | if (Context.getLangOptions().CPlusPlus && !Var->isExternC() && |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 240 | !ExplicitVisibility) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 241 | LVPair TypeLV = Var->getType()->getLinkageAndVisibility(); |
| 242 | if (TypeLV.first != ExternalLinkage) |
| 243 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
| 244 | LV.second = minVisibility(LV.second, TypeLV.second); |
| 245 | } |
| 246 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 247 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 248 | (Var->getStorageClass() == SC_Extern || |
| 249 | Var->getStorageClass() == SC_PrivateExtern)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 250 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 251 | LV.second = HiddenVisibility; |
| 252 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 253 | // C99 6.2.2p4: |
| 254 | // For an identifier declared with the storage-class specifier |
| 255 | // extern in a scope in which a prior declaration of that |
| 256 | // identifier is visible, if the prior declaration specifies |
| 257 | // internal or external linkage, the linkage of the identifier |
| 258 | // at the later declaration is the same as the linkage |
| 259 | // specified at the prior declaration. If no prior declaration |
| 260 | // is visible, or if the prior declaration specifies no |
| 261 | // linkage, then the identifier has external linkage. |
| 262 | if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 263 | LVPair PrevLV = PrevVar->getLinkageAndVisibility(); |
| 264 | if (PrevLV.first) LV.first = PrevLV.first; |
| 265 | LV.second = minVisibility(LV.second, PrevLV.second); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 269 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 270 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 271 | // In theory, we can modify the function's LV by the LV of its |
| 272 | // type unless it has C linkage (see comment above about variables |
| 273 | // for justification). In practice, GCC doesn't do this, so it's |
| 274 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 276 | // C99 6.2.2p5: |
| 277 | // If the declaration of an identifier for a function has no |
| 278 | // storage-class specifier, its linkage is determined exactly |
| 279 | // as if it were declared with the storage-class specifier |
| 280 | // extern. |
| 281 | if (!Context.getLangOptions().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 282 | (Function->getStorageClass() == SC_Extern || |
| 283 | Function->getStorageClass() == SC_PrivateExtern || |
| 284 | Function->getStorageClass() == SC_None)) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 285 | // C99 6.2.2p4: |
| 286 | // For an identifier declared with the storage-class specifier |
| 287 | // extern in a scope in which a prior declaration of that |
| 288 | // identifier is visible, if the prior declaration specifies |
| 289 | // internal or external linkage, the linkage of the identifier |
| 290 | // at the later declaration is the same as the linkage |
| 291 | // specified at the prior declaration. If no prior declaration |
| 292 | // is visible, or if the prior declaration specifies no |
| 293 | // linkage, then the identifier has external linkage. |
| 294 | if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 295 | LVPair PrevLV = PrevFunc->getLinkageAndVisibility(); |
| 296 | if (PrevLV.first) LV.first = PrevLV.first; |
| 297 | LV.second = minVisibility(LV.second, PrevLV.second); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 301 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 302 | = Function->getTemplateSpecializationInfo()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 303 | LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 304 | const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 305 | LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs)); |
| 306 | |
| 307 | if (SpecInfo->getTemplateSpecializationKind() |
| 308 | == TSK_ExplicitInstantiationDeclaration) |
| 309 | ConsiderDashFVisibility = false; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 310 | } |
| 311 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 312 | if (ConsiderDashFVisibility) |
| 313 | ConsiderDashFVisibility = Function->hasBody(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 314 | |
| 315 | // - a named class (Clause 9), or an unnamed class defined in a |
| 316 | // typedef declaration in which the class has the typedef name |
| 317 | // for linkage purposes (7.1.3); or |
| 318 | // - a named enumeration (7.2), or an unnamed enumeration |
| 319 | // defined in a typedef declaration in which the enumeration |
| 320 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 321 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 322 | // Unnamed tags have no linkage. |
| 323 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) |
| 324 | return LVPair(NoLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 325 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 326 | // If this is a class template specialization, consider the |
| 327 | // linkage of the template and template arguments. |
| 328 | if (const ClassTemplateSpecializationDecl *Spec |
| 329 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
| 330 | // From the template. Note below the restrictions on how we |
| 331 | // compute template visibility. |
| 332 | LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 333 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 334 | // The arguments at which the template was instantiated. |
| 335 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 336 | LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs)); |
| 337 | |
| 338 | if (Spec->getTemplateSpecializationKind() |
| 339 | == TSK_ExplicitInstantiationDeclaration) |
| 340 | ConsiderDashFVisibility = false; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 341 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 342 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 343 | // Consider -fvisibility unless the type has C linkage. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 344 | if (ConsiderDashFVisibility) |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 345 | ConsiderDashFVisibility = |
| 346 | (Context.getLangOptions().CPlusPlus && |
| 347 | !Tag->getDeclContext()->isExternCContext()); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 349 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 350 | } else if (isa<EnumConstantDecl>(D)) { |
| 351 | LVPair EnumLV = |
| 352 | cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility(); |
| 353 | if (!isExternalLinkage(EnumLV.first)) |
| 354 | return LVPair(NoLinkage, DefaultVisibility); |
| 355 | LV = merge(LV, EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 356 | |
| 357 | // - a template, unless it is a function template that has |
| 358 | // internal linkage (Clause 14); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 359 | } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 360 | LV = merge(LV, getLVForTemplateParameterList( |
| 361 | Template->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 362 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 363 | // We do not want to consider attributes or global settings when |
| 364 | // computing template visibility. |
| 365 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 366 | |
| 367 | // - a namespace (7.3), unless it is declared within an unnamed |
| 368 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 369 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 370 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 371 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 372 | // By extension, we assign external linkage to Objective-C |
| 373 | // interfaces. |
| 374 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 375 | // fallout |
| 376 | |
| 377 | // Everything not covered here has no linkage. |
| 378 | } else { |
| 379 | return LVPair(NoLinkage, DefaultVisibility); |
| 380 | } |
| 381 | |
| 382 | // If we ended up with non-external linkage, visibility should |
| 383 | // always be default. |
| 384 | if (LV.first != ExternalLinkage) |
| 385 | return LVPair(LV.first, DefaultVisibility); |
| 386 | |
| 387 | // If we didn't end up with hidden visibility, consider attributes |
| 388 | // and -fvisibility. |
| 389 | if (LV.second != HiddenVisibility) { |
| 390 | Visibility StandardV; |
| 391 | |
| 392 | // If we have an explicit visibility attribute, merge that in. |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 393 | if (ExplicitVisibility) |
| 394 | StandardV = GetVisibilityFromAttr(ExplicitVisibility); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 395 | else if (ConsiderDashFVisibility) |
| 396 | StandardV = Context.getLangOptions().getVisibilityMode(); |
| 397 | else |
| 398 | StandardV = DefaultVisibility; // no-op |
| 399 | |
| 400 | LV.second = minVisibility(LV.second, StandardV); |
| 401 | } |
| 402 | |
| 403 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 406 | static LVPair getLVForClassMember(const NamedDecl *D) { |
| 407 | // Only certain class members have linkage. Note that fields don't |
| 408 | // really have linkage, but it's convenient to say they do for the |
| 409 | // purposes of calculating linkage of pointer-to-data-member |
| 410 | // template arguments. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 411 | if (!(isa<CXXMethodDecl>(D) || |
| 412 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 413 | isa<FieldDecl>(D) || |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 414 | (isa<TagDecl>(D) && |
| 415 | (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl())))) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 416 | return LVPair(NoLinkage, DefaultVisibility); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 417 | |
| 418 | // Class members only have linkage if their class has external linkage. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 419 | LVPair ClassLV = |
| 420 | cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility(); |
| 421 | if (!isExternalLinkage(ClassLV.first)) |
| 422 | return LVPair(NoLinkage, DefaultVisibility); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 423 | |
| 424 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 425 | if (ClassLV.first == UniqueExternalLinkage) |
| 426 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 427 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 428 | // Start with the class's linkage and visibility. |
| 429 | LVPair LV = ClassLV; |
| 430 | |
| 431 | // If we have an explicit visibility attribute, merge that in. |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 432 | const VisibilityAttr *VA = GetExplicitVisibility(D); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 433 | if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA)); |
| 434 | |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 435 | // If it's a variable declaration and we don't have an explicit |
| 436 | // visibility attribute, apply the LV from its type. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 437 | // See the comment about namespace-scope variable decls above. |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 438 | if (!VA && isa<VarDecl>(D)) { |
| 439 | LVPair TypeLV = cast<VarDecl>(D)->getType()->getLinkageAndVisibility(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 440 | if (TypeLV.first != ExternalLinkage) |
| 441 | LV.first = minLinkage(LV.first, UniqueExternalLinkage); |
| 442 | LV.second = minVisibility(LV.second, TypeLV.second); |
| 443 | } |
| 444 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 445 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 446 | // If this is a method template specialization, use the linkage for |
| 447 | // the template parameters and arguments. |
| 448 | if (FunctionTemplateSpecializationInfo *Spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 449 | = MD->getTemplateSpecializationInfo()) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 450 | LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments)); |
| 451 | LV = merge(LV, getLVForTemplateParameterList( |
| 452 | Spec->getTemplate()->getTemplateParameters())); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 455 | // If -fvisibility-inlines-hidden was provided, then inline C++ |
| 456 | // member functions get "hidden" visibility if they don't have an |
| 457 | // explicit visibility attribute. |
| 458 | if (!VA && MD->isInlined() && LV.second > HiddenVisibility && |
| 459 | D->getASTContext().getLangOptions().InlineVisibilityHidden) |
| 460 | LV.second = HiddenVisibility; |
| 461 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 462 | // Similarly for member class template specializations. |
| 463 | } else if (const ClassTemplateSpecializationDecl *Spec |
| 464 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 465 | LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs())); |
| 466 | LV = merge(LV, getLVForTemplateParameterList( |
| 467 | Spec->getSpecializedTemplate()->getTemplateParameters())); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 468 | } |
| 469 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 470 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 471 | } |
| 472 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 473 | LVPair NamedDecl::getLinkageAndVisibility() const { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 474 | |
| 475 | // Objective-C: treat all Objective-C declarations as having external |
| 476 | // linkage. |
| 477 | switch (getKind()) { |
| 478 | default: |
| 479 | break; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 480 | case Decl::TemplateTemplateParm: // count these as external |
| 481 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 482 | case Decl::ObjCAtDefsField: |
| 483 | case Decl::ObjCCategory: |
| 484 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 485 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 486 | case Decl::ObjCForwardProtocol: |
| 487 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 488 | case Decl::ObjCMethod: |
| 489 | case Decl::ObjCProperty: |
| 490 | case Decl::ObjCPropertyImpl: |
| 491 | case Decl::ObjCProtocol: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 492 | return LVPair(ExternalLinkage, DefaultVisibility); |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 495 | // Handle linkage for namespace-scope names. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 496 | if (getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 497 | return getLVForNamespaceScopeDecl(this); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 498 | |
| 499 | // C++ [basic.link]p5: |
| 500 | // In addition, a member function, static data member, a named |
| 501 | // class or enumeration of class scope, or an unnamed class or |
| 502 | // enumeration defined in a class-scope typedef declaration such |
| 503 | // that the class or enumeration has the typedef name for linkage |
| 504 | // purposes (7.1.3), has external linkage if the name of the class |
| 505 | // has external linkage. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 506 | if (getDeclContext()->isRecord()) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 507 | return getLVForClassMember(this); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 508 | |
| 509 | // C++ [basic.link]p6: |
| 510 | // The name of a function declared in block scope and the name of |
| 511 | // an object declared by a block scope extern declaration have |
| 512 | // linkage. If there is a visible declaration of an entity with |
| 513 | // linkage having the same name and type, ignoring entities |
| 514 | // declared outside the innermost enclosing namespace scope, the |
| 515 | // block scope declaration declares that same entity and receives |
| 516 | // the linkage of the previous declaration. If there is more than |
| 517 | // one such matching entity, the program is ill-formed. Otherwise, |
| 518 | // if no matching entity is found, the block scope entity receives |
| 519 | // external linkage. |
| 520 | if (getLexicalDeclContext()->isFunctionOrMethod()) { |
| 521 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 522 | if (Function->isInAnonymousNamespace()) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 523 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 524 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 525 | LVPair LV(ExternalLinkage, DefaultVisibility); |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 526 | if (const VisibilityAttr *VA = GetExplicitVisibility(Function)) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 527 | LV.second = GetVisibilityFromAttr(VA); |
| 528 | |
| 529 | if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) { |
| 530 | LVPair PrevLV = Prev->getLinkageAndVisibility(); |
| 531 | if (PrevLV.first) LV.first = PrevLV.first; |
| 532 | LV.second = minVisibility(LV.second, PrevLV.second); |
| 533 | } |
| 534 | |
| 535 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 539 | if (Var->getStorageClass() == SC_Extern || |
| 540 | Var->getStorageClass() == SC_PrivateExtern) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 541 | if (Var->isInAnonymousNamespace()) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 542 | return LVPair(UniqueExternalLinkage, DefaultVisibility); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 543 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 544 | LVPair LV(ExternalLinkage, DefaultVisibility); |
| 545 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 546 | LV.second = HiddenVisibility; |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 547 | else if (const VisibilityAttr *VA = GetExplicitVisibility(Var)) |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 548 | LV.second = GetVisibilityFromAttr(VA); |
| 549 | |
| 550 | if (const VarDecl *Prev = Var->getPreviousDeclaration()) { |
| 551 | LVPair PrevLV = Prev->getLinkageAndVisibility(); |
| 552 | if (PrevLV.first) LV.first = PrevLV.first; |
| 553 | LV.second = minVisibility(LV.second, PrevLV.second); |
| 554 | } |
| 555 | |
| 556 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | // C++ [basic.link]p6: |
| 561 | // Names not covered by these rules have no linkage. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 562 | return LVPair(NoLinkage, DefaultVisibility); |
| 563 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 565 | std::string NamedDecl::getQualifiedNameAsString() const { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 566 | return getQualifiedNameAsString(getASTContext().getLangOptions()); |
| 567 | } |
| 568 | |
| 569 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 570 | const DeclContext *Ctx = getDeclContext(); |
| 571 | |
| 572 | if (Ctx->isFunctionOrMethod()) |
| 573 | return getNameAsString(); |
| 574 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 575 | typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy; |
| 576 | ContextsTy Contexts; |
| 577 | |
| 578 | // Collect contexts. |
| 579 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 580 | Contexts.push_back(Ctx); |
| 581 | Ctx = Ctx->getParent(); |
| 582 | }; |
| 583 | |
| 584 | std::string QualName; |
| 585 | llvm::raw_string_ostream OS(QualName); |
| 586 | |
| 587 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 588 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 590 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 591 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 592 | std::string TemplateArgsStr |
| 593 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 594 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 595 | TemplateArgs.flat_size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 596 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 597 | OS << Spec->getName() << TemplateArgsStr; |
| 598 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 599 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 600 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 601 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 602 | OS << ND; |
| 603 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 604 | if (!RD->getIdentifier()) |
| 605 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 606 | else |
| 607 | OS << RD; |
| 608 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 609 | const FunctionProtoType *FT = 0; |
| 610 | if (FD->hasWrittenPrototype()) |
| 611 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 612 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 613 | OS << FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 614 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 615 | unsigned NumParams = FD->getNumParams(); |
| 616 | for (unsigned i = 0; i < NumParams; ++i) { |
| 617 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 618 | OS << ", "; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 619 | std::string Param; |
| 620 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 621 | OS << Param; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | if (FT->isVariadic()) { |
| 625 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 626 | OS << ", "; |
| 627 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 630 | OS << ')'; |
| 631 | } else { |
| 632 | OS << cast<NamedDecl>(*I); |
| 633 | } |
| 634 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 635 | } |
| 636 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 637 | if (getDeclName()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 638 | OS << this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 639 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 640 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 641 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 642 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 645 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 646 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 647 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 648 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 649 | // We want to keep it, unless it nominates same namespace. |
| 650 | if (getKind() == Decl::UsingDirective) { |
| 651 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() == |
| 652 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace(); |
| 653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 655 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 656 | // For function declarations, we keep track of redeclarations. |
| 657 | return FD->getPreviousDeclaration() == OldD; |
| 658 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 659 | // For function templates, the underlying function declarations are linked. |
| 660 | if (const FunctionTemplateDecl *FunctionTemplate |
| 661 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 662 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 663 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 664 | return FunctionTemplate->getTemplatedDecl() |
| 665 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 667 | // For method declarations, we keep track of redeclarations. |
| 668 | if (isa<ObjCMethodDecl>(this)) |
| 669 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 671 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 672 | return true; |
| 673 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 674 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 675 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 676 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 677 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 678 | // For non-function declarations, if the declarations are of the |
| 679 | // same kind then this must be a redeclaration, or semantic analysis |
| 680 | // would not have given us the new declaration. |
| 681 | return this->getKind() == OldD->getKind(); |
| 682 | } |
| 683 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 684 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 685 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 686 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 688 | NamedDecl *NamedDecl::getUnderlyingDecl() { |
| 689 | NamedDecl *ND = this; |
| 690 | while (true) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 691 | if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 692 | ND = UD->getTargetDecl(); |
| 693 | else if (ObjCCompatibleAliasDecl *AD |
| 694 | = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 695 | return AD->getClassInterface(); |
| 696 | else |
| 697 | return ND; |
| 698 | } |
| 699 | } |
| 700 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 701 | bool NamedDecl::isCXXInstanceMember() const { |
| 702 | assert(isCXXClassMember() && |
| 703 | "checking whether non-member is instance member"); |
| 704 | |
| 705 | const NamedDecl *D = this; |
| 706 | if (isa<UsingShadowDecl>(D)) |
| 707 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 708 | |
| 709 | if (isa<FieldDecl>(D)) |
| 710 | return true; |
| 711 | if (isa<CXXMethodDecl>(D)) |
| 712 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 713 | if (isa<FunctionTemplateDecl>(D)) |
| 714 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 715 | ->getTemplatedDecl())->isInstance(); |
| 716 | return false; |
| 717 | } |
| 718 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 719 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 720 | // DeclaratorDecl Implementation |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 723 | template <typename DeclT> |
| 724 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 725 | if (decl->getNumTemplateParameterLists() > 0) |
| 726 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 727 | else |
| 728 | return decl->getInnerLocStart(); |
| 729 | } |
| 730 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 731 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 732 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 733 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 734 | return SourceLocation(); |
| 735 | } |
| 736 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 737 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 738 | SourceRange QualifierRange) { |
| 739 | if (Qualifier) { |
| 740 | // Make sure the extended decl info is allocated. |
| 741 | if (!hasExtInfo()) { |
| 742 | // Save (non-extended) type source info pointer. |
| 743 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 744 | // Allocate external info struct. |
| 745 | DeclInfo = new (getASTContext()) ExtInfo; |
| 746 | // Restore savedTInfo into (extended) decl info. |
| 747 | getExtInfo()->TInfo = savedTInfo; |
| 748 | } |
| 749 | // Set qualifier info. |
| 750 | getExtInfo()->NNS = Qualifier; |
| 751 | getExtInfo()->NNSRange = QualifierRange; |
| 752 | } |
| 753 | else { |
| 754 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 755 | assert(QualifierRange.isInvalid()); |
| 756 | if (hasExtInfo()) { |
| 757 | // Save type source info pointer. |
| 758 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 759 | // Deallocate the extended decl info. |
| 760 | getASTContext().Deallocate(getExtInfo()); |
| 761 | // Restore savedTInfo into (non-extended) decl info. |
| 762 | DeclInfo = savedTInfo; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 767 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 768 | return getTemplateOrInnerLocStart(this); |
| 769 | } |
| 770 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 771 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 772 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 773 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 774 | TemplateParameterList **TPLists) { |
| 775 | assert((NumTPLists == 0 || TPLists != 0) && |
| 776 | "Empty array of template parameters with positive size!"); |
| 777 | assert((NumTPLists == 0 || NNS) && |
| 778 | "Nonempty array of template parameters with no qualifier!"); |
| 779 | |
| 780 | // Free previous template parameters (if any). |
| 781 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 782 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 783 | TemplParamLists = 0; |
| 784 | NumTemplParamLists = 0; |
| 785 | } |
| 786 | // Set info on matched template parameter lists (if any). |
| 787 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 788 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 789 | NumTemplParamLists = NumTPLists; |
| 790 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 791 | TemplParamLists[i] = TPLists[i]; |
| 792 | } |
| 793 | } |
| 794 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 795 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 796 | // VarDecl Implementation |
| 797 | //===----------------------------------------------------------------------===// |
| 798 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 799 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 800 | switch (SC) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 801 | case SC_None: break; |
| 802 | case SC_Auto: return "auto"; break; |
| 803 | case SC_Extern: return "extern"; break; |
| 804 | case SC_PrivateExtern: return "__private_extern__"; break; |
| 805 | case SC_Register: return "register"; break; |
| 806 | case SC_Static: return "static"; break; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | assert(0 && "Invalid storage class"); |
| 810 | return 0; |
| 811 | } |
| 812 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 813 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 814 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 815 | StorageClass S, StorageClass SCAsWritten) { |
| 816 | return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 819 | SourceLocation VarDecl::getInnerLocStart() const { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 820 | SourceLocation Start = getTypeSpecStartLoc(); |
| 821 | if (Start.isInvalid()) |
| 822 | Start = getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 823 | return Start; |
| 824 | } |
| 825 | |
| 826 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 827 | if (getInit()) |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 828 | return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); |
| 829 | return SourceRange(getOuterLocStart(), getLocation()); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 832 | bool VarDecl::isExternC() const { |
| 833 | ASTContext &Context = getASTContext(); |
| 834 | if (!Context.getLangOptions().CPlusPlus) |
| 835 | return (getDeclContext()->isTranslationUnit() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 836 | getStorageClass() != SC_Static) || |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 837 | (getDeclContext()->isFunctionOrMethod() && hasExternalStorage()); |
| 838 | |
| 839 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
| 840 | DC = DC->getParent()) { |
| 841 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 842 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 843 | return getStorageClass() != SC_Static; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 844 | |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | if (DC->isFunctionOrMethod()) |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | VarDecl *VarDecl::getCanonicalDecl() { |
| 856 | return getFirstDeclaration(); |
| 857 | } |
| 858 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 859 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const { |
| 860 | // C++ [basic.def]p2: |
| 861 | // A declaration is a definition unless [...] it contains the 'extern' |
| 862 | // specifier or a linkage-specification and neither an initializer [...], |
| 863 | // it declares a static data member in a class declaration [...]. |
| 864 | // C++ [temp.expl.spec]p15: |
| 865 | // An explicit specialization of a static data member of a template is a |
| 866 | // definition if the declaration includes an initializer; otherwise, it is |
| 867 | // a declaration. |
| 868 | if (isStaticDataMember()) { |
| 869 | if (isOutOfLine() && (hasInit() || |
| 870 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 871 | return Definition; |
| 872 | else |
| 873 | return DeclarationOnly; |
| 874 | } |
| 875 | // C99 6.7p5: |
| 876 | // A definition of an identifier is a declaration for that identifier that |
| 877 | // [...] causes storage to be reserved for that object. |
| 878 | // Note: that applies for all non-file-scope objects. |
| 879 | // C99 6.9.2p1: |
| 880 | // If the declaration of an identifier for an object has file scope and an |
| 881 | // initializer, the declaration is an external definition for the identifier |
| 882 | if (hasInit()) |
| 883 | return Definition; |
| 884 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 885 | if (hasExternalStorage()) |
| 886 | return DeclarationOnly; |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 887 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 888 | if (getStorageClassAsWritten() == SC_Extern || |
| 889 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 890 | for (const VarDecl *PrevVar = getPreviousDeclaration(); |
| 891 | PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) { |
| 892 | if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) |
| 893 | return DeclarationOnly; |
| 894 | } |
| 895 | } |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 896 | // C99 6.9.2p2: |
| 897 | // A declaration of an object that has file scope without an initializer, |
| 898 | // and without a storage class specifier or the scs 'static', constitutes |
| 899 | // a tentative definition. |
| 900 | // No such thing in C++. |
| 901 | if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl()) |
| 902 | return TentativeDefinition; |
| 903 | |
| 904 | // What's left is (in C, block-scope) declarations without initializers or |
| 905 | // external storage. These are definitions. |
| 906 | return Definition; |
| 907 | } |
| 908 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 909 | VarDecl *VarDecl::getActingDefinition() { |
| 910 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 911 | if (Kind != TentativeDefinition) |
| 912 | return 0; |
| 913 | |
Chris Lattner | f0ed9ef | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 914 | VarDecl *LastTentative = 0; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 915 | VarDecl *First = getFirstDeclaration(); |
| 916 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 917 | I != E; ++I) { |
| 918 | Kind = (*I)->isThisDeclarationADefinition(); |
| 919 | if (Kind == Definition) |
| 920 | return 0; |
| 921 | else if (Kind == TentativeDefinition) |
| 922 | LastTentative = *I; |
| 923 | } |
| 924 | return LastTentative; |
| 925 | } |
| 926 | |
| 927 | bool VarDecl::isTentativeDefinitionNow() const { |
| 928 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 929 | if (Kind != TentativeDefinition) |
| 930 | return false; |
| 931 | |
| 932 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 933 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 934 | return false; |
| 935 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 936 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 939 | VarDecl *VarDecl::getDefinition() { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 940 | VarDecl *First = getFirstDeclaration(); |
| 941 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 942 | I != E; ++I) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 943 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 944 | return *I; |
| 945 | } |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 950 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 951 | while (I != E && !I->getInit()) |
| 952 | ++I; |
| 953 | |
| 954 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 955 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 956 | return I->getInit(); |
| 957 | } |
| 958 | return 0; |
| 959 | } |
| 960 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 961 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 962 | if (Decl::isOutOfLine()) |
| 963 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 964 | |
| 965 | if (!isStaticDataMember()) |
| 966 | return false; |
| 967 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 968 | // If this static data member was instantiated from a static data member of |
| 969 | // a class template, check whether that static data member was defined |
| 970 | // out-of-line. |
| 971 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 972 | return VD->isOutOfLine(); |
| 973 | |
| 974 | return false; |
| 975 | } |
| 976 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 977 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 978 | if (!isStaticDataMember()) |
| 979 | return 0; |
| 980 | |
| 981 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 982 | RD != RDEnd; ++RD) { |
| 983 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 984 | return *RD; |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 990 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 991 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 992 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 993 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | Init = I; |
| 997 | } |
| 998 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 999 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1000 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1001 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1006 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1007 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1008 | return MSI->getTemplateSpecializationKind(); |
| 1009 | |
| 1010 | return TSK_Undeclared; |
| 1011 | } |
| 1012 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1013 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1014 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 1015 | } |
| 1016 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1017 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1018 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1019 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1020 | assert(MSI && "Not an instantiated static data member?"); |
| 1021 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1022 | if (TSK != TSK_ExplicitSpecialization && |
| 1023 | PointOfInstantiation.isValid() && |
| 1024 | MSI->getPointOfInstantiation().isInvalid()) |
| 1025 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1028 | //===----------------------------------------------------------------------===// |
| 1029 | // ParmVarDecl Implementation |
| 1030 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1031 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1032 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1033 | SourceLocation L, IdentifierInfo *Id, |
| 1034 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1035 | StorageClass S, StorageClass SCAsWritten, |
| 1036 | Expr *DefArg) { |
| 1037 | return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, |
| 1038 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1041 | Expr *ParmVarDecl::getDefaultArg() { |
| 1042 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1043 | assert(!hasUninstantiatedDefaultArg() && |
| 1044 | "Default argument is not yet instantiated!"); |
| 1045 | |
| 1046 | Expr *Arg = getInit(); |
| 1047 | if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg)) |
| 1048 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1049 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1050 | return Arg; |
| 1051 | } |
| 1052 | |
| 1053 | unsigned ParmVarDecl::getNumDefaultArgTemporaries() const { |
| 1054 | if (const CXXExprWithTemporaries *E = |
| 1055 | dyn_cast<CXXExprWithTemporaries>(getInit())) |
| 1056 | return E->getNumTemporaries(); |
| 1057 | |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1058 | return 0; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1061 | CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) { |
| 1062 | assert(getNumDefaultArgTemporaries() && |
| 1063 | "Default arguments does not have any temporaries!"); |
| 1064 | |
| 1065 | CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit()); |
| 1066 | return E->getTemporary(i); |
| 1067 | } |
| 1068 | |
| 1069 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1070 | if (const Expr *E = getInit()) |
| 1071 | return E->getSourceRange(); |
| 1072 | |
| 1073 | if (hasUninstantiatedDefaultArg()) |
| 1074 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1075 | |
| 1076 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1079 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1080 | // FunctionDecl Implementation |
| 1081 | //===----------------------------------------------------------------------===// |
| 1082 | |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 1083 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1084 | const PrintingPolicy &Policy, |
| 1085 | bool Qualified) const { |
| 1086 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1087 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1088 | if (TemplateArgs) |
| 1089 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1090 | TemplateArgs->getFlatArgumentList(), |
| 1091 | TemplateArgs->flat_size(), |
| 1092 | Policy); |
| 1093 | |
| 1094 | } |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1096 | bool FunctionDecl::isVariadic() const { |
| 1097 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1098 | return FT->isVariadic(); |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1102 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1103 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1104 | if (I->Body) { |
| 1105 | Definition = *I; |
| 1106 | return true; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1113 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1114 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1115 | if (I->Body) { |
| 1116 | Definition = *I; |
| 1117 | return I->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1124 | void FunctionDecl::setBody(Stmt *B) { |
| 1125 | Body = B; |
Argyrios Kyrtzidis | 1a5364e | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 1126 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1127 | EndRangeLoc = B->getLocEnd(); |
| 1128 | } |
| 1129 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1130 | void FunctionDecl::setPure(bool P) { |
| 1131 | IsPure = P; |
| 1132 | if (P) |
| 1133 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1134 | Parent->markedVirtualFunctionPure(); |
| 1135 | } |
| 1136 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1137 | bool FunctionDecl::isMain() const { |
| 1138 | ASTContext &Context = getASTContext(); |
John McCall | 07a5c22 | 2009-08-15 02:09:25 +0000 | [diff] [blame] | 1139 | return !Context.getLangOptions().Freestanding && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1140 | getDeclContext()->getRedeclContext()->isTranslationUnit() && |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1141 | getIdentifier() && getIdentifier()->isStr("main"); |
| 1142 | } |
| 1143 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1144 | bool FunctionDecl::isExternC() const { |
| 1145 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1146 | // In C, any non-static, non-overloadable function has external |
| 1147 | // linkage. |
| 1148 | if (!Context.getLangOptions().CPlusPlus) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1149 | return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1150 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1152 | DC = DC->getParent()) { |
| 1153 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) { |
| 1154 | if (Linkage->getLanguage() == LinkageSpecDecl::lang_c) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1155 | return getStorageClass() != SC_Static && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1156 | !getAttr<OverloadableAttr>(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1157 | |
| 1158 | break; |
| 1159 | } |
Douglas Gregor | 4597553 | 2010-08-17 16:09:23 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (DC->isRecord()) |
| 1162 | break; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | 0bab54c | 2010-10-21 16:57:46 +0000 | [diff] [blame] | 1165 | return isMain(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1168 | bool FunctionDecl::isGlobal() const { |
| 1169 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1170 | return Method->isStatic(); |
| 1171 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1172 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1173 | return false; |
| 1174 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1176 | DC->isNamespace(); |
| 1177 | DC = DC->getParent()) { |
| 1178 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1179 | if (!Namespace->getDeclName()) |
| 1180 | return false; |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return true; |
| 1186 | } |
| 1187 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1188 | void |
| 1189 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1190 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1191 | |
| 1192 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1193 | FunctionTemplateDecl *PrevFunTmpl |
| 1194 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1195 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1196 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1201 | return getFirstDeclaration(); |
| 1202 | } |
| 1203 | |
| 1204 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1205 | return getFirstDeclaration(); |
| 1206 | } |
| 1207 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1208 | /// \brief Returns a value indicating whether this function |
| 1209 | /// corresponds to a builtin function. |
| 1210 | /// |
| 1211 | /// The function corresponds to a built-in function if it is |
| 1212 | /// declared at translation scope or within an extern "C" block and |
| 1213 | /// its name matches with the name of a builtin. The returned value |
| 1214 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1216 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1217 | unsigned FunctionDecl::getBuiltinID() const { |
| 1218 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1219 | if (!getIdentifier() || !getIdentifier()->getBuiltinID()) |
| 1220 | return 0; |
| 1221 | |
| 1222 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| 1223 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1224 | return BuiltinID; |
| 1225 | |
| 1226 | // This function has the name of a known C library |
| 1227 | // function. Determine whether it actually refers to the C library |
| 1228 | // function or whether it just has the same name. |
| 1229 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1230 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1231 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1232 | return 0; |
| 1233 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1234 | // If this function is at translation-unit scope and we're not in |
| 1235 | // C++, it refers to the C library function. |
| 1236 | if (!Context.getLangOptions().CPlusPlus && |
| 1237 | getDeclContext()->isTranslationUnit()) |
| 1238 | return BuiltinID; |
| 1239 | |
| 1240 | // If the function is in an extern "C" linkage specification and is |
| 1241 | // not marked "overloadable", it's the real function. |
| 1242 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1244 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1245 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1246 | return BuiltinID; |
| 1247 | |
| 1248 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1253 | /// getNumParams - Return the number of parameters this function must have |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1254 | /// based on its FunctionType. This is the length of the PararmInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1255 | /// after it has been created. |
| 1256 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1257 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1258 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1259 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1260 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1264 | void FunctionDecl::setParams(ASTContext &C, |
| 1265 | ParmVarDecl **NewParamInfo, unsigned NumParams) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | assert(ParamInfo == 0 && "Already has param info!"); |
Chris Lattner | 2dbd285 | 2009-04-25 06:12:16 +0000 | [diff] [blame] | 1267 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | // Zero params -> null pointer. |
| 1270 | if (NumParams) { |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1271 | void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 1272 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1274 | |
Argyrios Kyrtzidis | 96888cc | 2009-06-23 00:42:00 +0000 | [diff] [blame] | 1275 | // Update source range. The check below allows us to set EndRangeLoc before |
| 1276 | // setting the parameters. |
Argyrios Kyrtzidis | cb5f8f5 | 2009-06-23 00:42:15 +0000 | [diff] [blame] | 1277 | if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation()) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1278 | EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1282 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1283 | /// needed to call this function. This may be fewer than the number of |
| 1284 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 1285 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1286 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 1287 | unsigned NumRequiredArgs = getNumParams(); |
| 1288 | while (NumRequiredArgs > 0 |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1289 | && getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1290 | --NumRequiredArgs; |
| 1291 | |
| 1292 | return NumRequiredArgs; |
| 1293 | } |
| 1294 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1295 | bool FunctionDecl::isInlined() const { |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1296 | // FIXME: This is not enough. Consider: |
| 1297 | // |
| 1298 | // inline void f(); |
| 1299 | // void f() { } |
| 1300 | // |
| 1301 | // f is inlined, but does not have inline specified. |
| 1302 | // To fix this we should add an 'inline' flag to FunctionDecl. |
| 1303 | if (isInlineSpecified()) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1304 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (isa<CXXMethodDecl>(this)) { |
| 1307 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1308 | return true; |
| 1309 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1310 | |
| 1311 | switch (getTemplateSpecializationKind()) { |
| 1312 | case TSK_Undeclared: |
| 1313 | case TSK_ExplicitSpecialization: |
| 1314 | return false; |
| 1315 | |
| 1316 | case TSK_ImplicitInstantiation: |
| 1317 | case TSK_ExplicitInstantiationDeclaration: |
| 1318 | case TSK_ExplicitInstantiationDefinition: |
| 1319 | // Handle below. |
| 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1324 | bool HasPattern = false; |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1325 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1326 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1327 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1328 | if (HasPattern && PatternDecl) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1329 | return PatternDecl->isInlined(); |
| 1330 | |
| 1331 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1334 | /// \brief For an inline function definition in C or C++, determine whether the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1335 | /// definition will be externally visible. |
| 1336 | /// |
| 1337 | /// Inline function definitions are always available for inlining optimizations. |
| 1338 | /// However, depending on the language dialect, declaration specifiers, and |
| 1339 | /// attributes, the definition of an inline function may or may not be |
| 1340 | /// "externally" visible to other translation units in the program. |
| 1341 | /// |
| 1342 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 1343 | /// if even one of the global-scope declarations is marked "extern inline", the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1344 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 1345 | /// |
| 1346 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 1347 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 1348 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 1349 | /// an externally visible symbol, but "extern inline" will not create an |
| 1350 | /// externally visible symbol. |
| 1351 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 1352 | assert(isThisDeclarationADefinition() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1353 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1354 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1356 | if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) { |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1357 | // GNU inline semantics. Based on a number of examples, we came up with the |
| 1358 | // following heuristic: if the "inline" keyword is present on a |
| 1359 | // declaration of the function but "extern" is not present on that |
| 1360 | // declaration, then the symbol is externally visible. Otherwise, the GNU |
| 1361 | // "extern inline" semantics applies and the symbol is not externally |
| 1362 | // visible. |
| 1363 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1364 | Redecl != RedeclEnd; |
| 1365 | ++Redecl) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1366 | if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1367 | return true; |
| 1368 | } |
| 1369 | |
| 1370 | // GNU "extern inline" semantics; no externally visible symbol. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1371 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | // C99 6.7.4p6: |
| 1375 | // [...] If all of the file scope declarations for a function in a |
| 1376 | // translation unit include the inline function specifier without extern, |
| 1377 | // then the definition in that translation unit is an inline definition. |
| 1378 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 1379 | Redecl != RedeclEnd; |
| 1380 | ++Redecl) { |
| 1381 | // Only consider file-scope declarations in this test. |
| 1382 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1383 | continue; |
| 1384 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1385 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 1386 | return true; // Not an inline definition |
| 1387 | } |
| 1388 | |
| 1389 | // C99 6.7.4p6: |
| 1390 | // An inline definition does not provide an external definition for the |
| 1391 | // function, and does not forbid an external definition in another |
| 1392 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1393 | return false; |
| 1394 | } |
| 1395 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1396 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 1397 | /// function represents, if any. |
| 1398 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1399 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 1400 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1401 | else |
| 1402 | return OO_None; |
| 1403 | } |
| 1404 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 1405 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 1406 | /// represents, if any. |
| 1407 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 1408 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 1409 | return getDeclName().getCXXLiteralIdentifier(); |
| 1410 | else |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1414 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 1415 | if (TemplateOrSpecialization.isNull()) |
| 1416 | return TK_NonTemplate; |
| 1417 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 1418 | return TK_FunctionTemplate; |
| 1419 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 1420 | return TK_MemberSpecialization; |
| 1421 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 1422 | return TK_FunctionTemplateSpecialization; |
| 1423 | if (TemplateOrSpecialization.is |
| 1424 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 1425 | return TK_DependentFunctionTemplateSpecialization; |
| 1426 | |
| 1427 | assert(false && "Did we miss a TemplateOrSpecialization type?"); |
| 1428 | return TK_NonTemplate; |
| 1429 | } |
| 1430 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1431 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1432 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1433 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1438 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 1439 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1440 | } |
| 1441 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1442 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1443 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 1444 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1445 | TemplateSpecializationKind TSK) { |
| 1446 | assert(TemplateOrSpecialization.isNull() && |
| 1447 | "Member function is already a specialization"); |
| 1448 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1449 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1450 | TemplateOrSpecialization = Info; |
| 1451 | } |
| 1452 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1453 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1454 | // If the function is invalid, it can't be implicitly instantiated. |
| 1455 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | |
| 1458 | switch (getTemplateSpecializationKind()) { |
| 1459 | case TSK_Undeclared: |
| 1460 | case TSK_ExplicitSpecialization: |
| 1461 | case TSK_ExplicitInstantiationDefinition: |
| 1462 | return false; |
| 1463 | |
| 1464 | case TSK_ImplicitInstantiation: |
| 1465 | return true; |
| 1466 | |
| 1467 | case TSK_ExplicitInstantiationDeclaration: |
| 1468 | // Handled below. |
| 1469 | break; |
| 1470 | } |
| 1471 | |
| 1472 | // Find the actual template from which we will instantiate. |
| 1473 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1474 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1475 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1476 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1477 | |
| 1478 | // C++0x [temp.explicit]p9: |
| 1479 | // Except for inline functions, other explicit instantiation declarations |
| 1480 | // have the effect of suppressing the implicit instantiation of the entity |
| 1481 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1482 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1483 | return true; |
| 1484 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1485 | return PatternDecl->isInlined(); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| 1489 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 1490 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 1491 | // If we have hit a point where the user provided a specialization of |
| 1492 | // this template, we're done looking. |
| 1493 | if (Primary->isMemberSpecialization()) |
| 1494 | break; |
| 1495 | |
| 1496 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 1497 | } |
| 1498 | |
| 1499 | return Primary->getTemplatedDecl(); |
| 1500 | } |
| 1501 | |
| 1502 | return getInstantiatedFromMemberFunction(); |
| 1503 | } |
| 1504 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1505 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1507 | = TemplateOrSpecialization |
| 1508 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1509 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1510 | } |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | const TemplateArgumentList * |
| 1515 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1517 | = TemplateOrSpecialization |
| 1518 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1519 | return Info->TemplateArguments; |
| 1520 | } |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1524 | const TemplateArgumentListInfo * |
| 1525 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 1526 | if (FunctionTemplateSpecializationInfo *Info |
| 1527 | = TemplateOrSpecialization |
| 1528 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 1529 | return Info->TemplateArgumentsAsWritten; |
| 1530 | } |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1535 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 1536 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1537 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1538 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 1539 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 1540 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 1541 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1542 | assert(TSK != TSK_Undeclared && |
| 1543 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1545 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1546 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 1547 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 1548 | TemplateArgs, |
| 1549 | TemplateArgsAsWritten, |
| 1550 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1551 | TemplateOrSpecialization = Info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1553 | // Insert this function template specialization into the set of known |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1554 | // function template specializations. |
| 1555 | if (InsertPos) |
| 1556 | Template->getSpecializations().InsertNode(Info, InsertPos); |
| 1557 | else { |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1558 | // Try to insert the new node. If there is an existing node, leave it, the |
| 1559 | // set will contain the canonical decls while |
| 1560 | // FunctionTemplateDecl::findSpecialization will return |
| 1561 | // the most recent redeclarations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1562 | FunctionTemplateSpecializationInfo *Existing |
| 1563 | = Template->getSpecializations().GetOrInsertNode(Info); |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1564 | (void)Existing; |
| 1565 | assert((!Existing || Existing->Function->isCanonicalDecl()) && |
| 1566 | "Set is supposed to only contain canonical decls"); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 1567 | } |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1570 | void |
| 1571 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 1572 | const UnresolvedSetImpl &Templates, |
| 1573 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1574 | assert(TemplateOrSpecialization.isNull()); |
| 1575 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 1576 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 1577 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1578 | void *Buffer = Context.Allocate(Size); |
| 1579 | DependentFunctionTemplateSpecializationInfo *Info = |
| 1580 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 1581 | TemplateArgs); |
| 1582 | TemplateOrSpecialization = Info; |
| 1583 | } |
| 1584 | |
| 1585 | DependentFunctionTemplateSpecializationInfo:: |
| 1586 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 1587 | const TemplateArgumentListInfo &TArgs) |
| 1588 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 1589 | |
| 1590 | d.NumTemplates = Ts.size(); |
| 1591 | d.NumArgs = TArgs.size(); |
| 1592 | |
| 1593 | FunctionTemplateDecl **TsArray = |
| 1594 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 1595 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 1596 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 1597 | |
| 1598 | TemplateArgumentLoc *ArgsArray = |
| 1599 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 1600 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 1601 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 1602 | } |
| 1603 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1604 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1606 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1607 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1608 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1609 | if (FTSInfo) |
| 1610 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1612 | MemberSpecializationInfo *MSInfo |
| 1613 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 1614 | if (MSInfo) |
| 1615 | return MSInfo->getTemplateSpecializationKind(); |
| 1616 | |
| 1617 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1621 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1622 | SourceLocation PointOfInstantiation) { |
| 1623 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1624 | = TemplateOrSpecialization.dyn_cast< |
| 1625 | FunctionTemplateSpecializationInfo*>()) { |
| 1626 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 1627 | if (TSK != TSK_ExplicitSpecialization && |
| 1628 | PointOfInstantiation.isValid() && |
| 1629 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 1630 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1631 | } else if (MemberSpecializationInfo *MSInfo |
| 1632 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 1633 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1634 | if (TSK != TSK_ExplicitSpecialization && |
| 1635 | PointOfInstantiation.isValid() && |
| 1636 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 1637 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 1638 | } else |
| 1639 | assert(false && "Function cannot have a template specialization kind"); |
| 1640 | } |
| 1641 | |
| 1642 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1643 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 1644 | = TemplateOrSpecialization.dyn_cast< |
| 1645 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1646 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1647 | else if (MemberSpecializationInfo *MSInfo |
| 1648 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1649 | return MSInfo->getPointOfInstantiation(); |
| 1650 | |
| 1651 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1654 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1655 | if (Decl::isOutOfLine()) |
| 1656 | return true; |
| 1657 | |
| 1658 | // If this function was instantiated from a member function of a |
| 1659 | // class template, check whether that member function was defined out-of-line. |
| 1660 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 1661 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1662 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1663 | return Definition->isOutOfLine(); |
| 1664 | } |
| 1665 | |
| 1666 | // If this function was instantiated from a function template, |
| 1667 | // check whether that function template was defined out-of-line. |
| 1668 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 1669 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1670 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 1671 | return Definition->isOutOfLine(); |
| 1672 | } |
| 1673 | |
| 1674 | return false; |
| 1675 | } |
| 1676 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1677 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1678 | // FieldDecl Implementation |
| 1679 | //===----------------------------------------------------------------------===// |
| 1680 | |
| 1681 | FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1682 | IdentifierInfo *Id, QualType T, |
| 1683 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable) { |
| 1684 | return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable); |
| 1685 | } |
| 1686 | |
| 1687 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 1688 | if (!isImplicit() || getDeclName()) |
| 1689 | return false; |
| 1690 | |
| 1691 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 1692 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 1693 | |
| 1694 | return false; |
| 1695 | } |
| 1696 | |
| 1697 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1698 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1699 | //===----------------------------------------------------------------------===// |
| 1700 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1701 | SourceLocation TagDecl::getOuterLocStart() const { |
| 1702 | return getTemplateOrInnerLocStart(this); |
| 1703 | } |
| 1704 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1705 | SourceRange TagDecl::getSourceRange() const { |
| 1706 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1707 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1710 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1711 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1714 | void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) { |
| 1715 | TypedefDeclOrQualifier = TDD; |
| 1716 | if (TypeForDecl) |
| 1717 | TypeForDecl->ClearLinkageCache(); |
| 1718 | } |
| 1719 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1720 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1721 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1722 | |
| 1723 | if (isa<CXXRecordDecl>(this)) { |
| 1724 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 1725 | struct CXXRecordDecl::DefinitionData *Data = |
| 1726 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 1727 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 1728 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1729 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 1733 | assert((!isa<CXXRecordDecl>(this) || |
| 1734 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 1735 | "definition completed but not started"); |
| 1736 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1737 | IsDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1738 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 1739 | |
| 1740 | if (ASTMutationListener *L = getASTMutationListener()) |
| 1741 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1744 | TagDecl* TagDecl::getDefinition() const { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1745 | if (isDefinition()) |
| 1746 | return const_cast<TagDecl *>(this); |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 1747 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 1748 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
| 1750 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1751 | R != REnd; ++R) |
| 1752 | if (R->isDefinition()) |
| 1753 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1755 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1758 | void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, |
| 1759 | SourceRange QualifierRange) { |
| 1760 | if (Qualifier) { |
| 1761 | // Make sure the extended qualifier info is allocated. |
| 1762 | if (!hasExtInfo()) |
| 1763 | TypedefDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 1764 | // Set qualifier info. |
| 1765 | getExtInfo()->NNS = Qualifier; |
| 1766 | getExtInfo()->NNSRange = QualifierRange; |
| 1767 | } |
| 1768 | else { |
| 1769 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 1770 | assert(QualifierRange.isInvalid()); |
| 1771 | if (hasExtInfo()) { |
| 1772 | getASTContext().Deallocate(getExtInfo()); |
| 1773 | TypedefDeclOrQualifier = (TypedefDecl*) 0; |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1778 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1779 | // EnumDecl Implementation |
| 1780 | //===----------------------------------------------------------------------===// |
| 1781 | |
| 1782 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 1783 | IdentifierInfo *Id, SourceLocation TKL, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1784 | EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) { |
| 1785 | EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL, |
| 1786 | IsScoped, IsFixed); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1787 | C.getTypeDeclType(Enum, PrevDecl); |
| 1788 | return Enum; |
| 1789 | } |
| 1790 | |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1791 | EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1792 | return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(), |
| 1793 | false, false); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1796 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1797 | QualType NewPromotionType, |
| 1798 | unsigned NumPositiveBits, |
| 1799 | unsigned NumNegativeBits) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1800 | assert(!isDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1801 | if (!IntegerType) |
| 1802 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1803 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 1804 | setNumPositiveBits(NumPositiveBits); |
| 1805 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1806 | TagDecl::completeDefinition(); |
| 1807 | } |
| 1808 | |
| 1809 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1810 | // RecordDecl Implementation |
| 1811 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 1813 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1814 | IdentifierInfo *Id, RecordDecl *PrevDecl, |
| 1815 | SourceLocation TKL) |
| 1816 | : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1817 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1818 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1819 | HasObjectMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1820 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1821 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1825 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 1826 | SourceLocation TKL, RecordDecl* PrevDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 1828 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1829 | C.getTypeDeclType(R, PrevDecl); |
| 1830 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 1833 | RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) { |
| 1834 | return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0, |
| 1835 | SourceLocation()); |
| 1836 | } |
| 1837 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1838 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 1840 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 1841 | } |
| 1842 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1843 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 1844 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 1845 | LoadFieldsFromExternalStorage(); |
| 1846 | |
| 1847 | return field_iterator(decl_iterator(FirstDecl)); |
| 1848 | } |
| 1849 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1850 | /// completeDefinition - Notes that the definition of this type is now |
| 1851 | /// complete. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1852 | void RecordDecl::completeDefinition() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1853 | assert(!isDefinition() && "Cannot redefine record!"); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 1854 | TagDecl::completeDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
John McCall | bc365c5 | 2010-05-21 01:17:40 +0000 | [diff] [blame] | 1857 | ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() { |
| 1858 | // Force the decl chain to come into existence properly. |
| 1859 | if (!getNextDeclInContext()) getParent()->decls_begin(); |
| 1860 | |
| 1861 | assert(isAnonymousStructOrUnion()); |
| 1862 | ValueDecl *D = cast<ValueDecl>(getNextDeclInContext()); |
| 1863 | assert(D->getType()->isRecordType()); |
| 1864 | assert(D->getType()->getAs<RecordType>()->getDecl() == this); |
| 1865 | return D; |
| 1866 | } |
| 1867 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1868 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 1869 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 1870 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 1871 | |
| 1872 | // Notify that we have a RecordDecl doing some initialization. |
| 1873 | ExternalASTSource::Deserializing TheFields(Source); |
| 1874 | |
| 1875 | llvm::SmallVector<Decl*, 64> Decls; |
| 1876 | if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) |
| 1877 | return; |
| 1878 | |
| 1879 | #ifndef NDEBUG |
| 1880 | // Check that all decls we got were FieldDecls. |
| 1881 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 1882 | assert(isa<FieldDecl>(Decls[i])); |
| 1883 | #endif |
| 1884 | |
| 1885 | LoadedFieldsFromExternalStorage = true; |
| 1886 | |
| 1887 | if (Decls.empty()) |
| 1888 | return; |
| 1889 | |
| 1890 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls); |
| 1891 | } |
| 1892 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1893 | //===----------------------------------------------------------------------===// |
| 1894 | // BlockDecl Implementation |
| 1895 | //===----------------------------------------------------------------------===// |
| 1896 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1897 | void BlockDecl::setParams(ParmVarDecl **NewParamInfo, |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1898 | unsigned NParms) { |
| 1899 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1901 | // Zero params -> null pointer. |
| 1902 | if (NParms) { |
| 1903 | NumParams = NParms; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1904 | void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 1905 | ParamInfo = new (Mem) ParmVarDecl*[NumParams]; |
| 1906 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | unsigned BlockDecl::getNumParams() const { |
| 1911 | return NumParams; |
| 1912 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1913 | |
| 1914 | |
| 1915 | //===----------------------------------------------------------------------===// |
| 1916 | // Other Decl Allocation/Deallocation Method Implementations |
| 1917 | //===----------------------------------------------------------------------===// |
| 1918 | |
| 1919 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 1920 | return new (C) TranslationUnitDecl(C); |
| 1921 | } |
| 1922 | |
| 1923 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 1924 | SourceLocation L, IdentifierInfo *Id) { |
| 1925 | return new (C) NamespaceDecl(DC, L, Id); |
| 1926 | } |
| 1927 | |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 1928 | NamespaceDecl *NamespaceDecl::getNextNamespace() { |
| 1929 | return dyn_cast_or_null<NamespaceDecl>( |
| 1930 | NextNamespace.get(getASTContext().getExternalSource())); |
| 1931 | } |
| 1932 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1933 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 1934 | SourceLocation L, IdentifierInfo *Id, QualType T) { |
| 1935 | return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T); |
| 1936 | } |
| 1937 | |
| 1938 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1939 | const DeclarationNameInfo &NameInfo, |
| 1940 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1941 | StorageClass S, StorageClass SCAsWritten, |
| 1942 | bool isInline, bool hasWrittenPrototype) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1943 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1944 | S, SCAsWritten, isInline); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1945 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 1946 | return New; |
| 1947 | } |
| 1948 | |
| 1949 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 1950 | return new (C) BlockDecl(DC, L); |
| 1951 | } |
| 1952 | |
| 1953 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 1954 | SourceLocation L, |
| 1955 | IdentifierInfo *Id, QualType T, |
| 1956 | Expr *E, const llvm::APSInt &V) { |
| 1957 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 1958 | } |
| 1959 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 1960 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 1961 | SourceLocation End = getLocation(); |
| 1962 | if (Init) |
| 1963 | End = Init->getLocEnd(); |
| 1964 | return SourceRange(getLocation(), End); |
| 1965 | } |
| 1966 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1967 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 1968 | SourceLocation L, IdentifierInfo *Id, |
| 1969 | TypeSourceInfo *TInfo) { |
| 1970 | return new (C) TypedefDecl(DC, L, Id, TInfo); |
| 1971 | } |
| 1972 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1973 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 1974 | SourceLocation L, |
| 1975 | StringLiteral *Str) { |
| 1976 | return new (C) FileScopeAsmDecl(DC, L, Str); |
| 1977 | } |