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