Skip to content
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

[GSoC] Swift PR #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ namespace swift {
class LazyIterableDeclContextData;
class LazyMemberLoader;
class ModuleDependencies;
class PackageAttr;
class PatternBindingDecl;
class PatternBindingInitializer;
class SourceFile;
Expand Down Expand Up @@ -324,6 +325,9 @@ class ASTContext final {
llvm::DenseMap<SILAutoDiffDerivativeFunctionKey, CanSILFunctionType>
SILAutoDiffDerivativeFunctions;

/// Cache of `@package` attributes.
llvm::SetVector<std::tuple<StringRef, unsigned, PackageAttr *>> PackageAttrs;

/// Cache of `@differentiable` attributes keyed by parameter indices. Used to
/// diagnose duplicate `@differentiable` attributes for the same key.
llvm::DenseMap<std::pair<Decl *, IndexSubset *>, DifferentiableAttr *>
Expand Down
6 changes: 6 additions & 0 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ SIMPLE_DECL_ATTR(_assemblyVision, EmitAssemblyVisionRemarks,
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
120)

DECL_ATTR(package, Package, OnImport |
ABIStableToAdd | ABIStableToRemove |
APIStableToAdd | APIStableToRemove |
NotSerialized,
121)

#undef TYPE_ATTR
#undef DECL_ATTR_ALIAS
#undef CONTEXTUAL_DECL_ATTR_ALIAS
Expand Down
39 changes: 39 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,45 @@ class OriginallyDefinedInAttr: public DeclAttribute {
}
};

/// The `@package` attribute implies its following imports has
/// external package dependencies.
///
/// Syntax definition is passed in on frontend calls.
class PackageAttr final: public DeclAttribute,
public TrailingCallArguments<PackageAttr> {
/// The package declaration string.
const StringRef PackageDeclaration;

unsigned NumArgLabels : 16;
Expr *Arg;

public:
PackageAttr(SourceLoc AtLoc, SourceRange Range,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this has a body, it should be moved into the .cpp file.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK simple functions and the main initializer stay in Attr.h?

StringRef PackageDeclaration,
Expr * Arg,
ArrayRef<Identifier> ArgLabels,
ArrayRef<SourceLoc> ArgLabelLocs,
bool Implicit)
: DeclAttribute(DAK_Package, AtLoc, Range, Implicit),
PackageDeclaration(PackageDeclaration),
Arg(Arg) {
NumArgLabels = ArgLabels.size();
initializeCallArguments(ArgLabels, ArgLabelLocs);
}

/// The getter of `PackageDeclaration`.
const StringRef getPackageDeclaration() {
return PackageDeclaration;
}

Expr *getArg() const { return Arg; }
unsigned getNumArguments() const { return NumArgLabels; }

static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_Package;
}
};

/// Attribute that marks a function as differentiable.
///
/// Examples:
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1860,5 +1860,9 @@ ERROR(expected_closure_literal,none,
ERROR(expected_multiple_closures_block_rbrace,none,
"expected '}' at the end of a trailing closures block", ())

// Package declaration errors
ERROR(package_declarations_not_allowed,none,
"package declaration is not allowed", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
3 changes: 3 additions & 0 deletions include/swift/AST/Import.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ enum class ImportFlags {
/// implementation detail of this file.
SPIAccessControl = 0x10,

/// The module is explicitly marked to be imported from SwiftPM.
Package = 0x20,

/// Used for DenseMap.
Reserved = 0x80
};
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Basic/FileTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TYPE("json-dependencies", JSONDependencies, "dependencies.json",
// Complete feature information for the given Swift compiler.
TYPE("json-features", JSONFeatures, "features.json", "")

// Complete package dependency information for the given Swift files as JSON.
TYPE("package-declarations", PackageDeclarations, "package-declarations.json", "")

TYPE("index-data", IndexData, "", "")
TYPE("index-unit-output-path", IndexUnitOutputPath, "", "")
Expand Down
6 changes: 6 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ namespace swift {
// FrontendOptions.
bool AllowModuleWithCompilerErrors = false;

// Allow @package attributes in the file.
bool AllowPackageDeclarations = false;

// Ingore @package attributes in the file.
bool IgnorePackageDeclarations = false;

/// A helper enum to represent whether or not we customized the default
/// ASTVerifier behavior via a frontend flag. By default, we do not
/// customize.
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class FrontendOptions {
ScanDependencies, ///< Scan dependencies of Swift source files
PrintVersion, ///< Print version information.
PrintFeature, ///< Print supported feature of this compiler

PrintPackageDeclarations ///< Print package declarations from the Swift source file
};

/// Indicates the action the user requested that the frontend perform.
Expand Down Expand Up @@ -422,6 +424,9 @@ class FrontendOptions {
/// Whether to include symbols with SPI information in the symbol graph.
bool IncludeSPISymbolsInSymbolGraph = false;

/// Whether to ignore package declarations.
bool IgnorePackageDeclarations = false;

private:
static bool canActionEmitDependencies(ActionType);
static bool canActionEmitReferenceDependencies(ActionType);
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -872,4 +872,8 @@ def new_driver_path
: Separate<["-"], "new-driver-path">, MetaVarName<"<path>">,
HelpText<"Path of the new driver to be used">;

def ignore_package_declarations : Flag<["-"], "ignore-package-declarations">,
HelpText<"Ignore all package declarations in the file, assuming all external"
"dependencies are provided as modules.">;

} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,10 @@ def dump_pcm : Flag<["-"], "dump-pcm">,
HelpText<"Dump debugging information about a precompiled Clang module">,
ModeOpt,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
def print_package_declarations : Flag<["-"], "print-package-declarations">,
HelpText<"Print all package declarations in the file.">,
ModeOpt,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;

// Other Modes
def repl : Flag<["-"], "repl">,
Expand Down
14 changes: 14 additions & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ class Parser {

bool allowTopLevelCode() const;

bool ignorePackageDeclarations() const {
return Context.LangOpts.IgnorePackageDeclarations;
}
bool allowPackageDeclaration() const {
return Context.LangOpts.AllowPackageDeclarations ||
Context.LangOpts.IgnorePackageDeclarations;
}

void registerPackageDeclaration(PackageAttr *Package);

const std::vector<Token> &getSplitTokens() const { return SplitTokens; }

void markSplitToken(tok Kind, StringRef Txt);
Expand Down Expand Up @@ -1067,6 +1077,10 @@ class Parser {
ParserResult<ImplementsAttr> parseImplementsAttribute(SourceLoc AtLoc,
SourceLoc Loc);

/// Parse the @package attribute.
ParserResult<PackageAttr> parsePackageAttribute(SourceLoc AtLoc,
SourceLoc Loc);

/// Parse the @differentiable attribute.
ParserResult<DifferentiableAttr> parseDifferentiableAttribute(SourceLoc AtLoc,
SourceLoc Loc);
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ StringRef DeclAttribute::getAttrName() const {
return "transpose";
case DAK_CompletionHandlerAsync:
return "completionHandlerAsync";
case DAK_Package:
return "package";
}
llvm_unreachable("bad DeclAttrKind");
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Basic/FileTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ bool file_types::isTextual(ID Id) {
case file_types::TY_SwiftOverlayFile:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_PackageDeclarations:
return true;
case file_types::TY_Image:
case file_types::TY_Object:
Expand Down Expand Up @@ -157,6 +158,7 @@ bool file_types::isAfterLLVM(ID Id) {
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_IndexUnitOutputPath:
case file_types::TY_PackageDeclarations:
return false;
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID.");
Expand Down Expand Up @@ -208,6 +210,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_IndexUnitOutputPath:
case file_types::TY_PackageDeclarations:
return false;
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID.");
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
case file_types::TY_SwiftOverlayFile:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_PackageDeclarations:
// We could in theory handle assembly or LLVM input, but let's not.
// FIXME: What about LTO?
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
Expand Down
3 changes: 3 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
return "-typecheck";
case file_types::TY_Remapping:
return "-update-code";
case file_types::TY_PackageDeclarations:
return "-print-package-declarations";
case file_types::TY_Nothing:
// We were told to output nothing, so get the last mode option and use that.
if (const Arg *A = Args.getLastArg(options::OPT_modes_Group))
Expand Down Expand Up @@ -904,6 +906,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
case file_types::TY_IndexData:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_PackageDeclarations:
llvm_unreachable("Cannot be output from backend job");
case file_types::TY_Swift:
case file_types::TY_dSYM:
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ bool ArgsToFrontendOptionsConverter::convert(

Opts.Static = Args.hasArg(OPT_static);

Opts.IgnorePackageDeclarations = Args.hasArg(OPT_ignore_package_declarations);

return false;
}

Expand Down Expand Up @@ -472,6 +474,9 @@ ArgsToFrontendOptionsConverter::determineRequestedAction(const ArgList &args) {
return FrontendOptions::ActionType::TypecheckModuleFromInterface;
if (Opt.matches(OPT_emit_supported_features))
return FrontendOptions::ActionType::PrintFeature;

if (Opt.matches(OPT_print_package_declarations))
return FrontendOptions::ActionType::PrintPackageDeclarations;
llvm_unreachable("Unhandled mode option");
}

Expand Down
7 changes: 7 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.VerifySyntaxTree = true;
}

if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::PrintPackageDeclarations) {
Opts.AllowPackageDeclarations = true;
}
if (FrontendOpts.IgnorePackageDeclarations) {
Opts.IgnorePackageDeclarations = true;
}

// Configure lexing to parse and remember comments if:
// - Emitting a swiftdoc/swiftsourceinfo
// - Performing index-while-building
Expand Down
Loading