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