Skip to content

Const-Qualify LangOptions and TypeCheckerOptions #31721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ class ASTContext final {
UnifiedStatsReporter *Stats = nullptr;

/// The language options used for translation.
LangOptions &LangOpts;
const LangOptions &LangOpts;

/// The type checker options.
TypeCheckerOptions &TypeCheckerOpts;
const TypeCheckerOptions &TypeCheckerOpts;

/// The search path options used by this AST context.
SearchPathOptions &SearchPathOpts;
Expand Down
7 changes: 5 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class alignas(1 << DeclAlignInBits) Decl {
HasAnyUnavailableValues : 1
);

SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1,
/// If the module was or is being compiled with `-enable-testing`.
TestingEnabled : 1,

Expand All @@ -601,7 +601,10 @@ class alignas(1 << DeclAlignInBits) Decl {

/// Whether the module was imported from Clang (or, someday, maybe another
/// language).
IsNonSwiftModule : 1
IsNonSwiftModule : 1,

/// Whether this module is the main module.
IsMainModule : 1
);

SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,10 @@ ERROR(expectation_missing_opening_braces,none,
ERROR(expectation_missing_closing_braces,none,
"didn't find '}}' to match '{{' in expectation", ())

WARNING(module_incompatible_with_skip_function_bodies,none,
"module '%0' cannot be built with "
"-experimental-skip-non-inlinable-function-bodies; this option has "
"been automatically disabled", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
11 changes: 11 additions & 0 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ class ModuleDecl : public DeclContext, public TypeDecl {
return new (ctx) ModuleDecl(name, ctx, importInfo);
}

static ModuleDecl *
createMainModule(ASTContext &ctx, Identifier name, ImplicitImportInfo iinfo) {
auto *Mod = ModuleDecl::create(name, ctx, iinfo);
Mod->Bits.ModuleDecl.IsMainModule = true;
return Mod;
}

using Decl::getASTContext;

/// Retrieves information about which modules are implicitly imported by
Expand Down Expand Up @@ -542,6 +549,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
Bits.ModuleDecl.IsNonSwiftModule = flag;
}

bool isMainModule() const {
return Bits.ModuleDecl.IsMainModule;
}

/// Retrieve the top-level module. If this module is already top-level, this
/// returns itself. If this is a submodule such as \c Foo.Bar.Baz, this
/// returns the module \c Foo.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/PlatformKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ StringRef prettyPlatformString(PlatformKind platform);
/// If ForTargetVariant is true then for zippered builds the target-variant
/// triple will be used rather than the target to determine whether the
/// platform is active.
bool isPlatformActive(PlatformKind Platform, LangOptions &LangOpts,
bool isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
bool ForTargetVariant = false);

/// Returns the target platform for the given language options.
PlatformKind targetPlatform(LangOptions &LangOpts);
PlatformKind targetPlatform(const LangOptions &LangOpts);

/// Returns true when availability attributes from the "parent" platform
/// should also apply to the "child" platform for declarations without
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
setInterfaceType(ModuleType::get(this));

setAccess(AccessLevel::Public);

Bits.ModuleDecl.TestingEnabled = 0;
Bits.ModuleDecl.FailedToLoad = 0;
Bits.ModuleDecl.RawResilienceStrategy = 0;
Bits.ModuleDecl.HasResolvedImports = 0;
Bits.ModuleDecl.PrivateImportsEnabled = 0;
Bits.ModuleDecl.ImplicitDynamicEnabled = 0;
Bits.ModuleDecl.IsSystemModule = 0;
Bits.ModuleDecl.IsNonSwiftModule = 0;
Bits.ModuleDecl.IsMainModule = 0;
}

ArrayRef<ImplicitImport> ModuleDecl::getImplicitImports() const {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/PlatformKind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
llvm_unreachable("bad PlatformKind");
}

bool swift::isPlatformActive(PlatformKind Platform, LangOptions &LangOpts,
bool swift::isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
bool ForTargetVariant) {
llvm::Triple TT = LangOpts.Target;

Expand All @@ -105,7 +105,7 @@ bool swift::isPlatformActive(PlatformKind Platform, LangOptions &LangOpts,
LangOpts.EnableAppExtensionRestrictions);
}

PlatformKind swift::targetPlatform(LangOptions &LangOpts) {
PlatformKind swift::targetPlatform(const LangOptions &LangOpts) {
if (LangOpts.Target.isMacOSX()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::OSXApplicationExtension
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ ClangImporter::getWrapperForModule(const clang::Module *mod,
return clangUnit->getParentModule();
}

PlatformAvailability::PlatformAvailability(LangOptions &langOpts)
PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
: platformKind(targetPlatform(langOpts)) {
switch (platformKind) {
case PlatformKind::iOS:
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ struct PlatformAvailability {
/// API is now unavailable.
std::string deprecatedAsUnavailableMessage;

PlatformAvailability(LangOptions &opts);
PlatformAvailability(const LangOptions &opts);

private:
PlatformAvailability(const PlatformAvailability&) = delete;
Expand Down
28 changes: 24 additions & 4 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
}
}

if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::EmitSyntax) {
Opts.BuildSyntaxTree = true;
Opts.VerifySyntaxTree = true;
}

return HadError || UnsupportedOS || UnsupportedArch;
}

Expand Down Expand Up @@ -677,6 +682,17 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
// body skipping.
Opts.SkipNonInlinableFunctionBodies |= Args.hasArg(OPT_tbd_is_installapi);

if (Opts.SkipNonInlinableFunctionBodies &&
FrontendOpts.ModuleName == SWIFT_ONONE_SUPPORT) {
// Disable this optimization if we're compiling SwiftOnoneSupport, because
// we _definitely_ need to look inside every declaration to figure out
// what gets prespecialized.
Opts.SkipNonInlinableFunctionBodies = false;
Diags.diagnose(SourceLoc(),
diag::module_incompatible_with_skip_function_bodies,
SWIFT_ONONE_SUPPORT);
}

Opts.DisableConstraintSolverPerformanceHacks |=
Args.hasArg(OPT_disable_constraint_solver_performance_hacks);

Expand Down Expand Up @@ -903,12 +919,14 @@ void parseExclusivityEnforcementOptions(const llvm::opt::Arg *A,

static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
IRGenOptions &IRGenOpts,
FrontendOptions &FEOpts,
const FrontendOptions &FEOpts,
const TypeCheckerOptions &TCOpts,
DiagnosticEngine &Diags,
const llvm::Triple &Triple,
ClangImporterOptions &ClangOpts) {
using namespace options;


if (const Arg *A = Args.getLastArg(OPT_sil_inline_threshold)) {
if (StringRef(A->getValue()).getAsInteger(10, Opts.InlineThreshold)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
Expand Down Expand Up @@ -951,8 +969,9 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
if (Args.hasArg(OPT_sil_merge_partial_modules))
Opts.MergePartialModules = true;

if (Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies))
Opts.SkipNonInlinableFunctionBodies = true;
// Propagate the typechecker's understanding of
// -experimental-skip-non-inlinable-function-bodies to SIL.
Opts.SkipNonInlinableFunctionBodies = TCOpts.SkipNonInlinableFunctionBodies;

// Parse the optimization level.
// Default to Onone settings if no option is passed.
Expand Down Expand Up @@ -1624,7 +1643,8 @@ bool CompilerInvocation::parseArgs(
return true;
}

if (ParseSILArgs(SILOpts, ParsedArgs, IRGenOpts, FrontendOpts, Diags,
if (ParseSILArgs(SILOpts, ParsedArgs, IRGenOpts, FrontendOpts,
TypeCheckerOpts, Diags,
LangOpts.Target, ClangImporterOpts)) {
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
ModuleDecl *CompilerInstance::getMainModule() const {
if (!MainModule) {
Identifier ID = Context->getIdentifier(Invocation.getModuleName());
MainModule = ModuleDecl::create(ID, *Context, getImplicitImportInfo());
MainModule = ModuleDecl::createMainModule(*Context, ID,
getImplicitImportInfo());
if (Invocation.getFrontendOptions().EnableTesting)
MainModule->setTestingEnabled();
if (Invocation.getFrontendOptions().EnablePrivateImports)
Expand Down
7 changes: 1 addition & 6 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,12 +1200,7 @@ static bool performCompile(CompilerInstance &Instance,
FrontendObserver *observer) {
const auto &Invocation = Instance.getInvocation();
const auto &opts = Invocation.getFrontendOptions();
FrontendOptions::ActionType Action = opts.RequestedAction;

if (Action == FrontendOptions::ActionType::EmitSyntax) {
Instance.getASTContext().LangOpts.BuildSyntaxTree = true;
Instance.getASTContext().LangOpts.VerifySyntaxTree = true;
}
const FrontendOptions::ActionType Action = opts.RequestedAction;

// We've been asked to precompile a bridging header or module; we want to
// avoid touching any other inputs and just parse, emit and exit.
Expand Down
4 changes: 2 additions & 2 deletions lib/IDE/CompletionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ bool CompletionInstance::performCachedOperationIfPossible(

LangOptions langOpts = CI.getASTContext().LangOpts;
langOpts.DisableParserLookup = true;
// Ensure all non-function-body tokens are hashed into the interface hash
langOpts.EnableTypeFingerprints = false;
TypeCheckerOptions typeckOpts = CI.getASTContext().TypeCheckerOpts;
SearchPathOptions searchPathOpts = CI.getASTContext().SearchPathOpts;
DiagnosticEngine tmpDiags(tmpSM);
Expand All @@ -331,8 +333,6 @@ bool CompletionInstance::performCachedOperationIfPossible(
SourceFile(*tmpM, oldSF->Kind, tmpBufferID, /*KeepParsedTokens=*/false,
/*BuildSyntaxTree=*/false, oldSF->getParsingOptions());
tmpSF->enableInterfaceHash();
// Ensure all non-function-body tokens are hashed into the interface hash
tmpCtx->LangOpts.EnableTypeFingerprints = false;

// FIXME: Since we don't setup module loaders on the temporary AST context,
// 'canImport()' conditional compilation directive always fails. That causes
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8321,7 +8321,7 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
result.setExpr(resultExpr);

auto &ctx = cs.getASTContext();
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
if (cs.isDebugMode()) {
auto &log = ctx.TypeCheckerDebug->getStream();
log << "---Type-checked expression---\n";
resultExpr->dump(log);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ConstraintSystem::determineBestBindings() {

inferTransitiveSupertypeBindings(cache, bindings);

if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
bindings.dump(typeVar, log, solverState->depth * 2);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4364,7 +4364,7 @@ bool ConstraintSystem::generateConstraints(
target = *resultTarget;
}

if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
log << "---Initial constraints for the given expression---\n";
print(log, expr);
Expand Down
18 changes: 9 additions & 9 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
unsigned index = static_cast<unsigned>(kind);
CurrentScore.Data[index] += value;

if (getASTContext().TypeCheckerOpts.DebugConstraintSolver && value > 0) {
if (isDebugMode() && value > 0) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
if (solverState)
log.indent(solverState->depth * 2);
Expand Down Expand Up @@ -102,7 +102,7 @@ bool ConstraintSystem::worseThanBestSolution() const {
CurrentScore <= *solverState->BestScore)
return false;

if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
log.indent(solverState->depth * 2)
<< "(solution is worse than the best solution)\n";
Expand Down Expand Up @@ -386,7 +386,9 @@ bool CompareDeclSpecializationRequest::evaluate(
Evaluator &eval, DeclContext *dc, ValueDecl *decl1, ValueDecl *decl2,
bool isDynamicOverloadComparison) const {
auto &C = decl1->getASTContext();
if (C.TypeCheckerOpts.DebugConstraintSolver) {
// Construct a constraint system to compare the two declarations.
ConstraintSystem cs(dc, ConstraintSystemOptions());
if (cs.isDebugMode()) {
auto &log = C.TypeCheckerDebug->getStream();
log << "Comparing declarations\n";
decl1->print(log);
Expand All @@ -397,8 +399,8 @@ bool CompareDeclSpecializationRequest::evaluate(
log << ")\n";
}

auto completeResult = [&C](bool result) {
if (C.TypeCheckerOpts.DebugConstraintSolver) {
auto completeResult = [&C, &cs](bool result) {
if (cs.isDebugMode()) {
auto &log = C.TypeCheckerDebug->getStream();
log << "comparison result: " << (result ? "better" : "not better")
<< "\n";
Expand Down Expand Up @@ -499,8 +501,6 @@ bool CompareDeclSpecializationRequest::evaluate(
return cs.openType(type, replacements);
};

// Construct a constraint system to compare the two declarations.
ConstraintSystem cs(dc, ConstraintSystemOptions());
bool knownNonSubtype = false;

auto *locator = cs.getConstraintLocator({});
Expand Down Expand Up @@ -737,7 +737,7 @@ static void addKeyPathDynamicMemberOverloads(
SolutionCompareResult ConstraintSystem::compareSolutions(
ConstraintSystem &cs, ArrayRef<Solution> solutions,
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
if (cs.getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
if (cs.isDebugMode()) {
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
log.indent(cs.solverState->depth * 2)
<< "comparing solutions " << idx1 << " and " << idx2 <<"\n";
Expand Down Expand Up @@ -1261,7 +1261,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
if (viable.size() == 1)
return 0;

if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
log.indent(solverState->depth * 2)
<< "Comparing " << viable.size() << " viable solutions\n";
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8148,7 +8148,7 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
// If we have a common result type, bind the expected result type to it.
if (commonResultType && !commonResultType->is<ErrorType>()) {
ASTContext &ctx = getASTContext();
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = ctx.TypeCheckerDebug->getStream();
log.indent(solverState ? solverState->depth * 2 : 0)
<< "(common result type for $T" << fnTypeVar->getID() << " is "
Expand Down Expand Up @@ -9290,7 +9290,7 @@ static bool isAugmentingFix(ConstraintFix *fix) {

bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
auto &ctx = getASTContext();
if (ctx.TypeCheckerOpts.DebugConstraintSolver) {
if (isDebugMode()) {
auto &log = ctx.TypeCheckerDebug->getStream();
log.indent(solverState ? solverState->depth * 2 : 0)
<< "(attempting fix ";
Expand Down
Loading