Skip to content

Commit

Permalink
Better initialization of the original member for CXX nodes
Browse files Browse the repository at this point in the history
Remove extra initializers in CXX nodes.
  • Loading branch information
lucteo committed Dec 30, 2022
1 parent 3843f8f commit 07cf9da
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 104 deletions.
30 changes: 17 additions & 13 deletions Sources/CodeGen/CXX/CXXTranspiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public struct CXXTranspiler {
return emit(brace: stmt)

case .expr(let expr):
let exprBody = CXXReturnStmt(emitR(expr: expr), for: expr)
return CXXScopedBlock([exprBody], for: expr)
let exprBody = CXXReturnStmt(expr: emitR(expr: expr), original: expr)
return CXXScopedBlock(stmts: [exprBody], original: AnyNodeID.TypedNode(expr))
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public struct CXXTranspiler {
// No pattern found; just call the initializer, dropping the result.
return CXXVoidCast(baseExpr: cxxInitialzer, original: initializer)
} else {
return CXXScopedBlock(stmts, for: initializer)
return CXXScopedBlock(stmts: stmts, original: AnyNodeID.TypedNode(initializer))
}
} else {
return CXXComment("EMPTY borrowed local binding (\(capability))", for: decl)
Expand Down Expand Up @@ -134,7 +134,7 @@ public struct CXXTranspiler {
for s in stmt.stmts {
stmts.append(emit(stmt: s))
}
return CXXScopedBlock(stmts, for: stmt)
return CXXScopedBlock(stmts: stmts, original: AnyNodeID.TypedNode(stmt))
}

private mutating func emit(declStmt stmt: DeclStmt.Typed) -> CXXRepresentable {
Expand Down Expand Up @@ -191,7 +191,7 @@ public struct CXXTranspiler {
private mutating func emitR(
booleanLiteral expr: BooleanLiteralExpr.Typed
) -> CXXRepresentable {
return CXXBooleanLiteralExpr(expr.value, original: expr)
return CXXBooleanLiteralExpr(value: expr.value, original: expr)
}

private mutating func emitR(
Expand Down Expand Up @@ -292,7 +292,7 @@ public struct CXXTranspiler {
// The receiver as a borrowing convention.
switch calleeNameExpr.domain {
case .none:
receiver = CXXThisExpr(original: expr)
receiver = CXXThisExpr(original: AnyExprID.TypedNode(expr))

case .expr(let receiverID):
receiver = emitL(expr: receiverID, withCapability: type.capability)
Expand All @@ -304,7 +304,7 @@ public struct CXXTranspiler {
// The receiver is consumed.
switch calleeNameExpr.domain {
case .none:
receiver = CXXThisExpr(original: expr)
receiver = CXXThisExpr(original: AnyExprID.TypedNode(expr))

case .expr(let receiverID):
receiver = emitR(expr: receiverID)
Expand All @@ -316,7 +316,8 @@ public struct CXXTranspiler {

// Emit the function reference.
callee = CXXCompoundExpr(
base: receiver, id: CXXIdentifier(nameOfDecl(calleeDecl)), original: expr)
base: receiver, id: CXXIdentifier(nameOfDecl(calleeDecl)),
original: AnyExprID.TypedNode(expr))

default:
// Evaluate the callee as a function object.
Expand All @@ -327,13 +328,14 @@ public struct CXXTranspiler {
callee = emitR(expr: expr.callee)
}

return CXXFunctionCallExpr(callee: callee, arguments: arguments, original: expr)
return CXXFunctionCallExpr(
callee: callee, arguments: arguments, original: AnyExprID.TypedNode(expr))
}

private mutating func emitR(
integerLiteral expr: IntegerLiteralExpr.Typed
) -> CXXRepresentable {
return CXXIntegerLiteralExpr(expr.value, original: expr)
return CXXIntegerLiteralExpr(value: expr.value, original: expr)
}

private mutating func emitR(
Expand Down Expand Up @@ -412,13 +414,15 @@ public struct CXXTranspiler {
"<=", ">=", ">", "^", "&", "&&", "|", "||", "<<=", ">>=", "*=", "/=", "%=", "+=", "-=", "&=",
"&&=", "**":
// Expand this as a native infix operator call
return CXXInfixExpr(callee: CXXIdentifier(name), lhs: lhs, rhs: rhs, original: expr)
return CXXInfixExpr(
callee: CXXIdentifier(name), lhs: lhs, rhs: rhs, original: AnyExprID.TypedNode(expr))

default:
// Expand this as a regular function call.
let callee = emitL(name: expr, withCapability: .let)
let arguments = [lhs, rhs]
return CXXFunctionCallExpr(callee: callee, arguments: arguments, original: expr)
return CXXFunctionCallExpr(
callee: callee, arguments: arguments, original: AnyExprID.TypedNode(expr))
}
}

Expand Down Expand Up @@ -484,7 +488,7 @@ public struct CXXTranspiler {
// Emit the compound expression.
let idExpr = CXXIdentifier(nameOfDecl(decl))
if receiver != nil {
return CXXCompoundExpr(base: receiver!, id: idExpr, original: expr)
return CXXCompoundExpr(base: receiver!, id: idExpr, original: AnyExprID.TypedNode(expr))
} else {
return idExpr
}
Expand Down
12 changes: 1 addition & 11 deletions Sources/CodeGen/CXX/Expr/CXXBooleanLiteralExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ public struct CXXBooleanLiteralExpr: CXXRepresentable {
let value: Bool

/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the boolean literal expression from the original AST node.
init<ID: NodeIDProtocol>(_ value: Bool, original: TypedNode<ID>? = nil) {
self.value = value
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}
let original: BooleanLiteralExpr.Typed?

public func writeCode<Target: TextOutputStream>(into target: inout Target) {
target.write(value ? "true" : "false")
Expand Down
13 changes: 0 additions & 13 deletions Sources/CodeGen/CXX/Expr/CXXCompoundExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ struct CXXCompoundExpr: CXXRepresentable {
/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the CXX compound expression from the base expression, identifier expression, and optionaly the original AST node.
init<ID: NodeIDProtocol>(
base: CXXRepresentable, id: CXXRepresentable, original: TypedNode<ID>? = nil
) {
self.base = base
self.id = id
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}

func writeCode<Target: TextOutputStream>(into target: inout Target) {
base.writeCode(into: &target)
target.write(".")
Expand Down
13 changes: 0 additions & 13 deletions Sources/CodeGen/CXX/Expr/CXXFunctionCallExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ struct CXXFunctionCallExpr: CXXRepresentable {
/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the CXX function call expression from the callee and arguments, and optionaly the original AST node.
init<ID: NodeIDProtocol>(
callee: CXXRepresentable, arguments: [CXXRepresentable], original: TypedNode<ID>? = nil
) {
self.callee = callee
self.arguments = arguments
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}

func writeCode<Target: TextOutputStream>(into target: inout Target) {
callee.writeCode(into: &target)
target.write("(")
Expand Down
15 changes: 0 additions & 15 deletions Sources/CodeGen/CXX/Expr/CXXInfixExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ struct CXXInfixExpr: CXXRepresentable {
/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the CXX function call expression from the callee and arguments, and optionaly the original AST node.
init<ID: NodeIDProtocol>(
callee: CXXRepresentable, lhs: CXXRepresentable, rhs: CXXRepresentable,
original: TypedNode<ID>? = nil
) {
self.callee = callee
self.lhs = lhs
self.rhs = rhs
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}

func writeCode<Target: TextOutputStream>(into target: inout Target) {
lhs.writeCode(into: &target)
target.write(" ")
Expand Down
12 changes: 1 addition & 11 deletions Sources/CodeGen/CXX/Expr/CXXIntegerLiteralExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ public struct CXXIntegerLiteralExpr: CXXRepresentable {
let value: String

/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the integer literal expression from the original AST node.
init<ID: NodeIDProtocol>(_ value: String, original: TypedNode<ID>? = nil) {
self.value = value
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}
let original: IntegerLiteralExpr.Typed?

public func writeCode<Target: TextOutputStream>(into target: inout Target) {
target.write(value)
Expand Down
9 changes: 0 additions & 9 deletions Sources/CodeGen/CXX/Expr/CXXThisExpr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ struct CXXThisExpr: CXXRepresentable {
/// The original node in Val AST.
let original: AnyExprID.TypedNode?

/// Construct the `this` expression from the original AST node.
init<ID: NodeIDProtocol>(original: TypedNode<ID>? = nil) {
if let orig = original {
self.original = orig as? AnyExprID.TypedNode
} else {
self.original = nil
}
}

func writeCode<Target: TextOutputStream>(into target: inout Target) {
target.write("this")
}
Expand Down
11 changes: 1 addition & 10 deletions Sources/CodeGen/CXX/Stmt/CXXReturnStmt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ struct CXXReturnStmt: CXXRepresentable {

/// The original node in Val AST.
/// This node can be of any type.
let original: AnyNodeID.TypedNode?
let original: AnyExprID.TypedNode?

/// Construct the CXX scoped block from the list of CXX entities, and optionaly the original AST node.
init<ID: NodeIDProtocol>(_ expr: CXXRepresentable?, for original: TypedNode<ID>? = nil) {
self.expr = expr
if let orig = original {
self.original = orig as? AnyNodeID.TypedNode
} else {
self.original = nil
}
}
func writeCode<Target: TextOutputStream>(into target: inout Target) {
if expr != nil {
target.write("return ")
Expand Down
9 changes: 0 additions & 9 deletions Sources/CodeGen/CXX/Stmt/CXXScopedBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ struct CXXScopedBlock: CXXRepresentable {
/// This node can be of any type.
let original: AnyNodeID.TypedNode?

/// Construct the CXX scoped block from the list of CXX entities, and optionaly the original AST node.
init<ID: NodeIDProtocol>(_ stmts: [CXXRepresentable], for original: TypedNode<ID>? = nil) {
self.stmts = stmts
if let orig = original {
self.original = orig as? AnyNodeID.TypedNode
} else {
self.original = nil
}
}
func writeCode<Target: TextOutputStream>(into target: inout Target) {
target.write("{\n")
for stmt in stmts {
Expand Down
20 changes: 20 additions & 0 deletions Sources/Core/TypedNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,23 @@ extension TypedNode where ID == NodeID<SequenceExpr> {
}

}

extension TypedNode where ID == AnyNodeID {

/// Any typed node is convertible to TypedNode<AnyNodeID>.
public init<SourceID: NodeIDProtocol>(_ s: TypedNode<SourceID>) {
program = s.program
id = AnyNodeID(s.id)
}

}

extension TypedNode where ID == AnyExprID {

/// Any typed expression node is convertible to TypedNode<AnyExprID>.
public init<SourceID: ExprID>(_ s: TypedNode<SourceID>) {
program = s.program
id = AnyExprID(s.id)
}

}

0 comments on commit 07cf9da

Please sign in to comment.