Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | 6301884 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | c4173fa | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTMutationListener.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 26 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Module.h" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 28 | #include "clang/Basic/Specifiers.h" |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | ce20e8f | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 31 | |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 34 | using namespace clang; |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 37 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 40 | static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) { |
| 41 | // If this declaration has an explicit visibility attribute, use it. |
| 42 | if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) { |
| 43 | switch (A->getVisibility()) { |
| 44 | case VisibilityAttr::Default: |
| 45 | return DefaultVisibility; |
| 46 | case VisibilityAttr::Hidden: |
| 47 | return HiddenVisibility; |
| 48 | case VisibilityAttr::Protected: |
| 49 | return ProtectedVisibility; |
| 50 | } |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 51 | } |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 52 | |
| 53 | // If we're on Mac OS X, an 'availability' for Mac OS X attribute |
| 54 | // implies visibility(default). |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 55 | if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 56 | for (specific_attr_iterator<AvailabilityAttr> |
| 57 | A = D->specific_attr_begin<AvailabilityAttr>(), |
| 58 | AEnd = D->specific_attr_end<AvailabilityAttr>(); |
| 59 | A != AEnd; ++A) |
| 60 | if ((*A)->getPlatform()->getName().equals("macosx")) |
| 61 | return DefaultVisibility; |
| 62 | } |
| 63 | |
| 64 | return llvm::Optional<Visibility>(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 67 | typedef NamedDecl::LinkageInfo LinkageInfo; |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 68 | |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 69 | static LinkageInfo getLVForType(QualType T) { |
| 70 | std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility(); |
| 71 | return LinkageInfo(P.first, P.second, T->isVisibilityExplicit()); |
| 72 | } |
| 73 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 74 | /// \brief Get the most restrictive linkage for the types in the given |
| 75 | /// template parameter list. |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 76 | static LinkageInfo |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 77 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 78 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 79 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 80 | PEnd = Params->end(); |
| 81 | P != PEnd; ++P) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 82 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 83 | if (NTTP->isExpandedParameterPack()) { |
| 84 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 85 | QualType T = NTTP->getExpansionType(I); |
| 86 | if (!T->isDependentType()) |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 87 | LV.merge(getLVForType(T)); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 88 | } |
| 89 | continue; |
| 90 | } |
Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 92 | if (!NTTP->getType()->isDependentType()) { |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 93 | LV.merge(getLVForType(NTTP->getType())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 94 | continue; |
| 95 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 96 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 97 | |
| 98 | if (TemplateTemplateParmDecl *TTP |
| 99 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 100 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 104 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 107 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 108 | static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 110 | /// \brief Get the most restrictive linkage for the types and |
| 111 | /// declarations in the given template argument list. |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 112 | static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args, |
| 113 | unsigned NumArgs, |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 114 | bool OnlyTemplate) { |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 115 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 116 | |
| 117 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 118 | switch (Args[I].getKind()) { |
| 119 | case TemplateArgument::Null: |
| 120 | case TemplateArgument::Integral: |
| 121 | case TemplateArgument::Expression: |
| 122 | break; |
Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 124 | case TemplateArgument::Type: |
Rafael Espindola | b522a5f | 2012-04-23 17:51:55 +0000 | [diff] [blame] | 125 | LV.mergeWithMin(getLVForType(Args[I].getAsType())); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case TemplateArgument::Declaration: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 129 | // The decl can validly be null as the representation of nullptr |
| 130 | // arguments, valid only in C++0x. |
| 131 | if (Decl *D = Args[I].getAsDecl()) { |
Douglas Gregor | 91df6cf | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 132 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Rafael Espindola | b522a5f | 2012-04-23 17:51:55 +0000 | [diff] [blame] | 133 | LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate)); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 134 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 138 | case TemplateArgument::TemplateExpansion: |
Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 139 | if (TemplateDecl *Template |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 140 | = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Rafael Espindola | b522a5f | 2012-04-23 17:51:55 +0000 | [diff] [blame] | 141 | LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate)); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case TemplateArgument::Pack: |
Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 145 | LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(), |
| 146 | Args[I].pack_size(), |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 147 | OnlyTemplate)); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 152 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 155 | static LinkageInfo |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 156 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 157 | bool OnlyTemplate) { |
| 158 | return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rafael Espindola | 340941d | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 161 | static bool shouldConsiderTemplateVis(const FunctionDecl *fn, |
Rafael Espindola | 96dcb8d | 2012-05-21 20:31:27 +0000 | [diff] [blame] | 162 | const FunctionTemplateSpecializationInfo *spec) { |
| 163 | return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization(); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Rafael Espindola | 0cf10ac | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 166 | static bool |
| 167 | shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) { |
Rafael Espindola | 93c289c | 2012-05-21 20:15:56 +0000 | [diff] [blame] | 168 | return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization(); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 171 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 172 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
Rafael Espindola | 5cc7890 | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 173 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 174 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 175 | return false; |
| 176 | |
| 177 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 178 | if (!FD) |
| 179 | return false; |
| 180 | |
| 181 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 182 | if (FunctionTemplateSpecializationInfo *spec |
| 183 | = FD->getTemplateSpecializationInfo()) { |
| 184 | TSK = spec->getTemplateSpecializationKind(); |
| 185 | } else if (MemberSpecializationInfo *MSI = |
| 186 | FD->getMemberSpecializationInfo()) { |
| 187 | TSK = MSI->getTemplateSpecializationKind(); |
| 188 | } |
| 189 | |
| 190 | const FunctionDecl *Def = 0; |
| 191 | // InlineVisibilityHidden only applies to definitions, and |
| 192 | // isInlined() only gives meaningful answers on definitions |
| 193 | // anyway. |
| 194 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 195 | TSK != TSK_ExplicitInstantiationDefinition && |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 196 | FD->hasBody(Def) && Def->isInlined(); |
| 197 | } |
| 198 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 199 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, |
| 200 | bool OnlyTemplate) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 201 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | f73b282 | 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 | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 213 | if (Var->getStorageClass() == SC_Static) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 214 | return LinkageInfo::internal(); |
Douglas Gregor | f73b282 | 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) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 220 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | f873c2f | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 221 | Var->getType().isConstant(Context) && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 222 | Var->getStorageClass() != SC_Extern && |
| 223 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 224 | bool FoundExtern = false; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 225 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 226 | PrevVar && !FoundExtern; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 227 | PrevVar = PrevVar->getPreviousDecl()) |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 228 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 229 | FoundExtern = true; |
| 230 | |
| 231 | if (!FoundExtern) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 232 | return LinkageInfo::internal(); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 233 | } |
Fariborz Jahanian | 8feee2d | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 234 | if (Var->getStorageClass() == SC_None) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 235 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| 236 | for (; PrevVar; PrevVar = PrevVar->getPreviousDecl()) |
Fariborz Jahanian | 8feee2d | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 237 | if (PrevVar->getStorageClass() == SC_PrivateExtern) |
| 238 | break; |
| 239 | if (PrevVar) |
| 240 | return PrevVar->getLinkageAndVisibility(); |
| 241 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 242 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 243 | // C++ [temp]p4: |
| 244 | // A non-member function template can have internal linkage; any |
| 245 | // other template name shall have external linkage. |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 246 | const FunctionDecl *Function = 0; |
| 247 | if (const FunctionTemplateDecl *FunTmpl |
| 248 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 249 | Function = FunTmpl->getTemplatedDecl(); |
| 250 | else |
| 251 | Function = cast<FunctionDecl>(D); |
| 252 | |
| 253 | // Explicitly declared static. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 254 | if (Function->getStorageClass() == SC_Static) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 255 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 256 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 257 | // - a data member of an anonymous union. |
| 258 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 259 | return LinkageInfo::internal(); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 262 | if (D->isInAnonymousNamespace()) { |
| 263 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 264 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 265 | if ((!Var || !Var->getDeclContext()->isExternCContext()) && |
| 266 | (!Func || !Func->getDeclContext()->isExternCContext())) |
Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 267 | return LinkageInfo::uniqueExternal(); |
| 268 | } |
John McCall | b7139c4 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 269 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 270 | // Set up the defaults. |
| 271 | |
| 272 | // C99 6.2.2p5: |
| 273 | // If the declaration of an identifier for an object has file |
| 274 | // scope and no storage-class specifier, its linkage is |
| 275 | // external. |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 276 | LinkageInfo LV; |
| 277 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 278 | if (!OnlyTemplate) { |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 279 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 280 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 281 | } else { |
| 282 | // If we're declared in a namespace with a visibility attribute, |
| 283 | // use that namespace's visibility, but don't call it explicit. |
| 284 | for (const DeclContext *DC = D->getDeclContext(); |
| 285 | !isa<TranslationUnitDecl>(DC); |
| 286 | DC = DC->getParent()) { |
| 287 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); |
| 288 | if (!ND) continue; |
| 289 | if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) { |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 290 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 297 | if (!OnlyTemplate) { |
Rafael Espindola | b660efd | 2012-04-19 04:37:16 +0000 | [diff] [blame] | 298 | LV.mergeVisibility(Context.getLangOpts().getVisibilityMode()); |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 299 | // If we're paying attention to global visibility, apply |
| 300 | // -finline-visibility-hidden if this is an inline method. |
| 301 | if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D)) |
| 302 | LV.mergeVisibility(HiddenVisibility, true); |
| 303 | } |
Rafael Espindola | af690f5 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 305 | // C++ [basic.link]p4: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 307 | // A name having namespace scope has external linkage if it is the |
| 308 | // name of |
| 309 | // |
| 310 | // - an object or reference, unless it has internal linkage; or |
| 311 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 312 | // GCC applies the following optimization to variables and static |
| 313 | // data members, but not to functions: |
| 314 | // |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 315 | // Modify the variable's LV by the LV of its type unless this is |
| 316 | // C or extern "C". This follows from [basic.link]p9: |
| 317 | // A type without linkage shall not be used as the type of a |
| 318 | // variable or function with external linkage unless |
| 319 | // - the entity has C language linkage, or |
| 320 | // - the entity is declared within an unnamed namespace, or |
| 321 | // - the entity is not used or is defined in the same |
| 322 | // translation unit. |
| 323 | // and [basic.link]p10: |
| 324 | // ...the types specified by all declarations referring to a |
| 325 | // given variable or function shall be identical... |
| 326 | // C does not have an equivalent rule. |
| 327 | // |
John McCall | 5fe8412 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 328 | // Ignore this if we've got an explicit attribute; the user |
| 329 | // probably knows what they're doing. |
| 330 | // |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 331 | // Note that we don't want to make the variable non-external |
| 332 | // because of this, but unique-external linkage suits us. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 333 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 334 | !Var->getDeclContext()->isExternCContext()) { |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 335 | LinkageInfo TypeLV = getLVForType(Var->getType()); |
| 336 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 337 | return LinkageInfo::uniqueExternal(); |
Rafael Espindola | 1f07333 | 2012-04-19 05:24:05 +0000 | [diff] [blame] | 338 | LV.mergeVisibility(TypeLV); |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 339 | } |
| 340 | |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 341 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 342 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 343 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 344 | if (!Context.getLangOpts().CPlusPlus && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 345 | (Var->getStorageClass() == SC_Extern || |
| 346 | Var->getStorageClass() == SC_PrivateExtern)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 347 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 348 | // C99 6.2.2p4: |
| 349 | // For an identifier declared with the storage-class specifier |
| 350 | // extern in a scope in which a prior declaration of that |
| 351 | // identifier is visible, if the prior declaration specifies |
| 352 | // internal or external linkage, the linkage of the identifier |
| 353 | // at the later declaration is the same as the linkage |
| 354 | // specified at the prior declaration. If no prior declaration |
| 355 | // is visible, or if the prior declaration specifies no |
| 356 | // linkage, then the identifier has external linkage. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 357 | if (const VarDecl *PrevVar = Var->getPreviousDecl()) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 358 | LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 359 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 360 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 364 | // - a function, unless it has internal linkage; or |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 365 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 2efaf11 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 366 | // In theory, we can modify the function's LV by the LV of its |
| 367 | // type unless it has C linkage (see comment above about variables |
| 368 | // for justification). In practice, GCC doesn't do this, so it's |
| 369 | // just too painful to make work. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 370 | |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 371 | if (Function->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 372 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 374 | // C99 6.2.2p5: |
| 375 | // If the declaration of an identifier for a function has no |
| 376 | // storage-class specifier, its linkage is determined exactly |
| 377 | // as if it were declared with the storage-class specifier |
| 378 | // extern. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 379 | if (!Context.getLangOpts().CPlusPlus && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 380 | (Function->getStorageClass() == SC_Extern || |
| 381 | Function->getStorageClass() == SC_PrivateExtern || |
| 382 | Function->getStorageClass() == SC_None)) { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 383 | // C99 6.2.2p4: |
| 384 | // For an identifier declared with the storage-class specifier |
| 385 | // extern in a scope in which a prior declaration of that |
| 386 | // identifier is visible, if the prior declaration specifies |
| 387 | // internal or external linkage, the linkage of the identifier |
| 388 | // at the later declaration is the same as the linkage |
| 389 | // specified at the prior declaration. If no prior declaration |
| 390 | // is visible, or if the prior declaration specifies no |
| 391 | // linkage, then the identifier has external linkage. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 392 | if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 393 | LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 394 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 395 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 399 | // In C++, then if the type of the function uses a type with |
| 400 | // unique-external linkage, it's not legally usable from outside |
| 401 | // this translation unit. However, we should use the C linkage |
| 402 | // rules instead for extern "C" declarations. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 403 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 404 | !Function->getDeclContext()->isExternCContext() && |
John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 405 | Function->getType()->getLinkage() == UniqueExternalLinkage) |
| 406 | return LinkageInfo::uniqueExternal(); |
| 407 | |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 408 | // Consider LV from the template and the template arguments unless |
| 409 | // this is an explicit specialization with a visibility attribute. |
| 410 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 411 | = Function->getTemplateSpecializationInfo()) { |
Rafael Espindola | 340941d | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 412 | LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true); |
| 413 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| 414 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs, |
| 415 | OnlyTemplate); |
| 416 | if (shouldConsiderTemplateVis(Function, specInfo)) { |
Rafael Espindola | a486f48 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 417 | LV.mergeWithMin(TempLV); |
Rafael Espindola | 340941d | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 418 | LV.mergeWithMin(ArgsLV); |
| 419 | } else { |
| 420 | LV.mergeLinkage(TempLV); |
| 421 | LV.mergeLinkage(ArgsLV); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 422 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 425 | // - a named class (Clause 9), or an unnamed class defined in a |
| 426 | // typedef declaration in which the class has the typedef name |
| 427 | // for linkage purposes (7.1.3); or |
| 428 | // - a named enumeration (7.2), or an unnamed enumeration |
| 429 | // defined in a typedef declaration in which the enumeration |
| 430 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 431 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 432 | // Unnamed tags have no linkage. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 433 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 434 | return LinkageInfo::none(); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 435 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 436 | // If this is a class template specialization, consider the |
| 437 | // linkage of the template and template arguments. |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 438 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 439 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
Rafael Espindola | 0cf10ac | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 440 | // From the template. |
| 441 | LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 442 | |
Rafael Espindola | 0cf10ac | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 443 | // The arguments at which the template was instantiated. |
| 444 | const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); |
| 445 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 446 | OnlyTemplate); |
| 447 | if (shouldConsiderTemplateVis(spec)) { |
Rafael Espindola | a486f48 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 448 | LV.mergeWithMin(TempLV); |
Rafael Espindola | 0cf10ac | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 449 | LV.mergeWithMin(ArgsLV); |
| 450 | } else { |
| 451 | LV.mergeLinkage(TempLV); |
| 452 | LV.mergeLinkage(ArgsLV); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 453 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 454 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 455 | |
| 456 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 457 | } else if (isa<EnumConstantDecl>(D)) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 458 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
| 459 | OnlyTemplate); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 460 | if (!isExternalLinkage(EnumLV.linkage())) |
| 461 | return LinkageInfo::none(); |
| 462 | LV.merge(EnumLV); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 463 | |
| 464 | // - a template, unless it is a function template that has |
| 465 | // internal linkage (Clause 14); |
John McCall | 8bc6d5b | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 466 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
Rafael Espindola | 8add48e | 2012-04-22 00:43:48 +0000 | [diff] [blame] | 467 | LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters())); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 468 | // - a namespace (7.3), unless it is declared within an unnamed |
| 469 | // namespace. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 470 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 471 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 472 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 473 | // By extension, we assign external linkage to Objective-C |
| 474 | // interfaces. |
| 475 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 476 | // fallout |
| 477 | |
| 478 | // Everything not covered here has no linkage. |
| 479 | } else { |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 480 | return LinkageInfo::none(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // If we ended up with non-external linkage, visibility should |
| 484 | // always be default. |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 485 | if (LV.linkage() != ExternalLinkage) |
| 486 | return LinkageInfo(LV.linkage(), DefaultVisibility, false); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 487 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 488 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 491 | static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 492 | // Only certain class members have linkage. Note that fields don't |
| 493 | // really have linkage, but it's convenient to say they do for the |
| 494 | // purposes of calculating linkage of pointer-to-data-member |
| 495 | // template arguments. |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 496 | if (!(isa<CXXMethodDecl>(D) || |
| 497 | isa<VarDecl>(D) || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 498 | isa<FieldDecl>(D) || |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 499 | (isa<TagDecl>(D) && |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 500 | (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl())))) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 501 | return LinkageInfo::none(); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 502 | |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 503 | LinkageInfo LV; |
| 504 | |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 505 | // If we have an explicit visibility attribute, merge that in. |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 506 | if (!OnlyTemplate) { |
Rafael Espindola | 3d3d339 | 2012-04-19 04:27:47 +0000 | [diff] [blame] | 507 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 508 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 509 | // If we're paying attention to global visibility, apply |
| 510 | // -finline-visibility-hidden if this is an inline method. |
| 511 | // |
| 512 | // Note that we do this before merging information about |
| 513 | // the class visibility. |
| 514 | if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D)) |
| 515 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 516 | } |
Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 517 | |
| 518 | // If this class member has an explicit visibility attribute, the only |
| 519 | // thing that can change its visibility is the template arguments, so |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame^] | 520 | // only look for them when processing the class. |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 521 | bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate; |
Rafael Espindola | 505a7c8 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 522 | |
Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 523 | // If this member has an visibility attribute, ClassF will exclude |
| 524 | // attributes on the class or command line options, keeping only information |
| 525 | // about the template instantiation. If the member has no visibility |
| 526 | // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin |
| 527 | // produces the desired result. |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 528 | LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), |
| 529 | ClassOnlyTemplate)); |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 530 | if (!isExternalLinkage(LV.linkage())) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 531 | return LinkageInfo::none(); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 532 | |
| 533 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 534 | if (LV.linkage() == UniqueExternalLinkage) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 535 | return LinkageInfo::uniqueExternal(); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 537 | if (!OnlyTemplate) |
Rafael Espindola | b660efd | 2012-04-19 04:37:16 +0000 | [diff] [blame] | 538 | LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode()); |
Rafael Espindola | af690f5 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 539 | |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 540 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 541 | // If the type of the function uses a type with unique-external |
| 542 | // linkage, it's not legally usable from outside this translation unit. |
| 543 | if (MD->getType()->getLinkage() == UniqueExternalLinkage) |
| 544 | return LinkageInfo::uniqueExternal(); |
| 545 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 546 | // If this is a method template specialization, use the linkage for |
| 547 | // the template parameters and arguments. |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 548 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 549 | = MD->getTemplateSpecializationInfo()) { |
Rafael Espindola | 67a498c | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 550 | const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments; |
| 551 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 552 | OnlyTemplate); |
| 553 | TemplateParameterList *TemplateParams = |
| 554 | spec->getTemplate()->getTemplateParameters(); |
| 555 | LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams); |
Rafael Espindola | 340941d | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 556 | if (shouldConsiderTemplateVis(MD, spec)) { |
Rafael Espindola | 67a498c | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 557 | LV.mergeWithMin(ArgsLV); |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 558 | if (!OnlyTemplate) |
Rafael Espindola | a486f48 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 559 | LV.mergeWithMin(ParamsLV); |
Rafael Espindola | 67a498c | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 560 | } else { |
| 561 | LV.mergeLinkage(ArgsLV); |
| 562 | if (!OnlyTemplate) |
| 563 | LV.mergeLinkage(ParamsLV); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 564 | } |
John McCall | e6e622e | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 565 | } |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 566 | |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 567 | // Note that in contrast to basically every other situation, we |
| 568 | // *do* apply -fvisibility to method declarations. |
| 569 | |
| 570 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 571 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 572 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
Rafael Espindola | a28bf63 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 573 | // Merge template argument/parameter information for member |
| 574 | // class template specializations. |
| 575 | const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); |
| 576 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 577 | OnlyTemplate); |
| 578 | TemplateParameterList *TemplateParams = |
| 579 | spec->getSpecializedTemplate()->getTemplateParameters(); |
| 580 | LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams); |
Rafael Espindola | 0cf10ac | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 581 | if (shouldConsiderTemplateVis(spec)) { |
Rafael Espindola | a28bf63 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 582 | LV.mergeWithMin(ArgsLV); |
Rafael Espindola | 4d71d0f | 2012-05-25 14:17:45 +0000 | [diff] [blame] | 583 | if (!OnlyTemplate) |
Rafael Espindola | a486f48 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 584 | LV.mergeWithMin(ParamsLV); |
Rafael Espindola | a28bf63 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 585 | } else { |
| 586 | LV.mergeLinkage(ArgsLV); |
| 587 | if (!OnlyTemplate) |
| 588 | LV.mergeLinkage(ParamsLV); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 589 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 590 | } |
| 591 | |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 592 | // Static data members. |
| 593 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | 36cd5cc | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 594 | // Modify the variable's linkage by its type, but ignore the |
| 595 | // type's visibility unless it's a definition. |
Rafael Espindola | 2f869a3 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 596 | LinkageInfo TypeLV = getLVForType(VD->getType()); |
| 597 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 598 | LV.mergeLinkage(UniqueExternalLinkage); |
Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 599 | LV.mergeVisibility(TypeLV); |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 600 | } |
| 601 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 602 | return LV; |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 603 | } |
| 604 | |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 605 | static void clearLinkageForClass(const CXXRecordDecl *record) { |
| 606 | for (CXXRecordDecl::decl_iterator |
| 607 | i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { |
| 608 | Decl *child = *i; |
| 609 | if (isa<NamedDecl>(child)) |
| 610 | cast<NamedDecl>(child)->ClearLinkageCache(); |
| 611 | } |
| 612 | } |
| 613 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 614 | void NamedDecl::anchor() { } |
| 615 | |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 616 | void NamedDecl::ClearLinkageCache() { |
| 617 | // Note that we can't skip clearing the linkage of children just |
| 618 | // because the parent doesn't have cached linkage: we don't cache |
| 619 | // when computing linkage for parent contexts. |
| 620 | |
| 621 | HasCachedLinkage = 0; |
| 622 | |
| 623 | // If we're changing the linkage of a class, we need to reset the |
| 624 | // linkage of child declarations, too. |
| 625 | if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) |
| 626 | clearLinkageForClass(record); |
| 627 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 628 | if (ClassTemplateDecl *temp = |
| 629 | dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) { |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 630 | // Clear linkage for the template pattern. |
| 631 | CXXRecordDecl *record = temp->getTemplatedDecl(); |
| 632 | record->HasCachedLinkage = 0; |
| 633 | clearLinkageForClass(record); |
| 634 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 635 | // We need to clear linkage for specializations, too. |
| 636 | for (ClassTemplateDecl::spec_iterator |
| 637 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 638 | i->ClearLinkageCache(); |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 639 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 640 | |
| 641 | // Clear cached linkage for function template decls, too. |
| 642 | if (FunctionTemplateDecl *temp = |
John McCall | 8f9a429 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 643 | dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) { |
| 644 | temp->getTemplatedDecl()->ClearLinkageCache(); |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 645 | for (FunctionTemplateDecl::spec_iterator |
| 646 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 647 | i->ClearLinkageCache(); |
John McCall | 8f9a429 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 648 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 649 | |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 652 | Linkage NamedDecl::getLinkage() const { |
| 653 | if (HasCachedLinkage) { |
Benjamin Kramer | 87368ac | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 654 | assert(Linkage(CachedLinkage) == |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 655 | getLVForDecl(this, true).linkage()); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 656 | return Linkage(CachedLinkage); |
| 657 | } |
| 658 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 659 | CachedLinkage = getLVForDecl(this, true).linkage(); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 660 | HasCachedLinkage = 1; |
| 661 | return Linkage(CachedLinkage); |
| 662 | } |
| 663 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 664 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 665 | LinkageInfo LI = getLVForDecl(this, false); |
Benjamin Kramer | 87368ac | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 666 | assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage()); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 667 | HasCachedLinkage = 1; |
| 668 | CachedLinkage = LI.linkage(); |
| 669 | return LI; |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 670 | } |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 671 | |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 672 | llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const { |
| 673 | // Use the most recent declaration of a variable. |
Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 674 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) { |
| 675 | if (llvm::Optional<Visibility> V = |
| 676 | getVisibilityOf(Var->getMostRecentDecl())) |
| 677 | return V; |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 678 | |
Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 679 | if (Var->isStaticDataMember()) { |
| 680 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 681 | if (InstantiatedFrom) |
| 682 | return getVisibilityOf(InstantiatedFrom); |
| 683 | } |
| 684 | |
| 685 | return llvm::Optional<Visibility>(); |
| 686 | } |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 687 | // Use the most recent declaration of a function, and also handle |
| 688 | // function template specializations. |
| 689 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { |
| 690 | if (llvm::Optional<Visibility> V |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 691 | = getVisibilityOf(fn->getMostRecentDecl())) |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 692 | return V; |
| 693 | |
| 694 | // If the function is a specialization of a template with an |
| 695 | // explicit visibility attribute, use that. |
| 696 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 697 | = fn->getTemplateSpecializationInfo()) |
| 698 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl()); |
| 699 | |
Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 700 | // If the function is a member of a specialization of a class template |
| 701 | // and the corresponding decl has explicit visibility, use that. |
| 702 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 703 | if (InstantiatedFrom) |
| 704 | return getVisibilityOf(InstantiatedFrom); |
| 705 | |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 706 | return llvm::Optional<Visibility>(); |
| 707 | } |
| 708 | |
| 709 | // Otherwise, just check the declaration itself first. |
| 710 | if (llvm::Optional<Visibility> V = getVisibilityOf(this)) |
| 711 | return V; |
| 712 | |
| 713 | // If there wasn't explicit visibility there, and this is a |
| 714 | // specialization of a class template, check for visibility |
| 715 | // on the pattern. |
| 716 | if (const ClassTemplateSpecializationDecl *spec |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 717 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 718 | return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl()); |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 719 | |
Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 720 | // If this is a member class of a specialization of a class template |
| 721 | // and the corresponding decl has explicit visibility, use that. |
| 722 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) { |
| 723 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 724 | if (InstantiatedFrom) |
| 725 | return getVisibilityOf(InstantiatedFrom); |
| 726 | } |
| 727 | |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 728 | return llvm::Optional<Visibility>(); |
| 729 | } |
| 730 | |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 731 | static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) { |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 732 | // Objective-C: treat all Objective-C declarations as having external |
| 733 | // linkage. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 734 | switch (D->getKind()) { |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 735 | default: |
| 736 | break; |
Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 737 | case Decl::ParmVar: |
| 738 | return LinkageInfo::none(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 739 | case Decl::TemplateTemplateParm: // count these as external |
| 740 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 741 | case Decl::ObjCAtDefsField: |
| 742 | case Decl::ObjCCategory: |
| 743 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 744 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 745 | case Decl::ObjCImplementation: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 746 | case Decl::ObjCMethod: |
| 747 | case Decl::ObjCProperty: |
| 748 | case Decl::ObjCPropertyImpl: |
| 749 | case Decl::ObjCProtocol: |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 750 | return LinkageInfo::external(); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 751 | |
| 752 | case Decl::CXXRecord: { |
| 753 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); |
| 754 | if (Record->isLambda()) { |
| 755 | if (!Record->getLambdaManglingNumber()) { |
| 756 | // This lambda has no mangling number, so it's internal. |
| 757 | return LinkageInfo::internal(); |
| 758 | } |
| 759 | |
| 760 | // This lambda has its linkage/visibility determined by its owner. |
| 761 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 762 | if (Decl *ContextDecl = Record->getLambdaContextDecl()) { |
| 763 | if (isa<ParmVarDecl>(ContextDecl)) |
| 764 | DC = ContextDecl->getDeclContext()->getRedeclContext(); |
| 765 | else |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 766 | return getLVForDecl(cast<NamedDecl>(ContextDecl), |
| 767 | OnlyTemplate); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 771 | return getLVForDecl(ND, OnlyTemplate); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 772 | |
| 773 | return LinkageInfo::external(); |
| 774 | } |
| 775 | |
| 776 | break; |
| 777 | } |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 780 | // Handle linkage for namespace-scope names. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 781 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 782 | return getLVForNamespaceScopeDecl(D, OnlyTemplate); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 783 | |
| 784 | // C++ [basic.link]p5: |
| 785 | // In addition, a member function, static data member, a named |
| 786 | // class or enumeration of class scope, or an unnamed class or |
| 787 | // enumeration defined in a class-scope typedef declaration such |
| 788 | // that the class or enumeration has the typedef name for linkage |
| 789 | // purposes (7.1.3), has external linkage if the name of the class |
| 790 | // has external linkage. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 791 | if (D->getDeclContext()->isRecord()) |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 792 | return getLVForClassMember(D, OnlyTemplate); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 793 | |
| 794 | // C++ [basic.link]p6: |
| 795 | // The name of a function declared in block scope and the name of |
| 796 | // an object declared by a block scope extern declaration have |
| 797 | // linkage. If there is a visible declaration of an entity with |
| 798 | // linkage having the same name and type, ignoring entities |
| 799 | // declared outside the innermost enclosing namespace scope, the |
| 800 | // block scope declaration declares that same entity and receives |
| 801 | // the linkage of the previous declaration. If there is more than |
| 802 | // one such matching entity, the program is ill-formed. Otherwise, |
| 803 | // if no matching entity is found, the block scope entity receives |
| 804 | // external linkage. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 805 | if (D->getLexicalDeclContext()->isFunctionOrMethod()) { |
| 806 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 807 | if (Function->isInAnonymousNamespace() && |
| 808 | !Function->getDeclContext()->isExternCContext()) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 809 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 810 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 811 | LinkageInfo LV; |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 812 | if (!OnlyTemplate) { |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 813 | if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility()) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 814 | LV.mergeVisibility(*Vis, true); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 817 | if (const FunctionDecl *Prev = Function->getPreviousDecl()) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 818 | LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 819 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 820 | LV.mergeVisibility(PrevLV); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 824 | } |
| 825 | |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 826 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 827 | if (Var->getStorageClass() == SC_Extern || |
| 828 | Var->getStorageClass() == SC_PrivateExtern) { |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 829 | if (Var->isInAnonymousNamespace() && |
| 830 | !Var->getDeclContext()->isExternCContext()) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 831 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 832 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 833 | LinkageInfo LV; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 834 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 835 | LV.mergeVisibility(HiddenVisibility, true); |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 836 | else if (!OnlyTemplate) { |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 837 | if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility()) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 838 | LV.mergeVisibility(*Vis, true); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 841 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 842 | LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 843 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 844 | LV.mergeVisibility(PrevLV); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | // C++ [basic.link]p6: |
| 852 | // Names not covered by these rules have no linkage. |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 853 | return LinkageInfo::none(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 854 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 856 | std::string NamedDecl::getQualifiedNameAsString() const { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 857 | return getQualifiedNameAsString(getASTContext().getPrintingPolicy()); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 861 | const DeclContext *Ctx = getDeclContext(); |
| 862 | |
| 863 | if (Ctx->isFunctionOrMethod()) |
| 864 | return getNameAsString(); |
| 865 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 866 | typedef SmallVector<const DeclContext *, 8> ContextsTy; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 867 | ContextsTy Contexts; |
| 868 | |
| 869 | // Collect contexts. |
| 870 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 871 | Contexts.push_back(Ctx); |
| 872 | Ctx = Ctx->getParent(); |
| 873 | }; |
| 874 | |
| 875 | std::string QualName; |
| 876 | llvm::raw_string_ostream OS(QualName); |
| 877 | |
| 878 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 879 | I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 881 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 882 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 883 | std::string TemplateArgsStr |
| 884 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 885 | TemplateArgs.data(), |
| 886 | TemplateArgs.size(), |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 887 | P); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 888 | OS << Spec->getName() << TemplateArgsStr; |
| 889 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 890 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 891 | OS << "<anonymous namespace>"; |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 892 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 893 | OS << *ND; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 894 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 895 | if (!RD->getIdentifier()) |
| 896 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 897 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 898 | OS << *RD; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 899 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 900 | const FunctionProtoType *FT = 0; |
| 901 | if (FD->hasWrittenPrototype()) |
| 902 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 903 | |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 904 | OS << *FD << '('; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 905 | if (FT) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 906 | unsigned NumParams = FD->getNumParams(); |
| 907 | for (unsigned i = 0; i < NumParams; ++i) { |
| 908 | if (i) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 909 | OS << ", "; |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 910 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (FT->isVariadic()) { |
| 914 | if (NumParams > 0) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 915 | OS << ", "; |
| 916 | OS << "..."; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 919 | OS << ')'; |
| 920 | } else { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 921 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 922 | } |
| 923 | OS << "::"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 924 | } |
| 925 | |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 926 | if (getDeclName()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 927 | OS << *this; |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 928 | else |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 929 | OS << "<anonymous>"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 930 | |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 931 | return OS.str(); |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 934 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 935 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 936 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 937 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 938 | // We want to keep it, unless it nominates same namespace. |
| 939 | if (getKind() == Decl::UsingDirective) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 940 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() |
| 941 | ->getOriginalNamespace() == |
| 942 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 943 | ->getOriginalNamespace(); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 944 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 946 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 947 | // For function declarations, we keep track of redeclarations. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 948 | return FD->getPreviousDecl() == OldD; |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 950 | // For function templates, the underlying function declarations are linked. |
| 951 | if (const FunctionTemplateDecl *FunctionTemplate |
| 952 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 953 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 954 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 955 | return FunctionTemplate->getTemplatedDecl() |
| 956 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Steve Naroff | c4173fa | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 958 | // For method declarations, we keep track of redeclarations. |
| 959 | if (isa<ObjCMethodDecl>(this)) |
| 960 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 962 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 963 | return true; |
| 964 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 965 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 966 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 967 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 968 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 969 | if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { |
| 970 | ASTContext &Context = getASTContext(); |
| 971 | return Context.getCanonicalNestedNameSpecifier( |
| 972 | cast<UsingDecl>(this)->getQualifier()) == |
| 973 | Context.getCanonicalNestedNameSpecifier( |
| 974 | cast<UsingDecl>(OldD)->getQualifier()); |
| 975 | } |
Argyrios Kyrtzidis | 4b52007 | 2010-11-04 08:48:52 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | b59643b | 2012-01-03 23:26:26 +0000 | [diff] [blame] | 977 | // A typedef of an Objective-C class type can replace an Objective-C class |
| 978 | // declaration or definition, and vice versa. |
| 979 | if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) || |
| 980 | (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD))) |
| 981 | return true; |
| 982 | |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 983 | // For non-function declarations, if the declarations are of the |
| 984 | // same kind then this must be a redeclaration, or semantic analysis |
| 985 | // would not have given us the new declaration. |
| 986 | return this->getKind() == OldD->getKind(); |
| 987 | } |
| 988 | |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 989 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 990 | return getLinkage() != NoLinkage; |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 991 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 992 | |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 993 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 994 | NamedDecl *ND = this; |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 995 | while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
| 996 | ND = UD->getTargetDecl(); |
| 997 | |
| 998 | if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 999 | return AD->getClassInterface(); |
| 1000 | |
| 1001 | return ND; |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1004 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1005 | if (!isCXXClassMember()) |
| 1006 | return false; |
| 1007 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1008 | const NamedDecl *D = this; |
| 1009 | if (isa<UsingShadowDecl>(D)) |
| 1010 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1011 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1012 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1013 | return true; |
| 1014 | if (isa<CXXMethodDecl>(D)) |
| 1015 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 1016 | if (isa<FunctionTemplateDecl>(D)) |
| 1017 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 1018 | ->getTemplatedDecl())->isInstance(); |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1022 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1023 | // DeclaratorDecl Implementation |
| 1024 | //===----------------------------------------------------------------------===// |
| 1025 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1026 | template <typename DeclT> |
| 1027 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1028 | if (decl->getNumTemplateParameterLists() > 0) |
| 1029 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1030 | else |
| 1031 | return decl->getInnerLocStart(); |
| 1032 | } |
| 1033 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1034 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1035 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1036 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1037 | return SourceLocation(); |
| 1038 | } |
| 1039 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1040 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1041 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1042 | // Make sure the extended decl info is allocated. |
| 1043 | if (!hasExtInfo()) { |
| 1044 | // Save (non-extended) type source info pointer. |
| 1045 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1046 | // Allocate external info struct. |
| 1047 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1048 | // Restore savedTInfo into (extended) decl info. |
| 1049 | getExtInfo()->TInfo = savedTInfo; |
| 1050 | } |
| 1051 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1052 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1053 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1054 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1055 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1056 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1057 | // Save type source info pointer. |
| 1058 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1059 | // Deallocate the extended decl info. |
| 1060 | getASTContext().Deallocate(getExtInfo()); |
| 1061 | // Restore savedTInfo into (non-extended) decl info. |
| 1062 | DeclInfo = savedTInfo; |
| 1063 | } |
| 1064 | else |
| 1065 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1070 | void |
| 1071 | DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 1072 | unsigned NumTPLists, |
| 1073 | TemplateParameterList **TPLists) { |
| 1074 | assert(NumTPLists > 0); |
| 1075 | // Make sure the extended decl info is allocated. |
| 1076 | if (!hasExtInfo()) { |
| 1077 | // Save (non-extended) type source info pointer. |
| 1078 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1079 | // Allocate external info struct. |
| 1080 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1081 | // Restore savedTInfo into (extended) decl info. |
| 1082 | getExtInfo()->TInfo = savedTInfo; |
| 1083 | } |
| 1084 | // Set the template parameter lists info. |
| 1085 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 1086 | } |
| 1087 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1088 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1089 | return getTemplateOrInnerLocStart(this); |
| 1090 | } |
| 1091 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1092 | namespace { |
| 1093 | |
| 1094 | // Helper function: returns true if QT is or contains a type |
| 1095 | // having a postfix component. |
| 1096 | bool typeIsPostfix(clang::QualType QT) { |
| 1097 | while (true) { |
| 1098 | const Type* T = QT.getTypePtr(); |
| 1099 | switch (T->getTypeClass()) { |
| 1100 | default: |
| 1101 | return false; |
| 1102 | case Type::Pointer: |
| 1103 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1104 | break; |
| 1105 | case Type::BlockPointer: |
| 1106 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1107 | break; |
| 1108 | case Type::MemberPointer: |
| 1109 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1110 | break; |
| 1111 | case Type::LValueReference: |
| 1112 | case Type::RValueReference: |
| 1113 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1114 | break; |
| 1115 | case Type::PackExpansion: |
| 1116 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1117 | break; |
| 1118 | case Type::Paren: |
| 1119 | case Type::ConstantArray: |
| 1120 | case Type::DependentSizedArray: |
| 1121 | case Type::IncompleteArray: |
| 1122 | case Type::VariableArray: |
| 1123 | case Type::FunctionProto: |
| 1124 | case Type::FunctionNoProto: |
| 1125 | return true; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | } // namespace |
| 1131 | |
| 1132 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1133 | SourceLocation RangeEnd = getLocation(); |
| 1134 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 1135 | if (typeIsPostfix(TInfo->getType())) |
| 1136 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1137 | } |
| 1138 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1139 | } |
| 1140 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1141 | void |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1142 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 1143 | unsigned NumTPLists, |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1144 | TemplateParameterList **TPLists) { |
| 1145 | assert((NumTPLists == 0 || TPLists != 0) && |
| 1146 | "Empty array of template parameters with positive size!"); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Free previous template parameters (if any). |
| 1149 | if (NumTemplParamLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1150 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1151 | TemplParamLists = 0; |
| 1152 | NumTemplParamLists = 0; |
| 1153 | } |
| 1154 | // Set info on matched template parameter lists (if any). |
| 1155 | if (NumTPLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1156 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1157 | NumTemplParamLists = NumTPLists; |
| 1158 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 1159 | TemplParamLists[i] = TPLists[i]; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1163 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1164 | // VarDecl Implementation |
| 1165 | //===----------------------------------------------------------------------===// |
| 1166 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1167 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1168 | switch (SC) { |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1169 | case SC_None: break; |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1170 | case SC_Auto: return "auto"; |
| 1171 | case SC_Extern: return "extern"; |
| 1172 | case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; |
| 1173 | case SC_PrivateExtern: return "__private_extern__"; |
| 1174 | case SC_Register: return "register"; |
| 1175 | case SC_Static: return "static"; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1178 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1181 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1182 | SourceLocation StartL, SourceLocation IdL, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1183 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1184 | StorageClass S, StorageClass SCAsWritten) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1185 | return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1188 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1189 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl)); |
| 1190 | return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0, |
| 1191 | QualType(), 0, SC_None, SC_None); |
| 1192 | } |
| 1193 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1194 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1195 | assert(isLegalForVariable(SC)); |
| 1196 | if (getStorageClass() != SC) |
| 1197 | ClearLinkageCache(); |
| 1198 | |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1199 | VarDeclBits.SClass = SC; |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1202 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1203 | if (getInit()) |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1204 | return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1205 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1208 | bool VarDecl::isExternC() const { |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1209 | if (getLinkage() != ExternalLinkage) |
Chandler Carruth | 4322a28 | 2011-02-25 00:05:02 +0000 | [diff] [blame] | 1210 | return false; |
| 1211 | |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1212 | const DeclContext *DC = getDeclContext(); |
| 1213 | if (DC->isRecord()) |
| 1214 | return false; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1215 | |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1216 | ASTContext &Context = getASTContext(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1217 | if (!Context.getLangOpts().CPlusPlus) |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | return DC->isExternCContext(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | VarDecl *VarDecl::getCanonicalDecl() { |
| 1223 | return getFirstDeclaration(); |
| 1224 | } |
| 1225 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1226 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition( |
| 1227 | ASTContext &C) const |
| 1228 | { |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1229 | // C++ [basic.def]p2: |
| 1230 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1231 | // specifier or a linkage-specification and neither an initializer [...], |
| 1232 | // it declares a static data member in a class declaration [...]. |
| 1233 | // C++ [temp.expl.spec]p15: |
| 1234 | // An explicit specialization of a static data member of a template is a |
| 1235 | // definition if the declaration includes an initializer; otherwise, it is |
| 1236 | // a declaration. |
| 1237 | if (isStaticDataMember()) { |
| 1238 | if (isOutOfLine() && (hasInit() || |
| 1239 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 1240 | return Definition; |
| 1241 | else |
| 1242 | return DeclarationOnly; |
| 1243 | } |
| 1244 | // C99 6.7p5: |
| 1245 | // A definition of an identifier is a declaration for that identifier that |
| 1246 | // [...] causes storage to be reserved for that object. |
| 1247 | // Note: that applies for all non-file-scope objects. |
| 1248 | // C99 6.9.2p1: |
| 1249 | // If the declaration of an identifier for an object has file scope and an |
| 1250 | // initializer, the declaration is an external definition for the identifier |
| 1251 | if (hasInit()) |
| 1252 | return Definition; |
| 1253 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 1254 | if (hasExternalStorage()) |
| 1255 | return DeclarationOnly; |
Fariborz Jahanian | cc99b3c | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1256 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1257 | if (getStorageClassAsWritten() == SC_Extern || |
| 1258 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1259 | for (const VarDecl *PrevVar = getPreviousDecl(); |
| 1260 | PrevVar; PrevVar = PrevVar->getPreviousDecl()) { |
Fariborz Jahanian | cc99b3c | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1261 | if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit()) |
| 1262 | return DeclarationOnly; |
| 1263 | } |
| 1264 | } |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1265 | // C99 6.9.2p2: |
| 1266 | // A declaration of an object that has file scope without an initializer, |
| 1267 | // and without a storage class specifier or the scs 'static', constitutes |
| 1268 | // a tentative definition. |
| 1269 | // No such thing in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1270 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1271 | return TentativeDefinition; |
| 1272 | |
| 1273 | // What's left is (in C, block-scope) declarations without initializers or |
| 1274 | // external storage. These are definitions. |
| 1275 | return Definition; |
| 1276 | } |
| 1277 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1278 | VarDecl *VarDecl::getActingDefinition() { |
| 1279 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1280 | if (Kind != TentativeDefinition) |
| 1281 | return 0; |
| 1282 | |
Chris Lattner | 48eb14d | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 1283 | VarDecl *LastTentative = 0; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1284 | VarDecl *First = getFirstDeclaration(); |
| 1285 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1286 | I != E; ++I) { |
| 1287 | Kind = (*I)->isThisDeclarationADefinition(); |
| 1288 | if (Kind == Definition) |
| 1289 | return 0; |
| 1290 | else if (Kind == TentativeDefinition) |
| 1291 | LastTentative = *I; |
| 1292 | } |
| 1293 | return LastTentative; |
| 1294 | } |
| 1295 | |
| 1296 | bool VarDecl::isTentativeDefinitionNow() const { |
| 1297 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1298 | if (Kind != TentativeDefinition) |
| 1299 | return false; |
| 1300 | |
| 1301 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1302 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 1303 | return false; |
| 1304 | } |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1305 | return true; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1308 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Sebastian Redl | ccdb5ff | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 1309 | VarDecl *First = getFirstDeclaration(); |
| 1310 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1311 | I != E; ++I) { |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1312 | if ((*I)->isThisDeclarationADefinition(C) == Definition) |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1313 | return *I; |
| 1314 | } |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1318 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1319 | DefinitionKind Kind = DeclarationOnly; |
| 1320 | |
| 1321 | const VarDecl *First = getFirstDeclaration(); |
| 1322 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1323 | I != E; ++I) { |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1324 | Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1325 | if (Kind == Definition) |
| 1326 | break; |
| 1327 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1328 | |
| 1329 | return Kind; |
| 1330 | } |
| 1331 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1332 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1333 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 1334 | while (I != E && !I->getInit()) |
| 1335 | ++I; |
| 1336 | |
| 1337 | if (I != E) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1338 | D = *I; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1339 | return I->getInit(); |
| 1340 | } |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1344 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1345 | if (Decl::isOutOfLine()) |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1346 | return true; |
Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1347 | |
| 1348 | if (!isStaticDataMember()) |
| 1349 | return false; |
| 1350 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1351 | // If this static data member was instantiated from a static data member of |
| 1352 | // a class template, check whether that static data member was defined |
| 1353 | // out-of-line. |
| 1354 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 1355 | return VD->isOutOfLine(); |
| 1356 | |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1360 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 1361 | if (!isStaticDataMember()) |
| 1362 | return 0; |
| 1363 | |
| 1364 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 1365 | RD != RDEnd; ++RD) { |
| 1366 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 1367 | return *RD; |
| 1368 | } |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1373 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1374 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 1375 | Eval->~EvaluatedStmt(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1376 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | Init = I; |
| 1380 | } |
| 1381 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1382 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1383 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1384 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1385 | if (!Lang.CPlusPlus) |
| 1386 | return false; |
| 1387 | |
| 1388 | // In C++11, any variable of reference type can be used in a constant |
| 1389 | // expression if it is initialized by a constant expression. |
| 1390 | if (Lang.CPlusPlus0x && getType()->isReferenceType()) |
| 1391 | return true; |
| 1392 | |
| 1393 | // Only const objects can be used in constant expressions in C++. C++98 does |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1394 | // not require the variable to be non-volatile, but we consider this to be a |
| 1395 | // defect. |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1396 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1397 | return false; |
| 1398 | |
| 1399 | // In C++, const, non-volatile variables of integral or enumeration types |
| 1400 | // can be used in constant expressions. |
| 1401 | if (getType()->isIntegralOrEnumerationType()) |
| 1402 | return true; |
| 1403 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1404 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 1405 | // constant expressions. |
| 1406 | return Lang.CPlusPlus0x && isConstexpr(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1409 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 1410 | /// form, which contains extra information on the evaluated value of the |
| 1411 | /// initializer. |
| 1412 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| 1413 | EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| 1414 | if (!Eval) { |
| 1415 | Stmt *S = Init.get<Stmt *>(); |
| 1416 | Eval = new (getASTContext()) EvaluatedStmt; |
| 1417 | Eval->Value = S; |
| 1418 | Init = Eval; |
| 1419 | } |
| 1420 | return Eval; |
| 1421 | } |
| 1422 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1423 | APValue *VarDecl::evaluateValue() const { |
| 1424 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1425 | return evaluateValue(Notes); |
| 1426 | } |
| 1427 | |
| 1428 | APValue *VarDecl::evaluateValue( |
| 1429 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1430 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1431 | |
| 1432 | // We only produce notes indicating why an initializer is non-constant the |
| 1433 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 1434 | // first time we try evaluation, so might not be produced at all. |
| 1435 | if (Eval->WasEvaluated) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1436 | return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1437 | |
| 1438 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1439 | assert(!Init->isValueDependent()); |
| 1440 | |
| 1441 | if (Eval->IsEvaluating) { |
| 1442 | // FIXME: Produce a diagnostic for self-initialization. |
| 1443 | Eval->CheckedICE = true; |
| 1444 | Eval->IsICE = false; |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1445 | return 0; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | Eval->IsEvaluating = true; |
| 1449 | |
| 1450 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 1451 | this, Notes); |
| 1452 | |
| 1453 | // Ensure the result is an uninitialized APValue if evaluation fails. |
| 1454 | if (!Result) |
| 1455 | Eval->Evaluated = APValue(); |
| 1456 | |
| 1457 | Eval->IsEvaluating = false; |
| 1458 | Eval->WasEvaluated = true; |
| 1459 | |
| 1460 | // In C++11, we have determined whether the initializer was a constant |
| 1461 | // expression as a side-effect. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1462 | if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1463 | Eval->CheckedICE = true; |
Eli Friedman | 8f66cdf | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 1464 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1467 | return Result ? &Eval->Evaluated : 0; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | bool VarDecl::checkInitIsICE() const { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 1471 | // Initializers of weak variables are never ICEs. |
| 1472 | if (isWeak()) |
| 1473 | return false; |
| 1474 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1475 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1476 | if (Eval->CheckedICE) |
| 1477 | // We have already checked whether this subexpression is an |
| 1478 | // integral constant expression. |
| 1479 | return Eval->IsICE; |
| 1480 | |
| 1481 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1482 | assert(!Init->isValueDependent()); |
| 1483 | |
| 1484 | // In C++11, evaluate the initializer to check whether it's a constant |
| 1485 | // expression. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1486 | if (getASTContext().getLangOpts().CPlusPlus0x) { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1487 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1488 | evaluateValue(Notes); |
| 1489 | return Eval->IsICE; |
| 1490 | } |
| 1491 | |
| 1492 | // It's an ICE whether or not the definition we found is |
| 1493 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 1494 | // 6206 for details. |
| 1495 | |
| 1496 | if (Eval->CheckingICE) |
| 1497 | return false; |
| 1498 | Eval->CheckingICE = true; |
| 1499 | |
| 1500 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 1501 | Eval->CheckingICE = false; |
| 1502 | Eval->CheckedICE = true; |
| 1503 | return Eval->IsICE; |
| 1504 | } |
| 1505 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1506 | bool VarDecl::extendsLifetimeOfTemporary() const { |
Douglas Gregor | d410c08 | 2011-06-21 18:20:46 +0000 | [diff] [blame] | 1507 | assert(getType()->isReferenceType() &&"Non-references never extend lifetime"); |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1508 | |
| 1509 | const Expr *E = getInit(); |
| 1510 | if (!E) |
| 1511 | return false; |
| 1512 | |
| 1513 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E)) |
| 1514 | E = Cleanups->getSubExpr(); |
| 1515 | |
| 1516 | return isa<MaterializeTemporaryExpr>(E); |
| 1517 | } |
| 1518 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1519 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1520 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1521 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 1522 | |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1526 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1527 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1528 | return MSI->getTemplateSpecializationKind(); |
| 1529 | |
| 1530 | return TSK_Undeclared; |
| 1531 | } |
| 1532 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1533 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1534 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 1535 | } |
| 1536 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1537 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1538 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1539 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1540 | assert(MSI && "Not an instantiated static data member?"); |
| 1541 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1542 | if (TSK != TSK_ExplicitSpecialization && |
| 1543 | PointOfInstantiation.isValid() && |
| 1544 | MSI->getPointOfInstantiation().isInvalid()) |
| 1545 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1548 | //===----------------------------------------------------------------------===// |
| 1549 | // ParmVarDecl Implementation |
| 1550 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1551 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1552 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1553 | SourceLocation StartLoc, |
| 1554 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1555 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1556 | StorageClass S, StorageClass SCAsWritten, |
| 1557 | Expr *DefArg) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1558 | return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1559 | S, SCAsWritten, DefArg); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1562 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1563 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl)); |
| 1564 | return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(), |
| 1565 | 0, QualType(), 0, SC_None, SC_None, 0); |
| 1566 | } |
| 1567 | |
Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 1568 | SourceRange ParmVarDecl::getSourceRange() const { |
| 1569 | if (!hasInheritedDefaultArg()) { |
| 1570 | SourceRange ArgRange = getDefaultArgRange(); |
| 1571 | if (ArgRange.isValid()) |
| 1572 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 1573 | } |
| 1574 | |
| 1575 | return DeclaratorDecl::getSourceRange(); |
| 1576 | } |
| 1577 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1578 | Expr *ParmVarDecl::getDefaultArg() { |
| 1579 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1580 | assert(!hasUninstantiatedDefaultArg() && |
| 1581 | "Default argument is not yet instantiated!"); |
| 1582 | |
| 1583 | Expr *Arg = getInit(); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1584 | if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1585 | return E->getSubExpr(); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1586 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1587 | return Arg; |
| 1588 | } |
| 1589 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1590 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1591 | if (const Expr *E = getInit()) |
| 1592 | return E->getSourceRange(); |
| 1593 | |
| 1594 | if (hasUninstantiatedDefaultArg()) |
| 1595 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1596 | |
| 1597 | return SourceRange(); |
Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Douglas Gregor | 3c6bd2a | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 1600 | bool ParmVarDecl::isParameterPack() const { |
| 1601 | return isa<PackExpansionType>(getType()); |
| 1602 | } |
| 1603 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1604 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 1605 | getASTContext().setParameterIndex(this, parameterIndex); |
| 1606 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 1607 | } |
| 1608 | |
| 1609 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 1610 | return getASTContext().getParameterIndex(this); |
| 1611 | } |
| 1612 | |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1613 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1614 | // FunctionDecl Implementation |
| 1615 | //===----------------------------------------------------------------------===// |
| 1616 | |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1617 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1618 | const PrintingPolicy &Policy, |
| 1619 | bool Qualified) const { |
| 1620 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1621 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1622 | if (TemplateArgs) |
| 1623 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1624 | TemplateArgs->data(), |
| 1625 | TemplateArgs->size(), |
| 1626 | Policy); |
| 1627 | |
| 1628 | } |
| 1629 | |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1630 | bool FunctionDecl::isVariadic() const { |
| 1631 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1632 | return FT->isVariadic(); |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1636 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1637 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1638 | if (I->Body || I->IsLateTemplateParsed) { |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1639 | Definition = *I; |
| 1640 | return true; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1647 | bool FunctionDecl::hasTrivialBody() const |
| 1648 | { |
| 1649 | Stmt *S = getBody(); |
| 1650 | if (!S) { |
| 1651 | // Since we don't have a body for this function, we don't know if it's |
| 1652 | // trivial or not. |
| 1653 | return false; |
| 1654 | } |
| 1655 | |
| 1656 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 1657 | return true; |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1661 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
| 1662 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 1663 | if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1664 | Definition = I->IsDeleted ? I->getCanonicalDecl() : *I; |
| 1665 | return true; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | return false; |
| 1670 | } |
| 1671 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1672 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | 1506d9b | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1673 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1674 | if (I->Body) { |
| 1675 | Definition = *I; |
| 1676 | return I->Body.get(getASTContext().getExternalSource()); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1677 | } else if (I->IsLateTemplateParsed) { |
| 1678 | Definition = *I; |
| 1679 | return 0; |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | return 0; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1686 | void FunctionDecl::setBody(Stmt *B) { |
| 1687 | Body = B; |
Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 1688 | if (B) |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1689 | EndRangeLoc = B->getLocEnd(); |
| 1690 | } |
| 1691 | |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1692 | void FunctionDecl::setPure(bool P) { |
| 1693 | IsPure = P; |
| 1694 | if (P) |
| 1695 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1696 | Parent->markedVirtualFunctionPure(); |
| 1697 | } |
| 1698 | |
Richard Smith | 12f247f | 2012-06-08 21:09:22 +0000 | [diff] [blame] | 1699 | void FunctionDecl::setConstexpr(bool IC) { |
| 1700 | IsConstexpr = IC; |
| 1701 | CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this); |
| 1702 | if (IC && CD) |
| 1703 | CD->getParent()->markedConstructorConstexpr(CD); |
| 1704 | } |
| 1705 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1706 | bool FunctionDecl::isMain() const { |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1707 | const TranslationUnitDecl *tunit = |
| 1708 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 1709 | return tunit && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1710 | !tunit->getASTContext().getLangOpts().Freestanding && |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1711 | getIdentifier() && |
| 1712 | getIdentifier()->isStr("main"); |
| 1713 | } |
| 1714 | |
| 1715 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 1716 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 1717 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 1718 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 1719 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 1720 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 1721 | |
| 1722 | if (isa<CXXRecordDecl>(getDeclContext())) return false; |
| 1723 | assert(getDeclContext()->getRedeclContext()->isTranslationUnit()); |
| 1724 | |
| 1725 | const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); |
| 1726 | if (proto->getNumArgs() != 2 || proto->isVariadic()) return false; |
| 1727 | |
| 1728 | ASTContext &Context = |
| 1729 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 1730 | ->getASTContext(); |
| 1731 | |
| 1732 | // The result type and first argument type are constant across all |
| 1733 | // these operators. The second argument must be exactly void*. |
| 1734 | return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1737 | bool FunctionDecl::isExternC() const { |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1738 | if (getLinkage() != ExternalLinkage) |
| 1739 | return false; |
| 1740 | |
| 1741 | if (getAttr<OverloadableAttr>()) |
| 1742 | return false; |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1743 | |
Chandler Carruth | 4322a28 | 2011-02-25 00:05:02 +0000 | [diff] [blame] | 1744 | const DeclContext *DC = getDeclContext(); |
| 1745 | if (DC->isRecord()) |
| 1746 | return false; |
| 1747 | |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1748 | ASTContext &Context = getASTContext(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1749 | if (!Context.getLangOpts().CPlusPlus) |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1750 | return true; |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1751 | |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1752 | return isMain() || DC->isExternCContext(); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1755 | bool FunctionDecl::isGlobal() const { |
| 1756 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1757 | return Method->isStatic(); |
| 1758 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1759 | if (getStorageClass() == SC_Static) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1760 | return false; |
| 1761 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1763 | DC->isNamespace(); |
| 1764 | DC = DC->getParent()) { |
| 1765 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1766 | if (!Namespace->getDeclName()) |
| 1767 | return false; |
| 1768 | break; |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | return true; |
| 1773 | } |
| 1774 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1775 | void |
| 1776 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1777 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1778 | |
| 1779 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1780 | FunctionTemplateDecl *PrevFunTmpl |
| 1781 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1782 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1783 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1784 | } |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1785 | |
Axel Naumann | fbc7b98 | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 1786 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1787 | IsInline = true; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1791 | return getFirstDeclaration(); |
| 1792 | } |
| 1793 | |
| 1794 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1795 | return getFirstDeclaration(); |
| 1796 | } |
| 1797 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1798 | void FunctionDecl::setStorageClass(StorageClass SC) { |
| 1799 | assert(isLegalForFunction(SC)); |
| 1800 | if (getStorageClass() != SC) |
| 1801 | ClearLinkageCache(); |
| 1802 | |
| 1803 | SClass = SC; |
| 1804 | } |
| 1805 | |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1806 | /// \brief Returns a value indicating whether this function |
| 1807 | /// corresponds to a builtin function. |
| 1808 | /// |
| 1809 | /// The function corresponds to a built-in function if it is |
| 1810 | /// declared at translation scope or within an extern "C" block and |
| 1811 | /// its name matches with the name of a builtin. The returned value |
| 1812 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1814 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1815 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1816 | if (!getIdentifier()) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1817 | return 0; |
| 1818 | |
| 1819 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1820 | if (!BuiltinID) |
| 1821 | return 0; |
| 1822 | |
| 1823 | ASTContext &Context = getASTContext(); |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1824 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1825 | return BuiltinID; |
| 1826 | |
| 1827 | // This function has the name of a known C library |
| 1828 | // function. Determine whether it actually refers to the C library |
| 1829 | // function or whether it just has the same name. |
| 1830 | |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1831 | // If this is a static function, it's not a builtin. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1832 | if (getStorageClass() == SC_Static) |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1833 | return 0; |
| 1834 | |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1835 | // If this function is at translation-unit scope and we're not in |
| 1836 | // C++, it refers to the C library function. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1837 | if (!Context.getLangOpts().CPlusPlus && |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1838 | getDeclContext()->isTranslationUnit()) |
| 1839 | return BuiltinID; |
| 1840 | |
| 1841 | // If the function is in an extern "C" linkage specification and is |
| 1842 | // not marked "overloadable", it's the real function. |
| 1843 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1845 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1846 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1847 | return BuiltinID; |
| 1848 | |
| 1849 | // Not a builtin |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1854 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | b39017a | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 1855 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1856 | /// after it has been created. |
| 1857 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1858 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1859 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1860 | return 0; |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1861 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1865 | void FunctionDecl::setParams(ASTContext &C, |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1866 | llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1867 | assert(ParamInfo == 0 && "Already has param info!"); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1868 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 1870 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1871 | if (!NewParamInfo.empty()) { |
| 1872 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 1873 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 1874 | } |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 1875 | } |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 1876 | |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1877 | void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) { |
| 1878 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 1879 | |
| 1880 | if (!NewDecls.empty()) { |
| 1881 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 1882 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
| 1883 | DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size()); |
| 1884 | } |
| 1885 | } |
| 1886 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1887 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1888 | /// needed to call this function. This may be fewer than the number of |
| 1889 | /// function parameters, if some of the parameters have default |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1890 | /// arguments (in C++) or the last parameter is a parameter pack. |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1891 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1892 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1893 | return getNumParams(); |
| 1894 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1895 | unsigned NumRequiredArgs = getNumParams(); |
| 1896 | |
| 1897 | // If the last parameter is a parameter pack, we don't need an argument for |
| 1898 | // it. |
| 1899 | if (NumRequiredArgs > 0 && |
| 1900 | getParamDecl(NumRequiredArgs - 1)->isParameterPack()) |
| 1901 | --NumRequiredArgs; |
| 1902 | |
| 1903 | // If this parameter has a default argument, we don't need an argument for |
| 1904 | // it. |
| 1905 | while (NumRequiredArgs > 0 && |
| 1906 | getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1907 | --NumRequiredArgs; |
| 1908 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1909 | // We might have parameter packs before the end. These can't be deduced, |
| 1910 | // but they can still handle multiple arguments. |
| 1911 | unsigned ArgIdx = NumRequiredArgs; |
| 1912 | while (ArgIdx > 0) { |
| 1913 | if (getParamDecl(ArgIdx - 1)->isParameterPack()) |
| 1914 | NumRequiredArgs = ArgIdx; |
| 1915 | |
| 1916 | --ArgIdx; |
| 1917 | } |
| 1918 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1919 | return NumRequiredArgs; |
| 1920 | } |
| 1921 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1922 | bool FunctionDecl::isInlined() const { |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1923 | if (IsInline) |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1924 | return true; |
Anders Carlsson | cfb65d7 | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1925 | |
| 1926 | if (isa<CXXMethodDecl>(this)) { |
| 1927 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1928 | return true; |
| 1929 | } |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1930 | |
| 1931 | switch (getTemplateSpecializationKind()) { |
| 1932 | case TSK_Undeclared: |
| 1933 | case TSK_ExplicitSpecialization: |
| 1934 | return false; |
| 1935 | |
| 1936 | case TSK_ImplicitInstantiation: |
| 1937 | case TSK_ExplicitInstantiationDeclaration: |
| 1938 | case TSK_ExplicitInstantiationDefinition: |
| 1939 | // Handle below. |
| 1940 | break; |
| 1941 | } |
| 1942 | |
| 1943 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1944 | bool HasPattern = false; |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1945 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1946 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1947 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1948 | if (HasPattern && PatternDecl) |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1949 | return PatternDecl->isInlined(); |
| 1950 | |
| 1951 | return false; |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1954 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 1955 | // Only consider file-scope declarations in this test. |
| 1956 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1957 | return false; |
| 1958 | |
| 1959 | // Only consider explicit declarations; the presence of a builtin for a |
| 1960 | // libcall shouldn't affect whether a definition is externally visible. |
| 1961 | if (Redecl->isImplicit()) |
| 1962 | return false; |
| 1963 | |
| 1964 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 1965 | return true; // Not an inline definition |
| 1966 | |
| 1967 | return false; |
| 1968 | } |
| 1969 | |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1970 | /// \brief For a function declaration in C or C++, determine whether this |
| 1971 | /// declaration causes the definition to be externally visible. |
| 1972 | /// |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1973 | /// Specifically, this determines if adding the current declaration to the set |
| 1974 | /// of redeclarations of the given functions causes |
| 1975 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1976 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 1977 | assert(!doesThisDeclarationHaveABody() && |
| 1978 | "Must have a declaration without a body."); |
| 1979 | |
| 1980 | ASTContext &Context = getASTContext(); |
| 1981 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1982 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1983 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 1984 | // an externally visible definition. |
| 1985 | // |
| 1986 | // FIXME: What happens if gnu_inline gets added on after the first |
| 1987 | // declaration? |
| 1988 | if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern) |
| 1989 | return false; |
| 1990 | |
| 1991 | const FunctionDecl *Prev = this; |
| 1992 | bool FoundBody = false; |
| 1993 | while ((Prev = Prev->getPreviousDecl())) { |
| 1994 | FoundBody |= Prev->Body; |
| 1995 | |
| 1996 | if (Prev->Body) { |
| 1997 | // If it's not the case that both 'inline' and 'extern' are |
| 1998 | // specified on the definition, then it is always externally visible. |
| 1999 | if (!Prev->isInlineSpecified() || |
| 2000 | Prev->getStorageClassAsWritten() != SC_Extern) |
| 2001 | return false; |
| 2002 | } else if (Prev->isInlineSpecified() && |
| 2003 | Prev->getStorageClassAsWritten() != SC_Extern) { |
| 2004 | return false; |
| 2005 | } |
| 2006 | } |
| 2007 | return FoundBody; |
| 2008 | } |
| 2009 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2010 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2011 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2012 | |
| 2013 | // C99 6.7.4p6: |
| 2014 | // [...] If all of the file scope declarations for a function in a |
| 2015 | // translation unit include the inline function specifier without extern, |
| 2016 | // then the definition in that translation unit is an inline definition. |
| 2017 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2018 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2019 | const FunctionDecl *Prev = this; |
| 2020 | bool FoundBody = false; |
| 2021 | while ((Prev = Prev->getPreviousDecl())) { |
| 2022 | FoundBody |= Prev->Body; |
| 2023 | if (RedeclForcesDefC99(Prev)) |
| 2024 | return false; |
| 2025 | } |
| 2026 | return FoundBody; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2029 | /// \brief For an inline function definition in C or C++, determine whether the |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2030 | /// definition will be externally visible. |
| 2031 | /// |
| 2032 | /// Inline function definitions are always available for inlining optimizations. |
| 2033 | /// However, depending on the language dialect, declaration specifiers, and |
| 2034 | /// attributes, the definition of an inline function may or may not be |
| 2035 | /// "externally" visible to other translation units in the program. |
| 2036 | /// |
| 2037 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2038 | /// if even one of the global-scope declarations is marked "extern inline", the |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2039 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2040 | /// |
| 2041 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2042 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2043 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2044 | /// an externally visible symbol, but "extern inline" will not create an |
| 2045 | /// externally visible symbol. |
| 2046 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2047 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2048 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2049 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2050 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2051 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2052 | // Note: If you change the logic here, please change |
| 2053 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2054 | // |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2055 | // If it's not the case that both 'inline' and 'extern' are |
| 2056 | // specified on the definition, then this inline definition is |
| 2057 | // externally visible. |
| 2058 | if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) |
| 2059 | return true; |
| 2060 | |
| 2061 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2062 | // is externally visible. |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2063 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2064 | Redecl != RedeclEnd; |
| 2065 | ++Redecl) { |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2066 | if (Redecl->isInlineSpecified() && |
| 2067 | Redecl->getStorageClassAsWritten() != SC_Extern) |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2068 | return true; |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2069 | } |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2070 | |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2071 | return false; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2072 | } |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2074 | // C99 6.7.4p6: |
| 2075 | // [...] If all of the file scope declarations for a function in a |
| 2076 | // translation unit include the inline function specifier without extern, |
| 2077 | // then the definition in that translation unit is an inline definition. |
| 2078 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2079 | Redecl != RedeclEnd; |
| 2080 | ++Redecl) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2081 | if (RedeclForcesDefC99(*Redecl)) |
| 2082 | return true; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | // C99 6.7.4p6: |
| 2086 | // An inline definition does not provide an external definition for the |
| 2087 | // function, and does not forbid an external definition in another |
| 2088 | // translation unit. |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
| 2091 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2092 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2093 | /// function represents, if any. |
| 2094 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2095 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2096 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2097 | else |
| 2098 | return OO_None; |
| 2099 | } |
| 2100 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2101 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2102 | /// represents, if any. |
| 2103 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2104 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2105 | return getDeclName().getCXXLiteralIdentifier(); |
| 2106 | else |
| 2107 | return 0; |
| 2108 | } |
| 2109 | |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2110 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2111 | if (TemplateOrSpecialization.isNull()) |
| 2112 | return TK_NonTemplate; |
| 2113 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2114 | return TK_FunctionTemplate; |
| 2115 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2116 | return TK_MemberSpecialization; |
| 2117 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2118 | return TK_FunctionTemplateSpecialization; |
| 2119 | if (TemplateOrSpecialization.is |
| 2120 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2121 | return TK_DependentFunctionTemplateSpecialization; |
| 2122 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2123 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2126 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2127 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2128 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 2129 | |
| 2130 | return 0; |
| 2131 | } |
| 2132 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2133 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 2134 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2135 | } |
| 2136 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2137 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2138 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2139 | FunctionDecl *FD, |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2140 | TemplateSpecializationKind TSK) { |
| 2141 | assert(TemplateOrSpecialization.isNull() && |
| 2142 | "Member function is already a specialization"); |
| 2143 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2144 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2145 | TemplateOrSpecialization = Info; |
| 2146 | } |
| 2147 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2148 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2149 | // If the function is invalid, it can't be implicitly instantiated. |
| 2150 | if (isInvalidDecl()) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2151 | return false; |
| 2152 | |
| 2153 | switch (getTemplateSpecializationKind()) { |
| 2154 | case TSK_Undeclared: |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2155 | case TSK_ExplicitInstantiationDefinition: |
| 2156 | return false; |
| 2157 | |
| 2158 | case TSK_ImplicitInstantiation: |
| 2159 | return true; |
| 2160 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2161 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2162 | // if the FunctionDecl has a class scope specialization pattern. |
| 2163 | case TSK_ExplicitSpecialization: |
| 2164 | return getClassScopeSpecializationPattern() != 0; |
| 2165 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2166 | case TSK_ExplicitInstantiationDeclaration: |
| 2167 | // Handled below. |
| 2168 | break; |
| 2169 | } |
| 2170 | |
| 2171 | // Find the actual template from which we will instantiate. |
| 2172 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2173 | bool HasPattern = false; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2174 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2175 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2176 | |
| 2177 | // C++0x [temp.explicit]p9: |
| 2178 | // Except for inline functions, other explicit instantiation declarations |
| 2179 | // have the effect of suppressing the implicit instantiation of the entity |
| 2180 | // to which they refer. |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2181 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2182 | return true; |
| 2183 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2184 | return PatternDecl->isInlined(); |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | bool FunctionDecl::isTemplateInstantiation() const { |
| 2188 | switch (getTemplateSpecializationKind()) { |
| 2189 | case TSK_Undeclared: |
| 2190 | case TSK_ExplicitSpecialization: |
| 2191 | return false; |
| 2192 | case TSK_ImplicitInstantiation: |
| 2193 | case TSK_ExplicitInstantiationDeclaration: |
| 2194 | case TSK_ExplicitInstantiationDefinition: |
| 2195 | return true; |
| 2196 | } |
| 2197 | llvm_unreachable("All TSK values handled."); |
| 2198 | } |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2199 | |
| 2200 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2201 | // Handle class scope explicit specialization special case. |
| 2202 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2203 | return getClassScopeSpecializationPattern(); |
| 2204 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2205 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 2206 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 2207 | // If we have hit a point where the user provided a specialization of |
| 2208 | // this template, we're done looking. |
| 2209 | if (Primary->isMemberSpecialization()) |
| 2210 | break; |
| 2211 | |
| 2212 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 2213 | } |
| 2214 | |
| 2215 | return Primary->getTemplatedDecl(); |
| 2216 | } |
| 2217 | |
| 2218 | return getInstantiatedFromMemberFunction(); |
| 2219 | } |
| 2220 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2221 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2223 | = TemplateOrSpecialization |
| 2224 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2225 | return Info->Template.getPointer(); |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2226 | } |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2230 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 2231 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 2232 | } |
| 2233 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2234 | const TemplateArgumentList * |
| 2235 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2237 | = TemplateOrSpecialization |
| 2238 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2239 | return Info->TemplateArguments; |
| 2240 | } |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 2244 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2245 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 2246 | if (FunctionTemplateSpecializationInfo *Info |
| 2247 | = TemplateOrSpecialization |
| 2248 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 2249 | return Info->TemplateArgumentsAsWritten; |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2255 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 2256 | FunctionTemplateDecl *Template, |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2257 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2258 | void *InsertPos, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2259 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 2260 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 2261 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2262 | assert(TSK != TSK_Undeclared && |
| 2263 | "Must specify the type of function template specialization"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2265 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2266 | if (!Info) |
Argyrios Kyrtzidis | e262a95 | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 2267 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 2268 | TemplateArgs, |
| 2269 | TemplateArgsAsWritten, |
| 2270 | PointOfInstantiation); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2271 | TemplateOrSpecialization = Info; |
Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2272 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2275 | void |
| 2276 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 2277 | const UnresolvedSetImpl &Templates, |
| 2278 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2279 | assert(TemplateOrSpecialization.isNull()); |
| 2280 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 2281 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 900d980 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 2282 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2283 | void *Buffer = Context.Allocate(Size); |
| 2284 | DependentFunctionTemplateSpecializationInfo *Info = |
| 2285 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 2286 | TemplateArgs); |
| 2287 | TemplateOrSpecialization = Info; |
| 2288 | } |
| 2289 | |
| 2290 | DependentFunctionTemplateSpecializationInfo:: |
| 2291 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 2292 | const TemplateArgumentListInfo &TArgs) |
| 2293 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 2294 | |
| 2295 | d.NumTemplates = Ts.size(); |
| 2296 | d.NumArgs = TArgs.size(); |
| 2297 | |
| 2298 | FunctionTemplateDecl **TsArray = |
| 2299 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 2300 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 2301 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 2302 | |
| 2303 | TemplateArgumentLoc *ArgsArray = |
| 2304 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 2305 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 2306 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 2307 | } |
| 2308 | |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2309 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | // For a function template specialization, query the specialization |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2311 | // information object. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2312 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2313 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2314 | if (FTSInfo) |
| 2315 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2317 | MemberSpecializationInfo *MSInfo |
| 2318 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2319 | if (MSInfo) |
| 2320 | return MSInfo->getTemplateSpecializationKind(); |
| 2321 | |
| 2322 | return TSK_Undeclared; |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | void |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2326 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2327 | SourceLocation PointOfInstantiation) { |
| 2328 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2329 | = TemplateOrSpecialization.dyn_cast< |
| 2330 | FunctionTemplateSpecializationInfo*>()) { |
| 2331 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 2332 | if (TSK != TSK_ExplicitSpecialization && |
| 2333 | PointOfInstantiation.isValid() && |
| 2334 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 2335 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2336 | } else if (MemberSpecializationInfo *MSInfo |
| 2337 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 2338 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2339 | if (TSK != TSK_ExplicitSpecialization && |
| 2340 | PointOfInstantiation.isValid() && |
| 2341 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 2342 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2343 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2344 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2348 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2349 | = TemplateOrSpecialization.dyn_cast< |
| 2350 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2351 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2352 | else if (MemberSpecializationInfo *MSInfo |
| 2353 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2354 | return MSInfo->getPointOfInstantiation(); |
| 2355 | |
| 2356 | return SourceLocation(); |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2359 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2360 | if (Decl::isOutOfLine()) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2361 | return true; |
| 2362 | |
| 2363 | // If this function was instantiated from a member function of a |
| 2364 | // class template, check whether that member function was defined out-of-line. |
| 2365 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 2366 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2367 | if (FD->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2368 | return Definition->isOutOfLine(); |
| 2369 | } |
| 2370 | |
| 2371 | // If this function was instantiated from a function template, |
| 2372 | // check whether that function template was defined out-of-line. |
| 2373 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 2374 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2375 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2376 | return Definition->isOutOfLine(); |
| 2377 | } |
| 2378 | |
| 2379 | return false; |
| 2380 | } |
| 2381 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2382 | SourceRange FunctionDecl::getSourceRange() const { |
| 2383 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 2384 | } |
| 2385 | |
Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 2386 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2387 | IdentifierInfo *FnInfo = getIdentifier(); |
| 2388 | |
| 2389 | if (!FnInfo) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2390 | return 0; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2391 | |
| 2392 | // Builtin handling. |
| 2393 | switch (getBuiltinID()) { |
| 2394 | case Builtin::BI__builtin_memset: |
| 2395 | case Builtin::BI__builtin___memset_chk: |
| 2396 | case Builtin::BImemset: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2397 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2398 | |
| 2399 | case Builtin::BI__builtin_memcpy: |
| 2400 | case Builtin::BI__builtin___memcpy_chk: |
| 2401 | case Builtin::BImemcpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2402 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2403 | |
| 2404 | case Builtin::BI__builtin_memmove: |
| 2405 | case Builtin::BI__builtin___memmove_chk: |
| 2406 | case Builtin::BImemmove: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2407 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2408 | |
| 2409 | case Builtin::BIstrlcpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2410 | return Builtin::BIstrlcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2411 | case Builtin::BIstrlcat: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2412 | return Builtin::BIstrlcat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2413 | |
| 2414 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2415 | case Builtin::BImemcmp: |
| 2416 | return Builtin::BImemcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2417 | |
| 2418 | case Builtin::BI__builtin_strncpy: |
| 2419 | case Builtin::BI__builtin___strncpy_chk: |
| 2420 | case Builtin::BIstrncpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2421 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2422 | |
| 2423 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2424 | case Builtin::BIstrncmp: |
| 2425 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2426 | |
| 2427 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2428 | case Builtin::BIstrncasecmp: |
| 2429 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2430 | |
| 2431 | case Builtin::BI__builtin_strncat: |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2432 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2433 | case Builtin::BIstrncat: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2434 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2435 | |
| 2436 | case Builtin::BI__builtin_strndup: |
| 2437 | case Builtin::BIstrndup: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2438 | return Builtin::BIstrndup; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2439 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2440 | case Builtin::BI__builtin_strlen: |
| 2441 | case Builtin::BIstrlen: |
| 2442 | return Builtin::BIstrlen; |
| 2443 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2444 | default: |
Eli Friedman | 839192f | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 2445 | if (isExternC()) { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2446 | if (FnInfo->isStr("memset")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2447 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2448 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2449 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2450 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2451 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2452 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2453 | return Builtin::BImemcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2454 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2455 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2456 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2457 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2458 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2459 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2460 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2461 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2462 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2463 | return Builtin::BIstrndup; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2464 | else if (FnInfo->isStr("strlen")) |
| 2465 | return Builtin::BIstrlen; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2466 | } |
| 2467 | break; |
| 2468 | } |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2469 | return 0; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2472 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2473 | // FieldDecl Implementation |
| 2474 | //===----------------------------------------------------------------------===// |
| 2475 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2476 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2477 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2478 | IdentifierInfo *Id, QualType T, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2479 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2480 | InClassInitStyle InitStyle) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2481 | return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2482 | BW, Mutable, InitStyle); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2485 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2486 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl)); |
| 2487 | return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2488 | 0, QualType(), 0, 0, false, ICIS_NoInit); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2491 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 2492 | if (!isImplicit() || getDeclName()) |
| 2493 | return false; |
| 2494 | |
| 2495 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 2496 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 2497 | |
| 2498 | return false; |
| 2499 | } |
| 2500 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2501 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 2502 | assert(isBitField() && "not a bitfield"); |
| 2503 | Expr *BitWidth = InitializerOrBitWidth.getPointer(); |
| 2504 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 2505 | } |
| 2506 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2507 | unsigned FieldDecl::getFieldIndex() const { |
| 2508 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 2509 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2510 | unsigned Index = 0; |
Fariborz Jahanian | 8409bce4 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2511 | const RecordDecl *RD = getParent(); |
| 2512 | const FieldDecl *LastFD = 0; |
| 2513 | bool IsMsStruct = RD->hasAttr<MsStructAttr>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2514 | |
| 2515 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 2516 | I != E; ++I, ++Index) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2517 | I->CachedFieldIndex = Index + 1; |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2518 | |
Fariborz Jahanian | 8409bce4 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2519 | if (IsMsStruct) { |
| 2520 | // Zero-length bitfields following non-bitfield members are ignored. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2521 | if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2522 | --Index; |
Fariborz Jahanian | 8409bce4 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2523 | continue; |
| 2524 | } |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2525 | LastFD = *I; |
Fariborz Jahanian | 8409bce4 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2526 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2529 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 2530 | return CachedFieldIndex - 1; |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2533 | SourceRange FieldDecl::getSourceRange() const { |
Abramo Bagnara | ff371ac | 2011-08-05 08:02:55 +0000 | [diff] [blame] | 2534 | if (const Expr *E = InitializerOrBitWidth.getPointer()) |
| 2535 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2536 | return DeclaratorDecl::getSourceRange(); |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Abramo Bagnara | b1cdde7 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 2539 | void FieldDecl::setBitWidth(Expr *Width) { |
| 2540 | assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() && |
| 2541 | "bit width or initializer already set"); |
| 2542 | InitializerOrBitWidth.setPointer(Width); |
| 2543 | } |
| 2544 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2545 | void FieldDecl::setInClassInitializer(Expr *Init) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2546 | assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() && |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2547 | "bit width or initializer already set"); |
| 2548 | InitializerOrBitWidth.setPointer(Init); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2551 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2552 | // TagDecl Implementation |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2553 | //===----------------------------------------------------------------------===// |
| 2554 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2555 | SourceLocation TagDecl::getOuterLocStart() const { |
| 2556 | return getTemplateOrInnerLocStart(this); |
| 2557 | } |
| 2558 | |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2559 | SourceRange TagDecl::getSourceRange() const { |
| 2560 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2561 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2564 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2565 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2568 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| 2569 | TypedefNameDeclOrQualifier = TDD; |
Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2570 | if (TypeForDecl) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2571 | const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 2572 | ClearLinkageCache(); |
Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2575 | void TagDecl::startDefinition() { |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2576 | IsBeingDefined = true; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2577 | |
| 2578 | if (isa<CXXRecordDecl>(this)) { |
| 2579 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 2580 | struct CXXRecordDecl::DefinitionData *Data = |
| 2581 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 93cc732 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 2582 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 2583 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2584 | } |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | void TagDecl::completeDefinition() { |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2588 | assert((!isa<CXXRecordDecl>(this) || |
| 2589 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 2590 | "definition completed but not started"); |
| 2591 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2592 | IsCompleteDefinition = true; |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2593 | IsBeingDefined = false; |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 2594 | |
| 2595 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2596 | L->CompletedTagDefinition(this); |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2599 | TagDecl *TagDecl::getDefinition() const { |
| 2600 | if (isCompleteDefinition()) |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2601 | return const_cast<TagDecl *>(this); |
Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 2602 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 2603 | return CXXRD->getDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
| 2605 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2606 | R != REnd; ++R) |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2607 | if (R->isCompleteDefinition()) |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2608 | return *R; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2610 | return 0; |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2613 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 2614 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2615 | // Make sure the extended qualifier info is allocated. |
| 2616 | if (!hasExtInfo()) |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2617 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2618 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2619 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2620 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2621 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2622 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2623 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 2624 | getASTContext().Deallocate(getExtInfo()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2625 | TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2626 | } |
| 2627 | else |
| 2628 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2633 | void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 2634 | unsigned NumTPLists, |
| 2635 | TemplateParameterList **TPLists) { |
| 2636 | assert(NumTPLists > 0); |
| 2637 | // Make sure the extended decl info is allocated. |
| 2638 | if (!hasExtInfo()) |
| 2639 | // Allocate external info struct. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2640 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2641 | // Set the template parameter lists info. |
| 2642 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 2643 | } |
| 2644 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2645 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2646 | // EnumDecl Implementation |
| 2647 | //===----------------------------------------------------------------------===// |
| 2648 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2649 | void EnumDecl::anchor() { } |
| 2650 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2651 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 2652 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2653 | IdentifierInfo *Id, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2654 | EnumDecl *PrevDecl, bool IsScoped, |
| 2655 | bool IsScopedUsingClassTag, bool IsFixed) { |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2656 | EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2657 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2658 | C.getTypeDeclType(Enum, PrevDecl); |
| 2659 | return Enum; |
| 2660 | } |
| 2661 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2662 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2663 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl)); |
| 2664 | return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0, |
| 2665 | false, false, false); |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2668 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2669 | QualType NewPromotionType, |
| 2670 | unsigned NumPositiveBits, |
| 2671 | unsigned NumNegativeBits) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2672 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2673 | if (!IntegerType) |
| 2674 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2675 | PromotionType = NewPromotionType; |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2676 | setNumPositiveBits(NumPositiveBits); |
| 2677 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2678 | TagDecl::completeDefinition(); |
| 2679 | } |
| 2680 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2681 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 2682 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2683 | return MSI->getTemplateSpecializationKind(); |
| 2684 | |
| 2685 | return TSK_Undeclared; |
| 2686 | } |
| 2687 | |
| 2688 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2689 | SourceLocation PointOfInstantiation) { |
| 2690 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 2691 | assert(MSI && "Not an instantiated member enumeration?"); |
| 2692 | MSI->setTemplateSpecializationKind(TSK); |
| 2693 | if (TSK != TSK_ExplicitSpecialization && |
| 2694 | PointOfInstantiation.isValid() && |
| 2695 | MSI->getPointOfInstantiation().isInvalid()) |
| 2696 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 2697 | } |
| 2698 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2699 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 2700 | if (SpecializationInfo) |
| 2701 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 2702 | |
| 2703 | return 0; |
| 2704 | } |
| 2705 | |
| 2706 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 2707 | TemplateSpecializationKind TSK) { |
| 2708 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 2709 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 2710 | } |
| 2711 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2712 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2713 | // RecordDecl Implementation |
| 2714 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 2715 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2716 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, |
| 2717 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2718 | IdentifierInfo *Id, RecordDecl *PrevDecl) |
| 2719 | : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2720 | HasFlexibleArrayMember = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2721 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2722 | HasObjectMember = false; |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2723 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2724 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2727 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2728 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2729 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| 2730 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, |
| 2731 | PrevDecl); |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2732 | C.getTypeDeclType(R, PrevDecl); |
| 2733 | return R; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2736 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 2737 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl)); |
| 2738 | return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), |
| 2739 | SourceLocation(), 0, 0); |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2742 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2744 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 2745 | } |
| 2746 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2747 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 2748 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 2749 | LoadFieldsFromExternalStorage(); |
| 2750 | |
| 2751 | return field_iterator(decl_iterator(FirstDecl)); |
| 2752 | } |
| 2753 | |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2754 | /// completeDefinition - Notes that the definition of this type is now |
| 2755 | /// complete. |
| 2756 | void RecordDecl::completeDefinition() { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2757 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2758 | TagDecl::completeDefinition(); |
| 2759 | } |
| 2760 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2761 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 2762 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 2763 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 2764 | |
| 2765 | // Notify that we have a RecordDecl doing some initialization. |
| 2766 | ExternalASTSource::Deserializing TheFields(Source); |
| 2767 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2768 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | 3d0adb3 | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2769 | LoadedFieldsFromExternalStorage = true; |
| 2770 | switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) { |
| 2771 | case ELR_Success: |
| 2772 | break; |
| 2773 | |
| 2774 | case ELR_AlreadyLoaded: |
| 2775 | case ELR_Failure: |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2776 | return; |
Douglas Gregor | 3d0adb3 | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2777 | } |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2778 | |
| 2779 | #ifndef NDEBUG |
| 2780 | // Check that all decls we got were FieldDecls. |
| 2781 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 2782 | assert(isa<FieldDecl>(Decls[i])); |
| 2783 | #endif |
| 2784 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2785 | if (Decls.empty()) |
| 2786 | return; |
| 2787 | |
Argyrios Kyrtzidis | 094da73 | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 2788 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| 2789 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2792 | //===----------------------------------------------------------------------===// |
| 2793 | // BlockDecl Implementation |
| 2794 | //===----------------------------------------------------------------------===// |
| 2795 | |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2796 | void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2797 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2798 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2799 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2800 | if (!NewParamInfo.empty()) { |
| 2801 | NumParams = NewParamInfo.size(); |
| 2802 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 2803 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2807 | void BlockDecl::setCaptures(ASTContext &Context, |
| 2808 | const Capture *begin, |
| 2809 | const Capture *end, |
| 2810 | bool capturesCXXThis) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2811 | CapturesCXXThis = capturesCXXThis; |
| 2812 | |
| 2813 | if (begin == end) { |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2814 | NumCaptures = 0; |
| 2815 | Captures = 0; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2816 | return; |
| 2817 | } |
| 2818 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2819 | NumCaptures = end - begin; |
| 2820 | |
| 2821 | // Avoid new Capture[] because we don't want to provide a default |
| 2822 | // constructor. |
| 2823 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 2824 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 2825 | memcpy(buffer, begin, allocationSize); |
| 2826 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2827 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2828 | |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 2829 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| 2830 | for (capture_const_iterator |
| 2831 | i = capture_begin(), e = capture_end(); i != e; ++i) |
| 2832 | // Only auto vars can be captured, so no redeclaration worries. |
| 2833 | if (i->getVariable() == variable) |
| 2834 | return true; |
| 2835 | |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 2839 | SourceRange BlockDecl::getSourceRange() const { |
| 2840 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 2841 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2842 | |
| 2843 | //===----------------------------------------------------------------------===// |
| 2844 | // Other Decl Allocation/Deallocation Method Implementations |
| 2845 | //===----------------------------------------------------------------------===// |
| 2846 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2847 | void TranslationUnitDecl::anchor() { } |
| 2848 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2849 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 2850 | return new (C) TranslationUnitDecl(C); |
| 2851 | } |
| 2852 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2853 | void LabelDecl::anchor() { } |
| 2854 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2855 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 2856 | SourceLocation IdentL, IdentifierInfo *II) { |
| 2857 | return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); |
| 2858 | } |
| 2859 | |
| 2860 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 2861 | SourceLocation IdentL, IdentifierInfo *II, |
| 2862 | SourceLocation GnuLabelL) { |
| 2863 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| 2864 | return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2867 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2868 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl)); |
| 2869 | return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation()); |
Douglas Gregor | 417e87c | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2872 | void ValueDecl::anchor() { } |
| 2873 | |
| 2874 | void ImplicitParamDecl::anchor() { } |
| 2875 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2876 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2877 | SourceLocation IdLoc, |
| 2878 | IdentifierInfo *Id, |
| 2879 | QualType Type) { |
| 2880 | return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2883 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| 2884 | unsigned ID) { |
| 2885 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl)); |
| 2886 | return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType()); |
| 2887 | } |
| 2888 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2889 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2890 | SourceLocation StartLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2891 | const DeclarationNameInfo &NameInfo, |
| 2892 | QualType T, TypeSourceInfo *TInfo, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2893 | StorageClass SC, StorageClass SCAsWritten, |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2894 | bool isInlineSpecified, |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2895 | bool hasWrittenPrototype, |
| 2896 | bool isConstexprSpecified) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2897 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, |
| 2898 | T, TInfo, SC, SCAsWritten, |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2899 | isInlineSpecified, |
| 2900 | isConstexprSpecified); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2901 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 2902 | return New; |
| 2903 | } |
| 2904 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2905 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2906 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl)); |
| 2907 | return new (Mem) FunctionDecl(Function, 0, SourceLocation(), |
| 2908 | DeclarationNameInfo(), QualType(), 0, |
| 2909 | SC_None, SC_None, false, false); |
| 2910 | } |
| 2911 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2912 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 2913 | return new (C) BlockDecl(DC, L); |
| 2914 | } |
| 2915 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2916 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2917 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl)); |
| 2918 | return new (Mem) BlockDecl(0, SourceLocation()); |
| 2919 | } |
| 2920 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2921 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 2922 | SourceLocation L, |
| 2923 | IdentifierInfo *Id, QualType T, |
| 2924 | Expr *E, const llvm::APSInt &V) { |
| 2925 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 2926 | } |
| 2927 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2928 | EnumConstantDecl * |
| 2929 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2930 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl)); |
| 2931 | return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0, |
| 2932 | llvm::APSInt()); |
| 2933 | } |
| 2934 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2935 | void IndirectFieldDecl::anchor() { } |
| 2936 | |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 2937 | IndirectFieldDecl * |
| 2938 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 2939 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 2940 | unsigned CHS) { |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2941 | return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
| 2942 | } |
| 2943 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2944 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 2945 | unsigned ID) { |
| 2946 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl)); |
| 2947 | return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(), |
| 2948 | QualType(), 0, 0); |
| 2949 | } |
| 2950 | |
Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 2951 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 2952 | SourceLocation End = getLocation(); |
| 2953 | if (Init) |
| 2954 | End = Init->getLocEnd(); |
| 2955 | return SourceRange(getLocation(), End); |
| 2956 | } |
| 2957 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2958 | void TypeDecl::anchor() { } |
| 2959 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2960 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2961 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2962 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| 2963 | return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2966 | void TypedefNameDecl::anchor() { } |
| 2967 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2968 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2969 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl)); |
| 2970 | return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 2971 | } |
| 2972 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2973 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 2974 | SourceLocation StartLoc, |
| 2975 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2976 | TypeSourceInfo *TInfo) { |
| 2977 | return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); |
| 2978 | } |
| 2979 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2980 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2981 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl)); |
| 2982 | return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 2983 | } |
| 2984 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2985 | SourceRange TypedefDecl::getSourceRange() const { |
| 2986 | SourceLocation RangeEnd = getLocation(); |
| 2987 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 2988 | if (typeIsPostfix(TInfo->getType())) |
| 2989 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 2990 | } |
| 2991 | return SourceRange(getLocStart(), RangeEnd); |
| 2992 | } |
| 2993 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2994 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 2995 | SourceLocation RangeEnd = getLocStart(); |
| 2996 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 2997 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 2998 | return SourceRange(getLocStart(), RangeEnd); |
| 2999 | } |
| 3000 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3001 | void FileScopeAsmDecl::anchor() { } |
| 3002 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3003 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 3004 | StringLiteral *Str, |
| 3005 | SourceLocation AsmLoc, |
| 3006 | SourceLocation RParenLoc) { |
| 3007 | return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3008 | } |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3010 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| 3011 | unsigned ID) { |
| 3012 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl)); |
| 3013 | return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation()); |
| 3014 | } |
| 3015 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3016 | //===----------------------------------------------------------------------===// |
| 3017 | // ImportDecl Implementation |
| 3018 | //===----------------------------------------------------------------------===// |
| 3019 | |
| 3020 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 3021 | /// module. |
| 3022 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 3023 | unsigned Result = 1; |
| 3024 | while (Mod->Parent) { |
| 3025 | Mod = Mod->Parent; |
| 3026 | ++Result; |
| 3027 | } |
| 3028 | return Result; |
| 3029 | } |
| 3030 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3031 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3032 | Module *Imported, |
| 3033 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3034 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3035 | NextLocalImport() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3036 | { |
| 3037 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 3038 | SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); |
| 3039 | memcpy(StoredLocs, IdentifierLocs.data(), |
| 3040 | IdentifierLocs.size() * sizeof(SourceLocation)); |
| 3041 | } |
| 3042 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3043 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3044 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3045 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3046 | NextLocalImport() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3047 | { |
| 3048 | *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; |
| 3049 | } |
| 3050 | |
| 3051 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3052 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3053 | ArrayRef<SourceLocation> IdentifierLocs) { |
| 3054 | void *Mem = C.Allocate(sizeof(ImportDecl) + |
| 3055 | IdentifierLocs.size() * sizeof(SourceLocation)); |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3056 | return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3060 | SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3061 | Module *Imported, |
| 3062 | SourceLocation EndLoc) { |
| 3063 | void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation)); |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3064 | ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3065 | Import->setImplicit(); |
| 3066 | return Import; |
| 3067 | } |
| 3068 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3069 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 3070 | unsigned NumLocations) { |
| 3071 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 3072 | (sizeof(ImportDecl) + |
| 3073 | NumLocations * sizeof(SourceLocation))); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3074 | return new (Mem) ImportDecl(EmptyShell()); |
| 3075 | } |
| 3076 | |
| 3077 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 3078 | if (!ImportedAndComplete.getInt()) |
| 3079 | return ArrayRef<SourceLocation>(); |
| 3080 | |
| 3081 | const SourceLocation *StoredLocs |
| 3082 | = reinterpret_cast<const SourceLocation *>(this + 1); |
| 3083 | return ArrayRef<SourceLocation>(StoredLocs, |
| 3084 | getNumModuleIdentifiers(getImportedModule())); |
| 3085 | } |
| 3086 | |
| 3087 | SourceRange ImportDecl::getSourceRange() const { |
| 3088 | if (!ImportedAndComplete.getInt()) |
| 3089 | return SourceRange(getLocation(), |
| 3090 | *reinterpret_cast<const SourceLocation *>(this + 1)); |
| 3091 | |
| 3092 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 3093 | } |