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