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