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