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