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