Example #1
0
// buildInitializationCompoundExpression builds the compound expression for calling the specified initializers on a struct.
func (db *domBuilder) buildInitializationCompoundExpression(structType typegraph.TypeReference, initializers map[string]codedom.Expression, structValue codedom.Expression, node compilergraph.GraphNode) codedom.Expression {
	// Create a variable to hold the struct instance.
	structInstanceVarName := db.generateScopeVarName(node)

	// Build the assignment expressions.
	assignmentExpressions := make([]codedom.Expression, len(initializers))
	var index = 0
	for fieldName, initializer := range initializers {
		member, found := structType.ResolveMember(fieldName, typegraph.MemberResolutionInstance)
		if !found {
			panic("Member not found in struct initializer construction")
		}

		assignmentExpressions[index] =
			codedom.MemberAssignment(member,
				codedom.MemberReference(
					codedom.LocalReference(structInstanceVarName, node),
					member,
					node),
				initializer,
				node)

		index = index + 1
	}

	// Return a compound expression that takes in the struct value and the assignment expressions,
	// executes them, and returns the struct value.
	return codedom.CompoundExpression(structInstanceVarName, structValue, assignmentExpressions, codedom.LocalReference(structInstanceVarName, node), node)
}
Example #2
0
// buildAssignmentExpression builds an assignment expression for assigning the given value to the target node.
func (db *domBuilder) buildAssignmentExpression(targetNode compilergraph.GraphNode, value codedom.Expression, basisNode compilergraph.GraphNode) codedom.Expression {
	targetScope, _ := db.scopegraph.GetScope(targetNode)
	namedReference, _ := db.scopegraph.GetReferencedName(targetScope)

	if namedReference.IsLocal() {
		return codedom.LocalAssignment(namedReference.Name(), value, basisNode)
	} else {
		member, _ := namedReference.Member()
		return codedom.MemberAssignment(member, db.buildExpression(targetNode), value, basisNode)
	}
}