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