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