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