1 1.1 joerg //===- Decl.cpp - Declaration AST Node Implementation ---------------------===// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // This file implements the Decl subclasses. 10 1.1 joerg // 11 1.1 joerg //===----------------------------------------------------------------------===// 12 1.1 joerg 13 1.1 joerg #include "clang/AST/Decl.h" 14 1.1 joerg #include "Linkage.h" 15 1.1 joerg #include "clang/AST/ASTContext.h" 16 1.1 joerg #include "clang/AST/ASTDiagnostic.h" 17 1.1 joerg #include "clang/AST/ASTLambda.h" 18 1.1 joerg #include "clang/AST/ASTMutationListener.h" 19 1.1.1.2 joerg #include "clang/AST/Attr.h" 20 1.1 joerg #include "clang/AST/CanonicalType.h" 21 1.1 joerg #include "clang/AST/DeclBase.h" 22 1.1 joerg #include "clang/AST/DeclCXX.h" 23 1.1 joerg #include "clang/AST/DeclObjC.h" 24 1.1 joerg #include "clang/AST/DeclOpenMP.h" 25 1.1 joerg #include "clang/AST/DeclTemplate.h" 26 1.1 joerg #include "clang/AST/DeclarationName.h" 27 1.1 joerg #include "clang/AST/Expr.h" 28 1.1 joerg #include "clang/AST/ExprCXX.h" 29 1.1 joerg #include "clang/AST/ExternalASTSource.h" 30 1.1 joerg #include "clang/AST/ODRHash.h" 31 1.1 joerg #include "clang/AST/PrettyDeclStackTrace.h" 32 1.1 joerg #include "clang/AST/PrettyPrinter.h" 33 1.1 joerg #include "clang/AST/Redeclarable.h" 34 1.1 joerg #include "clang/AST/Stmt.h" 35 1.1 joerg #include "clang/AST/TemplateBase.h" 36 1.1 joerg #include "clang/AST/Type.h" 37 1.1 joerg #include "clang/AST/TypeLoc.h" 38 1.1 joerg #include "clang/Basic/Builtins.h" 39 1.1 joerg #include "clang/Basic/IdentifierTable.h" 40 1.1 joerg #include "clang/Basic/LLVM.h" 41 1.1 joerg #include "clang/Basic/LangOptions.h" 42 1.1 joerg #include "clang/Basic/Linkage.h" 43 1.1 joerg #include "clang/Basic/Module.h" 44 1.1.1.2 joerg #include "clang/Basic/NoSanitizeList.h" 45 1.1 joerg #include "clang/Basic/PartialDiagnostic.h" 46 1.1 joerg #include "clang/Basic/Sanitizers.h" 47 1.1 joerg #include "clang/Basic/SourceLocation.h" 48 1.1 joerg #include "clang/Basic/SourceManager.h" 49 1.1 joerg #include "clang/Basic/Specifiers.h" 50 1.1 joerg #include "clang/Basic/TargetCXXABI.h" 51 1.1 joerg #include "clang/Basic/TargetInfo.h" 52 1.1 joerg #include "clang/Basic/Visibility.h" 53 1.1 joerg #include "llvm/ADT/APSInt.h" 54 1.1 joerg #include "llvm/ADT/ArrayRef.h" 55 1.1 joerg #include "llvm/ADT/None.h" 56 1.1 joerg #include "llvm/ADT/Optional.h" 57 1.1 joerg #include "llvm/ADT/STLExtras.h" 58 1.1 joerg #include "llvm/ADT/SmallVector.h" 59 1.1 joerg #include "llvm/ADT/StringRef.h" 60 1.1.1.2 joerg #include "llvm/ADT/StringSwitch.h" 61 1.1 joerg #include "llvm/ADT/Triple.h" 62 1.1 joerg #include "llvm/Support/Casting.h" 63 1.1 joerg #include "llvm/Support/ErrorHandling.h" 64 1.1 joerg #include "llvm/Support/raw_ostream.h" 65 1.1 joerg #include <algorithm> 66 1.1 joerg #include <cassert> 67 1.1 joerg #include <cstddef> 68 1.1 joerg #include <cstring> 69 1.1 joerg #include <memory> 70 1.1 joerg #include <string> 71 1.1 joerg #include <tuple> 72 1.1 joerg #include <type_traits> 73 1.1 joerg 74 1.1 joerg using namespace clang; 75 1.1 joerg 76 1.1 joerg Decl *clang::getPrimaryMergedDecl(Decl *D) { 77 1.1 joerg return D->getASTContext().getPrimaryMergedDecl(D); 78 1.1 joerg } 79 1.1 joerg 80 1.1 joerg void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { 81 1.1 joerg SourceLocation Loc = this->Loc; 82 1.1 joerg if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); 83 1.1 joerg if (Loc.isValid()) { 84 1.1 joerg Loc.print(OS, Context.getSourceManager()); 85 1.1 joerg OS << ": "; 86 1.1 joerg } 87 1.1 joerg OS << Message; 88 1.1 joerg 89 1.1 joerg if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) { 90 1.1 joerg OS << " '"; 91 1.1 joerg ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); 92 1.1 joerg OS << "'"; 93 1.1 joerg } 94 1.1 joerg 95 1.1 joerg OS << '\n'; 96 1.1 joerg } 97 1.1 joerg 98 1.1 joerg // Defined here so that it can be inlined into its direct callers. 99 1.1 joerg bool Decl::isOutOfLine() const { 100 1.1 joerg return !getLexicalDeclContext()->Equals(getDeclContext()); 101 1.1 joerg } 102 1.1 joerg 103 1.1 joerg TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) 104 1.1 joerg : Decl(TranslationUnit, nullptr, SourceLocation()), 105 1.1 joerg DeclContext(TranslationUnit), Ctx(ctx) {} 106 1.1 joerg 107 1.1 joerg //===----------------------------------------------------------------------===// 108 1.1 joerg // NamedDecl Implementation 109 1.1 joerg //===----------------------------------------------------------------------===// 110 1.1 joerg 111 1.1 joerg // Visibility rules aren't rigorously externally specified, but here 112 1.1 joerg // are the basic principles behind what we implement: 113 1.1 joerg // 114 1.1 joerg // 1. An explicit visibility attribute is generally a direct expression 115 1.1 joerg // of the user's intent and should be honored. Only the innermost 116 1.1 joerg // visibility attribute applies. If no visibility attribute applies, 117 1.1 joerg // global visibility settings are considered. 118 1.1 joerg // 119 1.1 joerg // 2. There is one caveat to the above: on or in a template pattern, 120 1.1 joerg // an explicit visibility attribute is just a default rule, and 121 1.1 joerg // visibility can be decreased by the visibility of template 122 1.1 joerg // arguments. But this, too, has an exception: an attribute on an 123 1.1 joerg // explicit specialization or instantiation causes all the visibility 124 1.1 joerg // restrictions of the template arguments to be ignored. 125 1.1 joerg // 126 1.1 joerg // 3. A variable that does not otherwise have explicit visibility can 127 1.1 joerg // be restricted by the visibility of its type. 128 1.1 joerg // 129 1.1 joerg // 4. A visibility restriction is explicit if it comes from an 130 1.1 joerg // attribute (or something like it), not a global visibility setting. 131 1.1 joerg // When emitting a reference to an external symbol, visibility 132 1.1 joerg // restrictions are ignored unless they are explicit. 133 1.1 joerg // 134 1.1 joerg // 5. When computing the visibility of a non-type, including a 135 1.1 joerg // non-type member of a class, only non-type visibility restrictions 136 1.1 joerg // are considered: the 'visibility' attribute, global value-visibility 137 1.1 joerg // settings, and a few special cases like __private_extern. 138 1.1 joerg // 139 1.1 joerg // 6. When computing the visibility of a type, including a type member 140 1.1 joerg // of a class, only type visibility restrictions are considered: 141 1.1 joerg // the 'type_visibility' attribute and global type-visibility settings. 142 1.1 joerg // However, a 'visibility' attribute counts as a 'type_visibility' 143 1.1 joerg // attribute on any declaration that only has the former. 144 1.1 joerg // 145 1.1 joerg // The visibility of a "secondary" entity, like a template argument, 146 1.1 joerg // is computed using the kind of that entity, not the kind of the 147 1.1 joerg // primary entity for which we are computing visibility. For example, 148 1.1 joerg // the visibility of a specialization of either of these templates: 149 1.1 joerg // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); 150 1.1 joerg // template <class T, bool (&compare)(T, X)> class matcher; 151 1.1 joerg // is restricted according to the type visibility of the argument 'T', 152 1.1 joerg // the type visibility of 'bool(&)(T,X)', and the value visibility of 153 1.1 joerg // the argument function 'compare'. That 'has_match' is a value 154 1.1 joerg // and 'matcher' is a type only matters when looking for attributes 155 1.1 joerg // and settings from the immediate context. 156 1.1 joerg 157 1.1 joerg /// Does this computation kind permit us to consider additional 158 1.1 joerg /// visibility settings from attributes and the like? 159 1.1 joerg static bool hasExplicitVisibilityAlready(LVComputationKind computation) { 160 1.1 joerg return computation.IgnoreExplicitVisibility; 161 1.1 joerg } 162 1.1 joerg 163 1.1 joerg /// Given an LVComputationKind, return one of the same type/value sort 164 1.1 joerg /// that records that it already has explicit visibility. 165 1.1 joerg static LVComputationKind 166 1.1 joerg withExplicitVisibilityAlready(LVComputationKind Kind) { 167 1.1 joerg Kind.IgnoreExplicitVisibility = true; 168 1.1 joerg return Kind; 169 1.1 joerg } 170 1.1 joerg 171 1.1 joerg static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, 172 1.1 joerg LVComputationKind kind) { 173 1.1 joerg assert(!kind.IgnoreExplicitVisibility && 174 1.1 joerg "asking for explicit visibility when we shouldn't be"); 175 1.1 joerg return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); 176 1.1 joerg } 177 1.1 joerg 178 1.1 joerg /// Is the given declaration a "type" or a "value" for the purposes of 179 1.1 joerg /// visibility computation? 180 1.1 joerg static bool usesTypeVisibility(const NamedDecl *D) { 181 1.1 joerg return isa<TypeDecl>(D) || 182 1.1 joerg isa<ClassTemplateDecl>(D) || 183 1.1 joerg isa<ObjCInterfaceDecl>(D); 184 1.1 joerg } 185 1.1 joerg 186 1.1 joerg /// Does the given declaration have member specialization information, 187 1.1 joerg /// and if so, is it an explicit specialization? 188 1.1 joerg template <class T> static typename 189 1.1 joerg std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type 190 1.1 joerg isExplicitMemberSpecialization(const T *D) { 191 1.1 joerg if (const MemberSpecializationInfo *member = 192 1.1 joerg D->getMemberSpecializationInfo()) { 193 1.1 joerg return member->isExplicitSpecialization(); 194 1.1 joerg } 195 1.1 joerg return false; 196 1.1 joerg } 197 1.1 joerg 198 1.1 joerg /// For templates, this question is easier: a member template can't be 199 1.1 joerg /// explicitly instantiated, so there's a single bit indicating whether 200 1.1 joerg /// or not this is an explicit member specialization. 201 1.1 joerg static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { 202 1.1 joerg return D->isMemberSpecialization(); 203 1.1 joerg } 204 1.1 joerg 205 1.1 joerg /// Given a visibility attribute, return the explicit visibility 206 1.1 joerg /// associated with it. 207 1.1 joerg template <class T> 208 1.1 joerg static Visibility getVisibilityFromAttr(const T *attr) { 209 1.1 joerg switch (attr->getVisibility()) { 210 1.1 joerg case T::Default: 211 1.1 joerg return DefaultVisibility; 212 1.1 joerg case T::Hidden: 213 1.1 joerg return HiddenVisibility; 214 1.1 joerg case T::Protected: 215 1.1 joerg return ProtectedVisibility; 216 1.1 joerg } 217 1.1 joerg llvm_unreachable("bad visibility kind"); 218 1.1 joerg } 219 1.1 joerg 220 1.1 joerg /// Return the explicit visibility of the given declaration. 221 1.1 joerg static Optional<Visibility> getVisibilityOf(const NamedDecl *D, 222 1.1 joerg NamedDecl::ExplicitVisibilityKind kind) { 223 1.1 joerg // If we're ultimately computing the visibility of a type, look for 224 1.1 joerg // a 'type_visibility' attribute before looking for 'visibility'. 225 1.1 joerg if (kind == NamedDecl::VisibilityForType) { 226 1.1 joerg if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { 227 1.1 joerg return getVisibilityFromAttr(A); 228 1.1 joerg } 229 1.1 joerg } 230 1.1 joerg 231 1.1 joerg // If this declaration has an explicit visibility attribute, use it. 232 1.1 joerg if (const auto *A = D->getAttr<VisibilityAttr>()) { 233 1.1 joerg return getVisibilityFromAttr(A); 234 1.1 joerg } 235 1.1 joerg 236 1.1 joerg return None; 237 1.1 joerg } 238 1.1 joerg 239 1.1 joerg LinkageInfo LinkageComputer::getLVForType(const Type &T, 240 1.1 joerg LVComputationKind computation) { 241 1.1 joerg if (computation.IgnoreAllVisibility) 242 1.1 joerg return LinkageInfo(T.getLinkage(), DefaultVisibility, true); 243 1.1 joerg return getTypeLinkageAndVisibility(&T); 244 1.1 joerg } 245 1.1 joerg 246 1.1 joerg /// Get the most restrictive linkage for the types in the given 247 1.1 joerg /// template parameter list. For visibility purposes, template 248 1.1 joerg /// parameters are part of the signature of a template. 249 1.1 joerg LinkageInfo LinkageComputer::getLVForTemplateParameterList( 250 1.1 joerg const TemplateParameterList *Params, LVComputationKind computation) { 251 1.1 joerg LinkageInfo LV; 252 1.1 joerg for (const NamedDecl *P : *Params) { 253 1.1 joerg // Template type parameters are the most common and never 254 1.1 joerg // contribute to visibility, pack or not. 255 1.1 joerg if (isa<TemplateTypeParmDecl>(P)) 256 1.1 joerg continue; 257 1.1 joerg 258 1.1 joerg // Non-type template parameters can be restricted by the value type, e.g. 259 1.1 joerg // template <enum X> class A { ... }; 260 1.1 joerg // We have to be careful here, though, because we can be dealing with 261 1.1 joerg // dependent types. 262 1.1 joerg if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { 263 1.1 joerg // Handle the non-pack case first. 264 1.1 joerg if (!NTTP->isExpandedParameterPack()) { 265 1.1 joerg if (!NTTP->getType()->isDependentType()) { 266 1.1 joerg LV.merge(getLVForType(*NTTP->getType(), computation)); 267 1.1 joerg } 268 1.1 joerg continue; 269 1.1 joerg } 270 1.1 joerg 271 1.1 joerg // Look at all the types in an expanded pack. 272 1.1 joerg for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { 273 1.1 joerg QualType type = NTTP->getExpansionType(i); 274 1.1 joerg if (!type->isDependentType()) 275 1.1 joerg LV.merge(getTypeLinkageAndVisibility(type)); 276 1.1 joerg } 277 1.1 joerg continue; 278 1.1 joerg } 279 1.1 joerg 280 1.1 joerg // Template template parameters can be restricted by their 281 1.1 joerg // template parameters, recursively. 282 1.1 joerg const auto *TTP = cast<TemplateTemplateParmDecl>(P); 283 1.1 joerg 284 1.1 joerg // Handle the non-pack case first. 285 1.1 joerg if (!TTP->isExpandedParameterPack()) { 286 1.1 joerg LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), 287 1.1 joerg computation)); 288 1.1 joerg continue; 289 1.1 joerg } 290 1.1 joerg 291 1.1 joerg // Look at all expansions in an expanded pack. 292 1.1 joerg for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); 293 1.1 joerg i != n; ++i) { 294 1.1 joerg LV.merge(getLVForTemplateParameterList( 295 1.1 joerg TTP->getExpansionTemplateParameters(i), computation)); 296 1.1 joerg } 297 1.1 joerg } 298 1.1 joerg 299 1.1 joerg return LV; 300 1.1 joerg } 301 1.1 joerg 302 1.1 joerg static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { 303 1.1 joerg const Decl *Ret = nullptr; 304 1.1 joerg const DeclContext *DC = D->getDeclContext(); 305 1.1 joerg while (DC->getDeclKind() != Decl::TranslationUnit) { 306 1.1 joerg if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) 307 1.1 joerg Ret = cast<Decl>(DC); 308 1.1 joerg DC = DC->getParent(); 309 1.1 joerg } 310 1.1 joerg return Ret; 311 1.1 joerg } 312 1.1 joerg 313 1.1 joerg /// Get the most restrictive linkage for the types and 314 1.1 joerg /// declarations in the given template argument list. 315 1.1 joerg /// 316 1.1 joerg /// Note that we don't take an LVComputationKind because we always 317 1.1 joerg /// want to honor the visibility of template arguments in the same way. 318 1.1 joerg LinkageInfo 319 1.1 joerg LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, 320 1.1 joerg LVComputationKind computation) { 321 1.1 joerg LinkageInfo LV; 322 1.1 joerg 323 1.1 joerg for (const TemplateArgument &Arg : Args) { 324 1.1 joerg switch (Arg.getKind()) { 325 1.1 joerg case TemplateArgument::Null: 326 1.1 joerg case TemplateArgument::Integral: 327 1.1 joerg case TemplateArgument::Expression: 328 1.1 joerg continue; 329 1.1 joerg 330 1.1 joerg case TemplateArgument::Type: 331 1.1 joerg LV.merge(getLVForType(*Arg.getAsType(), computation)); 332 1.1 joerg continue; 333 1.1 joerg 334 1.1 joerg case TemplateArgument::Declaration: { 335 1.1 joerg const NamedDecl *ND = Arg.getAsDecl(); 336 1.1 joerg assert(!usesTypeVisibility(ND)); 337 1.1 joerg LV.merge(getLVForDecl(ND, computation)); 338 1.1 joerg continue; 339 1.1 joerg } 340 1.1 joerg 341 1.1 joerg case TemplateArgument::NullPtr: 342 1.1 joerg LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); 343 1.1 joerg continue; 344 1.1 joerg 345 1.1 joerg case TemplateArgument::Template: 346 1.1 joerg case TemplateArgument::TemplateExpansion: 347 1.1 joerg if (TemplateDecl *Template = 348 1.1 joerg Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) 349 1.1 joerg LV.merge(getLVForDecl(Template, computation)); 350 1.1 joerg continue; 351 1.1 joerg 352 1.1 joerg case TemplateArgument::Pack: 353 1.1 joerg LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); 354 1.1 joerg continue; 355 1.1 joerg } 356 1.1 joerg llvm_unreachable("bad template argument kind"); 357 1.1 joerg } 358 1.1 joerg 359 1.1 joerg return LV; 360 1.1 joerg } 361 1.1 joerg 362 1.1 joerg LinkageInfo 363 1.1 joerg LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, 364 1.1 joerg LVComputationKind computation) { 365 1.1 joerg return getLVForTemplateArgumentList(TArgs.asArray(), computation); 366 1.1 joerg } 367 1.1 joerg 368 1.1 joerg static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, 369 1.1 joerg const FunctionTemplateSpecializationInfo *specInfo) { 370 1.1 joerg // Include visibility from the template parameters and arguments 371 1.1 joerg // only if this is not an explicit instantiation or specialization 372 1.1 joerg // with direct explicit visibility. (Implicit instantiations won't 373 1.1 joerg // have a direct attribute.) 374 1.1 joerg if (!specInfo->isExplicitInstantiationOrSpecialization()) 375 1.1 joerg return true; 376 1.1 joerg 377 1.1 joerg return !fn->hasAttr<VisibilityAttr>(); 378 1.1 joerg } 379 1.1 joerg 380 1.1 joerg /// Merge in template-related linkage and visibility for the given 381 1.1 joerg /// function template specialization. 382 1.1 joerg /// 383 1.1 joerg /// We don't need a computation kind here because we can assume 384 1.1 joerg /// LVForValue. 385 1.1 joerg /// 386 1.1 joerg /// \param[out] LV the computation to use for the parent 387 1.1 joerg void LinkageComputer::mergeTemplateLV( 388 1.1 joerg LinkageInfo &LV, const FunctionDecl *fn, 389 1.1 joerg const FunctionTemplateSpecializationInfo *specInfo, 390 1.1 joerg LVComputationKind computation) { 391 1.1 joerg bool considerVisibility = 392 1.1 joerg shouldConsiderTemplateVisibility(fn, specInfo); 393 1.1 joerg 394 1.1 joerg // Merge information from the template parameters. 395 1.1 joerg FunctionTemplateDecl *temp = specInfo->getTemplate(); 396 1.1 joerg LinkageInfo tempLV = 397 1.1 joerg getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 398 1.1 joerg LV.mergeMaybeWithVisibility(tempLV, considerVisibility); 399 1.1 joerg 400 1.1 joerg // Merge information from the template arguments. 401 1.1 joerg const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; 402 1.1 joerg LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 403 1.1 joerg LV.mergeMaybeWithVisibility(argsLV, considerVisibility); 404 1.1 joerg } 405 1.1 joerg 406 1.1 joerg /// Does the given declaration have a direct visibility attribute 407 1.1 joerg /// that would match the given rules? 408 1.1 joerg static bool hasDirectVisibilityAttribute(const NamedDecl *D, 409 1.1 joerg LVComputationKind computation) { 410 1.1 joerg if (computation.IgnoreAllVisibility) 411 1.1 joerg return false; 412 1.1 joerg 413 1.1 joerg return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || 414 1.1 joerg D->hasAttr<VisibilityAttr>(); 415 1.1 joerg } 416 1.1 joerg 417 1.1 joerg /// Should we consider visibility associated with the template 418 1.1 joerg /// arguments and parameters of the given class template specialization? 419 1.1 joerg static bool shouldConsiderTemplateVisibility( 420 1.1 joerg const ClassTemplateSpecializationDecl *spec, 421 1.1 joerg LVComputationKind computation) { 422 1.1 joerg // Include visibility from the template parameters and arguments 423 1.1 joerg // only if this is not an explicit instantiation or specialization 424 1.1 joerg // with direct explicit visibility (and note that implicit 425 1.1 joerg // instantiations won't have a direct attribute). 426 1.1 joerg // 427 1.1 joerg // Furthermore, we want to ignore template parameters and arguments 428 1.1 joerg // for an explicit specialization when computing the visibility of a 429 1.1 joerg // member thereof with explicit visibility. 430 1.1 joerg // 431 1.1 joerg // This is a bit complex; let's unpack it. 432 1.1 joerg // 433 1.1 joerg // An explicit class specialization is an independent, top-level 434 1.1 joerg // declaration. As such, if it or any of its members has an 435 1.1 joerg // explicit visibility attribute, that must directly express the 436 1.1 joerg // user's intent, and we should honor it. The same logic applies to 437 1.1 joerg // an explicit instantiation of a member of such a thing. 438 1.1 joerg 439 1.1 joerg // Fast path: if this is not an explicit instantiation or 440 1.1 joerg // specialization, we always want to consider template-related 441 1.1 joerg // visibility restrictions. 442 1.1 joerg if (!spec->isExplicitInstantiationOrSpecialization()) 443 1.1 joerg return true; 444 1.1 joerg 445 1.1 joerg // This is the 'member thereof' check. 446 1.1 joerg if (spec->isExplicitSpecialization() && 447 1.1 joerg hasExplicitVisibilityAlready(computation)) 448 1.1 joerg return false; 449 1.1 joerg 450 1.1 joerg return !hasDirectVisibilityAttribute(spec, computation); 451 1.1 joerg } 452 1.1 joerg 453 1.1 joerg /// Merge in template-related linkage and visibility for the given 454 1.1 joerg /// class template specialization. 455 1.1 joerg void LinkageComputer::mergeTemplateLV( 456 1.1 joerg LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, 457 1.1 joerg LVComputationKind computation) { 458 1.1 joerg bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); 459 1.1 joerg 460 1.1 joerg // Merge information from the template parameters, but ignore 461 1.1 joerg // visibility if we're only considering template arguments. 462 1.1 joerg 463 1.1 joerg ClassTemplateDecl *temp = spec->getSpecializedTemplate(); 464 1.1 joerg LinkageInfo tempLV = 465 1.1 joerg getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 466 1.1 joerg LV.mergeMaybeWithVisibility(tempLV, 467 1.1 joerg considerVisibility && !hasExplicitVisibilityAlready(computation)); 468 1.1 joerg 469 1.1 joerg // Merge information from the template arguments. We ignore 470 1.1 joerg // template-argument visibility if we've got an explicit 471 1.1 joerg // instantiation with a visibility attribute. 472 1.1 joerg const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); 473 1.1 joerg LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 474 1.1 joerg if (considerVisibility) 475 1.1 joerg LV.mergeVisibility(argsLV); 476 1.1 joerg LV.mergeExternalVisibility(argsLV); 477 1.1 joerg } 478 1.1 joerg 479 1.1 joerg /// Should we consider visibility associated with the template 480 1.1 joerg /// arguments and parameters of the given variable template 481 1.1 joerg /// specialization? As usual, follow class template specialization 482 1.1 joerg /// logic up to initialization. 483 1.1 joerg static bool shouldConsiderTemplateVisibility( 484 1.1 joerg const VarTemplateSpecializationDecl *spec, 485 1.1 joerg LVComputationKind computation) { 486 1.1 joerg // Include visibility from the template parameters and arguments 487 1.1 joerg // only if this is not an explicit instantiation or specialization 488 1.1 joerg // with direct explicit visibility (and note that implicit 489 1.1 joerg // instantiations won't have a direct attribute). 490 1.1 joerg if (!spec->isExplicitInstantiationOrSpecialization()) 491 1.1 joerg return true; 492 1.1 joerg 493 1.1 joerg // An explicit variable specialization is an independent, top-level 494 1.1 joerg // declaration. As such, if it has an explicit visibility attribute, 495 1.1 joerg // that must directly express the user's intent, and we should honor 496 1.1 joerg // it. 497 1.1 joerg if (spec->isExplicitSpecialization() && 498 1.1 joerg hasExplicitVisibilityAlready(computation)) 499 1.1 joerg return false; 500 1.1 joerg 501 1.1 joerg return !hasDirectVisibilityAttribute(spec, computation); 502 1.1 joerg } 503 1.1 joerg 504 1.1 joerg /// Merge in template-related linkage and visibility for the given 505 1.1 joerg /// variable template specialization. As usual, follow class template 506 1.1 joerg /// specialization logic up to initialization. 507 1.1 joerg void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, 508 1.1 joerg const VarTemplateSpecializationDecl *spec, 509 1.1 joerg LVComputationKind computation) { 510 1.1 joerg bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); 511 1.1 joerg 512 1.1 joerg // Merge information from the template parameters, but ignore 513 1.1 joerg // visibility if we're only considering template arguments. 514 1.1 joerg 515 1.1 joerg VarTemplateDecl *temp = spec->getSpecializedTemplate(); 516 1.1 joerg LinkageInfo tempLV = 517 1.1 joerg getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 518 1.1 joerg LV.mergeMaybeWithVisibility(tempLV, 519 1.1 joerg considerVisibility && !hasExplicitVisibilityAlready(computation)); 520 1.1 joerg 521 1.1 joerg // Merge information from the template arguments. We ignore 522 1.1 joerg // template-argument visibility if we've got an explicit 523 1.1 joerg // instantiation with a visibility attribute. 524 1.1 joerg const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); 525 1.1 joerg LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 526 1.1 joerg if (considerVisibility) 527 1.1 joerg LV.mergeVisibility(argsLV); 528 1.1 joerg LV.mergeExternalVisibility(argsLV); 529 1.1 joerg } 530 1.1 joerg 531 1.1 joerg static bool useInlineVisibilityHidden(const NamedDecl *D) { 532 1.1 joerg // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. 533 1.1 joerg const LangOptions &Opts = D->getASTContext().getLangOpts(); 534 1.1 joerg if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) 535 1.1 joerg return false; 536 1.1 joerg 537 1.1 joerg const auto *FD = dyn_cast<FunctionDecl>(D); 538 1.1 joerg if (!FD) 539 1.1 joerg return false; 540 1.1 joerg 541 1.1 joerg TemplateSpecializationKind TSK = TSK_Undeclared; 542 1.1 joerg if (FunctionTemplateSpecializationInfo *spec 543 1.1 joerg = FD->getTemplateSpecializationInfo()) { 544 1.1 joerg TSK = spec->getTemplateSpecializationKind(); 545 1.1 joerg } else if (MemberSpecializationInfo *MSI = 546 1.1 joerg FD->getMemberSpecializationInfo()) { 547 1.1 joerg TSK = MSI->getTemplateSpecializationKind(); 548 1.1 joerg } 549 1.1 joerg 550 1.1 joerg const FunctionDecl *Def = nullptr; 551 1.1 joerg // InlineVisibilityHidden only applies to definitions, and 552 1.1 joerg // isInlined() only gives meaningful answers on definitions 553 1.1 joerg // anyway. 554 1.1 joerg return TSK != TSK_ExplicitInstantiationDeclaration && 555 1.1 joerg TSK != TSK_ExplicitInstantiationDefinition && 556 1.1 joerg FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); 557 1.1 joerg } 558 1.1 joerg 559 1.1 joerg template <typename T> static bool isFirstInExternCContext(T *D) { 560 1.1 joerg const T *First = D->getFirstDecl(); 561 1.1 joerg return First->isInExternCContext(); 562 1.1 joerg } 563 1.1 joerg 564 1.1 joerg static bool isSingleLineLanguageLinkage(const Decl &D) { 565 1.1 joerg if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) 566 1.1 joerg if (!SD->hasBraces()) 567 1.1 joerg return true; 568 1.1 joerg return false; 569 1.1 joerg } 570 1.1 joerg 571 1.1 joerg /// Determine whether D is declared in the purview of a named module. 572 1.1 joerg static bool isInModulePurview(const NamedDecl *D) { 573 1.1 joerg if (auto *M = D->getOwningModule()) 574 1.1 joerg return M->isModulePurview(); 575 1.1 joerg return false; 576 1.1 joerg } 577 1.1 joerg 578 1.1 joerg static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { 579 1.1 joerg // FIXME: Handle isModulePrivate. 580 1.1 joerg switch (D->getModuleOwnershipKind()) { 581 1.1 joerg case Decl::ModuleOwnershipKind::Unowned: 582 1.1 joerg case Decl::ModuleOwnershipKind::ModulePrivate: 583 1.1 joerg return false; 584 1.1 joerg case Decl::ModuleOwnershipKind::Visible: 585 1.1 joerg case Decl::ModuleOwnershipKind::VisibleWhenImported: 586 1.1 joerg return isInModulePurview(D); 587 1.1 joerg } 588 1.1 joerg llvm_unreachable("unexpected module ownership kind"); 589 1.1 joerg } 590 1.1 joerg 591 1.1 joerg static LinkageInfo getInternalLinkageFor(const NamedDecl *D) { 592 1.1 joerg // Internal linkage declarations within a module interface unit are modeled 593 1.1 joerg // as "module-internal linkage", which means that they have internal linkage 594 1.1 joerg // formally but can be indirectly accessed from outside the module via inline 595 1.1 joerg // functions and templates defined within the module. 596 1.1 joerg if (isInModulePurview(D)) 597 1.1 joerg return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false); 598 1.1 joerg 599 1.1 joerg return LinkageInfo::internal(); 600 1.1 joerg } 601 1.1 joerg 602 1.1 joerg static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { 603 1.1 joerg // C++ Modules TS [basic.link]/6.8: 604 1.1 joerg // - A name declared at namespace scope that does not have internal linkage 605 1.1 joerg // by the previous rules and that is introduced by a non-exported 606 1.1 joerg // declaration has module linkage. 607 1.1 joerg if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit( 608 1.1 joerg cast<NamedDecl>(D->getCanonicalDecl()))) 609 1.1 joerg return LinkageInfo(ModuleLinkage, DefaultVisibility, false); 610 1.1 joerg 611 1.1 joerg return LinkageInfo::external(); 612 1.1 joerg } 613 1.1 joerg 614 1.1 joerg static StorageClass getStorageClass(const Decl *D) { 615 1.1 joerg if (auto *TD = dyn_cast<TemplateDecl>(D)) 616 1.1 joerg D = TD->getTemplatedDecl(); 617 1.1 joerg if (D) { 618 1.1 joerg if (auto *VD = dyn_cast<VarDecl>(D)) 619 1.1 joerg return VD->getStorageClass(); 620 1.1 joerg if (auto *FD = dyn_cast<FunctionDecl>(D)) 621 1.1 joerg return FD->getStorageClass(); 622 1.1 joerg } 623 1.1 joerg return SC_None; 624 1.1 joerg } 625 1.1 joerg 626 1.1 joerg LinkageInfo 627 1.1 joerg LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, 628 1.1 joerg LVComputationKind computation, 629 1.1 joerg bool IgnoreVarTypeLinkage) { 630 1.1 joerg assert(D->getDeclContext()->getRedeclContext()->isFileContext() && 631 1.1 joerg "Not a name having namespace scope"); 632 1.1 joerg ASTContext &Context = D->getASTContext(); 633 1.1 joerg 634 1.1 joerg // C++ [basic.link]p3: 635 1.1 joerg // A name having namespace scope (3.3.6) has internal linkage if it 636 1.1 joerg // is the name of 637 1.1 joerg 638 1.1 joerg if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { 639 1.1 joerg // - a variable, variable template, function, or function template 640 1.1 joerg // that is explicitly declared static; or 641 1.1 joerg // (This bullet corresponds to C99 6.2.2p3.) 642 1.1 joerg return getInternalLinkageFor(D); 643 1.1 joerg } 644 1.1 joerg 645 1.1 joerg if (const auto *Var = dyn_cast<VarDecl>(D)) { 646 1.1 joerg // - a non-template variable of non-volatile const-qualified type, unless 647 1.1 joerg // - it is explicitly declared extern, or 648 1.1 joerg // - it is inline or exported, or 649 1.1 joerg // - it was previously declared and the prior declaration did not have 650 1.1 joerg // internal linkage 651 1.1 joerg // (There is no equivalent in C99.) 652 1.1 joerg if (Context.getLangOpts().CPlusPlus && 653 1.1 joerg Var->getType().isConstQualified() && 654 1.1 joerg !Var->getType().isVolatileQualified() && 655 1.1 joerg !Var->isInline() && 656 1.1 joerg !isExportedFromModuleInterfaceUnit(Var) && 657 1.1 joerg !isa<VarTemplateSpecializationDecl>(Var) && 658 1.1 joerg !Var->getDescribedVarTemplate()) { 659 1.1 joerg const VarDecl *PrevVar = Var->getPreviousDecl(); 660 1.1 joerg if (PrevVar) 661 1.1 joerg return getLVForDecl(PrevVar, computation); 662 1.1 joerg 663 1.1 joerg if (Var->getStorageClass() != SC_Extern && 664 1.1 joerg Var->getStorageClass() != SC_PrivateExtern && 665 1.1 joerg !isSingleLineLanguageLinkage(*Var)) 666 1.1 joerg return getInternalLinkageFor(Var); 667 1.1 joerg } 668 1.1 joerg 669 1.1 joerg for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; 670 1.1 joerg PrevVar = PrevVar->getPreviousDecl()) { 671 1.1 joerg if (PrevVar->getStorageClass() == SC_PrivateExtern && 672 1.1 joerg Var->getStorageClass() == SC_None) 673 1.1 joerg return getDeclLinkageAndVisibility(PrevVar); 674 1.1 joerg // Explicitly declared static. 675 1.1 joerg if (PrevVar->getStorageClass() == SC_Static) 676 1.1 joerg return getInternalLinkageFor(Var); 677 1.1 joerg } 678 1.1 joerg } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { 679 1.1 joerg // - a data member of an anonymous union. 680 1.1 joerg const VarDecl *VD = IFD->getVarDecl(); 681 1.1 joerg assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); 682 1.1 joerg return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); 683 1.1 joerg } 684 1.1 joerg assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); 685 1.1 joerg 686 1.1 joerg // FIXME: This gives internal linkage to names that should have no linkage 687 1.1 joerg // (those not covered by [basic.link]p6). 688 1.1 joerg if (D->isInAnonymousNamespace()) { 689 1.1 joerg const auto *Var = dyn_cast<VarDecl>(D); 690 1.1 joerg const auto *Func = dyn_cast<FunctionDecl>(D); 691 1.1 joerg // FIXME: The check for extern "C" here is not justified by the standard 692 1.1 joerg // wording, but we retain it from the pre-DR1113 model to avoid breaking 693 1.1 joerg // code. 694 1.1 joerg // 695 1.1 joerg // C++11 [basic.link]p4: 696 1.1 joerg // An unnamed namespace or a namespace declared directly or indirectly 697 1.1 joerg // within an unnamed namespace has internal linkage. 698 1.1 joerg if ((!Var || !isFirstInExternCContext(Var)) && 699 1.1 joerg (!Func || !isFirstInExternCContext(Func))) 700 1.1 joerg return getInternalLinkageFor(D); 701 1.1 joerg } 702 1.1 joerg 703 1.1 joerg // Set up the defaults. 704 1.1 joerg 705 1.1 joerg // C99 6.2.2p5: 706 1.1 joerg // If the declaration of an identifier for an object has file 707 1.1 joerg // scope and no storage-class specifier, its linkage is 708 1.1 joerg // external. 709 1.1 joerg LinkageInfo LV = getExternalLinkageFor(D); 710 1.1 joerg 711 1.1 joerg if (!hasExplicitVisibilityAlready(computation)) { 712 1.1 joerg if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { 713 1.1 joerg LV.mergeVisibility(*Vis, true); 714 1.1 joerg } else { 715 1.1 joerg // If we're declared in a namespace with a visibility attribute, 716 1.1 joerg // use that namespace's visibility, and it still counts as explicit. 717 1.1 joerg for (const DeclContext *DC = D->getDeclContext(); 718 1.1 joerg !isa<TranslationUnitDecl>(DC); 719 1.1 joerg DC = DC->getParent()) { 720 1.1 joerg const auto *ND = dyn_cast<NamespaceDecl>(DC); 721 1.1 joerg if (!ND) continue; 722 1.1 joerg if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { 723 1.1 joerg LV.mergeVisibility(*Vis, true); 724 1.1 joerg break; 725 1.1 joerg } 726 1.1 joerg } 727 1.1 joerg } 728 1.1 joerg 729 1.1 joerg // Add in global settings if the above didn't give us direct visibility. 730 1.1 joerg if (!LV.isVisibilityExplicit()) { 731 1.1 joerg // Use global type/value visibility as appropriate. 732 1.1 joerg Visibility globalVisibility = 733 1.1 joerg computation.isValueVisibility() 734 1.1 joerg ? Context.getLangOpts().getValueVisibilityMode() 735 1.1 joerg : Context.getLangOpts().getTypeVisibilityMode(); 736 1.1 joerg LV.mergeVisibility(globalVisibility, /*explicit*/ false); 737 1.1 joerg 738 1.1 joerg // If we're paying attention to global visibility, apply 739 1.1 joerg // -finline-visibility-hidden if this is an inline method. 740 1.1 joerg if (useInlineVisibilityHidden(D)) 741 1.1 joerg LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); 742 1.1 joerg } 743 1.1 joerg } 744 1.1 joerg 745 1.1 joerg // C++ [basic.link]p4: 746 1.1 joerg 747 1.1 joerg // A name having namespace scope that has not been given internal linkage 748 1.1 joerg // above and that is the name of 749 1.1 joerg // [...bullets...] 750 1.1 joerg // has its linkage determined as follows: 751 1.1 joerg // - if the enclosing namespace has internal linkage, the name has 752 1.1 joerg // internal linkage; [handled above] 753 1.1 joerg // - otherwise, if the declaration of the name is attached to a named 754 1.1 joerg // module and is not exported, the name has module linkage; 755 1.1 joerg // - otherwise, the name has external linkage. 756 1.1 joerg // LV is currently set up to handle the last two bullets. 757 1.1 joerg // 758 1.1 joerg // The bullets are: 759 1.1 joerg 760 1.1 joerg // - a variable; or 761 1.1 joerg if (const auto *Var = dyn_cast<VarDecl>(D)) { 762 1.1 joerg // GCC applies the following optimization to variables and static 763 1.1 joerg // data members, but not to functions: 764 1.1 joerg // 765 1.1 joerg // Modify the variable's LV by the LV of its type unless this is 766 1.1 joerg // C or extern "C". This follows from [basic.link]p9: 767 1.1 joerg // A type without linkage shall not be used as the type of a 768 1.1 joerg // variable or function with external linkage unless 769 1.1 joerg // - the entity has C language linkage, or 770 1.1 joerg // - the entity is declared within an unnamed namespace, or 771 1.1 joerg // - the entity is not used or is defined in the same 772 1.1 joerg // translation unit. 773 1.1 joerg // and [basic.link]p10: 774 1.1 joerg // ...the types specified by all declarations referring to a 775 1.1 joerg // given variable or function shall be identical... 776 1.1 joerg // C does not have an equivalent rule. 777 1.1 joerg // 778 1.1 joerg // Ignore this if we've got an explicit attribute; the user 779 1.1 joerg // probably knows what they're doing. 780 1.1 joerg // 781 1.1 joerg // Note that we don't want to make the variable non-external 782 1.1 joerg // because of this, but unique-external linkage suits us. 783 1.1 joerg if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && 784 1.1 joerg !IgnoreVarTypeLinkage) { 785 1.1 joerg LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); 786 1.1 joerg if (!isExternallyVisible(TypeLV.getLinkage())) 787 1.1 joerg return LinkageInfo::uniqueExternal(); 788 1.1 joerg if (!LV.isVisibilityExplicit()) 789 1.1 joerg LV.mergeVisibility(TypeLV); 790 1.1 joerg } 791 1.1 joerg 792 1.1 joerg if (Var->getStorageClass() == SC_PrivateExtern) 793 1.1 joerg LV.mergeVisibility(HiddenVisibility, true); 794 1.1 joerg 795 1.1 joerg // Note that Sema::MergeVarDecl already takes care of implementing 796 1.1 joerg // C99 6.2.2p4 and propagating the visibility attribute, so we don't have 797 1.1 joerg // to do it here. 798 1.1 joerg 799 1.1 joerg // As per function and class template specializations (below), 800 1.1 joerg // consider LV for the template and template arguments. We're at file 801 1.1 joerg // scope, so we do not need to worry about nested specializations. 802 1.1 joerg if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { 803 1.1 joerg mergeTemplateLV(LV, spec, computation); 804 1.1 joerg } 805 1.1 joerg 806 1.1 joerg // - a function; or 807 1.1 joerg } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { 808 1.1 joerg // In theory, we can modify the function's LV by the LV of its 809 1.1 joerg // type unless it has C linkage (see comment above about variables 810 1.1 joerg // for justification). In practice, GCC doesn't do this, so it's 811 1.1 joerg // just too painful to make work. 812 1.1 joerg 813 1.1 joerg if (Function->getStorageClass() == SC_PrivateExtern) 814 1.1 joerg LV.mergeVisibility(HiddenVisibility, true); 815 1.1 joerg 816 1.1 joerg // Note that Sema::MergeCompatibleFunctionDecls already takes care of 817 1.1 joerg // merging storage classes and visibility attributes, so we don't have to 818 1.1 joerg // look at previous decls in here. 819 1.1 joerg 820 1.1 joerg // In C++, then if the type of the function uses a type with 821 1.1 joerg // unique-external linkage, it's not legally usable from outside 822 1.1 joerg // this translation unit. However, we should use the C linkage 823 1.1 joerg // rules instead for extern "C" declarations. 824 1.1 joerg if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { 825 1.1 joerg // Only look at the type-as-written. Otherwise, deducing the return type 826 1.1 joerg // of a function could change its linkage. 827 1.1 joerg QualType TypeAsWritten = Function->getType(); 828 1.1 joerg if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) 829 1.1 joerg TypeAsWritten = TSI->getType(); 830 1.1 joerg if (!isExternallyVisible(TypeAsWritten->getLinkage())) 831 1.1 joerg return LinkageInfo::uniqueExternal(); 832 1.1 joerg } 833 1.1 joerg 834 1.1 joerg // Consider LV from the template and the template arguments. 835 1.1 joerg // We're at file scope, so we do not need to worry about nested 836 1.1 joerg // specializations. 837 1.1 joerg if (FunctionTemplateSpecializationInfo *specInfo 838 1.1 joerg = Function->getTemplateSpecializationInfo()) { 839 1.1 joerg mergeTemplateLV(LV, Function, specInfo, computation); 840 1.1 joerg } 841 1.1 joerg 842 1.1 joerg // - a named class (Clause 9), or an unnamed class defined in a 843 1.1 joerg // typedef declaration in which the class has the typedef name 844 1.1 joerg // for linkage purposes (7.1.3); or 845 1.1 joerg // - a named enumeration (7.2), or an unnamed enumeration 846 1.1 joerg // defined in a typedef declaration in which the enumeration 847 1.1 joerg // has the typedef name for linkage purposes (7.1.3); or 848 1.1 joerg } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { 849 1.1 joerg // Unnamed tags have no linkage. 850 1.1 joerg if (!Tag->hasNameForLinkage()) 851 1.1 joerg return LinkageInfo::none(); 852 1.1 joerg 853 1.1 joerg // If this is a class template specialization, consider the 854 1.1 joerg // linkage of the template and template arguments. We're at file 855 1.1 joerg // scope, so we do not need to worry about nested specializations. 856 1.1 joerg if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { 857 1.1 joerg mergeTemplateLV(LV, spec, computation); 858 1.1 joerg } 859 1.1 joerg 860 1.1 joerg // FIXME: This is not part of the C++ standard any more. 861 1.1 joerg // - an enumerator belonging to an enumeration with external linkage; or 862 1.1 joerg } else if (isa<EnumConstantDecl>(D)) { 863 1.1 joerg LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), 864 1.1 joerg computation); 865 1.1 joerg if (!isExternalFormalLinkage(EnumLV.getLinkage())) 866 1.1 joerg return LinkageInfo::none(); 867 1.1 joerg LV.merge(EnumLV); 868 1.1 joerg 869 1.1 joerg // - a template 870 1.1 joerg } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { 871 1.1 joerg bool considerVisibility = !hasExplicitVisibilityAlready(computation); 872 1.1 joerg LinkageInfo tempLV = 873 1.1 joerg getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 874 1.1 joerg LV.mergeMaybeWithVisibility(tempLV, considerVisibility); 875 1.1 joerg 876 1.1 joerg // An unnamed namespace or a namespace declared directly or indirectly 877 1.1 joerg // within an unnamed namespace has internal linkage. All other namespaces 878 1.1 joerg // have external linkage. 879 1.1 joerg // 880 1.1 joerg // We handled names in anonymous namespaces above. 881 1.1 joerg } else if (isa<NamespaceDecl>(D)) { 882 1.1 joerg return LV; 883 1.1 joerg 884 1.1 joerg // By extension, we assign external linkage to Objective-C 885 1.1 joerg // interfaces. 886 1.1 joerg } else if (isa<ObjCInterfaceDecl>(D)) { 887 1.1 joerg // fallout 888 1.1 joerg 889 1.1 joerg } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { 890 1.1 joerg // A typedef declaration has linkage if it gives a type a name for 891 1.1 joerg // linkage purposes. 892 1.1 joerg if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) 893 1.1 joerg return LinkageInfo::none(); 894 1.1 joerg 895 1.1.1.2 joerg } else if (isa<MSGuidDecl>(D)) { 896 1.1.1.2 joerg // A GUID behaves like an inline variable with external linkage. Fall 897 1.1.1.2 joerg // through. 898 1.1.1.2 joerg 899 1.1 joerg // Everything not covered here has no linkage. 900 1.1 joerg } else { 901 1.1 joerg return LinkageInfo::none(); 902 1.1 joerg } 903 1.1 joerg 904 1.1 joerg // If we ended up with non-externally-visible linkage, visibility should 905 1.1 joerg // always be default. 906 1.1 joerg if (!isExternallyVisible(LV.getLinkage())) 907 1.1 joerg return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); 908 1.1 joerg 909 1.1.1.2 joerg // Mark the symbols as hidden when compiling for the device. 910 1.1.1.2 joerg if (Context.getLangOpts().OpenMP && Context.getLangOpts().OpenMPIsDevice) 911 1.1.1.2 joerg LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); 912 1.1.1.2 joerg 913 1.1 joerg return LV; 914 1.1 joerg } 915 1.1 joerg 916 1.1 joerg LinkageInfo 917 1.1 joerg LinkageComputer::getLVForClassMember(const NamedDecl *D, 918 1.1 joerg LVComputationKind computation, 919 1.1 joerg bool IgnoreVarTypeLinkage) { 920 1.1 joerg // Only certain class members have linkage. Note that fields don't 921 1.1 joerg // really have linkage, but it's convenient to say they do for the 922 1.1 joerg // purposes of calculating linkage of pointer-to-data-member 923 1.1 joerg // template arguments. 924 1.1 joerg // 925 1.1 joerg // Templates also don't officially have linkage, but since we ignore 926 1.1 joerg // the C++ standard and look at template arguments when determining 927 1.1 joerg // linkage and visibility of a template specialization, we might hit 928 1.1 joerg // a template template argument that way. If we do, we need to 929 1.1 joerg // consider its linkage. 930 1.1 joerg if (!(isa<CXXMethodDecl>(D) || 931 1.1 joerg isa<VarDecl>(D) || 932 1.1 joerg isa<FieldDecl>(D) || 933 1.1 joerg isa<IndirectFieldDecl>(D) || 934 1.1 joerg isa<TagDecl>(D) || 935 1.1 joerg isa<TemplateDecl>(D))) 936 1.1 joerg return LinkageInfo::none(); 937 1.1 joerg 938 1.1 joerg LinkageInfo LV; 939 1.1 joerg 940 1.1 joerg // If we have an explicit visibility attribute, merge that in. 941 1.1 joerg if (!hasExplicitVisibilityAlready(computation)) { 942 1.1 joerg if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) 943 1.1 joerg LV.mergeVisibility(*Vis, true); 944 1.1 joerg // If we're paying attention to global visibility, apply 945 1.1 joerg // -finline-visibility-hidden if this is an inline method. 946 1.1 joerg // 947 1.1 joerg // Note that we do this before merging information about 948 1.1 joerg // the class visibility. 949 1.1 joerg if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) 950 1.1 joerg LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); 951 1.1 joerg } 952 1.1 joerg 953 1.1 joerg // If this class member has an explicit visibility attribute, the only 954 1.1 joerg // thing that can change its visibility is the template arguments, so 955 1.1 joerg // only look for them when processing the class. 956 1.1 joerg LVComputationKind classComputation = computation; 957 1.1 joerg if (LV.isVisibilityExplicit()) 958 1.1 joerg classComputation = withExplicitVisibilityAlready(computation); 959 1.1 joerg 960 1.1 joerg LinkageInfo classLV = 961 1.1 joerg getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); 962 1.1 joerg // The member has the same linkage as the class. If that's not externally 963 1.1 joerg // visible, we don't need to compute anything about the linkage. 964 1.1 joerg // FIXME: If we're only computing linkage, can we bail out here? 965 1.1 joerg if (!isExternallyVisible(classLV.getLinkage())) 966 1.1 joerg return classLV; 967 1.1 joerg 968 1.1 joerg 969 1.1 joerg // Otherwise, don't merge in classLV yet, because in certain cases 970 1.1 joerg // we need to completely ignore the visibility from it. 971 1.1 joerg 972 1.1 joerg // Specifically, if this decl exists and has an explicit attribute. 973 1.1 joerg const NamedDecl *explicitSpecSuppressor = nullptr; 974 1.1 joerg 975 1.1 joerg if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 976 1.1 joerg // Only look at the type-as-written. Otherwise, deducing the return type 977 1.1 joerg // of a function could change its linkage. 978 1.1 joerg QualType TypeAsWritten = MD->getType(); 979 1.1 joerg if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) 980 1.1 joerg TypeAsWritten = TSI->getType(); 981 1.1 joerg if (!isExternallyVisible(TypeAsWritten->getLinkage())) 982 1.1 joerg return LinkageInfo::uniqueExternal(); 983 1.1 joerg 984 1.1 joerg // If this is a method template specialization, use the linkage for 985 1.1 joerg // the template parameters and arguments. 986 1.1 joerg if (FunctionTemplateSpecializationInfo *spec 987 1.1 joerg = MD->getTemplateSpecializationInfo()) { 988 1.1 joerg mergeTemplateLV(LV, MD, spec, computation); 989 1.1 joerg if (spec->isExplicitSpecialization()) { 990 1.1 joerg explicitSpecSuppressor = MD; 991 1.1 joerg } else if (isExplicitMemberSpecialization(spec->getTemplate())) { 992 1.1 joerg explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); 993 1.1 joerg } 994 1.1 joerg } else if (isExplicitMemberSpecialization(MD)) { 995 1.1 joerg explicitSpecSuppressor = MD; 996 1.1 joerg } 997 1.1 joerg 998 1.1 joerg } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { 999 1.1 joerg if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 1000 1.1 joerg mergeTemplateLV(LV, spec, computation); 1001 1.1 joerg if (spec->isExplicitSpecialization()) { 1002 1.1 joerg explicitSpecSuppressor = spec; 1003 1.1 joerg } else { 1004 1.1 joerg const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); 1005 1.1 joerg if (isExplicitMemberSpecialization(temp)) { 1006 1.1 joerg explicitSpecSuppressor = temp->getTemplatedDecl(); 1007 1.1 joerg } 1008 1.1 joerg } 1009 1.1 joerg } else if (isExplicitMemberSpecialization(RD)) { 1010 1.1 joerg explicitSpecSuppressor = RD; 1011 1.1 joerg } 1012 1.1 joerg 1013 1.1 joerg // Static data members. 1014 1.1 joerg } else if (const auto *VD = dyn_cast<VarDecl>(D)) { 1015 1.1 joerg if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) 1016 1.1 joerg mergeTemplateLV(LV, spec, computation); 1017 1.1 joerg 1018 1.1 joerg // Modify the variable's linkage by its type, but ignore the 1019 1.1 joerg // type's visibility unless it's a definition. 1020 1.1 joerg if (!IgnoreVarTypeLinkage) { 1021 1.1 joerg LinkageInfo typeLV = getLVForType(*VD->getType(), computation); 1022 1.1 joerg // FIXME: If the type's linkage is not externally visible, we can 1023 1.1 joerg // give this static data member UniqueExternalLinkage. 1024 1.1 joerg if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) 1025 1.1 joerg LV.mergeVisibility(typeLV); 1026 1.1 joerg LV.mergeExternalVisibility(typeLV); 1027 1.1 joerg } 1028 1.1 joerg 1029 1.1 joerg if (isExplicitMemberSpecialization(VD)) { 1030 1.1 joerg explicitSpecSuppressor = VD; 1031 1.1 joerg } 1032 1.1 joerg 1033 1.1 joerg // Template members. 1034 1.1 joerg } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { 1035 1.1 joerg bool considerVisibility = 1036 1.1 joerg (!LV.isVisibilityExplicit() && 1037 1.1 joerg !classLV.isVisibilityExplicit() && 1038 1.1 joerg !hasExplicitVisibilityAlready(computation)); 1039 1.1 joerg LinkageInfo tempLV = 1040 1.1 joerg getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 1041 1.1 joerg LV.mergeMaybeWithVisibility(tempLV, considerVisibility); 1042 1.1 joerg 1043 1.1 joerg if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { 1044 1.1 joerg if (isExplicitMemberSpecialization(redeclTemp)) { 1045 1.1 joerg explicitSpecSuppressor = temp->getTemplatedDecl(); 1046 1.1 joerg } 1047 1.1 joerg } 1048 1.1 joerg } 1049 1.1 joerg 1050 1.1 joerg // We should never be looking for an attribute directly on a template. 1051 1.1 joerg assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); 1052 1.1 joerg 1053 1.1 joerg // If this member is an explicit member specialization, and it has 1054 1.1 joerg // an explicit attribute, ignore visibility from the parent. 1055 1.1 joerg bool considerClassVisibility = true; 1056 1.1 joerg if (explicitSpecSuppressor && 1057 1.1 joerg // optimization: hasDVA() is true only with explicit visibility. 1058 1.1 joerg LV.isVisibilityExplicit() && 1059 1.1 joerg classLV.getVisibility() != DefaultVisibility && 1060 1.1 joerg hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { 1061 1.1 joerg considerClassVisibility = false; 1062 1.1 joerg } 1063 1.1 joerg 1064 1.1 joerg // Finally, merge in information from the class. 1065 1.1 joerg LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); 1066 1.1 joerg return LV; 1067 1.1 joerg } 1068 1.1 joerg 1069 1.1 joerg void NamedDecl::anchor() {} 1070 1.1 joerg 1071 1.1 joerg bool NamedDecl::isLinkageValid() const { 1072 1.1 joerg if (!hasCachedLinkage()) 1073 1.1 joerg return true; 1074 1.1 joerg 1075 1.1 joerg Linkage L = LinkageComputer{} 1076 1.1 joerg .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) 1077 1.1 joerg .getLinkage(); 1078 1.1 joerg return L == getCachedLinkage(); 1079 1.1 joerg } 1080 1.1 joerg 1081 1.1.1.2 joerg ReservedIdentifierStatus 1082 1.1.1.2 joerg NamedDecl::isReserved(const LangOptions &LangOpts) const { 1083 1.1.1.2 joerg const IdentifierInfo *II = getIdentifier(); 1084 1.1.1.2 joerg if (!II) 1085 1.1.1.2 joerg if (const auto *FD = dyn_cast<FunctionDecl>(this)) 1086 1.1.1.2 joerg II = FD->getLiteralIdentifier(); 1087 1.1.1.2 joerg 1088 1.1.1.2 joerg if (!II) 1089 1.1.1.2 joerg return ReservedIdentifierStatus::NotReserved; 1090 1.1.1.2 joerg 1091 1.1.1.2 joerg ReservedIdentifierStatus Status = II->isReserved(LangOpts); 1092 1.1.1.2 joerg if (Status == ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope) { 1093 1.1.1.2 joerg // Check if we're at TU level or not. 1094 1.1.1.2 joerg if (isa<ParmVarDecl>(this) || isTemplateParameter()) 1095 1.1.1.2 joerg return ReservedIdentifierStatus::NotReserved; 1096 1.1.1.2 joerg const DeclContext *DC = getDeclContext()->getRedeclContext(); 1097 1.1.1.2 joerg if (!DC->isTranslationUnit()) 1098 1.1.1.2 joerg return ReservedIdentifierStatus::NotReserved; 1099 1.1.1.2 joerg } 1100 1.1.1.2 joerg 1101 1.1.1.2 joerg return Status; 1102 1.1.1.2 joerg } 1103 1.1.1.2 joerg 1104 1.1 joerg ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { 1105 1.1 joerg StringRef name = getName(); 1106 1.1 joerg if (name.empty()) return SFF_None; 1107 1.1 joerg 1108 1.1 joerg if (name.front() == 'C') 1109 1.1 joerg if (name == "CFStringCreateWithFormat" || 1110 1.1 joerg name == "CFStringCreateWithFormatAndArguments" || 1111 1.1 joerg name == "CFStringAppendFormat" || 1112 1.1 joerg name == "CFStringAppendFormatAndArguments") 1113 1.1 joerg return SFF_CFString; 1114 1.1 joerg return SFF_None; 1115 1.1 joerg } 1116 1.1 joerg 1117 1.1 joerg Linkage NamedDecl::getLinkageInternal() const { 1118 1.1 joerg // We don't care about visibility here, so ask for the cheapest 1119 1.1 joerg // possible visibility analysis. 1120 1.1 joerg return LinkageComputer{} 1121 1.1 joerg .getLVForDecl(this, LVComputationKind::forLinkageOnly()) 1122 1.1 joerg .getLinkage(); 1123 1.1 joerg } 1124 1.1 joerg 1125 1.1 joerg LinkageInfo NamedDecl::getLinkageAndVisibility() const { 1126 1.1 joerg return LinkageComputer{}.getDeclLinkageAndVisibility(this); 1127 1.1 joerg } 1128 1.1 joerg 1129 1.1 joerg static Optional<Visibility> 1130 1.1 joerg getExplicitVisibilityAux(const NamedDecl *ND, 1131 1.1 joerg NamedDecl::ExplicitVisibilityKind kind, 1132 1.1 joerg bool IsMostRecent) { 1133 1.1 joerg assert(!IsMostRecent || ND == ND->getMostRecentDecl()); 1134 1.1 joerg 1135 1.1 joerg // Check the declaration itself first. 1136 1.1 joerg if (Optional<Visibility> V = getVisibilityOf(ND, kind)) 1137 1.1 joerg return V; 1138 1.1 joerg 1139 1.1 joerg // If this is a member class of a specialization of a class template 1140 1.1 joerg // and the corresponding decl has explicit visibility, use that. 1141 1.1 joerg if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { 1142 1.1 joerg CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); 1143 1.1 joerg if (InstantiatedFrom) 1144 1.1 joerg return getVisibilityOf(InstantiatedFrom, kind); 1145 1.1 joerg } 1146 1.1 joerg 1147 1.1 joerg // If there wasn't explicit visibility there, and this is a 1148 1.1 joerg // specialization of a class template, check for visibility 1149 1.1 joerg // on the pattern. 1150 1.1 joerg if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 1151 1.1 joerg // Walk all the template decl till this point to see if there are 1152 1.1 joerg // explicit visibility attributes. 1153 1.1 joerg const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); 1154 1.1 joerg while (TD != nullptr) { 1155 1.1 joerg auto Vis = getVisibilityOf(TD, kind); 1156 1.1 joerg if (Vis != None) 1157 1.1 joerg return Vis; 1158 1.1 joerg TD = TD->getPreviousDecl(); 1159 1.1 joerg } 1160 1.1 joerg return None; 1161 1.1 joerg } 1162 1.1 joerg 1163 1.1 joerg // Use the most recent declaration. 1164 1.1 joerg if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { 1165 1.1 joerg const NamedDecl *MostRecent = ND->getMostRecentDecl(); 1166 1.1 joerg if (MostRecent != ND) 1167 1.1 joerg return getExplicitVisibilityAux(MostRecent, kind, true); 1168 1.1 joerg } 1169 1.1 joerg 1170 1.1 joerg if (const auto *Var = dyn_cast<VarDecl>(ND)) { 1171 1.1 joerg if (Var->isStaticDataMember()) { 1172 1.1 joerg VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); 1173 1.1 joerg if (InstantiatedFrom) 1174 1.1 joerg return getVisibilityOf(InstantiatedFrom, kind); 1175 1.1 joerg } 1176 1.1 joerg 1177 1.1 joerg if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) 1178 1.1 joerg return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), 1179 1.1 joerg kind); 1180 1.1 joerg 1181 1.1 joerg return None; 1182 1.1 joerg } 1183 1.1 joerg // Also handle function template specializations. 1184 1.1 joerg if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { 1185 1.1 joerg // If the function is a specialization of a template with an 1186 1.1 joerg // explicit visibility attribute, use that. 1187 1.1 joerg if (FunctionTemplateSpecializationInfo *templateInfo 1188 1.1 joerg = fn->getTemplateSpecializationInfo()) 1189 1.1 joerg return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), 1190 1.1 joerg kind); 1191 1.1 joerg 1192 1.1 joerg // If the function is a member of a specialization of a class template 1193 1.1 joerg // and the corresponding decl has explicit visibility, use that. 1194 1.1 joerg FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); 1195 1.1 joerg if (InstantiatedFrom) 1196 1.1 joerg return getVisibilityOf(InstantiatedFrom, kind); 1197 1.1 joerg 1198 1.1 joerg return None; 1199 1.1 joerg } 1200 1.1 joerg 1201 1.1 joerg // The visibility of a template is stored in the templated decl. 1202 1.1 joerg if (const auto *TD = dyn_cast<TemplateDecl>(ND)) 1203 1.1 joerg return getVisibilityOf(TD->getTemplatedDecl(), kind); 1204 1.1 joerg 1205 1.1 joerg return None; 1206 1.1 joerg } 1207 1.1 joerg 1208 1.1 joerg Optional<Visibility> 1209 1.1 joerg NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { 1210 1.1 joerg return getExplicitVisibilityAux(this, kind, false); 1211 1.1 joerg } 1212 1.1 joerg 1213 1.1 joerg LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, 1214 1.1 joerg Decl *ContextDecl, 1215 1.1 joerg LVComputationKind computation) { 1216 1.1 joerg // This lambda has its linkage/visibility determined by its owner. 1217 1.1 joerg const NamedDecl *Owner; 1218 1.1 joerg if (!ContextDecl) 1219 1.1 joerg Owner = dyn_cast<NamedDecl>(DC); 1220 1.1 joerg else if (isa<ParmVarDecl>(ContextDecl)) 1221 1.1 joerg Owner = 1222 1.1 joerg dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); 1223 1.1 joerg else 1224 1.1 joerg Owner = cast<NamedDecl>(ContextDecl); 1225 1.1 joerg 1226 1.1 joerg if (!Owner) 1227 1.1 joerg return LinkageInfo::none(); 1228 1.1 joerg 1229 1.1 joerg // If the owner has a deduced type, we need to skip querying the linkage and 1230 1.1 joerg // visibility of that type, because it might involve this closure type. The 1231 1.1 joerg // only effect of this is that we might give a lambda VisibleNoLinkage rather 1232 1.1 joerg // than NoLinkage when we don't strictly need to, which is benign. 1233 1.1 joerg auto *VD = dyn_cast<VarDecl>(Owner); 1234 1.1 joerg LinkageInfo OwnerLV = 1235 1.1 joerg VD && VD->getType()->getContainedDeducedType() 1236 1.1 joerg ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) 1237 1.1 joerg : getLVForDecl(Owner, computation); 1238 1.1 joerg 1239 1.1 joerg // A lambda never formally has linkage. But if the owner is externally 1240 1.1 joerg // visible, then the lambda is too. We apply the same rules to blocks. 1241 1.1 joerg if (!isExternallyVisible(OwnerLV.getLinkage())) 1242 1.1 joerg return LinkageInfo::none(); 1243 1.1 joerg return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(), 1244 1.1 joerg OwnerLV.isVisibilityExplicit()); 1245 1.1 joerg } 1246 1.1 joerg 1247 1.1 joerg LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, 1248 1.1 joerg LVComputationKind computation) { 1249 1.1 joerg if (const auto *Function = dyn_cast<FunctionDecl>(D)) { 1250 1.1 joerg if (Function->isInAnonymousNamespace() && 1251 1.1 joerg !isFirstInExternCContext(Function)) 1252 1.1 joerg return getInternalLinkageFor(Function); 1253 1.1 joerg 1254 1.1 joerg // This is a "void f();" which got merged with a file static. 1255 1.1 joerg if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) 1256 1.1 joerg return getInternalLinkageFor(Function); 1257 1.1 joerg 1258 1.1 joerg LinkageInfo LV; 1259 1.1 joerg if (!hasExplicitVisibilityAlready(computation)) { 1260 1.1 joerg if (Optional<Visibility> Vis = 1261 1.1 joerg getExplicitVisibility(Function, computation)) 1262 1.1 joerg LV.mergeVisibility(*Vis, true); 1263 1.1 joerg } 1264 1.1 joerg 1265 1.1 joerg // Note that Sema::MergeCompatibleFunctionDecls already takes care of 1266 1.1 joerg // merging storage classes and visibility attributes, so we don't have to 1267 1.1 joerg // look at previous decls in here. 1268 1.1 joerg 1269 1.1 joerg return LV; 1270 1.1 joerg } 1271 1.1 joerg 1272 1.1 joerg if (const auto *Var = dyn_cast<VarDecl>(D)) { 1273 1.1 joerg if (Var->hasExternalStorage()) { 1274 1.1 joerg if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) 1275 1.1 joerg return getInternalLinkageFor(Var); 1276 1.1 joerg 1277 1.1 joerg LinkageInfo LV; 1278 1.1 joerg if (Var->getStorageClass() == SC_PrivateExtern) 1279 1.1 joerg LV.mergeVisibility(HiddenVisibility, true); 1280 1.1 joerg else if (!hasExplicitVisibilityAlready(computation)) { 1281 1.1 joerg if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) 1282 1.1 joerg LV.mergeVisibility(*Vis, true); 1283 1.1 joerg } 1284 1.1 joerg 1285 1.1 joerg if (const VarDecl *Prev = Var->getPreviousDecl()) { 1286 1.1 joerg LinkageInfo PrevLV = getLVForDecl(Prev, computation); 1287 1.1 joerg if (PrevLV.getLinkage()) 1288 1.1 joerg LV.setLinkage(PrevLV.getLinkage()); 1289 1.1 joerg LV.mergeVisibility(PrevLV); 1290 1.1 joerg } 1291 1.1 joerg 1292 1.1 joerg return LV; 1293 1.1 joerg } 1294 1.1 joerg 1295 1.1 joerg if (!Var->isStaticLocal()) 1296 1.1 joerg return LinkageInfo::none(); 1297 1.1 joerg } 1298 1.1 joerg 1299 1.1 joerg ASTContext &Context = D->getASTContext(); 1300 1.1 joerg if (!Context.getLangOpts().CPlusPlus) 1301 1.1 joerg return LinkageInfo::none(); 1302 1.1 joerg 1303 1.1 joerg const Decl *OuterD = getOutermostFuncOrBlockContext(D); 1304 1.1 joerg if (!OuterD || OuterD->isInvalidDecl()) 1305 1.1 joerg return LinkageInfo::none(); 1306 1.1 joerg 1307 1.1 joerg LinkageInfo LV; 1308 1.1 joerg if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { 1309 1.1 joerg if (!BD->getBlockManglingNumber()) 1310 1.1 joerg return LinkageInfo::none(); 1311 1.1 joerg 1312 1.1 joerg LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), 1313 1.1 joerg BD->getBlockManglingContextDecl(), computation); 1314 1.1 joerg } else { 1315 1.1 joerg const auto *FD = cast<FunctionDecl>(OuterD); 1316 1.1 joerg if (!FD->isInlined() && 1317 1.1 joerg !isTemplateInstantiation(FD->getTemplateSpecializationKind())) 1318 1.1 joerg return LinkageInfo::none(); 1319 1.1 joerg 1320 1.1 joerg // If a function is hidden by -fvisibility-inlines-hidden option and 1321 1.1 joerg // is not explicitly attributed as a hidden function, 1322 1.1 joerg // we should not make static local variables in the function hidden. 1323 1.1 joerg LV = getLVForDecl(FD, computation); 1324 1.1 joerg if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && 1325 1.1.1.2 joerg !LV.isVisibilityExplicit() && 1326 1.1.1.2 joerg !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { 1327 1.1 joerg assert(cast<VarDecl>(D)->isStaticLocal()); 1328 1.1 joerg // If this was an implicitly hidden inline method, check again for 1329 1.1 joerg // explicit visibility on the parent class, and use that for static locals 1330 1.1 joerg // if present. 1331 1.1 joerg if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) 1332 1.1 joerg LV = getLVForDecl(MD->getParent(), computation); 1333 1.1 joerg if (!LV.isVisibilityExplicit()) { 1334 1.1 joerg Visibility globalVisibility = 1335 1.1 joerg computation.isValueVisibility() 1336 1.1 joerg ? Context.getLangOpts().getValueVisibilityMode() 1337 1.1 joerg : Context.getLangOpts().getTypeVisibilityMode(); 1338 1.1 joerg return LinkageInfo(VisibleNoLinkage, globalVisibility, 1339 1.1 joerg /*visibilityExplicit=*/false); 1340 1.1 joerg } 1341 1.1 joerg } 1342 1.1 joerg } 1343 1.1 joerg if (!isExternallyVisible(LV.getLinkage())) 1344 1.1 joerg return LinkageInfo::none(); 1345 1.1 joerg return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), 1346 1.1 joerg LV.isVisibilityExplicit()); 1347 1.1 joerg } 1348 1.1 joerg 1349 1.1 joerg LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, 1350 1.1 joerg LVComputationKind computation, 1351 1.1 joerg bool IgnoreVarTypeLinkage) { 1352 1.1 joerg // Internal_linkage attribute overrides other considerations. 1353 1.1 joerg if (D->hasAttr<InternalLinkageAttr>()) 1354 1.1 joerg return getInternalLinkageFor(D); 1355 1.1 joerg 1356 1.1 joerg // Objective-C: treat all Objective-C declarations as having external 1357 1.1 joerg // linkage. 1358 1.1 joerg switch (D->getKind()) { 1359 1.1 joerg default: 1360 1.1 joerg break; 1361 1.1 joerg 1362 1.1 joerg // Per C++ [basic.link]p2, only the names of objects, references, 1363 1.1 joerg // functions, types, templates, namespaces, and values ever have linkage. 1364 1.1 joerg // 1365 1.1 joerg // Note that the name of a typedef, namespace alias, using declaration, 1366 1.1 joerg // and so on are not the name of the corresponding type, namespace, or 1367 1.1 joerg // declaration, so they do *not* have linkage. 1368 1.1 joerg case Decl::ImplicitParam: 1369 1.1 joerg case Decl::Label: 1370 1.1 joerg case Decl::NamespaceAlias: 1371 1.1 joerg case Decl::ParmVar: 1372 1.1 joerg case Decl::Using: 1373 1.1 joerg case Decl::UsingShadow: 1374 1.1 joerg case Decl::UsingDirective: 1375 1.1 joerg return LinkageInfo::none(); 1376 1.1 joerg 1377 1.1 joerg case Decl::EnumConstant: 1378 1.1 joerg // C++ [basic.link]p4: an enumerator has the linkage of its enumeration. 1379 1.1 joerg if (D->getASTContext().getLangOpts().CPlusPlus) 1380 1.1 joerg return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); 1381 1.1 joerg return LinkageInfo::visible_none(); 1382 1.1 joerg 1383 1.1 joerg case Decl::Typedef: 1384 1.1 joerg case Decl::TypeAlias: 1385 1.1 joerg // A typedef declaration has linkage if it gives a type a name for 1386 1.1 joerg // linkage purposes. 1387 1.1 joerg if (!cast<TypedefNameDecl>(D) 1388 1.1 joerg ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) 1389 1.1 joerg return LinkageInfo::none(); 1390 1.1 joerg break; 1391 1.1 joerg 1392 1.1 joerg case Decl::TemplateTemplateParm: // count these as external 1393 1.1 joerg case Decl::NonTypeTemplateParm: 1394 1.1 joerg case Decl::ObjCAtDefsField: 1395 1.1 joerg case Decl::ObjCCategory: 1396 1.1 joerg case Decl::ObjCCategoryImpl: 1397 1.1 joerg case Decl::ObjCCompatibleAlias: 1398 1.1 joerg case Decl::ObjCImplementation: 1399 1.1 joerg case Decl::ObjCMethod: 1400 1.1 joerg case Decl::ObjCProperty: 1401 1.1 joerg case Decl::ObjCPropertyImpl: 1402 1.1 joerg case Decl::ObjCProtocol: 1403 1.1 joerg return getExternalLinkageFor(D); 1404 1.1 joerg 1405 1.1 joerg case Decl::CXXRecord: { 1406 1.1 joerg const auto *Record = cast<CXXRecordDecl>(D); 1407 1.1 joerg if (Record->isLambda()) { 1408 1.1 joerg if (Record->hasKnownLambdaInternalLinkage() || 1409 1.1 joerg !Record->getLambdaManglingNumber()) { 1410 1.1 joerg // This lambda has no mangling number, so it's internal. 1411 1.1 joerg return getInternalLinkageFor(D); 1412 1.1 joerg } 1413 1.1 joerg 1414 1.1 joerg return getLVForClosure( 1415 1.1.1.2 joerg Record->getDeclContext()->getRedeclContext(), 1416 1.1.1.2 joerg Record->getLambdaContextDecl(), computation); 1417 1.1 joerg } 1418 1.1 joerg 1419 1.1 joerg break; 1420 1.1 joerg } 1421 1.1.1.2 joerg 1422 1.1.1.2 joerg case Decl::TemplateParamObject: { 1423 1.1.1.2 joerg // The template parameter object can be referenced from anywhere its type 1424 1.1.1.2 joerg // and value can be referenced. 1425 1.1.1.2 joerg auto *TPO = cast<TemplateParamObjectDecl>(D); 1426 1.1.1.2 joerg LinkageInfo LV = getLVForType(*TPO->getType(), computation); 1427 1.1.1.2 joerg LV.merge(getLVForValue(TPO->getValue(), computation)); 1428 1.1.1.2 joerg return LV; 1429 1.1.1.2 joerg } 1430 1.1 joerg } 1431 1.1 joerg 1432 1.1 joerg // Handle linkage for namespace-scope names. 1433 1.1 joerg if (D->getDeclContext()->getRedeclContext()->isFileContext()) 1434 1.1 joerg return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); 1435 1.1 joerg 1436 1.1 joerg // C++ [basic.link]p5: 1437 1.1 joerg // In addition, a member function, static data member, a named 1438 1.1 joerg // class or enumeration of class scope, or an unnamed class or 1439 1.1 joerg // enumeration defined in a class-scope typedef declaration such 1440 1.1 joerg // that the class or enumeration has the typedef name for linkage 1441 1.1 joerg // purposes (7.1.3), has external linkage if the name of the class 1442 1.1 joerg // has external linkage. 1443 1.1 joerg if (D->getDeclContext()->isRecord()) 1444 1.1 joerg return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); 1445 1.1 joerg 1446 1.1 joerg // C++ [basic.link]p6: 1447 1.1 joerg // The name of a function declared in block scope and the name of 1448 1.1 joerg // an object declared by a block scope extern declaration have 1449 1.1 joerg // linkage. If there is a visible declaration of an entity with 1450 1.1 joerg // linkage having the same name and type, ignoring entities 1451 1.1 joerg // declared outside the innermost enclosing namespace scope, the 1452 1.1 joerg // block scope declaration declares that same entity and receives 1453 1.1 joerg // the linkage of the previous declaration. If there is more than 1454 1.1 joerg // one such matching entity, the program is ill-formed. Otherwise, 1455 1.1 joerg // if no matching entity is found, the block scope entity receives 1456 1.1 joerg // external linkage. 1457 1.1 joerg if (D->getDeclContext()->isFunctionOrMethod()) 1458 1.1 joerg return getLVForLocalDecl(D, computation); 1459 1.1 joerg 1460 1.1 joerg // C++ [basic.link]p6: 1461 1.1 joerg // Names not covered by these rules have no linkage. 1462 1.1 joerg return LinkageInfo::none(); 1463 1.1 joerg } 1464 1.1 joerg 1465 1.1 joerg /// getLVForDecl - Get the linkage and visibility for the given declaration. 1466 1.1 joerg LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, 1467 1.1 joerg LVComputationKind computation) { 1468 1.1 joerg // Internal_linkage attribute overrides other considerations. 1469 1.1 joerg if (D->hasAttr<InternalLinkageAttr>()) 1470 1.1 joerg return getInternalLinkageFor(D); 1471 1.1 joerg 1472 1.1 joerg if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) 1473 1.1 joerg return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); 1474 1.1 joerg 1475 1.1 joerg if (llvm::Optional<LinkageInfo> LI = lookup(D, computation)) 1476 1.1 joerg return *LI; 1477 1.1 joerg 1478 1.1 joerg LinkageInfo LV = computeLVForDecl(D, computation); 1479 1.1 joerg if (D->hasCachedLinkage()) 1480 1.1 joerg assert(D->getCachedLinkage() == LV.getLinkage()); 1481 1.1 joerg 1482 1.1 joerg D->setCachedLinkage(LV.getLinkage()); 1483 1.1 joerg cache(D, computation, LV); 1484 1.1 joerg 1485 1.1 joerg #ifndef NDEBUG 1486 1.1 joerg // In C (because of gnu inline) and in c++ with microsoft extensions an 1487 1.1 joerg // static can follow an extern, so we can have two decls with different 1488 1.1 joerg // linkages. 1489 1.1 joerg const LangOptions &Opts = D->getASTContext().getLangOpts(); 1490 1.1 joerg if (!Opts.CPlusPlus || Opts.MicrosoftExt) 1491 1.1 joerg return LV; 1492 1.1 joerg 1493 1.1 joerg // We have just computed the linkage for this decl. By induction we know 1494 1.1 joerg // that all other computed linkages match, check that the one we just 1495 1.1 joerg // computed also does. 1496 1.1 joerg NamedDecl *Old = nullptr; 1497 1.1 joerg for (auto I : D->redecls()) { 1498 1.1 joerg auto *T = cast<NamedDecl>(I); 1499 1.1 joerg if (T == D) 1500 1.1 joerg continue; 1501 1.1 joerg if (!T->isInvalidDecl() && T->hasCachedLinkage()) { 1502 1.1 joerg Old = T; 1503 1.1 joerg break; 1504 1.1 joerg } 1505 1.1 joerg } 1506 1.1 joerg assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); 1507 1.1 joerg #endif 1508 1.1 joerg 1509 1.1 joerg return LV; 1510 1.1 joerg } 1511 1.1 joerg 1512 1.1 joerg LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { 1513 1.1.1.2 joerg NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D) 1514 1.1.1.2 joerg ? NamedDecl::VisibilityForType 1515 1.1.1.2 joerg : NamedDecl::VisibilityForValue; 1516 1.1.1.2 joerg LVComputationKind CK(EK); 1517 1.1.1.2 joerg return getLVForDecl(D, D->getASTContext().getLangOpts().IgnoreXCOFFVisibility 1518 1.1.1.2 joerg ? CK.forLinkageOnly() 1519 1.1.1.2 joerg : CK); 1520 1.1 joerg } 1521 1.1 joerg 1522 1.1 joerg Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { 1523 1.1 joerg Module *M = getOwningModule(); 1524 1.1 joerg if (!M) 1525 1.1 joerg return nullptr; 1526 1.1 joerg 1527 1.1 joerg switch (M->Kind) { 1528 1.1 joerg case Module::ModuleMapModule: 1529 1.1 joerg // Module map modules have no special linkage semantics. 1530 1.1 joerg return nullptr; 1531 1.1 joerg 1532 1.1 joerg case Module::ModuleInterfaceUnit: 1533 1.1 joerg return M; 1534 1.1 joerg 1535 1.1 joerg case Module::GlobalModuleFragment: { 1536 1.1 joerg // External linkage declarations in the global module have no owning module 1537 1.1 joerg // for linkage purposes. But internal linkage declarations in the global 1538 1.1 joerg // module fragment of a particular module are owned by that module for 1539 1.1 joerg // linkage purposes. 1540 1.1 joerg if (IgnoreLinkage) 1541 1.1 joerg return nullptr; 1542 1.1 joerg bool InternalLinkage; 1543 1.1 joerg if (auto *ND = dyn_cast<NamedDecl>(this)) 1544 1.1 joerg InternalLinkage = !ND->hasExternalFormalLinkage(); 1545 1.1 joerg else { 1546 1.1 joerg auto *NSD = dyn_cast<NamespaceDecl>(this); 1547 1.1 joerg InternalLinkage = (NSD && NSD->isAnonymousNamespace()) || 1548 1.1 joerg isInAnonymousNamespace(); 1549 1.1 joerg } 1550 1.1 joerg return InternalLinkage ? M->Parent : nullptr; 1551 1.1 joerg } 1552 1.1 joerg 1553 1.1 joerg case Module::PrivateModuleFragment: 1554 1.1 joerg // The private module fragment is part of its containing module for linkage 1555 1.1 joerg // purposes. 1556 1.1 joerg return M->Parent; 1557 1.1 joerg } 1558 1.1 joerg 1559 1.1 joerg llvm_unreachable("unknown module kind"); 1560 1.1 joerg } 1561 1.1 joerg 1562 1.1 joerg void NamedDecl::printName(raw_ostream &os) const { 1563 1.1 joerg os << Name; 1564 1.1 joerg } 1565 1.1 joerg 1566 1.1 joerg std::string NamedDecl::getQualifiedNameAsString() const { 1567 1.1 joerg std::string QualName; 1568 1.1 joerg llvm::raw_string_ostream OS(QualName); 1569 1.1 joerg printQualifiedName(OS, getASTContext().getPrintingPolicy()); 1570 1.1 joerg return OS.str(); 1571 1.1 joerg } 1572 1.1 joerg 1573 1.1 joerg void NamedDecl::printQualifiedName(raw_ostream &OS) const { 1574 1.1 joerg printQualifiedName(OS, getASTContext().getPrintingPolicy()); 1575 1.1 joerg } 1576 1.1 joerg 1577 1.1 joerg void NamedDecl::printQualifiedName(raw_ostream &OS, 1578 1.1 joerg const PrintingPolicy &P) const { 1579 1.1 joerg if (getDeclContext()->isFunctionOrMethod()) { 1580 1.1 joerg // We do not print '(anonymous)' for function parameters without name. 1581 1.1 joerg printName(OS); 1582 1.1 joerg return; 1583 1.1 joerg } 1584 1.1 joerg printNestedNameSpecifier(OS, P); 1585 1.1.1.2 joerg if (getDeclName()) 1586 1.1 joerg OS << *this; 1587 1.1.1.2 joerg else { 1588 1.1.1.2 joerg // Give the printName override a chance to pick a different name before we 1589 1.1.1.2 joerg // fall back to "(anonymous)". 1590 1.1.1.2 joerg SmallString<64> NameBuffer; 1591 1.1.1.2 joerg llvm::raw_svector_ostream NameOS(NameBuffer); 1592 1.1.1.2 joerg printName(NameOS); 1593 1.1.1.2 joerg if (NameBuffer.empty()) 1594 1.1.1.2 joerg OS << "(anonymous)"; 1595 1.1.1.2 joerg else 1596 1.1.1.2 joerg OS << NameBuffer; 1597 1.1.1.2 joerg } 1598 1.1 joerg } 1599 1.1 joerg 1600 1.1 joerg void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { 1601 1.1 joerg printNestedNameSpecifier(OS, getASTContext().getPrintingPolicy()); 1602 1.1 joerg } 1603 1.1 joerg 1604 1.1 joerg void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, 1605 1.1 joerg const PrintingPolicy &P) const { 1606 1.1 joerg const DeclContext *Ctx = getDeclContext(); 1607 1.1 joerg 1608 1.1 joerg // For ObjC methods and properties, look through categories and use the 1609 1.1 joerg // interface as context. 1610 1.1.1.2 joerg if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) { 1611 1.1 joerg if (auto *ID = MD->getClassInterface()) 1612 1.1 joerg Ctx = ID; 1613 1.1.1.2 joerg } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { 1614 1.1 joerg if (auto *MD = PD->getGetterMethodDecl()) 1615 1.1 joerg if (auto *ID = MD->getClassInterface()) 1616 1.1 joerg Ctx = ID; 1617 1.1.1.2 joerg } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) { 1618 1.1.1.2 joerg if (auto *CI = ID->getContainingInterface()) 1619 1.1.1.2 joerg Ctx = CI; 1620 1.1 joerg } 1621 1.1 joerg 1622 1.1 joerg if (Ctx->isFunctionOrMethod()) 1623 1.1 joerg return; 1624 1.1 joerg 1625 1.1 joerg using ContextsTy = SmallVector<const DeclContext *, 8>; 1626 1.1 joerg ContextsTy Contexts; 1627 1.1 joerg 1628 1.1 joerg // Collect named contexts. 1629 1.1.1.2 joerg DeclarationName NameInScope = getDeclName(); 1630 1.1.1.2 joerg for (; Ctx; Ctx = Ctx->getParent()) { 1631 1.1.1.2 joerg // Suppress anonymous namespace if requested. 1632 1.1.1.2 joerg if (P.SuppressUnwrittenScope && isa<NamespaceDecl>(Ctx) && 1633 1.1.1.2 joerg cast<NamespaceDecl>(Ctx)->isAnonymousNamespace()) 1634 1.1.1.2 joerg continue; 1635 1.1.1.2 joerg 1636 1.1.1.2 joerg // Suppress inline namespace if it doesn't make the result ambiguous. 1637 1.1.1.2 joerg if (P.SuppressInlineNamespace && Ctx->isInlineNamespace() && NameInScope && 1638 1.1.1.2 joerg cast<NamespaceDecl>(Ctx)->isRedundantInlineQualifierFor(NameInScope)) 1639 1.1.1.2 joerg continue; 1640 1.1.1.2 joerg 1641 1.1.1.2 joerg // Skip non-named contexts such as linkage specifications and ExportDecls. 1642 1.1.1.2 joerg const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx); 1643 1.1.1.2 joerg if (!ND) 1644 1.1.1.2 joerg continue; 1645 1.1.1.2 joerg 1646 1.1.1.2 joerg Contexts.push_back(Ctx); 1647 1.1.1.2 joerg NameInScope = ND->getDeclName(); 1648 1.1 joerg } 1649 1.1 joerg 1650 1.1.1.2 joerg for (unsigned I = Contexts.size(); I != 0; --I) { 1651 1.1.1.2 joerg const DeclContext *DC = Contexts[I - 1]; 1652 1.1 joerg if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 1653 1.1 joerg OS << Spec->getName(); 1654 1.1 joerg const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1655 1.1.1.2 joerg printTemplateArgumentList( 1656 1.1.1.2 joerg OS, TemplateArgs.asArray(), P, 1657 1.1.1.2 joerg Spec->getSpecializedTemplate()->getTemplateParameters()); 1658 1.1 joerg } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { 1659 1.1 joerg if (ND->isAnonymousNamespace()) { 1660 1.1 joerg OS << (P.MSVCFormatting ? "`anonymous namespace\'" 1661 1.1 joerg : "(anonymous namespace)"); 1662 1.1 joerg } 1663 1.1 joerg else 1664 1.1 joerg OS << *ND; 1665 1.1 joerg } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { 1666 1.1 joerg if (!RD->getIdentifier()) 1667 1.1 joerg OS << "(anonymous " << RD->getKindName() << ')'; 1668 1.1 joerg else 1669 1.1 joerg OS << *RD; 1670 1.1 joerg } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { 1671 1.1 joerg const FunctionProtoType *FT = nullptr; 1672 1.1 joerg if (FD->hasWrittenPrototype()) 1673 1.1 joerg FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); 1674 1.1 joerg 1675 1.1 joerg OS << *FD << '('; 1676 1.1 joerg if (FT) { 1677 1.1 joerg unsigned NumParams = FD->getNumParams(); 1678 1.1 joerg for (unsigned i = 0; i < NumParams; ++i) { 1679 1.1 joerg if (i) 1680 1.1 joerg OS << ", "; 1681 1.1 joerg OS << FD->getParamDecl(i)->getType().stream(P); 1682 1.1 joerg } 1683 1.1 joerg 1684 1.1 joerg if (FT->isVariadic()) { 1685 1.1 joerg if (NumParams > 0) 1686 1.1 joerg OS << ", "; 1687 1.1 joerg OS << "..."; 1688 1.1 joerg } 1689 1.1 joerg } 1690 1.1 joerg OS << ')'; 1691 1.1 joerg } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { 1692 1.1 joerg // C++ [dcl.enum]p10: Each enum-name and each unscoped 1693 1.1 joerg // enumerator is declared in the scope that immediately contains 1694 1.1 joerg // the enum-specifier. Each scoped enumerator is declared in the 1695 1.1 joerg // scope of the enumeration. 1696 1.1 joerg // For the case of unscoped enumerator, do not include in the qualified 1697 1.1 joerg // name any information about its enum enclosing scope, as its visibility 1698 1.1 joerg // is global. 1699 1.1 joerg if (ED->isScoped()) 1700 1.1 joerg OS << *ED; 1701 1.1 joerg else 1702 1.1 joerg continue; 1703 1.1 joerg } else { 1704 1.1 joerg OS << *cast<NamedDecl>(DC); 1705 1.1 joerg } 1706 1.1 joerg OS << "::"; 1707 1.1 joerg } 1708 1.1 joerg } 1709 1.1 joerg 1710 1.1 joerg void NamedDecl::getNameForDiagnostic(raw_ostream &OS, 1711 1.1 joerg const PrintingPolicy &Policy, 1712 1.1 joerg bool Qualified) const { 1713 1.1 joerg if (Qualified) 1714 1.1 joerg printQualifiedName(OS, Policy); 1715 1.1 joerg else 1716 1.1 joerg printName(OS); 1717 1.1 joerg } 1718 1.1 joerg 1719 1.1 joerg template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { 1720 1.1 joerg return true; 1721 1.1 joerg } 1722 1.1 joerg static bool isRedeclarableImpl(...) { return false; } 1723 1.1 joerg static bool isRedeclarable(Decl::Kind K) { 1724 1.1 joerg switch (K) { 1725 1.1 joerg #define DECL(Type, Base) \ 1726 1.1 joerg case Decl::Type: \ 1727 1.1 joerg return isRedeclarableImpl((Type##Decl *)nullptr); 1728 1.1 joerg #define ABSTRACT_DECL(DECL) 1729 1.1 joerg #include "clang/AST/DeclNodes.inc" 1730 1.1 joerg } 1731 1.1 joerg llvm_unreachable("unknown decl kind"); 1732 1.1 joerg } 1733 1.1 joerg 1734 1.1 joerg bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { 1735 1.1 joerg assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 1736 1.1 joerg 1737 1.1 joerg // Never replace one imported declaration with another; we need both results 1738 1.1 joerg // when re-exporting. 1739 1.1 joerg if (OldD->isFromASTFile() && isFromASTFile()) 1740 1.1 joerg return false; 1741 1.1 joerg 1742 1.1 joerg // A kind mismatch implies that the declaration is not replaced. 1743 1.1 joerg if (OldD->getKind() != getKind()) 1744 1.1 joerg return false; 1745 1.1 joerg 1746 1.1 joerg // For method declarations, we never replace. (Why?) 1747 1.1 joerg if (isa<ObjCMethodDecl>(this)) 1748 1.1 joerg return false; 1749 1.1 joerg 1750 1.1 joerg // For parameters, pick the newer one. This is either an error or (in 1751 1.1 joerg // Objective-C) permitted as an extension. 1752 1.1 joerg if (isa<ParmVarDecl>(this)) 1753 1.1 joerg return true; 1754 1.1 joerg 1755 1.1 joerg // Inline namespaces can give us two declarations with the same 1756 1.1 joerg // name and kind in the same scope but different contexts; we should 1757 1.1 joerg // keep both declarations in this case. 1758 1.1 joerg if (!this->getDeclContext()->getRedeclContext()->Equals( 1759 1.1 joerg OldD->getDeclContext()->getRedeclContext())) 1760 1.1 joerg return false; 1761 1.1 joerg 1762 1.1 joerg // Using declarations can be replaced if they import the same name from the 1763 1.1 joerg // same context. 1764 1.1 joerg if (auto *UD = dyn_cast<UsingDecl>(this)) { 1765 1.1 joerg ASTContext &Context = getASTContext(); 1766 1.1 joerg return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == 1767 1.1 joerg Context.getCanonicalNestedNameSpecifier( 1768 1.1 joerg cast<UsingDecl>(OldD)->getQualifier()); 1769 1.1 joerg } 1770 1.1 joerg if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { 1771 1.1 joerg ASTContext &Context = getASTContext(); 1772 1.1 joerg return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == 1773 1.1 joerg Context.getCanonicalNestedNameSpecifier( 1774 1.1 joerg cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); 1775 1.1 joerg } 1776 1.1 joerg 1777 1.1 joerg if (isRedeclarable(getKind())) { 1778 1.1 joerg if (getCanonicalDecl() != OldD->getCanonicalDecl()) 1779 1.1 joerg return false; 1780 1.1 joerg 1781 1.1 joerg if (IsKnownNewer) 1782 1.1 joerg return true; 1783 1.1 joerg 1784 1.1 joerg // Check whether this is actually newer than OldD. We want to keep the 1785 1.1 joerg // newer declaration. This loop will usually only iterate once, because 1786 1.1 joerg // OldD is usually the previous declaration. 1787 1.1 joerg for (auto D : redecls()) { 1788 1.1 joerg if (D == OldD) 1789 1.1 joerg break; 1790 1.1 joerg 1791 1.1 joerg // If we reach the canonical declaration, then OldD is not actually older 1792 1.1 joerg // than this one. 1793 1.1 joerg // 1794 1.1 joerg // FIXME: In this case, we should not add this decl to the lookup table. 1795 1.1 joerg if (D->isCanonicalDecl()) 1796 1.1 joerg return false; 1797 1.1 joerg } 1798 1.1 joerg 1799 1.1 joerg // It's a newer declaration of the same kind of declaration in the same 1800 1.1 joerg // scope: we want this decl instead of the existing one. 1801 1.1 joerg return true; 1802 1.1 joerg } 1803 1.1 joerg 1804 1.1 joerg // In all other cases, we need to keep both declarations in case they have 1805 1.1 joerg // different visibility. Any attempt to use the name will result in an 1806 1.1 joerg // ambiguity if more than one is visible. 1807 1.1 joerg return false; 1808 1.1 joerg } 1809 1.1 joerg 1810 1.1 joerg bool NamedDecl::hasLinkage() const { 1811 1.1 joerg return getFormalLinkage() != NoLinkage; 1812 1.1 joerg } 1813 1.1 joerg 1814 1.1 joerg NamedDecl *NamedDecl::getUnderlyingDeclImpl() { 1815 1.1 joerg NamedDecl *ND = this; 1816 1.1 joerg while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) 1817 1.1 joerg ND = UD->getTargetDecl(); 1818 1.1 joerg 1819 1.1 joerg if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 1820 1.1 joerg return AD->getClassInterface(); 1821 1.1 joerg 1822 1.1 joerg if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) 1823 1.1 joerg return AD->getNamespace(); 1824 1.1 joerg 1825 1.1 joerg return ND; 1826 1.1 joerg } 1827 1.1 joerg 1828 1.1 joerg bool NamedDecl::isCXXInstanceMember() const { 1829 1.1 joerg if (!isCXXClassMember()) 1830 1.1 joerg return false; 1831 1.1 joerg 1832 1.1 joerg const NamedDecl *D = this; 1833 1.1 joerg if (isa<UsingShadowDecl>(D)) 1834 1.1 joerg D = cast<UsingShadowDecl>(D)->getTargetDecl(); 1835 1.1 joerg 1836 1.1 joerg if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) 1837 1.1 joerg return true; 1838 1.1 joerg if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) 1839 1.1 joerg return MD->isInstance(); 1840 1.1 joerg return false; 1841 1.1 joerg } 1842 1.1 joerg 1843 1.1 joerg //===----------------------------------------------------------------------===// 1844 1.1 joerg // DeclaratorDecl Implementation 1845 1.1 joerg //===----------------------------------------------------------------------===// 1846 1.1 joerg 1847 1.1 joerg template <typename DeclT> 1848 1.1 joerg static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { 1849 1.1 joerg if (decl->getNumTemplateParameterLists() > 0) 1850 1.1 joerg return decl->getTemplateParameterList(0)->getTemplateLoc(); 1851 1.1.1.2 joerg return decl->getInnerLocStart(); 1852 1.1 joerg } 1853 1.1 joerg 1854 1.1 joerg SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 1855 1.1 joerg TypeSourceInfo *TSI = getTypeSourceInfo(); 1856 1.1 joerg if (TSI) return TSI->getTypeLoc().getBeginLoc(); 1857 1.1 joerg return SourceLocation(); 1858 1.1 joerg } 1859 1.1 joerg 1860 1.1.1.2 joerg SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { 1861 1.1.1.2 joerg TypeSourceInfo *TSI = getTypeSourceInfo(); 1862 1.1.1.2 joerg if (TSI) return TSI->getTypeLoc().getEndLoc(); 1863 1.1.1.2 joerg return SourceLocation(); 1864 1.1.1.2 joerg } 1865 1.1.1.2 joerg 1866 1.1 joerg void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 1867 1.1 joerg if (QualifierLoc) { 1868 1.1 joerg // Make sure the extended decl info is allocated. 1869 1.1 joerg if (!hasExtInfo()) { 1870 1.1 joerg // Save (non-extended) type source info pointer. 1871 1.1 joerg auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 1872 1.1 joerg // Allocate external info struct. 1873 1.1 joerg DeclInfo = new (getASTContext()) ExtInfo; 1874 1.1 joerg // Restore savedTInfo into (extended) decl info. 1875 1.1 joerg getExtInfo()->TInfo = savedTInfo; 1876 1.1 joerg } 1877 1.1 joerg // Set qualifier info. 1878 1.1 joerg getExtInfo()->QualifierLoc = QualifierLoc; 1879 1.1.1.2 joerg } else if (hasExtInfo()) { 1880 1.1 joerg // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 1881 1.1.1.2 joerg getExtInfo()->QualifierLoc = QualifierLoc; 1882 1.1.1.2 joerg } 1883 1.1.1.2 joerg } 1884 1.1.1.2 joerg 1885 1.1.1.2 joerg void DeclaratorDecl::setTrailingRequiresClause(Expr *TrailingRequiresClause) { 1886 1.1.1.2 joerg assert(TrailingRequiresClause); 1887 1.1.1.2 joerg // Make sure the extended decl info is allocated. 1888 1.1.1.2 joerg if (!hasExtInfo()) { 1889 1.1.1.2 joerg // Save (non-extended) type source info pointer. 1890 1.1.1.2 joerg auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 1891 1.1.1.2 joerg // Allocate external info struct. 1892 1.1.1.2 joerg DeclInfo = new (getASTContext()) ExtInfo; 1893 1.1.1.2 joerg // Restore savedTInfo into (extended) decl info. 1894 1.1.1.2 joerg getExtInfo()->TInfo = savedTInfo; 1895 1.1 joerg } 1896 1.1.1.2 joerg // Set requires clause info. 1897 1.1.1.2 joerg getExtInfo()->TrailingRequiresClause = TrailingRequiresClause; 1898 1.1 joerg } 1899 1.1 joerg 1900 1.1 joerg void DeclaratorDecl::setTemplateParameterListsInfo( 1901 1.1 joerg ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 1902 1.1 joerg assert(!TPLists.empty()); 1903 1.1 joerg // Make sure the extended decl info is allocated. 1904 1.1 joerg if (!hasExtInfo()) { 1905 1.1 joerg // Save (non-extended) type source info pointer. 1906 1.1 joerg auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 1907 1.1 joerg // Allocate external info struct. 1908 1.1 joerg DeclInfo = new (getASTContext()) ExtInfo; 1909 1.1 joerg // Restore savedTInfo into (extended) decl info. 1910 1.1 joerg getExtInfo()->TInfo = savedTInfo; 1911 1.1 joerg } 1912 1.1 joerg // Set the template parameter lists info. 1913 1.1 joerg getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); 1914 1.1 joerg } 1915 1.1 joerg 1916 1.1 joerg SourceLocation DeclaratorDecl::getOuterLocStart() const { 1917 1.1 joerg return getTemplateOrInnerLocStart(this); 1918 1.1 joerg } 1919 1.1 joerg 1920 1.1 joerg // Helper function: returns true if QT is or contains a type 1921 1.1 joerg // having a postfix component. 1922 1.1 joerg static bool typeIsPostfix(QualType QT) { 1923 1.1 joerg while (true) { 1924 1.1 joerg const Type* T = QT.getTypePtr(); 1925 1.1 joerg switch (T->getTypeClass()) { 1926 1.1 joerg default: 1927 1.1 joerg return false; 1928 1.1 joerg case Type::Pointer: 1929 1.1 joerg QT = cast<PointerType>(T)->getPointeeType(); 1930 1.1 joerg break; 1931 1.1 joerg case Type::BlockPointer: 1932 1.1 joerg QT = cast<BlockPointerType>(T)->getPointeeType(); 1933 1.1 joerg break; 1934 1.1 joerg case Type::MemberPointer: 1935 1.1 joerg QT = cast<MemberPointerType>(T)->getPointeeType(); 1936 1.1 joerg break; 1937 1.1 joerg case Type::LValueReference: 1938 1.1 joerg case Type::RValueReference: 1939 1.1 joerg QT = cast<ReferenceType>(T)->getPointeeType(); 1940 1.1 joerg break; 1941 1.1 joerg case Type::PackExpansion: 1942 1.1 joerg QT = cast<PackExpansionType>(T)->getPattern(); 1943 1.1 joerg break; 1944 1.1 joerg case Type::Paren: 1945 1.1 joerg case Type::ConstantArray: 1946 1.1 joerg case Type::DependentSizedArray: 1947 1.1 joerg case Type::IncompleteArray: 1948 1.1 joerg case Type::VariableArray: 1949 1.1 joerg case Type::FunctionProto: 1950 1.1 joerg case Type::FunctionNoProto: 1951 1.1 joerg return true; 1952 1.1 joerg } 1953 1.1 joerg } 1954 1.1 joerg } 1955 1.1 joerg 1956 1.1 joerg SourceRange DeclaratorDecl::getSourceRange() const { 1957 1.1 joerg SourceLocation RangeEnd = getLocation(); 1958 1.1 joerg if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 1959 1.1 joerg // If the declaration has no name or the type extends past the name take the 1960 1.1 joerg // end location of the type. 1961 1.1 joerg if (!getDeclName() || typeIsPostfix(TInfo->getType())) 1962 1.1 joerg RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 1963 1.1 joerg } 1964 1.1 joerg return SourceRange(getOuterLocStart(), RangeEnd); 1965 1.1 joerg } 1966 1.1 joerg 1967 1.1 joerg void QualifierInfo::setTemplateParameterListsInfo( 1968 1.1 joerg ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 1969 1.1 joerg // Free previous template parameters (if any). 1970 1.1 joerg if (NumTemplParamLists > 0) { 1971 1.1 joerg Context.Deallocate(TemplParamLists); 1972 1.1 joerg TemplParamLists = nullptr; 1973 1.1 joerg NumTemplParamLists = 0; 1974 1.1 joerg } 1975 1.1 joerg // Set info on matched template parameter lists (if any). 1976 1.1 joerg if (!TPLists.empty()) { 1977 1.1 joerg TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; 1978 1.1 joerg NumTemplParamLists = TPLists.size(); 1979 1.1 joerg std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); 1980 1.1 joerg } 1981 1.1 joerg } 1982 1.1 joerg 1983 1.1 joerg //===----------------------------------------------------------------------===// 1984 1.1 joerg // VarDecl Implementation 1985 1.1 joerg //===----------------------------------------------------------------------===// 1986 1.1 joerg 1987 1.1 joerg const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 1988 1.1 joerg switch (SC) { 1989 1.1 joerg case SC_None: break; 1990 1.1 joerg case SC_Auto: return "auto"; 1991 1.1 joerg case SC_Extern: return "extern"; 1992 1.1 joerg case SC_PrivateExtern: return "__private_extern__"; 1993 1.1 joerg case SC_Register: return "register"; 1994 1.1 joerg case SC_Static: return "static"; 1995 1.1 joerg } 1996 1.1 joerg 1997 1.1 joerg llvm_unreachable("Invalid storage class"); 1998 1.1 joerg } 1999 1.1 joerg 2000 1.1 joerg VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, 2001 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc, 2002 1.1 joerg IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 2003 1.1 joerg StorageClass SC) 2004 1.1 joerg : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), 2005 1.1 joerg redeclarable_base(C) { 2006 1.1 joerg static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), 2007 1.1 joerg "VarDeclBitfields too large!"); 2008 1.1 joerg static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), 2009 1.1 joerg "ParmVarDeclBitfields too large!"); 2010 1.1 joerg static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), 2011 1.1 joerg "NonParmVarDeclBitfields too large!"); 2012 1.1 joerg AllBits = 0; 2013 1.1 joerg VarDeclBits.SClass = SC; 2014 1.1 joerg // Everything else is implicitly initialized to false. 2015 1.1 joerg } 2016 1.1 joerg 2017 1.1 joerg VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, 2018 1.1 joerg SourceLocation StartL, SourceLocation IdL, 2019 1.1 joerg IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 2020 1.1 joerg StorageClass S) { 2021 1.1 joerg return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); 2022 1.1 joerg } 2023 1.1 joerg 2024 1.1 joerg VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 2025 1.1 joerg return new (C, ID) 2026 1.1 joerg VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, 2027 1.1 joerg QualType(), nullptr, SC_None); 2028 1.1 joerg } 2029 1.1 joerg 2030 1.1 joerg void VarDecl::setStorageClass(StorageClass SC) { 2031 1.1 joerg assert(isLegalForVariable(SC)); 2032 1.1 joerg VarDeclBits.SClass = SC; 2033 1.1 joerg } 2034 1.1 joerg 2035 1.1 joerg VarDecl::TLSKind VarDecl::getTLSKind() const { 2036 1.1 joerg switch (VarDeclBits.TSCSpec) { 2037 1.1 joerg case TSCS_unspecified: 2038 1.1 joerg if (!hasAttr<ThreadAttr>() && 2039 1.1 joerg !(getASTContext().getLangOpts().OpenMPUseTLS && 2040 1.1 joerg getASTContext().getTargetInfo().isTLSSupported() && 2041 1.1 joerg hasAttr<OMPThreadPrivateDeclAttr>())) 2042 1.1 joerg return TLS_None; 2043 1.1 joerg return ((getASTContext().getLangOpts().isCompatibleWithMSVC( 2044 1.1 joerg LangOptions::MSVC2015)) || 2045 1.1 joerg hasAttr<OMPThreadPrivateDeclAttr>()) 2046 1.1 joerg ? TLS_Dynamic 2047 1.1 joerg : TLS_Static; 2048 1.1 joerg case TSCS___thread: // Fall through. 2049 1.1 joerg case TSCS__Thread_local: 2050 1.1 joerg return TLS_Static; 2051 1.1 joerg case TSCS_thread_local: 2052 1.1 joerg return TLS_Dynamic; 2053 1.1 joerg } 2054 1.1 joerg llvm_unreachable("Unknown thread storage class specifier!"); 2055 1.1 joerg } 2056 1.1 joerg 2057 1.1 joerg SourceRange VarDecl::getSourceRange() const { 2058 1.1 joerg if (const Expr *Init = getInit()) { 2059 1.1 joerg SourceLocation InitEnd = Init->getEndLoc(); 2060 1.1 joerg // If Init is implicit, ignore its source range and fallback on 2061 1.1 joerg // DeclaratorDecl::getSourceRange() to handle postfix elements. 2062 1.1 joerg if (InitEnd.isValid() && InitEnd != getLocation()) 2063 1.1 joerg return SourceRange(getOuterLocStart(), InitEnd); 2064 1.1 joerg } 2065 1.1 joerg return DeclaratorDecl::getSourceRange(); 2066 1.1 joerg } 2067 1.1 joerg 2068 1.1 joerg template<typename T> 2069 1.1 joerg static LanguageLinkage getDeclLanguageLinkage(const T &D) { 2070 1.1 joerg // C++ [dcl.link]p1: All function types, function names with external linkage, 2071 1.1 joerg // and variable names with external linkage have a language linkage. 2072 1.1 joerg if (!D.hasExternalFormalLinkage()) 2073 1.1 joerg return NoLanguageLinkage; 2074 1.1 joerg 2075 1.1 joerg // Language linkage is a C++ concept, but saying that everything else in C has 2076 1.1 joerg // C language linkage fits the implementation nicely. 2077 1.1 joerg ASTContext &Context = D.getASTContext(); 2078 1.1 joerg if (!Context.getLangOpts().CPlusPlus) 2079 1.1 joerg return CLanguageLinkage; 2080 1.1 joerg 2081 1.1 joerg // C++ [dcl.link]p4: A C language linkage is ignored in determining the 2082 1.1 joerg // language linkage of the names of class members and the function type of 2083 1.1 joerg // class member functions. 2084 1.1 joerg const DeclContext *DC = D.getDeclContext(); 2085 1.1 joerg if (DC->isRecord()) 2086 1.1 joerg return CXXLanguageLinkage; 2087 1.1 joerg 2088 1.1 joerg // If the first decl is in an extern "C" context, any other redeclaration 2089 1.1 joerg // will have C language linkage. If the first one is not in an extern "C" 2090 1.1 joerg // context, we would have reported an error for any other decl being in one. 2091 1.1 joerg if (isFirstInExternCContext(&D)) 2092 1.1 joerg return CLanguageLinkage; 2093 1.1 joerg return CXXLanguageLinkage; 2094 1.1 joerg } 2095 1.1 joerg 2096 1.1 joerg template<typename T> 2097 1.1 joerg static bool isDeclExternC(const T &D) { 2098 1.1 joerg // Since the context is ignored for class members, they can only have C++ 2099 1.1 joerg // language linkage or no language linkage. 2100 1.1 joerg const DeclContext *DC = D.getDeclContext(); 2101 1.1 joerg if (DC->isRecord()) { 2102 1.1 joerg assert(D.getASTContext().getLangOpts().CPlusPlus); 2103 1.1 joerg return false; 2104 1.1 joerg } 2105 1.1 joerg 2106 1.1 joerg return D.getLanguageLinkage() == CLanguageLinkage; 2107 1.1 joerg } 2108 1.1 joerg 2109 1.1 joerg LanguageLinkage VarDecl::getLanguageLinkage() const { 2110 1.1 joerg return getDeclLanguageLinkage(*this); 2111 1.1 joerg } 2112 1.1 joerg 2113 1.1 joerg bool VarDecl::isExternC() const { 2114 1.1 joerg return isDeclExternC(*this); 2115 1.1 joerg } 2116 1.1 joerg 2117 1.1 joerg bool VarDecl::isInExternCContext() const { 2118 1.1 joerg return getLexicalDeclContext()->isExternCContext(); 2119 1.1 joerg } 2120 1.1 joerg 2121 1.1 joerg bool VarDecl::isInExternCXXContext() const { 2122 1.1 joerg return getLexicalDeclContext()->isExternCXXContext(); 2123 1.1 joerg } 2124 1.1 joerg 2125 1.1 joerg VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } 2126 1.1 joerg 2127 1.1 joerg VarDecl::DefinitionKind 2128 1.1 joerg VarDecl::isThisDeclarationADefinition(ASTContext &C) const { 2129 1.1 joerg if (isThisDeclarationADemotedDefinition()) 2130 1.1 joerg return DeclarationOnly; 2131 1.1 joerg 2132 1.1 joerg // C++ [basic.def]p2: 2133 1.1 joerg // A declaration is a definition unless [...] it contains the 'extern' 2134 1.1 joerg // specifier or a linkage-specification and neither an initializer [...], 2135 1.1 joerg // it declares a non-inline static data member in a class declaration [...], 2136 1.1 joerg // it declares a static data member outside a class definition and the variable 2137 1.1 joerg // was defined within the class with the constexpr specifier [...], 2138 1.1 joerg // C++1y [temp.expl.spec]p15: 2139 1.1 joerg // An explicit specialization of a static data member or an explicit 2140 1.1 joerg // specialization of a static data member template is a definition if the 2141 1.1 joerg // declaration includes an initializer; otherwise, it is a declaration. 2142 1.1 joerg // 2143 1.1 joerg // FIXME: How do you declare (but not define) a partial specialization of 2144 1.1 joerg // a static data member template outside the containing class? 2145 1.1 joerg if (isStaticDataMember()) { 2146 1.1 joerg if (isOutOfLine() && 2147 1.1 joerg !(getCanonicalDecl()->isInline() && 2148 1.1 joerg getCanonicalDecl()->isConstexpr()) && 2149 1.1 joerg (hasInit() || 2150 1.1 joerg // If the first declaration is out-of-line, this may be an 2151 1.1 joerg // instantiation of an out-of-line partial specialization of a variable 2152 1.1 joerg // template for which we have not yet instantiated the initializer. 2153 1.1 joerg (getFirstDecl()->isOutOfLine() 2154 1.1 joerg ? getTemplateSpecializationKind() == TSK_Undeclared 2155 1.1 joerg : getTemplateSpecializationKind() != 2156 1.1 joerg TSK_ExplicitSpecialization) || 2157 1.1 joerg isa<VarTemplatePartialSpecializationDecl>(this))) 2158 1.1 joerg return Definition; 2159 1.1.1.2 joerg if (!isOutOfLine() && isInline()) 2160 1.1 joerg return Definition; 2161 1.1.1.2 joerg return DeclarationOnly; 2162 1.1 joerg } 2163 1.1 joerg // C99 6.7p5: 2164 1.1 joerg // A definition of an identifier is a declaration for that identifier that 2165 1.1 joerg // [...] causes storage to be reserved for that object. 2166 1.1 joerg // Note: that applies for all non-file-scope objects. 2167 1.1 joerg // C99 6.9.2p1: 2168 1.1 joerg // If the declaration of an identifier for an object has file scope and an 2169 1.1 joerg // initializer, the declaration is an external definition for the identifier 2170 1.1 joerg if (hasInit()) 2171 1.1 joerg return Definition; 2172 1.1 joerg 2173 1.1 joerg if (hasDefiningAttr()) 2174 1.1 joerg return Definition; 2175 1.1 joerg 2176 1.1 joerg if (const auto *SAA = getAttr<SelectAnyAttr>()) 2177 1.1 joerg if (!SAA->isInherited()) 2178 1.1 joerg return Definition; 2179 1.1 joerg 2180 1.1 joerg // A variable template specialization (other than a static data member 2181 1.1 joerg // template or an explicit specialization) is a declaration until we 2182 1.1 joerg // instantiate its initializer. 2183 1.1 joerg if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { 2184 1.1 joerg if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && 2185 1.1 joerg !isa<VarTemplatePartialSpecializationDecl>(VTSD) && 2186 1.1 joerg !VTSD->IsCompleteDefinition) 2187 1.1 joerg return DeclarationOnly; 2188 1.1 joerg } 2189 1.1 joerg 2190 1.1 joerg if (hasExternalStorage()) 2191 1.1 joerg return DeclarationOnly; 2192 1.1 joerg 2193 1.1 joerg // [dcl.link] p7: 2194 1.1 joerg // A declaration directly contained in a linkage-specification is treated 2195 1.1 joerg // as if it contains the extern specifier for the purpose of determining 2196 1.1 joerg // the linkage of the declared name and whether it is a definition. 2197 1.1 joerg if (isSingleLineLanguageLinkage(*this)) 2198 1.1 joerg return DeclarationOnly; 2199 1.1 joerg 2200 1.1 joerg // C99 6.9.2p2: 2201 1.1 joerg // A declaration of an object that has file scope without an initializer, 2202 1.1 joerg // and without a storage class specifier or the scs 'static', constitutes 2203 1.1 joerg // a tentative definition. 2204 1.1 joerg // No such thing in C++. 2205 1.1 joerg if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) 2206 1.1 joerg return TentativeDefinition; 2207 1.1 joerg 2208 1.1 joerg // What's left is (in C, block-scope) declarations without initializers or 2209 1.1 joerg // external storage. These are definitions. 2210 1.1 joerg return Definition; 2211 1.1 joerg } 2212 1.1 joerg 2213 1.1 joerg VarDecl *VarDecl::getActingDefinition() { 2214 1.1 joerg DefinitionKind Kind = isThisDeclarationADefinition(); 2215 1.1 joerg if (Kind != TentativeDefinition) 2216 1.1 joerg return nullptr; 2217 1.1 joerg 2218 1.1 joerg VarDecl *LastTentative = nullptr; 2219 1.1 joerg VarDecl *First = getFirstDecl(); 2220 1.1 joerg for (auto I : First->redecls()) { 2221 1.1 joerg Kind = I->isThisDeclarationADefinition(); 2222 1.1 joerg if (Kind == Definition) 2223 1.1 joerg return nullptr; 2224 1.1.1.2 joerg if (Kind == TentativeDefinition) 2225 1.1 joerg LastTentative = I; 2226 1.1 joerg } 2227 1.1 joerg return LastTentative; 2228 1.1 joerg } 2229 1.1 joerg 2230 1.1 joerg VarDecl *VarDecl::getDefinition(ASTContext &C) { 2231 1.1 joerg VarDecl *First = getFirstDecl(); 2232 1.1 joerg for (auto I : First->redecls()) { 2233 1.1 joerg if (I->isThisDeclarationADefinition(C) == Definition) 2234 1.1 joerg return I; 2235 1.1 joerg } 2236 1.1 joerg return nullptr; 2237 1.1 joerg } 2238 1.1 joerg 2239 1.1 joerg VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { 2240 1.1 joerg DefinitionKind Kind = DeclarationOnly; 2241 1.1 joerg 2242 1.1 joerg const VarDecl *First = getFirstDecl(); 2243 1.1 joerg for (auto I : First->redecls()) { 2244 1.1 joerg Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); 2245 1.1 joerg if (Kind == Definition) 2246 1.1 joerg break; 2247 1.1 joerg } 2248 1.1 joerg 2249 1.1 joerg return Kind; 2250 1.1 joerg } 2251 1.1 joerg 2252 1.1 joerg const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { 2253 1.1 joerg for (auto I : redecls()) { 2254 1.1 joerg if (auto Expr = I->getInit()) { 2255 1.1 joerg D = I; 2256 1.1 joerg return Expr; 2257 1.1 joerg } 2258 1.1 joerg } 2259 1.1 joerg return nullptr; 2260 1.1 joerg } 2261 1.1 joerg 2262 1.1 joerg bool VarDecl::hasInit() const { 2263 1.1 joerg if (auto *P = dyn_cast<ParmVarDecl>(this)) 2264 1.1 joerg if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) 2265 1.1 joerg return false; 2266 1.1 joerg 2267 1.1 joerg return !Init.isNull(); 2268 1.1 joerg } 2269 1.1 joerg 2270 1.1 joerg Expr *VarDecl::getInit() { 2271 1.1 joerg if (!hasInit()) 2272 1.1 joerg return nullptr; 2273 1.1 joerg 2274 1.1 joerg if (auto *S = Init.dyn_cast<Stmt *>()) 2275 1.1 joerg return cast<Expr>(S); 2276 1.1 joerg 2277 1.1 joerg return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value); 2278 1.1 joerg } 2279 1.1 joerg 2280 1.1 joerg Stmt **VarDecl::getInitAddress() { 2281 1.1 joerg if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) 2282 1.1 joerg return &ES->Value; 2283 1.1 joerg 2284 1.1 joerg return Init.getAddrOfPtr1(); 2285 1.1 joerg } 2286 1.1 joerg 2287 1.1 joerg VarDecl *VarDecl::getInitializingDeclaration() { 2288 1.1 joerg VarDecl *Def = nullptr; 2289 1.1 joerg for (auto I : redecls()) { 2290 1.1 joerg if (I->hasInit()) 2291 1.1 joerg return I; 2292 1.1 joerg 2293 1.1 joerg if (I->isThisDeclarationADefinition()) { 2294 1.1 joerg if (isStaticDataMember()) 2295 1.1 joerg return I; 2296 1.1.1.2 joerg Def = I; 2297 1.1 joerg } 2298 1.1 joerg } 2299 1.1 joerg return Def; 2300 1.1 joerg } 2301 1.1 joerg 2302 1.1 joerg bool VarDecl::isOutOfLine() const { 2303 1.1 joerg if (Decl::isOutOfLine()) 2304 1.1 joerg return true; 2305 1.1 joerg 2306 1.1 joerg if (!isStaticDataMember()) 2307 1.1 joerg return false; 2308 1.1 joerg 2309 1.1 joerg // If this static data member was instantiated from a static data member of 2310 1.1 joerg // a class template, check whether that static data member was defined 2311 1.1 joerg // out-of-line. 2312 1.1 joerg if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 2313 1.1 joerg return VD->isOutOfLine(); 2314 1.1 joerg 2315 1.1 joerg return false; 2316 1.1 joerg } 2317 1.1 joerg 2318 1.1 joerg void VarDecl::setInit(Expr *I) { 2319 1.1 joerg if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 2320 1.1 joerg Eval->~EvaluatedStmt(); 2321 1.1 joerg getASTContext().Deallocate(Eval); 2322 1.1 joerg } 2323 1.1 joerg 2324 1.1 joerg Init = I; 2325 1.1 joerg } 2326 1.1 joerg 2327 1.1.1.2 joerg bool VarDecl::mightBeUsableInConstantExpressions(const ASTContext &C) const { 2328 1.1 joerg const LangOptions &Lang = C.getLangOpts(); 2329 1.1 joerg 2330 1.1.1.2 joerg // OpenCL permits const integral variables to be used in constant 2331 1.1.1.2 joerg // expressions, like in C++98. 2332 1.1.1.2 joerg if (!Lang.CPlusPlus && !Lang.OpenCL) 2333 1.1 joerg return false; 2334 1.1 joerg 2335 1.1 joerg // Function parameters are never usable in constant expressions. 2336 1.1 joerg if (isa<ParmVarDecl>(this)) 2337 1.1 joerg return false; 2338 1.1 joerg 2339 1.1.1.2 joerg // The values of weak variables are never usable in constant expressions. 2340 1.1.1.2 joerg if (isWeak()) 2341 1.1.1.2 joerg return false; 2342 1.1.1.2 joerg 2343 1.1 joerg // In C++11, any variable of reference type can be used in a constant 2344 1.1 joerg // expression if it is initialized by a constant expression. 2345 1.1 joerg if (Lang.CPlusPlus11 && getType()->isReferenceType()) 2346 1.1 joerg return true; 2347 1.1 joerg 2348 1.1 joerg // Only const objects can be used in constant expressions in C++. C++98 does 2349 1.1 joerg // not require the variable to be non-volatile, but we consider this to be a 2350 1.1 joerg // defect. 2351 1.1.1.2 joerg if (!getType().isConstant(C) || getType().isVolatileQualified()) 2352 1.1 joerg return false; 2353 1.1 joerg 2354 1.1 joerg // In C++, const, non-volatile variables of integral or enumeration types 2355 1.1 joerg // can be used in constant expressions. 2356 1.1 joerg if (getType()->isIntegralOrEnumerationType()) 2357 1.1 joerg return true; 2358 1.1 joerg 2359 1.1 joerg // Additionally, in C++11, non-volatile constexpr variables can be used in 2360 1.1 joerg // constant expressions. 2361 1.1 joerg return Lang.CPlusPlus11 && isConstexpr(); 2362 1.1 joerg } 2363 1.1 joerg 2364 1.1.1.2 joerg bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const { 2365 1.1 joerg // C++2a [expr.const]p3: 2366 1.1 joerg // A variable is usable in constant expressions after its initializing 2367 1.1 joerg // declaration is encountered... 2368 1.1 joerg const VarDecl *DefVD = nullptr; 2369 1.1 joerg const Expr *Init = getAnyInitializer(DefVD); 2370 1.1 joerg if (!Init || Init->isValueDependent() || getType()->isDependentType()) 2371 1.1 joerg return false; 2372 1.1 joerg // ... if it is a constexpr variable, or it is of reference type or of 2373 1.1 joerg // const-qualified integral or enumeration type, ... 2374 1.1 joerg if (!DefVD->mightBeUsableInConstantExpressions(Context)) 2375 1.1 joerg return false; 2376 1.1 joerg // ... and its initializer is a constant initializer. 2377 1.1.1.2 joerg if (Context.getLangOpts().CPlusPlus && !DefVD->hasConstantInitialization()) 2378 1.1.1.2 joerg return false; 2379 1.1.1.2 joerg // C++98 [expr.const]p1: 2380 1.1.1.2 joerg // An integral constant-expression can involve only [...] const variables 2381 1.1.1.2 joerg // or static data members of integral or enumeration types initialized with 2382 1.1.1.2 joerg // [integer] constant expressions (dcl.init) 2383 1.1.1.2 joerg if ((Context.getLangOpts().CPlusPlus || Context.getLangOpts().OpenCL) && 2384 1.1.1.2 joerg !Context.getLangOpts().CPlusPlus11 && !DefVD->hasICEInitializer(Context)) 2385 1.1.1.2 joerg return false; 2386 1.1.1.2 joerg return true; 2387 1.1 joerg } 2388 1.1 joerg 2389 1.1 joerg /// Convert the initializer for this declaration to the elaborated EvaluatedStmt 2390 1.1 joerg /// form, which contains extra information on the evaluated value of the 2391 1.1 joerg /// initializer. 2392 1.1 joerg EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { 2393 1.1 joerg auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); 2394 1.1 joerg if (!Eval) { 2395 1.1 joerg // Note: EvaluatedStmt contains an APValue, which usually holds 2396 1.1 joerg // resources not allocated from the ASTContext. We need to do some 2397 1.1 joerg // work to avoid leaking those, but we do so in VarDecl::evaluateValue 2398 1.1 joerg // where we can detect whether there's anything to clean up or not. 2399 1.1 joerg Eval = new (getASTContext()) EvaluatedStmt; 2400 1.1 joerg Eval->Value = Init.get<Stmt *>(); 2401 1.1 joerg Init = Eval; 2402 1.1 joerg } 2403 1.1 joerg return Eval; 2404 1.1 joerg } 2405 1.1 joerg 2406 1.1.1.2 joerg EvaluatedStmt *VarDecl::getEvaluatedStmt() const { 2407 1.1.1.2 joerg return Init.dyn_cast<EvaluatedStmt *>(); 2408 1.1.1.2 joerg } 2409 1.1.1.2 joerg 2410 1.1 joerg APValue *VarDecl::evaluateValue() const { 2411 1.1 joerg SmallVector<PartialDiagnosticAt, 8> Notes; 2412 1.1.1.2 joerg return evaluateValueImpl(Notes, hasConstantInitialization()); 2413 1.1 joerg } 2414 1.1 joerg 2415 1.1.1.2 joerg APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, 2416 1.1.1.2 joerg bool IsConstantInitialization) const { 2417 1.1 joerg EvaluatedStmt *Eval = ensureEvaluatedStmt(); 2418 1.1 joerg 2419 1.1.1.2 joerg const auto *Init = cast<Expr>(Eval->Value); 2420 1.1.1.2 joerg assert(!Init->isValueDependent()); 2421 1.1.1.2 joerg 2422 1.1 joerg // We only produce notes indicating why an initializer is non-constant the 2423 1.1 joerg // first time it is evaluated. FIXME: The notes won't always be emitted the 2424 1.1 joerg // first time we try evaluation, so might not be produced at all. 2425 1.1 joerg if (Eval->WasEvaluated) 2426 1.1 joerg return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; 2427 1.1 joerg 2428 1.1 joerg if (Eval->IsEvaluating) { 2429 1.1 joerg // FIXME: Produce a diagnostic for self-initialization. 2430 1.1 joerg return nullptr; 2431 1.1 joerg } 2432 1.1 joerg 2433 1.1 joerg Eval->IsEvaluating = true; 2434 1.1 joerg 2435 1.1.1.2 joerg ASTContext &Ctx = getASTContext(); 2436 1.1.1.2 joerg bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes, 2437 1.1.1.2 joerg IsConstantInitialization); 2438 1.1.1.2 joerg 2439 1.1.1.2 joerg // In C++11, this isn't a constant initializer if we produced notes. In that 2440 1.1.1.2 joerg // case, we can't keep the result, because it may only be correct under the 2441 1.1.1.2 joerg // assumption that the initializer is a constant context. 2442 1.1.1.2 joerg if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11 && 2443 1.1.1.2 joerg !Notes.empty()) 2444 1.1.1.2 joerg Result = false; 2445 1.1 joerg 2446 1.1 joerg // Ensure the computed APValue is cleaned up later if evaluation succeeded, 2447 1.1 joerg // or that it's empty (so that there's nothing to clean up) if evaluation 2448 1.1 joerg // failed. 2449 1.1 joerg if (!Result) 2450 1.1 joerg Eval->Evaluated = APValue(); 2451 1.1 joerg else if (Eval->Evaluated.needsCleanup()) 2452 1.1.1.2 joerg Ctx.addDestruction(&Eval->Evaluated); 2453 1.1 joerg 2454 1.1 joerg Eval->IsEvaluating = false; 2455 1.1 joerg Eval->WasEvaluated = true; 2456 1.1 joerg 2457 1.1 joerg return Result ? &Eval->Evaluated : nullptr; 2458 1.1 joerg } 2459 1.1 joerg 2460 1.1 joerg APValue *VarDecl::getEvaluatedValue() const { 2461 1.1.1.2 joerg if (EvaluatedStmt *Eval = getEvaluatedStmt()) 2462 1.1 joerg if (Eval->WasEvaluated) 2463 1.1 joerg return &Eval->Evaluated; 2464 1.1 joerg 2465 1.1 joerg return nullptr; 2466 1.1 joerg } 2467 1.1 joerg 2468 1.1.1.2 joerg bool VarDecl::hasICEInitializer(const ASTContext &Context) const { 2469 1.1.1.2 joerg const Expr *Init = getInit(); 2470 1.1.1.2 joerg assert(Init && "no initializer"); 2471 1.1 joerg 2472 1.1.1.2 joerg EvaluatedStmt *Eval = ensureEvaluatedStmt(); 2473 1.1.1.2 joerg if (!Eval->CheckedForICEInit) { 2474 1.1.1.2 joerg Eval->CheckedForICEInit = true; 2475 1.1.1.2 joerg Eval->HasICEInit = Init->isIntegerConstantExpr(Context); 2476 1.1.1.2 joerg } 2477 1.1.1.2 joerg return Eval->HasICEInit; 2478 1.1 joerg } 2479 1.1 joerg 2480 1.1.1.2 joerg bool VarDecl::hasConstantInitialization() const { 2481 1.1.1.2 joerg // In C, all globals (and only globals) have constant initialization. 2482 1.1.1.2 joerg if (hasGlobalStorage() && !getASTContext().getLangOpts().CPlusPlus) 2483 1.1.1.2 joerg return true; 2484 1.1 joerg 2485 1.1.1.2 joerg // In C++, it depends on whether the evaluation at the point of definition 2486 1.1.1.2 joerg // was evaluatable as a constant initializer. 2487 1.1.1.2 joerg if (EvaluatedStmt *Eval = getEvaluatedStmt()) 2488 1.1.1.2 joerg return Eval->HasConstantInitialization; 2489 1.1 joerg 2490 1.1.1.2 joerg return false; 2491 1.1.1.2 joerg } 2492 1.1 joerg 2493 1.1.1.2 joerg bool VarDecl::checkForConstantInitialization( 2494 1.1.1.2 joerg SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 2495 1.1.1.2 joerg EvaluatedStmt *Eval = ensureEvaluatedStmt(); 2496 1.1.1.2 joerg // If we ask for the value before we know whether we have a constant 2497 1.1.1.2 joerg // initializer, we can compute the wrong value (for example, due to 2498 1.1.1.2 joerg // std::is_constant_evaluated()). 2499 1.1.1.2 joerg assert(!Eval->WasEvaluated && 2500 1.1.1.2 joerg "already evaluated var value before checking for constant init"); 2501 1.1.1.2 joerg assert(getASTContext().getLangOpts().CPlusPlus && "only meaningful in C++"); 2502 1.1.1.2 joerg 2503 1.1.1.2 joerg assert(!cast<Expr>(Eval->Value)->isValueDependent()); 2504 1.1.1.2 joerg 2505 1.1.1.2 joerg // Evaluate the initializer to check whether it's a constant expression. 2506 1.1.1.2 joerg Eval->HasConstantInitialization = 2507 1.1.1.2 joerg evaluateValueImpl(Notes, true) && Notes.empty(); 2508 1.1.1.2 joerg 2509 1.1.1.2 joerg // If evaluation as a constant initializer failed, allow re-evaluation as a 2510 1.1.1.2 joerg // non-constant initializer if we later find we want the value. 2511 1.1.1.2 joerg if (!Eval->HasConstantInitialization) 2512 1.1.1.2 joerg Eval->WasEvaluated = false; 2513 1.1 joerg 2514 1.1.1.2 joerg return Eval->HasConstantInitialization; 2515 1.1 joerg } 2516 1.1 joerg 2517 1.1 joerg bool VarDecl::isParameterPack() const { 2518 1.1 joerg return isa<PackExpansionType>(getType()); 2519 1.1 joerg } 2520 1.1 joerg 2521 1.1 joerg template<typename DeclT> 2522 1.1 joerg static DeclT *getDefinitionOrSelf(DeclT *D) { 2523 1.1 joerg assert(D); 2524 1.1 joerg if (auto *Def = D->getDefinition()) 2525 1.1 joerg return Def; 2526 1.1 joerg return D; 2527 1.1 joerg } 2528 1.1 joerg 2529 1.1 joerg bool VarDecl::isEscapingByref() const { 2530 1.1 joerg return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; 2531 1.1 joerg } 2532 1.1 joerg 2533 1.1 joerg bool VarDecl::isNonEscapingByref() const { 2534 1.1 joerg return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; 2535 1.1 joerg } 2536 1.1 joerg 2537 1.1 joerg VarDecl *VarDecl::getTemplateInstantiationPattern() const { 2538 1.1 joerg const VarDecl *VD = this; 2539 1.1 joerg 2540 1.1 joerg // If this is an instantiated member, walk back to the template from which 2541 1.1 joerg // it was instantiated. 2542 1.1 joerg if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { 2543 1.1 joerg if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { 2544 1.1 joerg VD = VD->getInstantiatedFromStaticDataMember(); 2545 1.1 joerg while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) 2546 1.1 joerg VD = NewVD; 2547 1.1 joerg } 2548 1.1 joerg } 2549 1.1 joerg 2550 1.1 joerg // If it's an instantiated variable template specialization, find the 2551 1.1 joerg // template or partial specialization from which it was instantiated. 2552 1.1 joerg if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { 2553 1.1 joerg if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { 2554 1.1 joerg auto From = VDTemplSpec->getInstantiatedFrom(); 2555 1.1 joerg if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { 2556 1.1 joerg while (!VTD->isMemberSpecialization()) { 2557 1.1 joerg auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); 2558 1.1 joerg if (!NewVTD) 2559 1.1 joerg break; 2560 1.1 joerg VTD = NewVTD; 2561 1.1 joerg } 2562 1.1 joerg return getDefinitionOrSelf(VTD->getTemplatedDecl()); 2563 1.1 joerg } 2564 1.1 joerg if (auto *VTPSD = 2565 1.1 joerg From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { 2566 1.1 joerg while (!VTPSD->isMemberSpecialization()) { 2567 1.1 joerg auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); 2568 1.1 joerg if (!NewVTPSD) 2569 1.1 joerg break; 2570 1.1 joerg VTPSD = NewVTPSD; 2571 1.1 joerg } 2572 1.1 joerg return getDefinitionOrSelf<VarDecl>(VTPSD); 2573 1.1 joerg } 2574 1.1 joerg } 2575 1.1 joerg } 2576 1.1 joerg 2577 1.1 joerg // If this is the pattern of a variable template, find where it was 2578 1.1 joerg // instantiated from. FIXME: Is this necessary? 2579 1.1 joerg if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { 2580 1.1 joerg while (!VarTemplate->isMemberSpecialization()) { 2581 1.1 joerg auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); 2582 1.1 joerg if (!NewVT) 2583 1.1 joerg break; 2584 1.1 joerg VarTemplate = NewVT; 2585 1.1 joerg } 2586 1.1 joerg 2587 1.1 joerg return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); 2588 1.1 joerg } 2589 1.1 joerg 2590 1.1 joerg if (VD == this) 2591 1.1 joerg return nullptr; 2592 1.1 joerg return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); 2593 1.1 joerg } 2594 1.1 joerg 2595 1.1 joerg VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 2596 1.1 joerg if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 2597 1.1 joerg return cast<VarDecl>(MSI->getInstantiatedFrom()); 2598 1.1 joerg 2599 1.1 joerg return nullptr; 2600 1.1 joerg } 2601 1.1 joerg 2602 1.1 joerg TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 2603 1.1 joerg if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 2604 1.1 joerg return Spec->getSpecializationKind(); 2605 1.1 joerg 2606 1.1 joerg if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 2607 1.1 joerg return MSI->getTemplateSpecializationKind(); 2608 1.1 joerg 2609 1.1 joerg return TSK_Undeclared; 2610 1.1 joerg } 2611 1.1 joerg 2612 1.1 joerg TemplateSpecializationKind 2613 1.1 joerg VarDecl::getTemplateSpecializationKindForInstantiation() const { 2614 1.1 joerg if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 2615 1.1 joerg return MSI->getTemplateSpecializationKind(); 2616 1.1 joerg 2617 1.1 joerg if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 2618 1.1 joerg return Spec->getSpecializationKind(); 2619 1.1 joerg 2620 1.1 joerg return TSK_Undeclared; 2621 1.1 joerg } 2622 1.1 joerg 2623 1.1 joerg SourceLocation VarDecl::getPointOfInstantiation() const { 2624 1.1 joerg if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 2625 1.1 joerg return Spec->getPointOfInstantiation(); 2626 1.1 joerg 2627 1.1 joerg if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 2628 1.1 joerg return MSI->getPointOfInstantiation(); 2629 1.1 joerg 2630 1.1 joerg return SourceLocation(); 2631 1.1 joerg } 2632 1.1 joerg 2633 1.1 joerg VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { 2634 1.1 joerg return getASTContext().getTemplateOrSpecializationInfo(this) 2635 1.1 joerg .dyn_cast<VarTemplateDecl *>(); 2636 1.1 joerg } 2637 1.1 joerg 2638 1.1 joerg void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { 2639 1.1 joerg getASTContext().setTemplateOrSpecializationInfo(this, Template); 2640 1.1 joerg } 2641 1.1 joerg 2642 1.1 joerg bool VarDecl::isKnownToBeDefined() const { 2643 1.1 joerg const auto &LangOpts = getASTContext().getLangOpts(); 2644 1.1 joerg // In CUDA mode without relocatable device code, variables of form 'extern 2645 1.1 joerg // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared 2646 1.1 joerg // memory pool. These are never undefined variables, even if they appear 2647 1.1 joerg // inside of an anon namespace or static function. 2648 1.1 joerg // 2649 1.1 joerg // With CUDA relocatable device code enabled, these variables don't get 2650 1.1 joerg // special handling; they're treated like regular extern variables. 2651 1.1 joerg if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && 2652 1.1 joerg hasExternalStorage() && hasAttr<CUDASharedAttr>() && 2653 1.1 joerg isa<IncompleteArrayType>(getType())) 2654 1.1 joerg return true; 2655 1.1 joerg 2656 1.1 joerg return hasDefinition(); 2657 1.1 joerg } 2658 1.1 joerg 2659 1.1 joerg bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { 2660 1.1 joerg return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || 2661 1.1 joerg (!Ctx.getLangOpts().RegisterStaticDestructors && 2662 1.1 joerg !hasAttr<AlwaysDestroyAttr>())); 2663 1.1 joerg } 2664 1.1 joerg 2665 1.1 joerg QualType::DestructionKind 2666 1.1 joerg VarDecl::needsDestruction(const ASTContext &Ctx) const { 2667 1.1.1.2 joerg if (EvaluatedStmt *Eval = getEvaluatedStmt()) 2668 1.1 joerg if (Eval->HasConstantDestruction) 2669 1.1 joerg return QualType::DK_none; 2670 1.1 joerg 2671 1.1 joerg if (isNoDestroy(Ctx)) 2672 1.1 joerg return QualType::DK_none; 2673 1.1 joerg 2674 1.1 joerg return getType().isDestructedType(); 2675 1.1 joerg } 2676 1.1 joerg 2677 1.1 joerg MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 2678 1.1 joerg if (isStaticDataMember()) 2679 1.1 joerg // FIXME: Remove ? 2680 1.1 joerg // return getASTContext().getInstantiatedFromStaticDataMember(this); 2681 1.1 joerg return getASTContext().getTemplateOrSpecializationInfo(this) 2682 1.1 joerg .dyn_cast<MemberSpecializationInfo *>(); 2683 1.1 joerg return nullptr; 2684 1.1 joerg } 2685 1.1 joerg 2686 1.1 joerg void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 2687 1.1 joerg SourceLocation PointOfInstantiation) { 2688 1.1 joerg assert((isa<VarTemplateSpecializationDecl>(this) || 2689 1.1 joerg getMemberSpecializationInfo()) && 2690 1.1 joerg "not a variable or static data member template specialization"); 2691 1.1 joerg 2692 1.1 joerg if (VarTemplateSpecializationDecl *Spec = 2693 1.1 joerg dyn_cast<VarTemplateSpecializationDecl>(this)) { 2694 1.1 joerg Spec->setSpecializationKind(TSK); 2695 1.1 joerg if (TSK != TSK_ExplicitSpecialization && 2696 1.1 joerg PointOfInstantiation.isValid() && 2697 1.1 joerg Spec->getPointOfInstantiation().isInvalid()) { 2698 1.1 joerg Spec->setPointOfInstantiation(PointOfInstantiation); 2699 1.1 joerg if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 2700 1.1 joerg L->InstantiationRequested(this); 2701 1.1 joerg } 2702 1.1 joerg } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { 2703 1.1 joerg MSI->setTemplateSpecializationKind(TSK); 2704 1.1 joerg if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && 2705 1.1 joerg MSI->getPointOfInstantiation().isInvalid()) { 2706 1.1 joerg MSI->setPointOfInstantiation(PointOfInstantiation); 2707 1.1 joerg if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 2708 1.1 joerg L->InstantiationRequested(this); 2709 1.1 joerg } 2710 1.1 joerg } 2711 1.1 joerg } 2712 1.1 joerg 2713 1.1 joerg void 2714 1.1 joerg VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, 2715 1.1 joerg TemplateSpecializationKind TSK) { 2716 1.1 joerg assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && 2717 1.1 joerg "Previous template or instantiation?"); 2718 1.1 joerg getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); 2719 1.1 joerg } 2720 1.1 joerg 2721 1.1 joerg //===----------------------------------------------------------------------===// 2722 1.1 joerg // ParmVarDecl Implementation 2723 1.1 joerg //===----------------------------------------------------------------------===// 2724 1.1 joerg 2725 1.1 joerg ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 2726 1.1 joerg SourceLocation StartLoc, 2727 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id, 2728 1.1 joerg QualType T, TypeSourceInfo *TInfo, 2729 1.1 joerg StorageClass S, Expr *DefArg) { 2730 1.1 joerg return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, 2731 1.1 joerg S, DefArg); 2732 1.1 joerg } 2733 1.1 joerg 2734 1.1 joerg QualType ParmVarDecl::getOriginalType() const { 2735 1.1 joerg TypeSourceInfo *TSI = getTypeSourceInfo(); 2736 1.1 joerg QualType T = TSI ? TSI->getType() : getType(); 2737 1.1 joerg if (const auto *DT = dyn_cast<DecayedType>(T)) 2738 1.1 joerg return DT->getOriginalType(); 2739 1.1 joerg return T; 2740 1.1 joerg } 2741 1.1 joerg 2742 1.1 joerg ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 2743 1.1 joerg return new (C, ID) 2744 1.1 joerg ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), 2745 1.1 joerg nullptr, QualType(), nullptr, SC_None, nullptr); 2746 1.1 joerg } 2747 1.1 joerg 2748 1.1 joerg SourceRange ParmVarDecl::getSourceRange() const { 2749 1.1 joerg if (!hasInheritedDefaultArg()) { 2750 1.1 joerg SourceRange ArgRange = getDefaultArgRange(); 2751 1.1 joerg if (ArgRange.isValid()) 2752 1.1 joerg return SourceRange(getOuterLocStart(), ArgRange.getEnd()); 2753 1.1 joerg } 2754 1.1 joerg 2755 1.1 joerg // DeclaratorDecl considers the range of postfix types as overlapping with the 2756 1.1 joerg // declaration name, but this is not the case with parameters in ObjC methods. 2757 1.1 joerg if (isa<ObjCMethodDecl>(getDeclContext())) 2758 1.1 joerg return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); 2759 1.1 joerg 2760 1.1 joerg return DeclaratorDecl::getSourceRange(); 2761 1.1 joerg } 2762 1.1 joerg 2763 1.1.1.2 joerg bool ParmVarDecl::isDestroyedInCallee() const { 2764 1.1.1.2 joerg if (hasAttr<NSConsumedAttr>()) 2765 1.1.1.2 joerg return true; 2766 1.1.1.2 joerg 2767 1.1.1.2 joerg auto *RT = getType()->getAs<RecordType>(); 2768 1.1.1.2 joerg if (RT && RT->getDecl()->isParamDestroyedInCallee()) 2769 1.1.1.2 joerg return true; 2770 1.1.1.2 joerg 2771 1.1.1.2 joerg return false; 2772 1.1.1.2 joerg } 2773 1.1.1.2 joerg 2774 1.1 joerg Expr *ParmVarDecl::getDefaultArg() { 2775 1.1 joerg assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); 2776 1.1 joerg assert(!hasUninstantiatedDefaultArg() && 2777 1.1 joerg "Default argument is not yet instantiated!"); 2778 1.1 joerg 2779 1.1 joerg Expr *Arg = getInit(); 2780 1.1 joerg if (auto *E = dyn_cast_or_null<FullExpr>(Arg)) 2781 1.1 joerg return E->getSubExpr(); 2782 1.1 joerg 2783 1.1 joerg return Arg; 2784 1.1 joerg } 2785 1.1 joerg 2786 1.1 joerg void ParmVarDecl::setDefaultArg(Expr *defarg) { 2787 1.1 joerg ParmVarDeclBits.DefaultArgKind = DAK_Normal; 2788 1.1 joerg Init = defarg; 2789 1.1 joerg } 2790 1.1 joerg 2791 1.1 joerg SourceRange ParmVarDecl::getDefaultArgRange() const { 2792 1.1 joerg switch (ParmVarDeclBits.DefaultArgKind) { 2793 1.1 joerg case DAK_None: 2794 1.1 joerg case DAK_Unparsed: 2795 1.1 joerg // Nothing we can do here. 2796 1.1 joerg return SourceRange(); 2797 1.1 joerg 2798 1.1 joerg case DAK_Uninstantiated: 2799 1.1 joerg return getUninstantiatedDefaultArg()->getSourceRange(); 2800 1.1 joerg 2801 1.1 joerg case DAK_Normal: 2802 1.1 joerg if (const Expr *E = getInit()) 2803 1.1 joerg return E->getSourceRange(); 2804 1.1 joerg 2805 1.1 joerg // Missing an actual expression, may be invalid. 2806 1.1 joerg return SourceRange(); 2807 1.1 joerg } 2808 1.1 joerg llvm_unreachable("Invalid default argument kind."); 2809 1.1 joerg } 2810 1.1 joerg 2811 1.1 joerg void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { 2812 1.1 joerg ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; 2813 1.1 joerg Init = arg; 2814 1.1 joerg } 2815 1.1 joerg 2816 1.1 joerg Expr *ParmVarDecl::getUninstantiatedDefaultArg() { 2817 1.1 joerg assert(hasUninstantiatedDefaultArg() && 2818 1.1 joerg "Wrong kind of initialization expression!"); 2819 1.1 joerg return cast_or_null<Expr>(Init.get<Stmt *>()); 2820 1.1 joerg } 2821 1.1 joerg 2822 1.1 joerg bool ParmVarDecl::hasDefaultArg() const { 2823 1.1 joerg // FIXME: We should just return false for DAK_None here once callers are 2824 1.1 joerg // prepared for the case that we encountered an invalid default argument and 2825 1.1 joerg // were unable to even build an invalid expression. 2826 1.1 joerg return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || 2827 1.1 joerg !Init.isNull(); 2828 1.1 joerg } 2829 1.1 joerg 2830 1.1 joerg void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { 2831 1.1 joerg getASTContext().setParameterIndex(this, parameterIndex); 2832 1.1 joerg ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; 2833 1.1 joerg } 2834 1.1 joerg 2835 1.1 joerg unsigned ParmVarDecl::getParameterIndexLarge() const { 2836 1.1 joerg return getASTContext().getParameterIndex(this); 2837 1.1 joerg } 2838 1.1 joerg 2839 1.1 joerg //===----------------------------------------------------------------------===// 2840 1.1 joerg // FunctionDecl Implementation 2841 1.1 joerg //===----------------------------------------------------------------------===// 2842 1.1 joerg 2843 1.1 joerg FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, 2844 1.1 joerg SourceLocation StartLoc, 2845 1.1 joerg const DeclarationNameInfo &NameInfo, QualType T, 2846 1.1 joerg TypeSourceInfo *TInfo, StorageClass S, 2847 1.1 joerg bool isInlineSpecified, 2848 1.1.1.2 joerg ConstexprSpecKind ConstexprKind, 2849 1.1.1.2 joerg Expr *TrailingRequiresClause) 2850 1.1 joerg : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, 2851 1.1 joerg StartLoc), 2852 1.1.1.2 joerg DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), 2853 1.1 joerg EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { 2854 1.1 joerg assert(T.isNull() || T->isFunctionType()); 2855 1.1 joerg FunctionDeclBits.SClass = S; 2856 1.1 joerg FunctionDeclBits.IsInline = isInlineSpecified; 2857 1.1 joerg FunctionDeclBits.IsInlineSpecified = isInlineSpecified; 2858 1.1 joerg FunctionDeclBits.IsVirtualAsWritten = false; 2859 1.1 joerg FunctionDeclBits.IsPure = false; 2860 1.1 joerg FunctionDeclBits.HasInheritedPrototype = false; 2861 1.1 joerg FunctionDeclBits.HasWrittenPrototype = true; 2862 1.1 joerg FunctionDeclBits.IsDeleted = false; 2863 1.1 joerg FunctionDeclBits.IsTrivial = false; 2864 1.1 joerg FunctionDeclBits.IsTrivialForCall = false; 2865 1.1 joerg FunctionDeclBits.IsDefaulted = false; 2866 1.1 joerg FunctionDeclBits.IsExplicitlyDefaulted = false; 2867 1.1.1.2 joerg FunctionDeclBits.HasDefaultedFunctionInfo = false; 2868 1.1 joerg FunctionDeclBits.HasImplicitReturnZero = false; 2869 1.1 joerg FunctionDeclBits.IsLateTemplateParsed = false; 2870 1.1.1.2 joerg FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind); 2871 1.1 joerg FunctionDeclBits.InstantiationIsPending = false; 2872 1.1 joerg FunctionDeclBits.UsesSEHTry = false; 2873 1.1.1.2 joerg FunctionDeclBits.UsesFPIntrin = false; 2874 1.1 joerg FunctionDeclBits.HasSkippedBody = false; 2875 1.1 joerg FunctionDeclBits.WillHaveBody = false; 2876 1.1 joerg FunctionDeclBits.IsMultiVersion = false; 2877 1.1 joerg FunctionDeclBits.IsCopyDeductionCandidate = false; 2878 1.1 joerg FunctionDeclBits.HasODRHash = false; 2879 1.1.1.2 joerg if (TrailingRequiresClause) 2880 1.1.1.2 joerg setTrailingRequiresClause(TrailingRequiresClause); 2881 1.1 joerg } 2882 1.1 joerg 2883 1.1 joerg void FunctionDecl::getNameForDiagnostic( 2884 1.1 joerg raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { 2885 1.1 joerg NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); 2886 1.1 joerg const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 2887 1.1 joerg if (TemplateArgs) 2888 1.1 joerg printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); 2889 1.1 joerg } 2890 1.1 joerg 2891 1.1 joerg bool FunctionDecl::isVariadic() const { 2892 1.1 joerg if (const auto *FT = getType()->getAs<FunctionProtoType>()) 2893 1.1 joerg return FT->isVariadic(); 2894 1.1 joerg return false; 2895 1.1 joerg } 2896 1.1 joerg 2897 1.1.1.2 joerg FunctionDecl::DefaultedFunctionInfo * 2898 1.1.1.2 joerg FunctionDecl::DefaultedFunctionInfo::Create(ASTContext &Context, 2899 1.1.1.2 joerg ArrayRef<DeclAccessPair> Lookups) { 2900 1.1.1.2 joerg DefaultedFunctionInfo *Info = new (Context.Allocate( 2901 1.1.1.2 joerg totalSizeToAlloc<DeclAccessPair>(Lookups.size()), 2902 1.1.1.2 joerg std::max(alignof(DefaultedFunctionInfo), alignof(DeclAccessPair)))) 2903 1.1.1.2 joerg DefaultedFunctionInfo; 2904 1.1.1.2 joerg Info->NumLookups = Lookups.size(); 2905 1.1.1.2 joerg std::uninitialized_copy(Lookups.begin(), Lookups.end(), 2906 1.1.1.2 joerg Info->getTrailingObjects<DeclAccessPair>()); 2907 1.1.1.2 joerg return Info; 2908 1.1.1.2 joerg } 2909 1.1.1.2 joerg 2910 1.1.1.2 joerg void FunctionDecl::setDefaultedFunctionInfo(DefaultedFunctionInfo *Info) { 2911 1.1.1.2 joerg assert(!FunctionDeclBits.HasDefaultedFunctionInfo && "already have this"); 2912 1.1.1.2 joerg assert(!Body && "can't replace function body with defaulted function info"); 2913 1.1.1.2 joerg 2914 1.1.1.2 joerg FunctionDeclBits.HasDefaultedFunctionInfo = true; 2915 1.1.1.2 joerg DefaultedInfo = Info; 2916 1.1.1.2 joerg } 2917 1.1.1.2 joerg 2918 1.1.1.2 joerg FunctionDecl::DefaultedFunctionInfo * 2919 1.1.1.2 joerg FunctionDecl::getDefaultedFunctionInfo() const { 2920 1.1.1.2 joerg return FunctionDeclBits.HasDefaultedFunctionInfo ? DefaultedInfo : nullptr; 2921 1.1.1.2 joerg } 2922 1.1.1.2 joerg 2923 1.1 joerg bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { 2924 1.1 joerg for (auto I : redecls()) { 2925 1.1 joerg if (I->doesThisDeclarationHaveABody()) { 2926 1.1 joerg Definition = I; 2927 1.1 joerg return true; 2928 1.1 joerg } 2929 1.1 joerg } 2930 1.1 joerg 2931 1.1 joerg return false; 2932 1.1 joerg } 2933 1.1 joerg 2934 1.1.1.2 joerg bool FunctionDecl::hasTrivialBody() const { 2935 1.1 joerg Stmt *S = getBody(); 2936 1.1 joerg if (!S) { 2937 1.1 joerg // Since we don't have a body for this function, we don't know if it's 2938 1.1 joerg // trivial or not. 2939 1.1 joerg return false; 2940 1.1 joerg } 2941 1.1 joerg 2942 1.1 joerg if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) 2943 1.1 joerg return true; 2944 1.1 joerg return false; 2945 1.1 joerg } 2946 1.1 joerg 2947 1.1.1.2 joerg bool FunctionDecl::isThisDeclarationInstantiatedFromAFriendDefinition() const { 2948 1.1.1.2 joerg if (!getFriendObjectKind()) 2949 1.1.1.2 joerg return false; 2950 1.1.1.2 joerg 2951 1.1.1.2 joerg // Check for a friend function instantiated from a friend function 2952 1.1.1.2 joerg // definition in a templated class. 2953 1.1.1.2 joerg if (const FunctionDecl *InstantiatedFrom = 2954 1.1.1.2 joerg getInstantiatedFromMemberFunction()) 2955 1.1.1.2 joerg return InstantiatedFrom->getFriendObjectKind() && 2956 1.1.1.2 joerg InstantiatedFrom->isThisDeclarationADefinition(); 2957 1.1.1.2 joerg 2958 1.1.1.2 joerg // Check for a friend function template instantiated from a friend 2959 1.1.1.2 joerg // function template definition in a templated class. 2960 1.1.1.2 joerg if (const FunctionTemplateDecl *Template = getDescribedFunctionTemplate()) { 2961 1.1.1.2 joerg if (const FunctionTemplateDecl *InstantiatedFrom = 2962 1.1.1.2 joerg Template->getInstantiatedFromMemberTemplate()) 2963 1.1.1.2 joerg return InstantiatedFrom->getFriendObjectKind() && 2964 1.1.1.2 joerg InstantiatedFrom->isThisDeclarationADefinition(); 2965 1.1.1.2 joerg } 2966 1.1.1.2 joerg 2967 1.1.1.2 joerg return false; 2968 1.1.1.2 joerg } 2969 1.1.1.2 joerg 2970 1.1.1.2 joerg bool FunctionDecl::isDefined(const FunctionDecl *&Definition, 2971 1.1.1.2 joerg bool CheckForPendingFriendDefinition) const { 2972 1.1.1.2 joerg for (const FunctionDecl *FD : redecls()) { 2973 1.1.1.2 joerg if (FD->isThisDeclarationADefinition()) { 2974 1.1.1.2 joerg Definition = FD; 2975 1.1.1.2 joerg return true; 2976 1.1.1.2 joerg } 2977 1.1.1.2 joerg 2978 1.1.1.2 joerg // If this is a friend function defined in a class template, it does not 2979 1.1.1.2 joerg // have a body until it is used, nevertheless it is a definition, see 2980 1.1.1.2 joerg // [temp.inst]p2: 2981 1.1.1.2 joerg // 2982 1.1.1.2 joerg // ... for the purpose of determining whether an instantiated redeclaration 2983 1.1.1.2 joerg // is valid according to [basic.def.odr] and [class.mem], a declaration that 2984 1.1.1.2 joerg // corresponds to a definition in the template is considered to be a 2985 1.1.1.2 joerg // definition. 2986 1.1.1.2 joerg // 2987 1.1.1.2 joerg // The following code must produce redefinition error: 2988 1.1.1.2 joerg // 2989 1.1.1.2 joerg // template<typename T> struct C20 { friend void func_20() {} }; 2990 1.1.1.2 joerg // C20<int> c20i; 2991 1.1.1.2 joerg // void func_20() {} 2992 1.1.1.2 joerg // 2993 1.1.1.2 joerg if (CheckForPendingFriendDefinition && 2994 1.1.1.2 joerg FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { 2995 1.1.1.2 joerg Definition = FD; 2996 1.1 joerg return true; 2997 1.1 joerg } 2998 1.1 joerg } 2999 1.1 joerg 3000 1.1 joerg return false; 3001 1.1 joerg } 3002 1.1 joerg 3003 1.1 joerg Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 3004 1.1 joerg if (!hasBody(Definition)) 3005 1.1 joerg return nullptr; 3006 1.1 joerg 3007 1.1.1.2 joerg assert(!Definition->FunctionDeclBits.HasDefaultedFunctionInfo && 3008 1.1.1.2 joerg "definition should not have a body"); 3009 1.1 joerg if (Definition->Body) 3010 1.1 joerg return Definition->Body.get(getASTContext().getExternalSource()); 3011 1.1 joerg 3012 1.1 joerg return nullptr; 3013 1.1 joerg } 3014 1.1 joerg 3015 1.1 joerg void FunctionDecl::setBody(Stmt *B) { 3016 1.1.1.2 joerg FunctionDeclBits.HasDefaultedFunctionInfo = false; 3017 1.1.1.2 joerg Body = LazyDeclStmtPtr(B); 3018 1.1 joerg if (B) 3019 1.1 joerg EndRangeLoc = B->getEndLoc(); 3020 1.1 joerg } 3021 1.1 joerg 3022 1.1 joerg void FunctionDecl::setPure(bool P) { 3023 1.1 joerg FunctionDeclBits.IsPure = P; 3024 1.1 joerg if (P) 3025 1.1 joerg if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) 3026 1.1 joerg Parent->markedVirtualFunctionPure(); 3027 1.1 joerg } 3028 1.1 joerg 3029 1.1 joerg template<std::size_t Len> 3030 1.1 joerg static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { 3031 1.1 joerg IdentifierInfo *II = ND->getIdentifier(); 3032 1.1 joerg return II && II->isStr(Str); 3033 1.1 joerg } 3034 1.1 joerg 3035 1.1 joerg bool FunctionDecl::isMain() const { 3036 1.1 joerg const TranslationUnitDecl *tunit = 3037 1.1 joerg dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); 3038 1.1 joerg return tunit && 3039 1.1 joerg !tunit->getASTContext().getLangOpts().Freestanding && 3040 1.1 joerg isNamed(this, "main"); 3041 1.1 joerg } 3042 1.1 joerg 3043 1.1 joerg bool FunctionDecl::isMSVCRTEntryPoint() const { 3044 1.1 joerg const TranslationUnitDecl *TUnit = 3045 1.1 joerg dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); 3046 1.1 joerg if (!TUnit) 3047 1.1 joerg return false; 3048 1.1 joerg 3049 1.1 joerg // Even though we aren't really targeting MSVCRT if we are freestanding, 3050 1.1 joerg // semantic analysis for these functions remains the same. 3051 1.1 joerg 3052 1.1 joerg // MSVCRT entry points only exist on MSVCRT targets. 3053 1.1 joerg if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) 3054 1.1 joerg return false; 3055 1.1 joerg 3056 1.1 joerg // Nameless functions like constructors cannot be entry points. 3057 1.1 joerg if (!getIdentifier()) 3058 1.1 joerg return false; 3059 1.1 joerg 3060 1.1 joerg return llvm::StringSwitch<bool>(getName()) 3061 1.1 joerg .Cases("main", // an ANSI console app 3062 1.1 joerg "wmain", // a Unicode console App 3063 1.1 joerg "WinMain", // an ANSI GUI app 3064 1.1 joerg "wWinMain", // a Unicode GUI app 3065 1.1 joerg "DllMain", // a DLL 3066 1.1 joerg true) 3067 1.1 joerg .Default(false); 3068 1.1 joerg } 3069 1.1 joerg 3070 1.1 joerg bool FunctionDecl::isReservedGlobalPlacementOperator() const { 3071 1.1 joerg assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); 3072 1.1 joerg assert(getDeclName().getCXXOverloadedOperator() == OO_New || 3073 1.1 joerg getDeclName().getCXXOverloadedOperator() == OO_Delete || 3074 1.1 joerg getDeclName().getCXXOverloadedOperator() == OO_Array_New || 3075 1.1 joerg getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); 3076 1.1 joerg 3077 1.1 joerg if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) 3078 1.1 joerg return false; 3079 1.1 joerg 3080 1.1 joerg const auto *proto = getType()->castAs<FunctionProtoType>(); 3081 1.1 joerg if (proto->getNumParams() != 2 || proto->isVariadic()) 3082 1.1 joerg return false; 3083 1.1 joerg 3084 1.1 joerg ASTContext &Context = 3085 1.1 joerg cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) 3086 1.1 joerg ->getASTContext(); 3087 1.1 joerg 3088 1.1 joerg // The result type and first argument type are constant across all 3089 1.1 joerg // these operators. The second argument must be exactly void*. 3090 1.1 joerg return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); 3091 1.1 joerg } 3092 1.1 joerg 3093 1.1.1.2 joerg bool FunctionDecl::isReplaceableGlobalAllocationFunction( 3094 1.1.1.2 joerg Optional<unsigned> *AlignmentParam, bool *IsNothrow) const { 3095 1.1 joerg if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) 3096 1.1 joerg return false; 3097 1.1 joerg if (getDeclName().getCXXOverloadedOperator() != OO_New && 3098 1.1 joerg getDeclName().getCXXOverloadedOperator() != OO_Delete && 3099 1.1 joerg getDeclName().getCXXOverloadedOperator() != OO_Array_New && 3100 1.1 joerg getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) 3101 1.1 joerg return false; 3102 1.1 joerg 3103 1.1 joerg if (isa<CXXRecordDecl>(getDeclContext())) 3104 1.1 joerg return false; 3105 1.1 joerg 3106 1.1 joerg // This can only fail for an invalid 'operator new' declaration. 3107 1.1 joerg if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) 3108 1.1 joerg return false; 3109 1.1 joerg 3110 1.1 joerg const auto *FPT = getType()->castAs<FunctionProtoType>(); 3111 1.1 joerg if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic()) 3112 1.1 joerg return false; 3113 1.1 joerg 3114 1.1 joerg // If this is a single-parameter function, it must be a replaceable global 3115 1.1 joerg // allocation or deallocation function. 3116 1.1 joerg if (FPT->getNumParams() == 1) 3117 1.1 joerg return true; 3118 1.1 joerg 3119 1.1 joerg unsigned Params = 1; 3120 1.1 joerg QualType Ty = FPT->getParamType(Params); 3121 1.1 joerg ASTContext &Ctx = getASTContext(); 3122 1.1 joerg 3123 1.1 joerg auto Consume = [&] { 3124 1.1 joerg ++Params; 3125 1.1 joerg Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); 3126 1.1 joerg }; 3127 1.1 joerg 3128 1.1 joerg // In C++14, the next parameter can be a 'std::size_t' for sized delete. 3129 1.1 joerg bool IsSizedDelete = false; 3130 1.1 joerg if (Ctx.getLangOpts().SizedDeallocation && 3131 1.1 joerg (getDeclName().getCXXOverloadedOperator() == OO_Delete || 3132 1.1 joerg getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && 3133 1.1 joerg Ctx.hasSameType(Ty, Ctx.getSizeType())) { 3134 1.1 joerg IsSizedDelete = true; 3135 1.1 joerg Consume(); 3136 1.1 joerg } 3137 1.1 joerg 3138 1.1 joerg // In C++17, the next parameter can be a 'std::align_val_t' for aligned 3139 1.1 joerg // new/delete. 3140 1.1 joerg if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { 3141 1.1 joerg Consume(); 3142 1.1.1.2 joerg if (AlignmentParam) 3143 1.1.1.2 joerg *AlignmentParam = Params; 3144 1.1 joerg } 3145 1.1 joerg 3146 1.1 joerg // Finally, if this is not a sized delete, the final parameter can 3147 1.1 joerg // be a 'const std::nothrow_t&'. 3148 1.1 joerg if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { 3149 1.1 joerg Ty = Ty->getPointeeType(); 3150 1.1 joerg if (Ty.getCVRQualifiers() != Qualifiers::Const) 3151 1.1 joerg return false; 3152 1.1.1.2 joerg if (Ty->isNothrowT()) { 3153 1.1.1.2 joerg if (IsNothrow) 3154 1.1.1.2 joerg *IsNothrow = true; 3155 1.1 joerg Consume(); 3156 1.1.1.2 joerg } 3157 1.1 joerg } 3158 1.1 joerg 3159 1.1 joerg return Params == FPT->getNumParams(); 3160 1.1 joerg } 3161 1.1 joerg 3162 1.1.1.2 joerg bool FunctionDecl::isInlineBuiltinDeclaration() const { 3163 1.1.1.2 joerg if (!getBuiltinID()) 3164 1.1.1.2 joerg return false; 3165 1.1.1.2 joerg 3166 1.1.1.2 joerg const FunctionDecl *Definition; 3167 1.1.1.2 joerg return hasBody(Definition) && Definition->isInlineSpecified(); 3168 1.1.1.2 joerg } 3169 1.1.1.2 joerg 3170 1.1 joerg bool FunctionDecl::isDestroyingOperatorDelete() const { 3171 1.1 joerg // C++ P0722: 3172 1.1 joerg // Within a class C, a single object deallocation function with signature 3173 1.1 joerg // (T, std::destroying_delete_t, <more params>) 3174 1.1 joerg // is a destroying operator delete. 3175 1.1 joerg if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || 3176 1.1 joerg getNumParams() < 2) 3177 1.1 joerg return false; 3178 1.1 joerg 3179 1.1 joerg auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); 3180 1.1 joerg return RD && RD->isInStdNamespace() && RD->getIdentifier() && 3181 1.1 joerg RD->getIdentifier()->isStr("destroying_delete_t"); 3182 1.1 joerg } 3183 1.1 joerg 3184 1.1 joerg LanguageLinkage FunctionDecl::getLanguageLinkage() const { 3185 1.1 joerg return getDeclLanguageLinkage(*this); 3186 1.1 joerg } 3187 1.1 joerg 3188 1.1 joerg bool FunctionDecl::isExternC() const { 3189 1.1 joerg return isDeclExternC(*this); 3190 1.1 joerg } 3191 1.1 joerg 3192 1.1 joerg bool FunctionDecl::isInExternCContext() const { 3193 1.1 joerg if (hasAttr<OpenCLKernelAttr>()) 3194 1.1 joerg return true; 3195 1.1 joerg return getLexicalDeclContext()->isExternCContext(); 3196 1.1 joerg } 3197 1.1 joerg 3198 1.1 joerg bool FunctionDecl::isInExternCXXContext() const { 3199 1.1 joerg return getLexicalDeclContext()->isExternCXXContext(); 3200 1.1 joerg } 3201 1.1 joerg 3202 1.1 joerg bool FunctionDecl::isGlobal() const { 3203 1.1 joerg if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) 3204 1.1 joerg return Method->isStatic(); 3205 1.1 joerg 3206 1.1 joerg if (getCanonicalDecl()->getStorageClass() == SC_Static) 3207 1.1 joerg return false; 3208 1.1 joerg 3209 1.1 joerg for (const DeclContext *DC = getDeclContext(); 3210 1.1 joerg DC->isNamespace(); 3211 1.1 joerg DC = DC->getParent()) { 3212 1.1 joerg if (const auto *Namespace = cast<NamespaceDecl>(DC)) { 3213 1.1 joerg if (!Namespace->getDeclName()) 3214 1.1 joerg return false; 3215 1.1 joerg break; 3216 1.1 joerg } 3217 1.1 joerg } 3218 1.1 joerg 3219 1.1 joerg return true; 3220 1.1 joerg } 3221 1.1 joerg 3222 1.1 joerg bool FunctionDecl::isNoReturn() const { 3223 1.1 joerg if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || 3224 1.1 joerg hasAttr<C11NoReturnAttr>()) 3225 1.1 joerg return true; 3226 1.1 joerg 3227 1.1 joerg if (auto *FnTy = getType()->getAs<FunctionType>()) 3228 1.1 joerg return FnTy->getNoReturnAttr(); 3229 1.1 joerg 3230 1.1 joerg return false; 3231 1.1 joerg } 3232 1.1 joerg 3233 1.1 joerg 3234 1.1 joerg MultiVersionKind FunctionDecl::getMultiVersionKind() const { 3235 1.1 joerg if (hasAttr<TargetAttr>()) 3236 1.1 joerg return MultiVersionKind::Target; 3237 1.1 joerg if (hasAttr<CPUDispatchAttr>()) 3238 1.1 joerg return MultiVersionKind::CPUDispatch; 3239 1.1 joerg if (hasAttr<CPUSpecificAttr>()) 3240 1.1 joerg return MultiVersionKind::CPUSpecific; 3241 1.1 joerg return MultiVersionKind::None; 3242 1.1 joerg } 3243 1.1 joerg 3244 1.1 joerg bool FunctionDecl::isCPUDispatchMultiVersion() const { 3245 1.1 joerg return isMultiVersion() && hasAttr<CPUDispatchAttr>(); 3246 1.1 joerg } 3247 1.1 joerg 3248 1.1 joerg bool FunctionDecl::isCPUSpecificMultiVersion() const { 3249 1.1 joerg return isMultiVersion() && hasAttr<CPUSpecificAttr>(); 3250 1.1 joerg } 3251 1.1 joerg 3252 1.1 joerg bool FunctionDecl::isTargetMultiVersion() const { 3253 1.1 joerg return isMultiVersion() && hasAttr<TargetAttr>(); 3254 1.1 joerg } 3255 1.1 joerg 3256 1.1 joerg void 3257 1.1 joerg FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 3258 1.1 joerg redeclarable_base::setPreviousDecl(PrevDecl); 3259 1.1 joerg 3260 1.1 joerg if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 3261 1.1 joerg FunctionTemplateDecl *PrevFunTmpl 3262 1.1 joerg = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; 3263 1.1 joerg assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 3264 1.1 joerg FunTmpl->setPreviousDecl(PrevFunTmpl); 3265 1.1 joerg } 3266 1.1 joerg 3267 1.1 joerg if (PrevDecl && PrevDecl->isInlined()) 3268 1.1 joerg setImplicitlyInline(true); 3269 1.1 joerg } 3270 1.1 joerg 3271 1.1 joerg FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } 3272 1.1 joerg 3273 1.1 joerg /// Returns a value indicating whether this function corresponds to a builtin 3274 1.1 joerg /// function. 3275 1.1 joerg /// 3276 1.1 joerg /// The function corresponds to a built-in function if it is declared at 3277 1.1 joerg /// translation scope or within an extern "C" block and its name matches with 3278 1.1 joerg /// the name of a builtin. The returned value will be 0 for functions that do 3279 1.1 joerg /// not correspond to a builtin, a value of type \c Builtin::ID if in the 3280 1.1 joerg /// target-independent range \c [1,Builtin::First), or a target-specific builtin 3281 1.1 joerg /// value. 3282 1.1 joerg /// 3283 1.1 joerg /// \param ConsiderWrapperFunctions If true, we should consider wrapper 3284 1.1 joerg /// functions as their wrapped builtins. This shouldn't be done in general, but 3285 1.1 joerg /// it's useful in Sema to diagnose calls to wrappers based on their semantics. 3286 1.1 joerg unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { 3287 1.1.1.2 joerg unsigned BuiltinID = 0; 3288 1.1 joerg 3289 1.1.1.2 joerg if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { 3290 1.1.1.2 joerg BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); 3291 1.1.1.2 joerg } else if (const auto *BAA = getAttr<BuiltinAliasAttr>()) { 3292 1.1.1.2 joerg BuiltinID = BAA->getBuiltinName()->getBuiltinID(); 3293 1.1.1.2 joerg } else if (const auto *A = getAttr<BuiltinAttr>()) { 3294 1.1.1.2 joerg BuiltinID = A->getID(); 3295 1.1 joerg } 3296 1.1 joerg 3297 1.1 joerg if (!BuiltinID) 3298 1.1 joerg return 0; 3299 1.1 joerg 3300 1.1 joerg // If the function is marked "overloadable", it has a different mangled name 3301 1.1 joerg // and is not the C library function. 3302 1.1 joerg if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && 3303 1.1.1.2 joerg (!hasAttr<ArmBuiltinAliasAttr>() && !hasAttr<BuiltinAliasAttr>())) 3304 1.1 joerg return 0; 3305 1.1 joerg 3306 1.1.1.2 joerg ASTContext &Context = getASTContext(); 3307 1.1 joerg if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 3308 1.1 joerg return BuiltinID; 3309 1.1 joerg 3310 1.1 joerg // This function has the name of a known C library 3311 1.1 joerg // function. Determine whether it actually refers to the C library 3312 1.1 joerg // function or whether it just has the same name. 3313 1.1 joerg 3314 1.1 joerg // If this is a static function, it's not a builtin. 3315 1.1 joerg if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) 3316 1.1 joerg return 0; 3317 1.1 joerg 3318 1.1 joerg // OpenCL v1.2 s6.9.f - The library functions defined in 3319 1.1 joerg // the C99 standard headers are not available. 3320 1.1 joerg if (Context.getLangOpts().OpenCL && 3321 1.1 joerg Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 3322 1.1 joerg return 0; 3323 1.1 joerg 3324 1.1 joerg // CUDA does not have device-side standard library. printf and malloc are the 3325 1.1 joerg // only special cases that are supported by device-side runtime. 3326 1.1 joerg if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && 3327 1.1 joerg !hasAttr<CUDAHostAttr>() && 3328 1.1 joerg !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) 3329 1.1 joerg return 0; 3330 1.1 joerg 3331 1.1.1.2 joerg // As AMDGCN implementation of OpenMP does not have a device-side standard 3332 1.1.1.2 joerg // library, none of the predefined library functions except printf and malloc 3333 1.1.1.2 joerg // should be treated as a builtin i.e. 0 should be returned for them. 3334 1.1.1.2 joerg if (Context.getTargetInfo().getTriple().isAMDGCN() && 3335 1.1.1.2 joerg Context.getLangOpts().OpenMPIsDevice && 3336 1.1.1.2 joerg Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && 3337 1.1.1.2 joerg !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) 3338 1.1.1.2 joerg return 0; 3339 1.1.1.2 joerg 3340 1.1 joerg return BuiltinID; 3341 1.1 joerg } 3342 1.1 joerg 3343 1.1 joerg /// getNumParams - Return the number of parameters this function must have 3344 1.1 joerg /// based on its FunctionType. This is the length of the ParamInfo array 3345 1.1 joerg /// after it has been created. 3346 1.1 joerg unsigned FunctionDecl::getNumParams() const { 3347 1.1 joerg const auto *FPT = getType()->getAs<FunctionProtoType>(); 3348 1.1 joerg return FPT ? FPT->getNumParams() : 0; 3349 1.1 joerg } 3350 1.1 joerg 3351 1.1 joerg void FunctionDecl::setParams(ASTContext &C, 3352 1.1 joerg ArrayRef<ParmVarDecl *> NewParamInfo) { 3353 1.1 joerg assert(!ParamInfo && "Already has param info!"); 3354 1.1 joerg assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); 3355 1.1 joerg 3356 1.1 joerg // Zero params -> null pointer. 3357 1.1 joerg if (!NewParamInfo.empty()) { 3358 1.1 joerg ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; 3359 1.1 joerg std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 3360 1.1 joerg } 3361 1.1 joerg } 3362 1.1 joerg 3363 1.1 joerg /// getMinRequiredArguments - Returns the minimum number of arguments 3364 1.1 joerg /// needed to call this function. This may be fewer than the number of 3365 1.1 joerg /// function parameters, if some of the parameters have default 3366 1.1 joerg /// arguments (in C++) or are parameter packs (C++11). 3367 1.1 joerg unsigned FunctionDecl::getMinRequiredArguments() const { 3368 1.1 joerg if (!getASTContext().getLangOpts().CPlusPlus) 3369 1.1 joerg return getNumParams(); 3370 1.1 joerg 3371 1.1.1.2 joerg // Note that it is possible for a parameter with no default argument to 3372 1.1.1.2 joerg // follow a parameter with a default argument. 3373 1.1 joerg unsigned NumRequiredArgs = 0; 3374 1.1.1.2 joerg unsigned MinParamsSoFar = 0; 3375 1.1.1.2 joerg for (auto *Param : parameters()) { 3376 1.1.1.2 joerg if (!Param->isParameterPack()) { 3377 1.1.1.2 joerg ++MinParamsSoFar; 3378 1.1.1.2 joerg if (!Param->hasDefaultArg()) 3379 1.1.1.2 joerg NumRequiredArgs = MinParamsSoFar; 3380 1.1.1.2 joerg } 3381 1.1.1.2 joerg } 3382 1.1 joerg return NumRequiredArgs; 3383 1.1 joerg } 3384 1.1 joerg 3385 1.1.1.2 joerg bool FunctionDecl::hasOneParamOrDefaultArgs() const { 3386 1.1.1.2 joerg return getNumParams() == 1 || 3387 1.1.1.2 joerg (getNumParams() > 1 && 3388 1.1.1.2 joerg std::all_of(param_begin() + 1, param_end(), 3389 1.1.1.2 joerg [](ParmVarDecl *P) { return P->hasDefaultArg(); })); 3390 1.1.1.2 joerg } 3391 1.1.1.2 joerg 3392 1.1 joerg /// The combination of the extern and inline keywords under MSVC forces 3393 1.1 joerg /// the function to be required. 3394 1.1 joerg /// 3395 1.1 joerg /// Note: This function assumes that we will only get called when isInlined() 3396 1.1 joerg /// would return true for this FunctionDecl. 3397 1.1 joerg bool FunctionDecl::isMSExternInline() const { 3398 1.1 joerg assert(isInlined() && "expected to get called on an inlined function!"); 3399 1.1 joerg 3400 1.1 joerg const ASTContext &Context = getASTContext(); 3401 1.1 joerg if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && 3402 1.1 joerg !hasAttr<DLLExportAttr>()) 3403 1.1 joerg return false; 3404 1.1 joerg 3405 1.1 joerg for (const FunctionDecl *FD = getMostRecentDecl(); FD; 3406 1.1 joerg FD = FD->getPreviousDecl()) 3407 1.1 joerg if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) 3408 1.1 joerg return true; 3409 1.1 joerg 3410 1.1 joerg return false; 3411 1.1 joerg } 3412 1.1 joerg 3413 1.1 joerg static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { 3414 1.1 joerg if (Redecl->getStorageClass() != SC_Extern) 3415 1.1 joerg return false; 3416 1.1 joerg 3417 1.1 joerg for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; 3418 1.1 joerg FD = FD->getPreviousDecl()) 3419 1.1 joerg if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) 3420 1.1 joerg return false; 3421 1.1 joerg 3422 1.1 joerg return true; 3423 1.1 joerg } 3424 1.1 joerg 3425 1.1 joerg static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { 3426 1.1 joerg // Only consider file-scope declarations in this test. 3427 1.1 joerg if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 3428 1.1 joerg return false; 3429 1.1 joerg 3430 1.1 joerg // Only consider explicit declarations; the presence of a builtin for a 3431 1.1 joerg // libcall shouldn't affect whether a definition is externally visible. 3432 1.1 joerg if (Redecl->isImplicit()) 3433 1.1 joerg return false; 3434 1.1 joerg 3435 1.1 joerg if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) 3436 1.1 joerg return true; // Not an inline definition 3437 1.1 joerg 3438 1.1 joerg return false; 3439 1.1 joerg } 3440 1.1 joerg 3441 1.1 joerg /// For a function declaration in C or C++, determine whether this 3442 1.1 joerg /// declaration causes the definition to be externally visible. 3443 1.1 joerg /// 3444 1.1 joerg /// For instance, this determines if adding the current declaration to the set 3445 1.1 joerg /// of redeclarations of the given functions causes 3446 1.1 joerg /// isInlineDefinitionExternallyVisible to change from false to true. 3447 1.1 joerg bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { 3448 1.1 joerg assert(!doesThisDeclarationHaveABody() && 3449 1.1 joerg "Must have a declaration without a body."); 3450 1.1 joerg 3451 1.1 joerg ASTContext &Context = getASTContext(); 3452 1.1 joerg 3453 1.1 joerg if (Context.getLangOpts().MSVCCompat) { 3454 1.1 joerg const FunctionDecl *Definition; 3455 1.1 joerg if (hasBody(Definition) && Definition->isInlined() && 3456 1.1 joerg redeclForcesDefMSVC(this)) 3457 1.1 joerg return true; 3458 1.1 joerg } 3459 1.1 joerg 3460 1.1 joerg if (Context.getLangOpts().CPlusPlus) 3461 1.1 joerg return false; 3462 1.1 joerg 3463 1.1 joerg if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 3464 1.1 joerg // With GNU inlining, a declaration with 'inline' but not 'extern', forces 3465 1.1 joerg // an externally visible definition. 3466 1.1 joerg // 3467 1.1 joerg // FIXME: What happens if gnu_inline gets added on after the first 3468 1.1 joerg // declaration? 3469 1.1 joerg if (!isInlineSpecified() || getStorageClass() == SC_Extern) 3470 1.1 joerg return false; 3471 1.1 joerg 3472 1.1 joerg const FunctionDecl *Prev = this; 3473 1.1 joerg bool FoundBody = false; 3474 1.1 joerg while ((Prev = Prev->getPreviousDecl())) { 3475 1.1.1.2 joerg FoundBody |= Prev->doesThisDeclarationHaveABody(); 3476 1.1 joerg 3477 1.1.1.2 joerg if (Prev->doesThisDeclarationHaveABody()) { 3478 1.1 joerg // If it's not the case that both 'inline' and 'extern' are 3479 1.1 joerg // specified on the definition, then it is always externally visible. 3480 1.1 joerg if (!Prev->isInlineSpecified() || 3481 1.1 joerg Prev->getStorageClass() != SC_Extern) 3482 1.1 joerg return false; 3483 1.1 joerg } else if (Prev->isInlineSpecified() && 3484 1.1 joerg Prev->getStorageClass() != SC_Extern) { 3485 1.1 joerg return false; 3486 1.1 joerg } 3487 1.1 joerg } 3488 1.1 joerg return FoundBody; 3489 1.1 joerg } 3490 1.1 joerg 3491 1.1 joerg // C99 6.7.4p6: 3492 1.1 joerg // [...] If all of the file scope declarations for a function in a 3493 1.1 joerg // translation unit include the inline function specifier without extern, 3494 1.1 joerg // then the definition in that translation unit is an inline definition. 3495 1.1 joerg if (isInlineSpecified() && getStorageClass() != SC_Extern) 3496 1.1 joerg return false; 3497 1.1 joerg const FunctionDecl *Prev = this; 3498 1.1 joerg bool FoundBody = false; 3499 1.1 joerg while ((Prev = Prev->getPreviousDecl())) { 3500 1.1.1.2 joerg FoundBody |= Prev->doesThisDeclarationHaveABody(); 3501 1.1 joerg if (RedeclForcesDefC99(Prev)) 3502 1.1 joerg return false; 3503 1.1 joerg } 3504 1.1 joerg return FoundBody; 3505 1.1 joerg } 3506 1.1 joerg 3507 1.1 joerg FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { 3508 1.1 joerg const TypeSourceInfo *TSI = getTypeSourceInfo(); 3509 1.1 joerg return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>() 3510 1.1 joerg : FunctionTypeLoc(); 3511 1.1 joerg } 3512 1.1 joerg 3513 1.1 joerg SourceRange FunctionDecl::getReturnTypeSourceRange() const { 3514 1.1 joerg FunctionTypeLoc FTL = getFunctionTypeLoc(); 3515 1.1 joerg if (!FTL) 3516 1.1 joerg return SourceRange(); 3517 1.1 joerg 3518 1.1 joerg // Skip self-referential return types. 3519 1.1 joerg const SourceManager &SM = getASTContext().getSourceManager(); 3520 1.1 joerg SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); 3521 1.1 joerg SourceLocation Boundary = getNameInfo().getBeginLoc(); 3522 1.1 joerg if (RTRange.isInvalid() || Boundary.isInvalid() || 3523 1.1 joerg !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) 3524 1.1 joerg return SourceRange(); 3525 1.1 joerg 3526 1.1 joerg return RTRange; 3527 1.1 joerg } 3528 1.1 joerg 3529 1.1.1.2 joerg SourceRange FunctionDecl::getParametersSourceRange() const { 3530 1.1.1.2 joerg unsigned NP = getNumParams(); 3531 1.1.1.2 joerg SourceLocation EllipsisLoc = getEllipsisLoc(); 3532 1.1.1.2 joerg 3533 1.1.1.2 joerg if (NP == 0 && EllipsisLoc.isInvalid()) 3534 1.1.1.2 joerg return SourceRange(); 3535 1.1.1.2 joerg 3536 1.1.1.2 joerg SourceLocation Begin = 3537 1.1.1.2 joerg NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; 3538 1.1.1.2 joerg SourceLocation End = EllipsisLoc.isValid() 3539 1.1.1.2 joerg ? EllipsisLoc 3540 1.1.1.2 joerg : ParamInfo[NP - 1]->getSourceRange().getEnd(); 3541 1.1.1.2 joerg 3542 1.1.1.2 joerg return SourceRange(Begin, End); 3543 1.1.1.2 joerg } 3544 1.1.1.2 joerg 3545 1.1 joerg SourceRange FunctionDecl::getExceptionSpecSourceRange() const { 3546 1.1 joerg FunctionTypeLoc FTL = getFunctionTypeLoc(); 3547 1.1 joerg return FTL ? FTL.getExceptionSpecRange() : SourceRange(); 3548 1.1 joerg } 3549 1.1 joerg 3550 1.1 joerg /// For an inline function definition in C, or for a gnu_inline function 3551 1.1 joerg /// in C++, determine whether the definition will be externally visible. 3552 1.1 joerg /// 3553 1.1 joerg /// Inline function definitions are always available for inlining optimizations. 3554 1.1 joerg /// However, depending on the language dialect, declaration specifiers, and 3555 1.1 joerg /// attributes, the definition of an inline function may or may not be 3556 1.1 joerg /// "externally" visible to other translation units in the program. 3557 1.1 joerg /// 3558 1.1 joerg /// In C99, inline definitions are not externally visible by default. However, 3559 1.1 joerg /// if even one of the global-scope declarations is marked "extern inline", the 3560 1.1 joerg /// inline definition becomes externally visible (C99 6.7.4p6). 3561 1.1 joerg /// 3562 1.1 joerg /// In GNU89 mode, or if the gnu_inline attribute is attached to the function 3563 1.1 joerg /// definition, we use the GNU semantics for inline, which are nearly the 3564 1.1 joerg /// opposite of C99 semantics. In particular, "inline" by itself will create 3565 1.1 joerg /// an externally visible symbol, but "extern inline" will not create an 3566 1.1 joerg /// externally visible symbol. 3567 1.1 joerg bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 3568 1.1 joerg assert((doesThisDeclarationHaveABody() || willHaveBody() || 3569 1.1 joerg hasAttr<AliasAttr>()) && 3570 1.1 joerg "Must be a function definition"); 3571 1.1 joerg assert(isInlined() && "Function must be inline"); 3572 1.1 joerg ASTContext &Context = getASTContext(); 3573 1.1 joerg 3574 1.1 joerg if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 3575 1.1 joerg // Note: If you change the logic here, please change 3576 1.1 joerg // doesDeclarationForceExternallyVisibleDefinition as well. 3577 1.1 joerg // 3578 1.1 joerg // If it's not the case that both 'inline' and 'extern' are 3579 1.1 joerg // specified on the definition, then this inline definition is 3580 1.1 joerg // externally visible. 3581 1.1 joerg if (Context.getLangOpts().CPlusPlus) 3582 1.1 joerg return false; 3583 1.1 joerg if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) 3584 1.1 joerg return true; 3585 1.1 joerg 3586 1.1 joerg // If any declaration is 'inline' but not 'extern', then this definition 3587 1.1 joerg // is externally visible. 3588 1.1 joerg for (auto Redecl : redecls()) { 3589 1.1 joerg if (Redecl->isInlineSpecified() && 3590 1.1 joerg Redecl->getStorageClass() != SC_Extern) 3591 1.1 joerg return true; 3592 1.1 joerg } 3593 1.1 joerg 3594 1.1 joerg return false; 3595 1.1 joerg } 3596 1.1 joerg 3597 1.1 joerg // The rest of this function is C-only. 3598 1.1 joerg assert(!Context.getLangOpts().CPlusPlus && 3599 1.1 joerg "should not use C inline rules in C++"); 3600 1.1 joerg 3601 1.1 joerg // C99 6.7.4p6: 3602 1.1 joerg // [...] If all of the file scope declarations for a function in a 3603 1.1 joerg // translation unit include the inline function specifier without extern, 3604 1.1 joerg // then the definition in that translation unit is an inline definition. 3605 1.1 joerg for (auto Redecl : redecls()) { 3606 1.1 joerg if (RedeclForcesDefC99(Redecl)) 3607 1.1 joerg return true; 3608 1.1 joerg } 3609 1.1 joerg 3610 1.1 joerg // C99 6.7.4p6: 3611 1.1 joerg // An inline definition does not provide an external definition for the 3612 1.1 joerg // function, and does not forbid an external definition in another 3613 1.1 joerg // translation unit. 3614 1.1 joerg return false; 3615 1.1 joerg } 3616 1.1 joerg 3617 1.1 joerg /// getOverloadedOperator - Which C++ overloaded operator this 3618 1.1 joerg /// function represents, if any. 3619 1.1 joerg OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 3620 1.1 joerg if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 3621 1.1 joerg return getDeclName().getCXXOverloadedOperator(); 3622 1.1.1.2 joerg return OO_None; 3623 1.1 joerg } 3624 1.1 joerg 3625 1.1 joerg /// getLiteralIdentifier - The literal suffix identifier this function 3626 1.1 joerg /// represents, if any. 3627 1.1 joerg const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { 3628 1.1 joerg if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) 3629 1.1 joerg return getDeclName().getCXXLiteralIdentifier(); 3630 1.1.1.2 joerg return nullptr; 3631 1.1 joerg } 3632 1.1 joerg 3633 1.1 joerg FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { 3634 1.1 joerg if (TemplateOrSpecialization.isNull()) 3635 1.1 joerg return TK_NonTemplate; 3636 1.1 joerg if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) 3637 1.1 joerg return TK_FunctionTemplate; 3638 1.1 joerg if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) 3639 1.1 joerg return TK_MemberSpecialization; 3640 1.1 joerg if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) 3641 1.1 joerg return TK_FunctionTemplateSpecialization; 3642 1.1 joerg if (TemplateOrSpecialization.is 3643 1.1 joerg <DependentFunctionTemplateSpecializationInfo*>()) 3644 1.1 joerg return TK_DependentFunctionTemplateSpecialization; 3645 1.1 joerg 3646 1.1 joerg llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); 3647 1.1 joerg } 3648 1.1 joerg 3649 1.1 joerg FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 3650 1.1 joerg if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 3651 1.1 joerg return cast<FunctionDecl>(Info->getInstantiatedFrom()); 3652 1.1 joerg 3653 1.1 joerg return nullptr; 3654 1.1 joerg } 3655 1.1 joerg 3656 1.1 joerg MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 3657 1.1 joerg if (auto *MSI = 3658 1.1 joerg TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 3659 1.1 joerg return MSI; 3660 1.1 joerg if (auto *FTSI = TemplateOrSpecialization 3661 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo *>()) 3662 1.1 joerg return FTSI->getMemberSpecializationInfo(); 3663 1.1 joerg return nullptr; 3664 1.1 joerg } 3665 1.1 joerg 3666 1.1 joerg void 3667 1.1 joerg FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, 3668 1.1 joerg FunctionDecl *FD, 3669 1.1 joerg TemplateSpecializationKind TSK) { 3670 1.1 joerg assert(TemplateOrSpecialization.isNull() && 3671 1.1 joerg "Member function is already a specialization"); 3672 1.1 joerg MemberSpecializationInfo *Info 3673 1.1 joerg = new (C) MemberSpecializationInfo(FD, TSK); 3674 1.1 joerg TemplateOrSpecialization = Info; 3675 1.1 joerg } 3676 1.1 joerg 3677 1.1 joerg FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { 3678 1.1 joerg return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>(); 3679 1.1 joerg } 3680 1.1 joerg 3681 1.1 joerg void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { 3682 1.1 joerg assert(TemplateOrSpecialization.isNull() && 3683 1.1 joerg "Member function is already a specialization"); 3684 1.1 joerg TemplateOrSpecialization = Template; 3685 1.1 joerg } 3686 1.1 joerg 3687 1.1 joerg bool FunctionDecl::isImplicitlyInstantiable() const { 3688 1.1 joerg // If the function is invalid, it can't be implicitly instantiated. 3689 1.1 joerg if (isInvalidDecl()) 3690 1.1 joerg return false; 3691 1.1 joerg 3692 1.1 joerg switch (getTemplateSpecializationKindForInstantiation()) { 3693 1.1 joerg case TSK_Undeclared: 3694 1.1 joerg case TSK_ExplicitInstantiationDefinition: 3695 1.1 joerg case TSK_ExplicitSpecialization: 3696 1.1 joerg return false; 3697 1.1 joerg 3698 1.1 joerg case TSK_ImplicitInstantiation: 3699 1.1 joerg return true; 3700 1.1 joerg 3701 1.1 joerg case TSK_ExplicitInstantiationDeclaration: 3702 1.1 joerg // Handled below. 3703 1.1 joerg break; 3704 1.1 joerg } 3705 1.1 joerg 3706 1.1 joerg // Find the actual template from which we will instantiate. 3707 1.1 joerg const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 3708 1.1 joerg bool HasPattern = false; 3709 1.1 joerg if (PatternDecl) 3710 1.1 joerg HasPattern = PatternDecl->hasBody(PatternDecl); 3711 1.1 joerg 3712 1.1 joerg // C++0x [temp.explicit]p9: 3713 1.1 joerg // Except for inline functions, other explicit instantiation declarations 3714 1.1 joerg // have the effect of suppressing the implicit instantiation of the entity 3715 1.1 joerg // to which they refer. 3716 1.1 joerg if (!HasPattern || !PatternDecl) 3717 1.1 joerg return true; 3718 1.1 joerg 3719 1.1 joerg return PatternDecl->isInlined(); 3720 1.1 joerg } 3721 1.1 joerg 3722 1.1 joerg bool FunctionDecl::isTemplateInstantiation() const { 3723 1.1 joerg // FIXME: Remove this, it's not clear what it means. (Which template 3724 1.1 joerg // specialization kind?) 3725 1.1 joerg return clang::isTemplateInstantiation(getTemplateSpecializationKind()); 3726 1.1 joerg } 3727 1.1 joerg 3728 1.1.1.2 joerg FunctionDecl * 3729 1.1.1.2 joerg FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const { 3730 1.1 joerg // If this is a generic lambda call operator specialization, its 3731 1.1 joerg // instantiation pattern is always its primary template's pattern 3732 1.1 joerg // even if its primary template was instantiated from another 3733 1.1 joerg // member template (which happens with nested generic lambdas). 3734 1.1 joerg // Since a lambda's call operator's body is transformed eagerly, 3735 1.1 joerg // we don't have to go hunting for a prototype definition template 3736 1.1 joerg // (i.e. instantiated-from-member-template) to use as an instantiation 3737 1.1 joerg // pattern. 3738 1.1 joerg 3739 1.1 joerg if (isGenericLambdaCallOperatorSpecialization( 3740 1.1 joerg dyn_cast<CXXMethodDecl>(this))) { 3741 1.1 joerg assert(getPrimaryTemplate() && "not a generic lambda call operator?"); 3742 1.1 joerg return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); 3743 1.1 joerg } 3744 1.1 joerg 3745 1.1.1.2 joerg // Check for a declaration of this function that was instantiated from a 3746 1.1.1.2 joerg // friend definition. 3747 1.1.1.2 joerg const FunctionDecl *FD = nullptr; 3748 1.1.1.2 joerg if (!isDefined(FD, /*CheckForPendingFriendDefinition=*/true)) 3749 1.1.1.2 joerg FD = this; 3750 1.1.1.2 joerg 3751 1.1.1.2 joerg if (MemberSpecializationInfo *Info = FD->getMemberSpecializationInfo()) { 3752 1.1.1.2 joerg if (ForDefinition && 3753 1.1.1.2 joerg !clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) 3754 1.1 joerg return nullptr; 3755 1.1 joerg return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); 3756 1.1 joerg } 3757 1.1 joerg 3758 1.1.1.2 joerg if (ForDefinition && 3759 1.1.1.2 joerg !clang::isTemplateInstantiation(getTemplateSpecializationKind())) 3760 1.1 joerg return nullptr; 3761 1.1 joerg 3762 1.1 joerg if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 3763 1.1 joerg // If we hit a point where the user provided a specialization of this 3764 1.1 joerg // template, we're done looking. 3765 1.1.1.2 joerg while (!ForDefinition || !Primary->isMemberSpecialization()) { 3766 1.1 joerg auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); 3767 1.1 joerg if (!NewPrimary) 3768 1.1 joerg break; 3769 1.1 joerg Primary = NewPrimary; 3770 1.1 joerg } 3771 1.1 joerg 3772 1.1 joerg return getDefinitionOrSelf(Primary->getTemplatedDecl()); 3773 1.1 joerg } 3774 1.1 joerg 3775 1.1 joerg return nullptr; 3776 1.1 joerg } 3777 1.1 joerg 3778 1.1 joerg FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 3779 1.1 joerg if (FunctionTemplateSpecializationInfo *Info 3780 1.1 joerg = TemplateOrSpecialization 3781 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 3782 1.1 joerg return Info->getTemplate(); 3783 1.1 joerg } 3784 1.1 joerg return nullptr; 3785 1.1 joerg } 3786 1.1 joerg 3787 1.1 joerg FunctionTemplateSpecializationInfo * 3788 1.1 joerg FunctionDecl::getTemplateSpecializationInfo() const { 3789 1.1 joerg return TemplateOrSpecialization 3790 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo *>(); 3791 1.1 joerg } 3792 1.1 joerg 3793 1.1 joerg const TemplateArgumentList * 3794 1.1 joerg FunctionDecl::getTemplateSpecializationArgs() const { 3795 1.1 joerg if (FunctionTemplateSpecializationInfo *Info 3796 1.1 joerg = TemplateOrSpecialization 3797 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 3798 1.1 joerg return Info->TemplateArguments; 3799 1.1 joerg } 3800 1.1 joerg return nullptr; 3801 1.1 joerg } 3802 1.1 joerg 3803 1.1 joerg const ASTTemplateArgumentListInfo * 3804 1.1 joerg FunctionDecl::getTemplateSpecializationArgsAsWritten() const { 3805 1.1 joerg if (FunctionTemplateSpecializationInfo *Info 3806 1.1 joerg = TemplateOrSpecialization 3807 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 3808 1.1 joerg return Info->TemplateArgumentsAsWritten; 3809 1.1 joerg } 3810 1.1 joerg return nullptr; 3811 1.1 joerg } 3812 1.1 joerg 3813 1.1 joerg void 3814 1.1 joerg FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, 3815 1.1 joerg FunctionTemplateDecl *Template, 3816 1.1 joerg const TemplateArgumentList *TemplateArgs, 3817 1.1 joerg void *InsertPos, 3818 1.1 joerg TemplateSpecializationKind TSK, 3819 1.1 joerg const TemplateArgumentListInfo *TemplateArgsAsWritten, 3820 1.1 joerg SourceLocation PointOfInstantiation) { 3821 1.1 joerg assert((TemplateOrSpecialization.isNull() || 3822 1.1 joerg TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && 3823 1.1 joerg "Member function is already a specialization"); 3824 1.1 joerg assert(TSK != TSK_Undeclared && 3825 1.1 joerg "Must specify the type of function template specialization"); 3826 1.1 joerg assert((TemplateOrSpecialization.isNull() || 3827 1.1 joerg TSK == TSK_ExplicitSpecialization) && 3828 1.1 joerg "Member specialization must be an explicit specialization"); 3829 1.1 joerg FunctionTemplateSpecializationInfo *Info = 3830 1.1 joerg FunctionTemplateSpecializationInfo::Create( 3831 1.1 joerg C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, 3832 1.1 joerg PointOfInstantiation, 3833 1.1 joerg TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); 3834 1.1 joerg TemplateOrSpecialization = Info; 3835 1.1 joerg Template->addSpecialization(Info, InsertPos); 3836 1.1 joerg } 3837 1.1 joerg 3838 1.1 joerg void 3839 1.1 joerg FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, 3840 1.1 joerg const UnresolvedSetImpl &Templates, 3841 1.1 joerg const TemplateArgumentListInfo &TemplateArgs) { 3842 1.1 joerg assert(TemplateOrSpecialization.isNull()); 3843 1.1 joerg DependentFunctionTemplateSpecializationInfo *Info = 3844 1.1 joerg DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, 3845 1.1 joerg TemplateArgs); 3846 1.1 joerg TemplateOrSpecialization = Info; 3847 1.1 joerg } 3848 1.1 joerg 3849 1.1 joerg DependentFunctionTemplateSpecializationInfo * 3850 1.1 joerg FunctionDecl::getDependentSpecializationInfo() const { 3851 1.1 joerg return TemplateOrSpecialization 3852 1.1 joerg .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); 3853 1.1 joerg } 3854 1.1 joerg 3855 1.1 joerg DependentFunctionTemplateSpecializationInfo * 3856 1.1 joerg DependentFunctionTemplateSpecializationInfo::Create( 3857 1.1 joerg ASTContext &Context, const UnresolvedSetImpl &Ts, 3858 1.1 joerg const TemplateArgumentListInfo &TArgs) { 3859 1.1 joerg void *Buffer = Context.Allocate( 3860 1.1 joerg totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( 3861 1.1 joerg TArgs.size(), Ts.size())); 3862 1.1 joerg return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); 3863 1.1 joerg } 3864 1.1 joerg 3865 1.1 joerg DependentFunctionTemplateSpecializationInfo:: 3866 1.1 joerg DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, 3867 1.1 joerg const TemplateArgumentListInfo &TArgs) 3868 1.1 joerg : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { 3869 1.1 joerg NumTemplates = Ts.size(); 3870 1.1 joerg NumArgs = TArgs.size(); 3871 1.1 joerg 3872 1.1 joerg FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); 3873 1.1 joerg for (unsigned I = 0, E = Ts.size(); I != E; ++I) 3874 1.1 joerg TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); 3875 1.1 joerg 3876 1.1 joerg TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); 3877 1.1 joerg for (unsigned I = 0, E = TArgs.size(); I != E; ++I) 3878 1.1 joerg new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); 3879 1.1 joerg } 3880 1.1 joerg 3881 1.1 joerg TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 3882 1.1 joerg // For a function template specialization, query the specialization 3883 1.1 joerg // information object. 3884 1.1 joerg if (FunctionTemplateSpecializationInfo *FTSInfo = 3885 1.1 joerg TemplateOrSpecialization 3886 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo *>()) 3887 1.1 joerg return FTSInfo->getTemplateSpecializationKind(); 3888 1.1 joerg 3889 1.1 joerg if (MemberSpecializationInfo *MSInfo = 3890 1.1 joerg TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 3891 1.1 joerg return MSInfo->getTemplateSpecializationKind(); 3892 1.1 joerg 3893 1.1 joerg return TSK_Undeclared; 3894 1.1 joerg } 3895 1.1 joerg 3896 1.1 joerg TemplateSpecializationKind 3897 1.1 joerg FunctionDecl::getTemplateSpecializationKindForInstantiation() const { 3898 1.1 joerg // This is the same as getTemplateSpecializationKind(), except that for a 3899 1.1 joerg // function that is both a function template specialization and a member 3900 1.1 joerg // specialization, we prefer the member specialization information. Eg: 3901 1.1 joerg // 3902 1.1 joerg // template<typename T> struct A { 3903 1.1 joerg // template<typename U> void f() {} 3904 1.1 joerg // template<> void f<int>() {} 3905 1.1 joerg // }; 3906 1.1 joerg // 3907 1.1 joerg // For A<int>::f<int>(): 3908 1.1 joerg // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization 3909 1.1 joerg // * getTemplateSpecializationKindForInstantiation() will return 3910 1.1 joerg // TSK_ImplicitInstantiation 3911 1.1 joerg // 3912 1.1 joerg // This reflects the facts that A<int>::f<int> is an explicit specialization 3913 1.1 joerg // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated 3914 1.1 joerg // from A::f<int> if a definition is needed. 3915 1.1 joerg if (FunctionTemplateSpecializationInfo *FTSInfo = 3916 1.1 joerg TemplateOrSpecialization 3917 1.1 joerg .dyn_cast<FunctionTemplateSpecializationInfo *>()) { 3918 1.1 joerg if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) 3919 1.1 joerg return MSInfo->getTemplateSpecializationKind(); 3920 1.1 joerg return FTSInfo->getTemplateSpecializationKind(); 3921 1.1 joerg } 3922 1.1 joerg 3923 1.1 joerg if (MemberSpecializationInfo *MSInfo = 3924 1.1 joerg TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 3925 1.1 joerg return MSInfo->getTemplateSpecializationKind(); 3926 1.1 joerg 3927 1.1 joerg return TSK_Undeclared; 3928 1.1 joerg } 3929 1.1 joerg 3930 1.1 joerg void 3931 1.1 joerg FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 3932 1.1 joerg SourceLocation PointOfInstantiation) { 3933 1.1 joerg if (FunctionTemplateSpecializationInfo *FTSInfo 3934 1.1 joerg = TemplateOrSpecialization.dyn_cast< 3935 1.1 joerg FunctionTemplateSpecializationInfo*>()) { 3936 1.1 joerg FTSInfo->setTemplateSpecializationKind(TSK); 3937 1.1 joerg if (TSK != TSK_ExplicitSpecialization && 3938 1.1 joerg PointOfInstantiation.isValid() && 3939 1.1 joerg FTSInfo->getPointOfInstantiation().isInvalid()) { 3940 1.1 joerg FTSInfo->setPointOfInstantiation(PointOfInstantiation); 3941 1.1 joerg if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 3942 1.1 joerg L->InstantiationRequested(this); 3943 1.1 joerg } 3944 1.1 joerg } else if (MemberSpecializationInfo *MSInfo 3945 1.1 joerg = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 3946 1.1 joerg MSInfo->setTemplateSpecializationKind(TSK); 3947 1.1 joerg if (TSK != TSK_ExplicitSpecialization && 3948 1.1 joerg PointOfInstantiation.isValid() && 3949 1.1 joerg MSInfo->getPointOfInstantiation().isInvalid()) { 3950 1.1 joerg MSInfo->setPointOfInstantiation(PointOfInstantiation); 3951 1.1 joerg if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 3952 1.1 joerg L->InstantiationRequested(this); 3953 1.1 joerg } 3954 1.1 joerg } else 3955 1.1 joerg llvm_unreachable("Function cannot have a template specialization kind"); 3956 1.1 joerg } 3957 1.1 joerg 3958 1.1 joerg SourceLocation FunctionDecl::getPointOfInstantiation() const { 3959 1.1 joerg if (FunctionTemplateSpecializationInfo *FTSInfo 3960 1.1 joerg = TemplateOrSpecialization.dyn_cast< 3961 1.1 joerg FunctionTemplateSpecializationInfo*>()) 3962 1.1 joerg return FTSInfo->getPointOfInstantiation(); 3963 1.1.1.2 joerg if (MemberSpecializationInfo *MSInfo = 3964 1.1.1.2 joerg TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 3965 1.1 joerg return MSInfo->getPointOfInstantiation(); 3966 1.1 joerg 3967 1.1 joerg return SourceLocation(); 3968 1.1 joerg } 3969 1.1 joerg 3970 1.1 joerg bool FunctionDecl::isOutOfLine() const { 3971 1.1 joerg if (Decl::isOutOfLine()) 3972 1.1 joerg return true; 3973 1.1 joerg 3974 1.1 joerg // If this function was instantiated from a member function of a 3975 1.1 joerg // class template, check whether that member function was defined out-of-line. 3976 1.1 joerg if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 3977 1.1 joerg const FunctionDecl *Definition; 3978 1.1 joerg if (FD->hasBody(Definition)) 3979 1.1 joerg return Definition->isOutOfLine(); 3980 1.1 joerg } 3981 1.1 joerg 3982 1.1 joerg // If this function was instantiated from a function template, 3983 1.1 joerg // check whether that function template was defined out-of-line. 3984 1.1 joerg if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 3985 1.1 joerg const FunctionDecl *Definition; 3986 1.1 joerg if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) 3987 1.1 joerg return Definition->isOutOfLine(); 3988 1.1 joerg } 3989 1.1 joerg 3990 1.1 joerg return false; 3991 1.1 joerg } 3992 1.1 joerg 3993 1.1 joerg SourceRange FunctionDecl::getSourceRange() const { 3994 1.1 joerg return SourceRange(getOuterLocStart(), EndRangeLoc); 3995 1.1 joerg } 3996 1.1 joerg 3997 1.1 joerg unsigned FunctionDecl::getMemoryFunctionKind() const { 3998 1.1 joerg IdentifierInfo *FnInfo = getIdentifier(); 3999 1.1 joerg 4000 1.1 joerg if (!FnInfo) 4001 1.1 joerg return 0; 4002 1.1 joerg 4003 1.1 joerg // Builtin handling. 4004 1.1 joerg switch (getBuiltinID()) { 4005 1.1 joerg case Builtin::BI__builtin_memset: 4006 1.1 joerg case Builtin::BI__builtin___memset_chk: 4007 1.1 joerg case Builtin::BImemset: 4008 1.1 joerg return Builtin::BImemset; 4009 1.1 joerg 4010 1.1 joerg case Builtin::BI__builtin_memcpy: 4011 1.1 joerg case Builtin::BI__builtin___memcpy_chk: 4012 1.1 joerg case Builtin::BImemcpy: 4013 1.1 joerg return Builtin::BImemcpy; 4014 1.1 joerg 4015 1.1.1.2 joerg case Builtin::BI__builtin_mempcpy: 4016 1.1.1.2 joerg case Builtin::BI__builtin___mempcpy_chk: 4017 1.1.1.2 joerg case Builtin::BImempcpy: 4018 1.1.1.2 joerg return Builtin::BImempcpy; 4019 1.1.1.2 joerg 4020 1.1 joerg case Builtin::BI__builtin_memmove: 4021 1.1 joerg case Builtin::BI__builtin___memmove_chk: 4022 1.1 joerg case Builtin::BImemmove: 4023 1.1 joerg return Builtin::BImemmove; 4024 1.1 joerg 4025 1.1 joerg case Builtin::BIstrlcpy: 4026 1.1 joerg case Builtin::BI__builtin___strlcpy_chk: 4027 1.1 joerg return Builtin::BIstrlcpy; 4028 1.1 joerg 4029 1.1 joerg case Builtin::BIstrlcat: 4030 1.1 joerg case Builtin::BI__builtin___strlcat_chk: 4031 1.1 joerg return Builtin::BIstrlcat; 4032 1.1 joerg 4033 1.1 joerg case Builtin::BI__builtin_memcmp: 4034 1.1 joerg case Builtin::BImemcmp: 4035 1.1 joerg return Builtin::BImemcmp; 4036 1.1 joerg 4037 1.1 joerg case Builtin::BI__builtin_bcmp: 4038 1.1 joerg case Builtin::BIbcmp: 4039 1.1 joerg return Builtin::BIbcmp; 4040 1.1 joerg 4041 1.1 joerg case Builtin::BI__builtin_strncpy: 4042 1.1 joerg case Builtin::BI__builtin___strncpy_chk: 4043 1.1 joerg case Builtin::BIstrncpy: 4044 1.1 joerg return Builtin::BIstrncpy; 4045 1.1 joerg 4046 1.1 joerg case Builtin::BI__builtin_strncmp: 4047 1.1 joerg case Builtin::BIstrncmp: 4048 1.1 joerg return Builtin::BIstrncmp; 4049 1.1 joerg 4050 1.1 joerg case Builtin::BI__builtin_strncasecmp: 4051 1.1 joerg case Builtin::BIstrncasecmp: 4052 1.1 joerg return Builtin::BIstrncasecmp; 4053 1.1 joerg 4054 1.1 joerg case Builtin::BI__builtin_strncat: 4055 1.1 joerg case Builtin::BI__builtin___strncat_chk: 4056 1.1 joerg case Builtin::BIstrncat: 4057 1.1 joerg return Builtin::BIstrncat; 4058 1.1 joerg 4059 1.1 joerg case Builtin::BI__builtin_strndup: 4060 1.1 joerg case Builtin::BIstrndup: 4061 1.1 joerg return Builtin::BIstrndup; 4062 1.1 joerg 4063 1.1 joerg case Builtin::BI__builtin_strlen: 4064 1.1 joerg case Builtin::BIstrlen: 4065 1.1 joerg return Builtin::BIstrlen; 4066 1.1 joerg 4067 1.1 joerg case Builtin::BI__builtin_bzero: 4068 1.1 joerg case Builtin::BIbzero: 4069 1.1 joerg return Builtin::BIbzero; 4070 1.1 joerg 4071 1.1.1.2 joerg case Builtin::BIfree: 4072 1.1.1.2 joerg return Builtin::BIfree; 4073 1.1.1.2 joerg 4074 1.1 joerg default: 4075 1.1 joerg if (isExternC()) { 4076 1.1 joerg if (FnInfo->isStr("memset")) 4077 1.1 joerg return Builtin::BImemset; 4078 1.1.1.2 joerg if (FnInfo->isStr("memcpy")) 4079 1.1 joerg return Builtin::BImemcpy; 4080 1.1.1.2 joerg if (FnInfo->isStr("mempcpy")) 4081 1.1.1.2 joerg return Builtin::BImempcpy; 4082 1.1.1.2 joerg if (FnInfo->isStr("memmove")) 4083 1.1 joerg return Builtin::BImemmove; 4084 1.1.1.2 joerg if (FnInfo->isStr("memcmp")) 4085 1.1 joerg return Builtin::BImemcmp; 4086 1.1.1.2 joerg if (FnInfo->isStr("bcmp")) 4087 1.1 joerg return Builtin::BIbcmp; 4088 1.1.1.2 joerg if (FnInfo->isStr("strncpy")) 4089 1.1 joerg return Builtin::BIstrncpy; 4090 1.1.1.2 joerg if (FnInfo->isStr("strncmp")) 4091 1.1 joerg return Builtin::BIstrncmp; 4092 1.1.1.2 joerg if (FnInfo->isStr("strncasecmp")) 4093 1.1 joerg return Builtin::BIstrncasecmp; 4094 1.1.1.2 joerg if (FnInfo->isStr("strncat")) 4095 1.1 joerg return Builtin::BIstrncat; 4096 1.1.1.2 joerg if (FnInfo->isStr("strndup")) 4097 1.1 joerg return Builtin::BIstrndup; 4098 1.1.1.2 joerg if (FnInfo->isStr("strlen")) 4099 1.1 joerg return Builtin::BIstrlen; 4100 1.1.1.2 joerg if (FnInfo->isStr("bzero")) 4101 1.1 joerg return Builtin::BIbzero; 4102 1.1.1.2 joerg } else if (isInStdNamespace()) { 4103 1.1.1.2 joerg if (FnInfo->isStr("free")) 4104 1.1.1.2 joerg return Builtin::BIfree; 4105 1.1 joerg } 4106 1.1 joerg break; 4107 1.1 joerg } 4108 1.1 joerg return 0; 4109 1.1 joerg } 4110 1.1 joerg 4111 1.1 joerg unsigned FunctionDecl::getODRHash() const { 4112 1.1 joerg assert(hasODRHash()); 4113 1.1 joerg return ODRHash; 4114 1.1 joerg } 4115 1.1 joerg 4116 1.1 joerg unsigned FunctionDecl::getODRHash() { 4117 1.1 joerg if (hasODRHash()) 4118 1.1 joerg return ODRHash; 4119 1.1 joerg 4120 1.1 joerg if (auto *FT = getInstantiatedFromMemberFunction()) { 4121 1.1 joerg setHasODRHash(true); 4122 1.1 joerg ODRHash = FT->getODRHash(); 4123 1.1 joerg return ODRHash; 4124 1.1 joerg } 4125 1.1 joerg 4126 1.1 joerg class ODRHash Hash; 4127 1.1 joerg Hash.AddFunctionDecl(this); 4128 1.1 joerg setHasODRHash(true); 4129 1.1 joerg ODRHash = Hash.CalculateHash(); 4130 1.1 joerg return ODRHash; 4131 1.1 joerg } 4132 1.1 joerg 4133 1.1 joerg //===----------------------------------------------------------------------===// 4134 1.1 joerg // FieldDecl Implementation 4135 1.1 joerg //===----------------------------------------------------------------------===// 4136 1.1 joerg 4137 1.1 joerg FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, 4138 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc, 4139 1.1 joerg IdentifierInfo *Id, QualType T, 4140 1.1 joerg TypeSourceInfo *TInfo, Expr *BW, bool Mutable, 4141 1.1 joerg InClassInitStyle InitStyle) { 4142 1.1 joerg return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, 4143 1.1 joerg BW, Mutable, InitStyle); 4144 1.1 joerg } 4145 1.1 joerg 4146 1.1 joerg FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4147 1.1 joerg return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), 4148 1.1 joerg SourceLocation(), nullptr, QualType(), nullptr, 4149 1.1 joerg nullptr, false, ICIS_NoInit); 4150 1.1 joerg } 4151 1.1 joerg 4152 1.1 joerg bool FieldDecl::isAnonymousStructOrUnion() const { 4153 1.1 joerg if (!isImplicit() || getDeclName()) 4154 1.1 joerg return false; 4155 1.1 joerg 4156 1.1 joerg if (const auto *Record = getType()->getAs<RecordType>()) 4157 1.1 joerg return Record->getDecl()->isAnonymousStructOrUnion(); 4158 1.1 joerg 4159 1.1 joerg return false; 4160 1.1 joerg } 4161 1.1 joerg 4162 1.1 joerg unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { 4163 1.1 joerg assert(isBitField() && "not a bitfield"); 4164 1.1 joerg return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); 4165 1.1 joerg } 4166 1.1 joerg 4167 1.1 joerg bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { 4168 1.1 joerg return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && 4169 1.1 joerg getBitWidthValue(Ctx) == 0; 4170 1.1 joerg } 4171 1.1 joerg 4172 1.1 joerg bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { 4173 1.1 joerg if (isZeroLengthBitField(Ctx)) 4174 1.1 joerg return true; 4175 1.1 joerg 4176 1.1 joerg // C++2a [intro.object]p7: 4177 1.1 joerg // An object has nonzero size if it 4178 1.1 joerg // -- is not a potentially-overlapping subobject, or 4179 1.1 joerg if (!hasAttr<NoUniqueAddressAttr>()) 4180 1.1 joerg return false; 4181 1.1 joerg 4182 1.1 joerg // -- is not of class type, or 4183 1.1 joerg const auto *RT = getType()->getAs<RecordType>(); 4184 1.1 joerg if (!RT) 4185 1.1 joerg return false; 4186 1.1 joerg const RecordDecl *RD = RT->getDecl()->getDefinition(); 4187 1.1 joerg if (!RD) { 4188 1.1 joerg assert(isInvalidDecl() && "valid field has incomplete type"); 4189 1.1 joerg return false; 4190 1.1 joerg } 4191 1.1 joerg 4192 1.1 joerg // -- [has] virtual member functions or virtual base classes, or 4193 1.1 joerg // -- has subobjects of nonzero size or bit-fields of nonzero length 4194 1.1 joerg const auto *CXXRD = cast<CXXRecordDecl>(RD); 4195 1.1 joerg if (!CXXRD->isEmpty()) 4196 1.1 joerg return false; 4197 1.1 joerg 4198 1.1 joerg // Otherwise, [...] the circumstances under which the object has zero size 4199 1.1 joerg // are implementation-defined. 4200 1.1 joerg // FIXME: This might be Itanium ABI specific; we don't yet know what the MS 4201 1.1 joerg // ABI will do. 4202 1.1 joerg return true; 4203 1.1 joerg } 4204 1.1 joerg 4205 1.1 joerg unsigned FieldDecl::getFieldIndex() const { 4206 1.1 joerg const FieldDecl *Canonical = getCanonicalDecl(); 4207 1.1 joerg if (Canonical != this) 4208 1.1 joerg return Canonical->getFieldIndex(); 4209 1.1 joerg 4210 1.1 joerg if (CachedFieldIndex) return CachedFieldIndex - 1; 4211 1.1 joerg 4212 1.1 joerg unsigned Index = 0; 4213 1.1 joerg const RecordDecl *RD = getParent()->getDefinition(); 4214 1.1 joerg assert(RD && "requested index for field of struct with no definition"); 4215 1.1 joerg 4216 1.1 joerg for (auto *Field : RD->fields()) { 4217 1.1 joerg Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; 4218 1.1 joerg ++Index; 4219 1.1 joerg } 4220 1.1 joerg 4221 1.1 joerg assert(CachedFieldIndex && "failed to find field in parent"); 4222 1.1 joerg return CachedFieldIndex - 1; 4223 1.1 joerg } 4224 1.1 joerg 4225 1.1 joerg SourceRange FieldDecl::getSourceRange() const { 4226 1.1 joerg const Expr *FinalExpr = getInClassInitializer(); 4227 1.1 joerg if (!FinalExpr) 4228 1.1 joerg FinalExpr = getBitWidth(); 4229 1.1 joerg if (FinalExpr) 4230 1.1 joerg return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); 4231 1.1 joerg return DeclaratorDecl::getSourceRange(); 4232 1.1 joerg } 4233 1.1 joerg 4234 1.1 joerg void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { 4235 1.1 joerg assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && 4236 1.1 joerg "capturing type in non-lambda or captured record."); 4237 1.1 joerg assert(InitStorage.getInt() == ISK_NoInit && 4238 1.1 joerg InitStorage.getPointer() == nullptr && 4239 1.1 joerg "bit width, initializer or captured type already set"); 4240 1.1 joerg InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), 4241 1.1 joerg ISK_CapturedVLAType); 4242 1.1 joerg } 4243 1.1 joerg 4244 1.1 joerg //===----------------------------------------------------------------------===// 4245 1.1 joerg // TagDecl Implementation 4246 1.1 joerg //===----------------------------------------------------------------------===// 4247 1.1 joerg 4248 1.1 joerg TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, 4249 1.1 joerg SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, 4250 1.1 joerg SourceLocation StartL) 4251 1.1 joerg : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), 4252 1.1 joerg TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { 4253 1.1 joerg assert((DK != Enum || TK == TTK_Enum) && 4254 1.1 joerg "EnumDecl not matched with TTK_Enum"); 4255 1.1 joerg setPreviousDecl(PrevDecl); 4256 1.1 joerg setTagKind(TK); 4257 1.1 joerg setCompleteDefinition(false); 4258 1.1 joerg setBeingDefined(false); 4259 1.1 joerg setEmbeddedInDeclarator(false); 4260 1.1 joerg setFreeStanding(false); 4261 1.1 joerg setCompleteDefinitionRequired(false); 4262 1.1 joerg } 4263 1.1 joerg 4264 1.1 joerg SourceLocation TagDecl::getOuterLocStart() const { 4265 1.1 joerg return getTemplateOrInnerLocStart(this); 4266 1.1 joerg } 4267 1.1 joerg 4268 1.1 joerg SourceRange TagDecl::getSourceRange() const { 4269 1.1 joerg SourceLocation RBraceLoc = BraceRange.getEnd(); 4270 1.1 joerg SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 4271 1.1 joerg return SourceRange(getOuterLocStart(), E); 4272 1.1 joerg } 4273 1.1 joerg 4274 1.1 joerg TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } 4275 1.1 joerg 4276 1.1 joerg void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { 4277 1.1 joerg TypedefNameDeclOrQualifier = TDD; 4278 1.1 joerg if (const Type *T = getTypeForDecl()) { 4279 1.1 joerg (void)T; 4280 1.1 joerg assert(T->isLinkageValid()); 4281 1.1 joerg } 4282 1.1 joerg assert(isLinkageValid()); 4283 1.1 joerg } 4284 1.1 joerg 4285 1.1 joerg void TagDecl::startDefinition() { 4286 1.1 joerg setBeingDefined(true); 4287 1.1 joerg 4288 1.1 joerg if (auto *D = dyn_cast<CXXRecordDecl>(this)) { 4289 1.1 joerg struct CXXRecordDecl::DefinitionData *Data = 4290 1.1 joerg new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); 4291 1.1 joerg for (auto I : redecls()) 4292 1.1 joerg cast<CXXRecordDecl>(I)->DefinitionData = Data; 4293 1.1 joerg } 4294 1.1 joerg } 4295 1.1 joerg 4296 1.1 joerg void TagDecl::completeDefinition() { 4297 1.1 joerg assert((!isa<CXXRecordDecl>(this) || 4298 1.1 joerg cast<CXXRecordDecl>(this)->hasDefinition()) && 4299 1.1 joerg "definition completed but not started"); 4300 1.1 joerg 4301 1.1 joerg setCompleteDefinition(true); 4302 1.1 joerg setBeingDefined(false); 4303 1.1 joerg 4304 1.1 joerg if (ASTMutationListener *L = getASTMutationListener()) 4305 1.1 joerg L->CompletedTagDefinition(this); 4306 1.1 joerg } 4307 1.1 joerg 4308 1.1 joerg TagDecl *TagDecl::getDefinition() const { 4309 1.1 joerg if (isCompleteDefinition()) 4310 1.1 joerg return const_cast<TagDecl *>(this); 4311 1.1 joerg 4312 1.1 joerg // If it's possible for us to have an out-of-date definition, check now. 4313 1.1 joerg if (mayHaveOutOfDateDef()) { 4314 1.1 joerg if (IdentifierInfo *II = getIdentifier()) { 4315 1.1 joerg if (II->isOutOfDate()) { 4316 1.1 joerg updateOutOfDate(*II); 4317 1.1 joerg } 4318 1.1 joerg } 4319 1.1 joerg } 4320 1.1 joerg 4321 1.1 joerg if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) 4322 1.1 joerg return CXXRD->getDefinition(); 4323 1.1 joerg 4324 1.1 joerg for (auto R : redecls()) 4325 1.1 joerg if (R->isCompleteDefinition()) 4326 1.1 joerg return R; 4327 1.1 joerg 4328 1.1 joerg return nullptr; 4329 1.1 joerg } 4330 1.1 joerg 4331 1.1 joerg void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 4332 1.1 joerg if (QualifierLoc) { 4333 1.1 joerg // Make sure the extended qualifier info is allocated. 4334 1.1 joerg if (!hasExtInfo()) 4335 1.1 joerg TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 4336 1.1 joerg // Set qualifier info. 4337 1.1 joerg getExtInfo()->QualifierLoc = QualifierLoc; 4338 1.1 joerg } else { 4339 1.1 joerg // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 4340 1.1 joerg if (hasExtInfo()) { 4341 1.1 joerg if (getExtInfo()->NumTemplParamLists == 0) { 4342 1.1 joerg getASTContext().Deallocate(getExtInfo()); 4343 1.1 joerg TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; 4344 1.1 joerg } 4345 1.1 joerg else 4346 1.1 joerg getExtInfo()->QualifierLoc = QualifierLoc; 4347 1.1 joerg } 4348 1.1 joerg } 4349 1.1 joerg } 4350 1.1 joerg 4351 1.1 joerg void TagDecl::setTemplateParameterListsInfo( 4352 1.1 joerg ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 4353 1.1 joerg assert(!TPLists.empty()); 4354 1.1 joerg // Make sure the extended decl info is allocated. 4355 1.1 joerg if (!hasExtInfo()) 4356 1.1 joerg // Allocate external info struct. 4357 1.1 joerg TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 4358 1.1 joerg // Set the template parameter lists info. 4359 1.1 joerg getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); 4360 1.1 joerg } 4361 1.1 joerg 4362 1.1 joerg //===----------------------------------------------------------------------===// 4363 1.1 joerg // EnumDecl Implementation 4364 1.1 joerg //===----------------------------------------------------------------------===// 4365 1.1 joerg 4366 1.1 joerg EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 4367 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, 4368 1.1 joerg bool Scoped, bool ScopedUsingClassTag, bool Fixed) 4369 1.1 joerg : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { 4370 1.1 joerg assert(Scoped || !ScopedUsingClassTag); 4371 1.1 joerg IntegerType = nullptr; 4372 1.1 joerg setNumPositiveBits(0); 4373 1.1 joerg setNumNegativeBits(0); 4374 1.1 joerg setScoped(Scoped); 4375 1.1 joerg setScopedUsingClassTag(ScopedUsingClassTag); 4376 1.1 joerg setFixed(Fixed); 4377 1.1 joerg setHasODRHash(false); 4378 1.1 joerg ODRHash = 0; 4379 1.1 joerg } 4380 1.1 joerg 4381 1.1 joerg void EnumDecl::anchor() {} 4382 1.1 joerg 4383 1.1 joerg EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, 4384 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc, 4385 1.1 joerg IdentifierInfo *Id, 4386 1.1 joerg EnumDecl *PrevDecl, bool IsScoped, 4387 1.1 joerg bool IsScopedUsingClassTag, bool IsFixed) { 4388 1.1 joerg auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, 4389 1.1 joerg IsScoped, IsScopedUsingClassTag, IsFixed); 4390 1.1 joerg Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 4391 1.1 joerg C.getTypeDeclType(Enum, PrevDecl); 4392 1.1 joerg return Enum; 4393 1.1 joerg } 4394 1.1 joerg 4395 1.1 joerg EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4396 1.1 joerg EnumDecl *Enum = 4397 1.1 joerg new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), 4398 1.1 joerg nullptr, nullptr, false, false, false); 4399 1.1 joerg Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 4400 1.1 joerg return Enum; 4401 1.1 joerg } 4402 1.1 joerg 4403 1.1 joerg SourceRange EnumDecl::getIntegerTypeRange() const { 4404 1.1 joerg if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) 4405 1.1 joerg return TI->getTypeLoc().getSourceRange(); 4406 1.1 joerg return SourceRange(); 4407 1.1 joerg } 4408 1.1 joerg 4409 1.1 joerg void EnumDecl::completeDefinition(QualType NewType, 4410 1.1 joerg QualType NewPromotionType, 4411 1.1 joerg unsigned NumPositiveBits, 4412 1.1 joerg unsigned NumNegativeBits) { 4413 1.1 joerg assert(!isCompleteDefinition() && "Cannot redefine enums!"); 4414 1.1 joerg if (!IntegerType) 4415 1.1 joerg IntegerType = NewType.getTypePtr(); 4416 1.1 joerg PromotionType = NewPromotionType; 4417 1.1 joerg setNumPositiveBits(NumPositiveBits); 4418 1.1 joerg setNumNegativeBits(NumNegativeBits); 4419 1.1 joerg TagDecl::completeDefinition(); 4420 1.1 joerg } 4421 1.1 joerg 4422 1.1 joerg bool EnumDecl::isClosed() const { 4423 1.1 joerg if (const auto *A = getAttr<EnumExtensibilityAttr>()) 4424 1.1 joerg return A->getExtensibility() == EnumExtensibilityAttr::Closed; 4425 1.1 joerg return true; 4426 1.1 joerg } 4427 1.1 joerg 4428 1.1 joerg bool EnumDecl::isClosedFlag() const { 4429 1.1 joerg return isClosed() && hasAttr<FlagEnumAttr>(); 4430 1.1 joerg } 4431 1.1 joerg 4432 1.1 joerg bool EnumDecl::isClosedNonFlag() const { 4433 1.1 joerg return isClosed() && !hasAttr<FlagEnumAttr>(); 4434 1.1 joerg } 4435 1.1 joerg 4436 1.1 joerg TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { 4437 1.1 joerg if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 4438 1.1 joerg return MSI->getTemplateSpecializationKind(); 4439 1.1 joerg 4440 1.1 joerg return TSK_Undeclared; 4441 1.1 joerg } 4442 1.1 joerg 4443 1.1 joerg void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 4444 1.1 joerg SourceLocation PointOfInstantiation) { 4445 1.1 joerg MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 4446 1.1 joerg assert(MSI && "Not an instantiated member enumeration?"); 4447 1.1 joerg MSI->setTemplateSpecializationKind(TSK); 4448 1.1 joerg if (TSK != TSK_ExplicitSpecialization && 4449 1.1 joerg PointOfInstantiation.isValid() && 4450 1.1 joerg MSI->getPointOfInstantiation().isInvalid()) 4451 1.1 joerg MSI->setPointOfInstantiation(PointOfInstantiation); 4452 1.1 joerg } 4453 1.1 joerg 4454 1.1 joerg EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { 4455 1.1 joerg if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 4456 1.1 joerg if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { 4457 1.1 joerg EnumDecl *ED = getInstantiatedFromMemberEnum(); 4458 1.1 joerg while (auto *NewED = ED->getInstantiatedFromMemberEnum()) 4459 1.1 joerg ED = NewED; 4460 1.1 joerg return getDefinitionOrSelf(ED); 4461 1.1 joerg } 4462 1.1 joerg } 4463 1.1 joerg 4464 1.1 joerg assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && 4465 1.1 joerg "couldn't find pattern for enum instantiation"); 4466 1.1 joerg return nullptr; 4467 1.1 joerg } 4468 1.1 joerg 4469 1.1 joerg EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { 4470 1.1 joerg if (SpecializationInfo) 4471 1.1 joerg return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); 4472 1.1 joerg 4473 1.1 joerg return nullptr; 4474 1.1 joerg } 4475 1.1 joerg 4476 1.1 joerg void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, 4477 1.1 joerg TemplateSpecializationKind TSK) { 4478 1.1 joerg assert(!SpecializationInfo && "Member enum is already a specialization"); 4479 1.1 joerg SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); 4480 1.1 joerg } 4481 1.1 joerg 4482 1.1 joerg unsigned EnumDecl::getODRHash() { 4483 1.1 joerg if (hasODRHash()) 4484 1.1 joerg return ODRHash; 4485 1.1 joerg 4486 1.1 joerg class ODRHash Hash; 4487 1.1 joerg Hash.AddEnumDecl(this); 4488 1.1 joerg setHasODRHash(true); 4489 1.1 joerg ODRHash = Hash.CalculateHash(); 4490 1.1 joerg return ODRHash; 4491 1.1 joerg } 4492 1.1 joerg 4493 1.1 joerg //===----------------------------------------------------------------------===// 4494 1.1 joerg // RecordDecl Implementation 4495 1.1 joerg //===----------------------------------------------------------------------===// 4496 1.1 joerg 4497 1.1 joerg RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, 4498 1.1 joerg DeclContext *DC, SourceLocation StartLoc, 4499 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id, 4500 1.1 joerg RecordDecl *PrevDecl) 4501 1.1 joerg : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { 4502 1.1 joerg assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); 4503 1.1 joerg setHasFlexibleArrayMember(false); 4504 1.1 joerg setAnonymousStructOrUnion(false); 4505 1.1 joerg setHasObjectMember(false); 4506 1.1 joerg setHasVolatileMember(false); 4507 1.1 joerg setHasLoadedFieldsFromExternalStorage(false); 4508 1.1 joerg setNonTrivialToPrimitiveDefaultInitialize(false); 4509 1.1 joerg setNonTrivialToPrimitiveCopy(false); 4510 1.1 joerg setNonTrivialToPrimitiveDestroy(false); 4511 1.1 joerg setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); 4512 1.1 joerg setHasNonTrivialToPrimitiveDestructCUnion(false); 4513 1.1 joerg setHasNonTrivialToPrimitiveCopyCUnion(false); 4514 1.1 joerg setParamDestroyedInCallee(false); 4515 1.1 joerg setArgPassingRestrictions(APK_CanPassInRegs); 4516 1.1 joerg } 4517 1.1 joerg 4518 1.1 joerg RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, 4519 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc, 4520 1.1 joerg IdentifierInfo *Id, RecordDecl* PrevDecl) { 4521 1.1 joerg RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, 4522 1.1 joerg StartLoc, IdLoc, Id, PrevDecl); 4523 1.1 joerg R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 4524 1.1 joerg 4525 1.1 joerg C.getTypeDeclType(R, PrevDecl); 4526 1.1 joerg return R; 4527 1.1 joerg } 4528 1.1 joerg 4529 1.1 joerg RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { 4530 1.1 joerg RecordDecl *R = 4531 1.1 joerg new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), 4532 1.1 joerg SourceLocation(), nullptr, nullptr); 4533 1.1 joerg R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 4534 1.1 joerg return R; 4535 1.1 joerg } 4536 1.1 joerg 4537 1.1 joerg bool RecordDecl::isInjectedClassName() const { 4538 1.1 joerg return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 4539 1.1 joerg cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 4540 1.1 joerg } 4541 1.1 joerg 4542 1.1 joerg bool RecordDecl::isLambda() const { 4543 1.1 joerg if (auto RD = dyn_cast<CXXRecordDecl>(this)) 4544 1.1 joerg return RD->isLambda(); 4545 1.1 joerg return false; 4546 1.1 joerg } 4547 1.1 joerg 4548 1.1 joerg bool RecordDecl::isCapturedRecord() const { 4549 1.1 joerg return hasAttr<CapturedRecordAttr>(); 4550 1.1 joerg } 4551 1.1 joerg 4552 1.1 joerg void RecordDecl::setCapturedRecord() { 4553 1.1 joerg addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); 4554 1.1 joerg } 4555 1.1 joerg 4556 1.1.1.2 joerg bool RecordDecl::isOrContainsUnion() const { 4557 1.1.1.2 joerg if (isUnion()) 4558 1.1.1.2 joerg return true; 4559 1.1.1.2 joerg 4560 1.1.1.2 joerg if (const RecordDecl *Def = getDefinition()) { 4561 1.1.1.2 joerg for (const FieldDecl *FD : Def->fields()) { 4562 1.1.1.2 joerg const RecordType *RT = FD->getType()->getAs<RecordType>(); 4563 1.1.1.2 joerg if (RT && RT->getDecl()->isOrContainsUnion()) 4564 1.1.1.2 joerg return true; 4565 1.1.1.2 joerg } 4566 1.1.1.2 joerg } 4567 1.1.1.2 joerg 4568 1.1.1.2 joerg return false; 4569 1.1.1.2 joerg } 4570 1.1.1.2 joerg 4571 1.1 joerg RecordDecl::field_iterator RecordDecl::field_begin() const { 4572 1.1 joerg if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) 4573 1.1 joerg LoadFieldsFromExternalStorage(); 4574 1.1 joerg 4575 1.1 joerg return field_iterator(decl_iterator(FirstDecl)); 4576 1.1 joerg } 4577 1.1 joerg 4578 1.1 joerg /// completeDefinition - Notes that the definition of this type is now 4579 1.1 joerg /// complete. 4580 1.1 joerg void RecordDecl::completeDefinition() { 4581 1.1 joerg assert(!isCompleteDefinition() && "Cannot redefine record!"); 4582 1.1 joerg TagDecl::completeDefinition(); 4583 1.1 joerg } 4584 1.1 joerg 4585 1.1 joerg /// isMsStruct - Get whether or not this record uses ms_struct layout. 4586 1.1 joerg /// This which can be turned on with an attribute, pragma, or the 4587 1.1 joerg /// -mms-bitfields command-line option. 4588 1.1 joerg bool RecordDecl::isMsStruct(const ASTContext &C) const { 4589 1.1 joerg return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; 4590 1.1 joerg } 4591 1.1 joerg 4592 1.1 joerg void RecordDecl::LoadFieldsFromExternalStorage() const { 4593 1.1 joerg ExternalASTSource *Source = getASTContext().getExternalSource(); 4594 1.1 joerg assert(hasExternalLexicalStorage() && Source && "No external storage?"); 4595 1.1 joerg 4596 1.1 joerg // Notify that we have a RecordDecl doing some initialization. 4597 1.1 joerg ExternalASTSource::Deserializing TheFields(Source); 4598 1.1 joerg 4599 1.1 joerg SmallVector<Decl*, 64> Decls; 4600 1.1 joerg setHasLoadedFieldsFromExternalStorage(true); 4601 1.1 joerg Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { 4602 1.1 joerg return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); 4603 1.1 joerg }, Decls); 4604 1.1 joerg 4605 1.1 joerg #ifndef NDEBUG 4606 1.1 joerg // Check that all decls we got were FieldDecls. 4607 1.1 joerg for (unsigned i=0, e=Decls.size(); i != e; ++i) 4608 1.1 joerg assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); 4609 1.1 joerg #endif 4610 1.1 joerg 4611 1.1 joerg if (Decls.empty()) 4612 1.1 joerg return; 4613 1.1 joerg 4614 1.1 joerg std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, 4615 1.1 joerg /*FieldsAlreadyLoaded=*/false); 4616 1.1 joerg } 4617 1.1 joerg 4618 1.1 joerg bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { 4619 1.1 joerg ASTContext &Context = getASTContext(); 4620 1.1 joerg const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & 4621 1.1 joerg (SanitizerKind::Address | SanitizerKind::KernelAddress); 4622 1.1 joerg if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) 4623 1.1 joerg return false; 4624 1.1.1.2 joerg const auto &NoSanitizeList = Context.getNoSanitizeList(); 4625 1.1 joerg const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); 4626 1.1 joerg // We may be able to relax some of these requirements. 4627 1.1 joerg int ReasonToReject = -1; 4628 1.1 joerg if (!CXXRD || CXXRD->isExternCContext()) 4629 1.1 joerg ReasonToReject = 0; // is not C++. 4630 1.1 joerg else if (CXXRD->hasAttr<PackedAttr>()) 4631 1.1 joerg ReasonToReject = 1; // is packed. 4632 1.1 joerg else if (CXXRD->isUnion()) 4633 1.1 joerg ReasonToReject = 2; // is a union. 4634 1.1 joerg else if (CXXRD->isTriviallyCopyable()) 4635 1.1 joerg ReasonToReject = 3; // is trivially copyable. 4636 1.1 joerg else if (CXXRD->hasTrivialDestructor()) 4637 1.1 joerg ReasonToReject = 4; // has trivial destructor. 4638 1.1 joerg else if (CXXRD->isStandardLayout()) 4639 1.1 joerg ReasonToReject = 5; // is standard layout. 4640 1.1.1.2 joerg else if (NoSanitizeList.containsLocation(EnabledAsanMask, getLocation(), 4641 1.1 joerg "field-padding")) 4642 1.1.1.2 joerg ReasonToReject = 6; // is in an excluded file. 4643 1.1.1.2 joerg else if (NoSanitizeList.containsType( 4644 1.1.1.2 joerg EnabledAsanMask, getQualifiedNameAsString(), "field-padding")) 4645 1.1.1.2 joerg ReasonToReject = 7; // The type is excluded. 4646 1.1 joerg 4647 1.1 joerg if (EmitRemark) { 4648 1.1 joerg if (ReasonToReject >= 0) 4649 1.1 joerg Context.getDiagnostics().Report( 4650 1.1 joerg getLocation(), 4651 1.1 joerg diag::remark_sanitize_address_insert_extra_padding_rejected) 4652 1.1 joerg << getQualifiedNameAsString() << ReasonToReject; 4653 1.1 joerg else 4654 1.1 joerg Context.getDiagnostics().Report( 4655 1.1 joerg getLocation(), 4656 1.1 joerg diag::remark_sanitize_address_insert_extra_padding_accepted) 4657 1.1 joerg << getQualifiedNameAsString(); 4658 1.1 joerg } 4659 1.1 joerg return ReasonToReject < 0; 4660 1.1 joerg } 4661 1.1 joerg 4662 1.1 joerg const FieldDecl *RecordDecl::findFirstNamedDataMember() const { 4663 1.1 joerg for (const auto *I : fields()) { 4664 1.1 joerg if (I->getIdentifier()) 4665 1.1 joerg return I; 4666 1.1 joerg 4667 1.1 joerg if (const auto *RT = I->getType()->getAs<RecordType>()) 4668 1.1 joerg if (const FieldDecl *NamedDataMember = 4669 1.1 joerg RT->getDecl()->findFirstNamedDataMember()) 4670 1.1 joerg return NamedDataMember; 4671 1.1 joerg } 4672 1.1 joerg 4673 1.1 joerg // We didn't find a named data member. 4674 1.1 joerg return nullptr; 4675 1.1 joerg } 4676 1.1 joerg 4677 1.1 joerg //===----------------------------------------------------------------------===// 4678 1.1 joerg // BlockDecl Implementation 4679 1.1 joerg //===----------------------------------------------------------------------===// 4680 1.1 joerg 4681 1.1 joerg BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 4682 1.1 joerg : Decl(Block, DC, CaretLoc), DeclContext(Block) { 4683 1.1 joerg setIsVariadic(false); 4684 1.1 joerg setCapturesCXXThis(false); 4685 1.1 joerg setBlockMissingReturnType(true); 4686 1.1 joerg setIsConversionFromLambda(false); 4687 1.1 joerg setDoesNotEscape(false); 4688 1.1 joerg setCanAvoidCopyToHeap(false); 4689 1.1 joerg } 4690 1.1 joerg 4691 1.1 joerg void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { 4692 1.1 joerg assert(!ParamInfo && "Already has param info!"); 4693 1.1 joerg 4694 1.1 joerg // Zero params -> null pointer. 4695 1.1 joerg if (!NewParamInfo.empty()) { 4696 1.1 joerg NumParams = NewParamInfo.size(); 4697 1.1 joerg ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; 4698 1.1 joerg std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 4699 1.1 joerg } 4700 1.1 joerg } 4701 1.1 joerg 4702 1.1 joerg void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, 4703 1.1 joerg bool CapturesCXXThis) { 4704 1.1 joerg this->setCapturesCXXThis(CapturesCXXThis); 4705 1.1 joerg this->NumCaptures = Captures.size(); 4706 1.1 joerg 4707 1.1 joerg if (Captures.empty()) { 4708 1.1 joerg this->Captures = nullptr; 4709 1.1 joerg return; 4710 1.1 joerg } 4711 1.1 joerg 4712 1.1 joerg this->Captures = Captures.copy(Context).data(); 4713 1.1 joerg } 4714 1.1 joerg 4715 1.1 joerg bool BlockDecl::capturesVariable(const VarDecl *variable) const { 4716 1.1 joerg for (const auto &I : captures()) 4717 1.1 joerg // Only auto vars can be captured, so no redeclaration worries. 4718 1.1 joerg if (I.getVariable() == variable) 4719 1.1 joerg return true; 4720 1.1 joerg 4721 1.1 joerg return false; 4722 1.1 joerg } 4723 1.1 joerg 4724 1.1 joerg SourceRange BlockDecl::getSourceRange() const { 4725 1.1 joerg return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); 4726 1.1 joerg } 4727 1.1 joerg 4728 1.1 joerg //===----------------------------------------------------------------------===// 4729 1.1 joerg // Other Decl Allocation/Deallocation Method Implementations 4730 1.1 joerg //===----------------------------------------------------------------------===// 4731 1.1 joerg 4732 1.1 joerg void TranslationUnitDecl::anchor() {} 4733 1.1 joerg 4734 1.1 joerg TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 4735 1.1 joerg return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); 4736 1.1 joerg } 4737 1.1 joerg 4738 1.1 joerg void PragmaCommentDecl::anchor() {} 4739 1.1 joerg 4740 1.1 joerg PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, 4741 1.1 joerg TranslationUnitDecl *DC, 4742 1.1 joerg SourceLocation CommentLoc, 4743 1.1 joerg PragmaMSCommentKind CommentKind, 4744 1.1 joerg StringRef Arg) { 4745 1.1 joerg PragmaCommentDecl *PCD = 4746 1.1 joerg new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) 4747 1.1 joerg PragmaCommentDecl(DC, CommentLoc, CommentKind); 4748 1.1 joerg memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); 4749 1.1 joerg PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; 4750 1.1 joerg return PCD; 4751 1.1 joerg } 4752 1.1 joerg 4753 1.1 joerg PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, 4754 1.1 joerg unsigned ID, 4755 1.1 joerg unsigned ArgSize) { 4756 1.1 joerg return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) 4757 1.1 joerg PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); 4758 1.1 joerg } 4759 1.1 joerg 4760 1.1 joerg void PragmaDetectMismatchDecl::anchor() {} 4761 1.1 joerg 4762 1.1 joerg PragmaDetectMismatchDecl * 4763 1.1 joerg PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, 4764 1.1 joerg SourceLocation Loc, StringRef Name, 4765 1.1 joerg StringRef Value) { 4766 1.1 joerg size_t ValueStart = Name.size() + 1; 4767 1.1 joerg PragmaDetectMismatchDecl *PDMD = 4768 1.1 joerg new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) 4769 1.1 joerg PragmaDetectMismatchDecl(DC, Loc, ValueStart); 4770 1.1 joerg memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); 4771 1.1 joerg PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; 4772 1.1 joerg memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), 4773 1.1 joerg Value.size()); 4774 1.1 joerg PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; 4775 1.1 joerg return PDMD; 4776 1.1 joerg } 4777 1.1 joerg 4778 1.1 joerg PragmaDetectMismatchDecl * 4779 1.1 joerg PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, 4780 1.1 joerg unsigned NameValueSize) { 4781 1.1 joerg return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) 4782 1.1 joerg PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); 4783 1.1 joerg } 4784 1.1 joerg 4785 1.1 joerg void ExternCContextDecl::anchor() {} 4786 1.1 joerg 4787 1.1 joerg ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, 4788 1.1 joerg TranslationUnitDecl *DC) { 4789 1.1 joerg return new (C, DC) ExternCContextDecl(DC); 4790 1.1 joerg } 4791 1.1 joerg 4792 1.1 joerg void LabelDecl::anchor() {} 4793 1.1 joerg 4794 1.1 joerg LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 4795 1.1 joerg SourceLocation IdentL, IdentifierInfo *II) { 4796 1.1 joerg return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); 4797 1.1 joerg } 4798 1.1 joerg 4799 1.1 joerg LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 4800 1.1 joerg SourceLocation IdentL, IdentifierInfo *II, 4801 1.1 joerg SourceLocation GnuLabelL) { 4802 1.1 joerg assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); 4803 1.1 joerg return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); 4804 1.1 joerg } 4805 1.1 joerg 4806 1.1 joerg LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4807 1.1 joerg return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, 4808 1.1 joerg SourceLocation()); 4809 1.1 joerg } 4810 1.1 joerg 4811 1.1 joerg void LabelDecl::setMSAsmLabel(StringRef Name) { 4812 1.1.1.2 joerg char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; 4813 1.1 joerg memcpy(Buffer, Name.data(), Name.size()); 4814 1.1 joerg Buffer[Name.size()] = '\0'; 4815 1.1 joerg MSAsmName = Buffer; 4816 1.1 joerg } 4817 1.1 joerg 4818 1.1 joerg void ValueDecl::anchor() {} 4819 1.1 joerg 4820 1.1 joerg bool ValueDecl::isWeak() const { 4821 1.1.1.2 joerg auto *MostRecent = getMostRecentDecl(); 4822 1.1.1.2 joerg return MostRecent->hasAttr<WeakAttr>() || 4823 1.1.1.2 joerg MostRecent->hasAttr<WeakRefAttr>() || isWeakImported(); 4824 1.1 joerg } 4825 1.1 joerg 4826 1.1 joerg void ImplicitParamDecl::anchor() {} 4827 1.1 joerg 4828 1.1 joerg ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 4829 1.1 joerg SourceLocation IdLoc, 4830 1.1 joerg IdentifierInfo *Id, QualType Type, 4831 1.1 joerg ImplicitParamKind ParamKind) { 4832 1.1 joerg return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); 4833 1.1 joerg } 4834 1.1 joerg 4835 1.1 joerg ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, 4836 1.1 joerg ImplicitParamKind ParamKind) { 4837 1.1 joerg return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); 4838 1.1 joerg } 4839 1.1 joerg 4840 1.1 joerg ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, 4841 1.1 joerg unsigned ID) { 4842 1.1 joerg return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); 4843 1.1 joerg } 4844 1.1 joerg 4845 1.1 joerg FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, 4846 1.1 joerg SourceLocation StartLoc, 4847 1.1 joerg const DeclarationNameInfo &NameInfo, 4848 1.1 joerg QualType T, TypeSourceInfo *TInfo, 4849 1.1 joerg StorageClass SC, bool isInlineSpecified, 4850 1.1 joerg bool hasWrittenPrototype, 4851 1.1.1.2 joerg ConstexprSpecKind ConstexprKind, 4852 1.1.1.2 joerg Expr *TrailingRequiresClause) { 4853 1.1 joerg FunctionDecl *New = 4854 1.1 joerg new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, 4855 1.1.1.2 joerg SC, isInlineSpecified, ConstexprKind, 4856 1.1.1.2 joerg TrailingRequiresClause); 4857 1.1 joerg New->setHasWrittenPrototype(hasWrittenPrototype); 4858 1.1 joerg return New; 4859 1.1 joerg } 4860 1.1 joerg 4861 1.1 joerg FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4862 1.1.1.2 joerg return new (C, ID) FunctionDecl( 4863 1.1.1.2 joerg Function, C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), 4864 1.1.1.2 joerg nullptr, SC_None, false, ConstexprSpecKind::Unspecified, nullptr); 4865 1.1 joerg } 4866 1.1 joerg 4867 1.1 joerg BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 4868 1.1 joerg return new (C, DC) BlockDecl(DC, L); 4869 1.1 joerg } 4870 1.1 joerg 4871 1.1 joerg BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4872 1.1 joerg return new (C, ID) BlockDecl(nullptr, SourceLocation()); 4873 1.1 joerg } 4874 1.1 joerg 4875 1.1 joerg CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) 4876 1.1 joerg : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), 4877 1.1 joerg NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} 4878 1.1 joerg 4879 1.1 joerg CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, 4880 1.1 joerg unsigned NumParams) { 4881 1.1 joerg return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) 4882 1.1 joerg CapturedDecl(DC, NumParams); 4883 1.1 joerg } 4884 1.1 joerg 4885 1.1 joerg CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, 4886 1.1 joerg unsigned NumParams) { 4887 1.1 joerg return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) 4888 1.1 joerg CapturedDecl(nullptr, NumParams); 4889 1.1 joerg } 4890 1.1 joerg 4891 1.1 joerg Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } 4892 1.1 joerg void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } 4893 1.1 joerg 4894 1.1 joerg bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } 4895 1.1 joerg void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } 4896 1.1 joerg 4897 1.1 joerg EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 4898 1.1 joerg SourceLocation L, 4899 1.1 joerg IdentifierInfo *Id, QualType T, 4900 1.1 joerg Expr *E, const llvm::APSInt &V) { 4901 1.1 joerg return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); 4902 1.1 joerg } 4903 1.1 joerg 4904 1.1 joerg EnumConstantDecl * 4905 1.1 joerg EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4906 1.1 joerg return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, 4907 1.1 joerg QualType(), nullptr, llvm::APSInt()); 4908 1.1 joerg } 4909 1.1 joerg 4910 1.1 joerg void IndirectFieldDecl::anchor() {} 4911 1.1 joerg 4912 1.1 joerg IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, 4913 1.1 joerg SourceLocation L, DeclarationName N, 4914 1.1 joerg QualType T, 4915 1.1 joerg MutableArrayRef<NamedDecl *> CH) 4916 1.1 joerg : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), 4917 1.1 joerg ChainingSize(CH.size()) { 4918 1.1 joerg // In C++, indirect field declarations conflict with tag declarations in the 4919 1.1 joerg // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. 4920 1.1 joerg if (C.getLangOpts().CPlusPlus) 4921 1.1 joerg IdentifierNamespace |= IDNS_Tag; 4922 1.1 joerg } 4923 1.1 joerg 4924 1.1 joerg IndirectFieldDecl * 4925 1.1 joerg IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 4926 1.1 joerg IdentifierInfo *Id, QualType T, 4927 1.1 joerg llvm::MutableArrayRef<NamedDecl *> CH) { 4928 1.1 joerg return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); 4929 1.1 joerg } 4930 1.1 joerg 4931 1.1 joerg IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, 4932 1.1 joerg unsigned ID) { 4933 1.1 joerg return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), 4934 1.1 joerg DeclarationName(), QualType(), None); 4935 1.1 joerg } 4936 1.1 joerg 4937 1.1 joerg SourceRange EnumConstantDecl::getSourceRange() const { 4938 1.1 joerg SourceLocation End = getLocation(); 4939 1.1 joerg if (Init) 4940 1.1 joerg End = Init->getEndLoc(); 4941 1.1 joerg return SourceRange(getLocation(), End); 4942 1.1 joerg } 4943 1.1 joerg 4944 1.1 joerg void TypeDecl::anchor() {} 4945 1.1 joerg 4946 1.1 joerg TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 4947 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc, 4948 1.1 joerg IdentifierInfo *Id, TypeSourceInfo *TInfo) { 4949 1.1 joerg return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); 4950 1.1 joerg } 4951 1.1 joerg 4952 1.1 joerg void TypedefNameDecl::anchor() {} 4953 1.1 joerg 4954 1.1 joerg TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { 4955 1.1 joerg if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { 4956 1.1 joerg auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); 4957 1.1 joerg auto *ThisTypedef = this; 4958 1.1 joerg if (AnyRedecl && OwningTypedef) { 4959 1.1 joerg OwningTypedef = OwningTypedef->getCanonicalDecl(); 4960 1.1 joerg ThisTypedef = ThisTypedef->getCanonicalDecl(); 4961 1.1 joerg } 4962 1.1 joerg if (OwningTypedef == ThisTypedef) 4963 1.1 joerg return TT->getDecl(); 4964 1.1 joerg } 4965 1.1 joerg 4966 1.1 joerg return nullptr; 4967 1.1 joerg } 4968 1.1 joerg 4969 1.1 joerg bool TypedefNameDecl::isTransparentTagSlow() const { 4970 1.1 joerg auto determineIsTransparent = [&]() { 4971 1.1 joerg if (auto *TT = getUnderlyingType()->getAs<TagType>()) { 4972 1.1 joerg if (auto *TD = TT->getDecl()) { 4973 1.1 joerg if (TD->getName() != getName()) 4974 1.1 joerg return false; 4975 1.1 joerg SourceLocation TTLoc = getLocation(); 4976 1.1 joerg SourceLocation TDLoc = TD->getLocation(); 4977 1.1 joerg if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) 4978 1.1 joerg return false; 4979 1.1 joerg SourceManager &SM = getASTContext().getSourceManager(); 4980 1.1 joerg return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); 4981 1.1 joerg } 4982 1.1 joerg } 4983 1.1 joerg return false; 4984 1.1 joerg }; 4985 1.1 joerg 4986 1.1 joerg bool isTransparent = determineIsTransparent(); 4987 1.1 joerg MaybeModedTInfo.setInt((isTransparent << 1) | 1); 4988 1.1 joerg return isTransparent; 4989 1.1 joerg } 4990 1.1 joerg 4991 1.1 joerg TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 4992 1.1 joerg return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), 4993 1.1 joerg nullptr, nullptr); 4994 1.1 joerg } 4995 1.1 joerg 4996 1.1 joerg TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, 4997 1.1 joerg SourceLocation StartLoc, 4998 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id, 4999 1.1 joerg TypeSourceInfo *TInfo) { 5000 1.1 joerg return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); 5001 1.1 joerg } 5002 1.1 joerg 5003 1.1 joerg TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5004 1.1 joerg return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), 5005 1.1 joerg SourceLocation(), nullptr, nullptr); 5006 1.1 joerg } 5007 1.1 joerg 5008 1.1 joerg SourceRange TypedefDecl::getSourceRange() const { 5009 1.1 joerg SourceLocation RangeEnd = getLocation(); 5010 1.1 joerg if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 5011 1.1 joerg if (typeIsPostfix(TInfo->getType())) 5012 1.1 joerg RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 5013 1.1 joerg } 5014 1.1 joerg return SourceRange(getBeginLoc(), RangeEnd); 5015 1.1 joerg } 5016 1.1 joerg 5017 1.1 joerg SourceRange TypeAliasDecl::getSourceRange() const { 5018 1.1 joerg SourceLocation RangeEnd = getBeginLoc(); 5019 1.1 joerg if (TypeSourceInfo *TInfo = getTypeSourceInfo()) 5020 1.1 joerg RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 5021 1.1 joerg return SourceRange(getBeginLoc(), RangeEnd); 5022 1.1 joerg } 5023 1.1 joerg 5024 1.1 joerg void FileScopeAsmDecl::anchor() {} 5025 1.1 joerg 5026 1.1 joerg FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 5027 1.1 joerg StringLiteral *Str, 5028 1.1 joerg SourceLocation AsmLoc, 5029 1.1 joerg SourceLocation RParenLoc) { 5030 1.1 joerg return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); 5031 1.1 joerg } 5032 1.1 joerg 5033 1.1 joerg FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, 5034 1.1 joerg unsigned ID) { 5035 1.1 joerg return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), 5036 1.1 joerg SourceLocation()); 5037 1.1 joerg } 5038 1.1 joerg 5039 1.1 joerg void EmptyDecl::anchor() {} 5040 1.1 joerg 5041 1.1 joerg EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 5042 1.1 joerg return new (C, DC) EmptyDecl(DC, L); 5043 1.1 joerg } 5044 1.1 joerg 5045 1.1 joerg EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5046 1.1 joerg return new (C, ID) EmptyDecl(nullptr, SourceLocation()); 5047 1.1 joerg } 5048 1.1 joerg 5049 1.1 joerg //===----------------------------------------------------------------------===// 5050 1.1 joerg // ImportDecl Implementation 5051 1.1 joerg //===----------------------------------------------------------------------===// 5052 1.1 joerg 5053 1.1 joerg /// Retrieve the number of module identifiers needed to name the given 5054 1.1 joerg /// module. 5055 1.1 joerg static unsigned getNumModuleIdentifiers(Module *Mod) { 5056 1.1 joerg unsigned Result = 1; 5057 1.1 joerg while (Mod->Parent) { 5058 1.1 joerg Mod = Mod->Parent; 5059 1.1 joerg ++Result; 5060 1.1 joerg } 5061 1.1 joerg return Result; 5062 1.1 joerg } 5063 1.1 joerg 5064 1.1 joerg ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 5065 1.1 joerg Module *Imported, 5066 1.1 joerg ArrayRef<SourceLocation> IdentifierLocs) 5067 1.1.1.2 joerg : Decl(Import, DC, StartLoc), ImportedModule(Imported), 5068 1.1.1.2 joerg NextLocalImportAndComplete(nullptr, true) { 5069 1.1 joerg assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); 5070 1.1 joerg auto *StoredLocs = getTrailingObjects<SourceLocation>(); 5071 1.1 joerg std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), 5072 1.1 joerg StoredLocs); 5073 1.1 joerg } 5074 1.1 joerg 5075 1.1 joerg ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 5076 1.1 joerg Module *Imported, SourceLocation EndLoc) 5077 1.1.1.2 joerg : Decl(Import, DC, StartLoc), ImportedModule(Imported), 5078 1.1.1.2 joerg NextLocalImportAndComplete(nullptr, false) { 5079 1.1 joerg *getTrailingObjects<SourceLocation>() = EndLoc; 5080 1.1 joerg } 5081 1.1 joerg 5082 1.1 joerg ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, 5083 1.1 joerg SourceLocation StartLoc, Module *Imported, 5084 1.1 joerg ArrayRef<SourceLocation> IdentifierLocs) { 5085 1.1 joerg return new (C, DC, 5086 1.1 joerg additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) 5087 1.1 joerg ImportDecl(DC, StartLoc, Imported, IdentifierLocs); 5088 1.1 joerg } 5089 1.1 joerg 5090 1.1 joerg ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, 5091 1.1 joerg SourceLocation StartLoc, 5092 1.1 joerg Module *Imported, 5093 1.1 joerg SourceLocation EndLoc) { 5094 1.1 joerg ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) 5095 1.1 joerg ImportDecl(DC, StartLoc, Imported, EndLoc); 5096 1.1 joerg Import->setImplicit(); 5097 1.1 joerg return Import; 5098 1.1 joerg } 5099 1.1 joerg 5100 1.1 joerg ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, 5101 1.1 joerg unsigned NumLocations) { 5102 1.1 joerg return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) 5103 1.1 joerg ImportDecl(EmptyShell()); 5104 1.1 joerg } 5105 1.1 joerg 5106 1.1 joerg ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { 5107 1.1.1.2 joerg if (!isImportComplete()) 5108 1.1 joerg return None; 5109 1.1 joerg 5110 1.1 joerg const auto *StoredLocs = getTrailingObjects<SourceLocation>(); 5111 1.1 joerg return llvm::makeArrayRef(StoredLocs, 5112 1.1 joerg getNumModuleIdentifiers(getImportedModule())); 5113 1.1 joerg } 5114 1.1 joerg 5115 1.1 joerg SourceRange ImportDecl::getSourceRange() const { 5116 1.1.1.2 joerg if (!isImportComplete()) 5117 1.1 joerg return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); 5118 1.1 joerg 5119 1.1 joerg return SourceRange(getLocation(), getIdentifierLocs().back()); 5120 1.1 joerg } 5121 1.1 joerg 5122 1.1 joerg //===----------------------------------------------------------------------===// 5123 1.1 joerg // ExportDecl Implementation 5124 1.1 joerg //===----------------------------------------------------------------------===// 5125 1.1 joerg 5126 1.1 joerg void ExportDecl::anchor() {} 5127 1.1 joerg 5128 1.1 joerg ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, 5129 1.1 joerg SourceLocation ExportLoc) { 5130 1.1 joerg return new (C, DC) ExportDecl(DC, ExportLoc); 5131 1.1 joerg } 5132 1.1 joerg 5133 1.1 joerg ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5134 1.1 joerg return new (C, ID) ExportDecl(nullptr, SourceLocation()); 5135 1.1 joerg } 5136