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