Skip to content

Commit d4d69cb

Browse files
committed
Merge branch 'main' of github.com:hylo-lang/hylo into range-of-non-comparable
2 parents d2038b3 + c0405e5 commit d4d69cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1394
-326
lines changed

Docs/CLI.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Import the built-in module. Allows using Hylo's built-in types and functions (e
4848

4949
Do not include the standard library module.
5050

51+
### `--freestanding`
52+
53+
Compile in freestanding mode (don't use libc).
54+
5155
### `--typecheck`
5256

5357
Type-check the input file(s). Exits the compilation pipeline after type-checking and does not produce an output file.

Sources/CodeGen/LLVM/Transpilation.swift

+5
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,11 @@ extension LLVM.Module {
934934
let source = llvm(s.operands[0])
935935
register[.register(i)] = insertZeroExtend(source, to: target, at: insertionPoint)
936936

937+
case .sext(_, let t):
938+
let target = ir.llvm(builtinType: t, in: &self)
939+
let source = llvm(s.operands[0])
940+
register[.register(i)] = insertSignExtend(source, to: target, at: insertionPoint)
941+
937942
case .inttoptr(_):
938943
let source = llvm(s.operands[0])
939944
register[.register(i)] = insertIntToPtr(source, at: insertionPoint)

Sources/Core/AST/AST+Walk.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ extension AST {
706706
public func traverse<O: ASTWalkObserver>(
707707
_ n: ConditionalCompilationStmt, notifying o: inout O
708708
) {
709-
walk(roots: n.expansion, notifying: &o)
709+
walk(roots: n.expansion(for: compiler), notifying: &o)
710710
}
711711

712712
/// Visits the children of `n` in pre-order, notifying `o` when a node is entered or left.

Sources/Core/AST/AST.swift

+23-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ public struct AST {
2020
/// The module containing Hylo's core library, if any.
2121
public var coreLibrary: ModuleDecl.ID?
2222

23+
/// Information about the compiler processing `self`.
24+
public let compiler: CompilerConfiguration
25+
26+
/// Creates an empty AST for given compiler.
27+
public init(for compiler: CompilerConfiguration) {
28+
self.compiler = compiler
29+
}
30+
2331
}
2432

2533
/// The notional stored properties of `self`; distinguished for encoding/decoding purposes.
26-
private var storage = Storage()
34+
private var storage: Storage
2735

2836
/// The traits in Hylo's standard library that are known by the compiler.
2937
public var coreTraits: CoreTraits?
@@ -50,8 +58,15 @@ public struct AST {
5058
set { storage.coreLibrary = newValue }
5159
}
5260

61+
/// Information about the compiler processing `self`.
62+
public var compiler: CompilerConfiguration {
63+
return storage.compiler
64+
}
65+
5366
/// Creates an empty AST.
54-
public init() {}
67+
public init(for compiler: CompilerConfiguration) {
68+
self.storage = Storage(for: compiler)
69+
}
5570

5671
/// Inserts `n` into `self`, updating `diagnostics` if `n` is ill-formed.
5772
public mutating func insert<T: Node>(_ n: T, diagnostics: inout DiagnosticSet) -> T.ID {
@@ -121,10 +136,10 @@ public struct AST {
121136
public func coreType(_ name: String) -> ProductType? {
122137
precondition(isCoreModuleLoaded, "Core library is not loaded")
123138

124-
for id in topLevelDecls(coreLibrary!) where id.kind == ProductTypeDecl.self {
125-
let id = ProductTypeDecl.ID(id)!
126-
if self[id].baseName == name {
127-
return ProductType(id, ast: self)
139+
for d in topLevelDecls(coreLibrary!) where d.kind == ProductTypeDecl.self {
140+
let d = ProductTypeDecl.ID(d)!
141+
if self[d].baseName == name {
142+
return ProductType(d, ast: self)
128143
}
129144
}
130145

@@ -169,8 +184,8 @@ public struct AST {
169184
modules.first(where: { self[$0].baseName == n })
170185
}
171186

172-
/// Returns the IDs of the top-level declarations in the lexical scope of `module`.
173-
public func topLevelDecls(_ module: ModuleDecl.ID) -> some Collection<AnyDeclID> {
187+
/// Returns the top-level declarations in the lexical scope of `module`.
188+
private func topLevelDecls(_ module: ModuleDecl.ID) -> some Collection<AnyDeclID> {
174189
self[self[module].sources].map(\.decls).joined()
175190
}
176191

Sources/Core/AST/BundledNode.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ extension BundledNode where T: ConcreteNodeID {
4949
extension BundledNode where T: ScopeID {
5050

5151
/// The declarations in this immediate scope.
52-
public var decls: [AnyDeclID] {
53-
container.scopeToDecls[id, default: []]
52+
public var decls: DeclIDs {
53+
container.scopeToDecls[id, default: .init()]
5454
}
5555

5656
}

Sources/Core/AST/NodeIDs/DeclID.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public protocol DeclID: NodeIDProtocol {}
55

66
extension DeclID {
77

8-
/// Indicates whether `self` denotes an overloadable declaration.
8+
/// `true` iff `self` denotes an overloadable declaration.
99
public var isOverloadable: Bool {
1010
switch kind {
1111
case FunctionDecl.self, InitializerDecl.self, MethodDecl.self, SubscriptDecl.self:
@@ -25,6 +25,11 @@ extension DeclID {
2525
(kind.value as! Decl.Type).isCallable
2626
}
2727

28+
/// `true` iff `self` denotes a type extending declaration.
29+
public var isTypeExtendingDecl: Bool {
30+
(kind == ExtensionDecl.self) || (kind == ConformanceDecl.self)
31+
}
32+
2833
/// `true` iff `self` denotes a conformance source.
2934
public var isConformanceSource: Bool {
3035
kind.value is ConformanceSource.Type
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Utils
2+
3+
/// A collection of declaration identifiers.
4+
public struct DeclIDs {
5+
6+
/// Instances are notionally the concatenation of two collections `front` and `back` such that
7+
/// `front` only contains type extending declarations and `back` contains none. `insert(_:)`
8+
/// maintains this invariant as well as the relative order of the elements in these collections.
9+
10+
/// All identifiers in `self`.
11+
private(set) var all: [AnyDeclID]
12+
13+
/// The position immediately after the last ID of a type extending declaration in `all`.
14+
private var extensionEndIndex: Int
15+
16+
/// Creates an empty instance.
17+
public init() {
18+
all = []
19+
extensionEndIndex = 0
20+
}
21+
22+
/// Creates an instance with the contents of each collection in `batch`.
23+
public init<S: Sequence<DeclIDs>>(formingUnionOf batch: S) {
24+
all = []
25+
26+
var end: [AnyDeclID] = []
27+
for c in batch {
28+
all.append(contentsOf: c[..<c.extensionEndIndex])
29+
end.append(contentsOf: c[c.extensionEndIndex...])
30+
}
31+
32+
extensionEndIndex = all.endIndex
33+
all.append(contentsOf: end)
34+
}
35+
36+
/// The identifiers in `self` denoting type extending declarations.
37+
public var extensions: ArraySlice<AnyDeclID> {
38+
all[0 ..< extensionEndIndex]
39+
}
40+
41+
/// The identifiers in `self` _not_ denoting type extending declarations.
42+
public var withoutExtensions: ArraySlice<AnyDeclID> {
43+
all[extensionEndIndex...]
44+
}
45+
46+
/// Inserts `d` in `self`.
47+
public mutating func insert(_ d: AnyDeclID) {
48+
if d.isTypeExtendingDecl {
49+
all.insert(d, at: extensionEndIndex)
50+
extensionEndIndex += 1
51+
} else {
52+
all.append(d)
53+
}
54+
}
55+
56+
}
57+
58+
extension DeclIDs: Equatable {}
59+
60+
extension DeclIDs: RandomAccessCollection {
61+
62+
public typealias Index = Int
63+
64+
public typealias Element = AnyDeclID
65+
66+
public var startIndex: Int { 0 }
67+
68+
public var endIndex: Int { all.endIndex }
69+
70+
public func index(after position: Int) -> Int {
71+
position + 1
72+
}
73+
74+
public func index(before position: Int) -> Int {
75+
position - 1
76+
}
77+
78+
public func index(_ position: Int, offsetBy distance: Int) -> Int {
79+
position + distance
80+
}
81+
82+
public subscript(position: Int) -> AnyDeclID {
83+
all[position]
84+
}
85+
86+
}

Sources/Core/AST/Stmt/ConditionalCompilationStmt.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public struct ConditionalCompilationStmt: Stmt {
6969
}
7070

7171
/// Returns `true` iff `self` holds for the current process.
72-
public func holds(for info: CompilerInfo) -> Bool {
72+
public func holds(for info: CompilerConfiguration) -> Bool {
7373
switch self {
7474
case .`true`: return true
7575
case .`false`: return false
@@ -108,8 +108,8 @@ public struct ConditionalCompilationStmt: Stmt {
108108
}
109109

110110
/// Returns the statements that this expands to.
111-
public var expansion: [AnyStmtID] {
112-
if condition.holds(for: CompilerInfo.instance) {
111+
public func expansion(for compiler: CompilerConfiguration) -> [AnyStmtID] {
112+
if condition.holds(for: compiler) {
113113
return stmts
114114
} else {
115115
return fallback

Sources/Core/CompilerInfo.swift

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Describes the compiler; used as source of truth for conditional compilation.
2-
public struct CompilerInfo {
2+
public struct CompilerConfiguration: Codable {
33

44
/// The operating-system used when performing the compilation.
55
let os: String
@@ -19,17 +19,14 @@ public struct CompilerInfo {
1919
/// The set of features supported in the current compilation.
2020
let features: [String]
2121

22-
/// We only need one instance of this struct, to represent the compiler information.
23-
public static let instance = CompilerInfo()
24-
25-
/// Creates an instance with the properties of the machine running this initializer.
26-
private init() {
27-
os = CompilerInfo.currentOS()
28-
arch = CompilerInfo.currentArch()
22+
/// Creates an instance with the properties of the machine running this initializer, using features `f`.
23+
public init(_ f: [String] = []) {
24+
os = CompilerConfiguration.currentOS()
25+
arch = CompilerConfiguration.currentArch()
2926
compiler = "hc"
3027
compilerVersion = SemanticVersion(major: 0, minor: 1, patch: 0)
3128
hyloVersion = SemanticVersion(major: 0, minor: 1, patch: 0)
32-
features = ["useLibC"]
29+
features = f
3330
}
3431

3532
/// The name of the operating system on which this function is run.

Sources/Core/Conformance.swift

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public struct Conformance {
2222
}
2323
}
2424

25+
/// `true` iff `self` is synthetic.
26+
public var isSynthetic: Bool {
27+
if case .synthetic = self {
28+
return true
29+
} else {
30+
return false
31+
}
32+
}
33+
2534
}
2635

2736
/// The type on the left-hand side of this conformance.

Sources/Core/Program.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public protocol Program {
99
/// A map from node to the innermost scope that contains it.
1010
var nodeToScope: ASTProperty<AnyScopeID> { get }
1111

12-
/// A map from scope to the declarations directly contained in them.
13-
var scopeToDecls: ASTProperty<[AnyDeclID]> { get }
12+
/// A map from scope to the declarations that it contains.
13+
var scopeToDecls: ASTProperty<DeclIDs> { get }
1414

1515
/// A map from variable declaration its containing binding declaration.
1616
var varToBinding: [VarDecl.ID: BindingDecl.ID] { get }

0 commit comments

Comments
 (0)