cue: improve naming of Runtime methods

They also have slightly improved semantics.
Most notably, the fields of a struct expression
are unrolled instead of included as a emit
declaration.

Change-Id: Ic1f09100865836182eece3b8e79911477c867d3f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2563
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/import.go b/cmd/cue/cmd/import.go
index 89a48ea..574bf59 100644
--- a/cmd/cue/cmd/import.go
+++ b/cmd/cue/cmd/import.go
@@ -423,7 +423,7 @@
 
 		switch {
 		case flagPath.String(cmd) != "":
-			inst, err := runtime.FromExpr(expr)
+			inst, err := runtime.CompileExpr(expr)
 			if err != nil {
 				return err
 			}
diff --git a/cue/build.go b/cue/build.go
index c1a6b70..6883249 100644
--- a/cue/build.go
+++ b/cue/build.go
@@ -56,10 +56,68 @@
 	return inst, nil
 }
 
+// Compile compiles the given source into an Instance. The source code may be
+// provided as a string, byte slice, io.Reader. The name is used as the file
+// name in position information. The source may import builtin packages. Use
+// Build to allow importing non-builtin packages.
+func (r *Runtime) Compile(filename string, source interface{}) (*Instance, error) {
+	ctx := r.Context
+	if ctx == nil {
+		ctx = build.NewContext()
+	}
+	p := ctx.NewInstance(filename, dummyLoad)
+	if err := p.AddFile(filename, source); err != nil {
+		return nil, p.Err
+	}
+	return r.complete(p)
+}
+
+// CompileFile compiles the given source file into an Instance. The source may
+// import builtin packages. Use Build to allow importing non-builtin packages.
+func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
+	ctx := r.Context
+	if ctx == nil {
+		ctx = build.NewContext()
+	}
+	p := ctx.NewInstance(file.Filename, dummyLoad)
+	err := p.AddSyntax(file)
+	if err != nil {
+		return nil, err
+	}
+	if file.Name != nil {
+		p.PkgName = file.Name.Name
+	}
+	return r.complete(p)
+}
+
+// CompileExpr compiles the given source expression into an Instance. The source
+// may import builtin packages. Use Build to allow importing non-builtin
+// packages.
+func (r *Runtime) CompileExpr(expr ast.Expr) (*Instance, error) {
+	ctx := r.Context
+	if ctx == nil {
+		ctx = build.NewContext()
+	}
+	p := ctx.NewInstance("", dummyLoad)
+	switch x := expr.(type) {
+	case *ast.StructLit:
+		_ = p.AddSyntax(&ast.File{Decls: x.Elts})
+	default:
+		_ = p.AddSyntax(&ast.File{
+			Decls: []ast.Decl{&ast.EmitDecl{Expr: expr}},
+		})
+	}
+	if p.Err != nil {
+		return nil, p.Err
+	}
+	return r.complete(p)
+}
+
 // Parse parses a CUE source value into a CUE Instance. The source code may
 // be provided as a string, byte slice, or io.Reader. The name is used as the
 // file name in position information. The source may import builtin packages.
 //
+// Deprecated: use Compile
 func (r *Runtime) Parse(name string, source interface{}) (*Instance, error) {
 	ctx := r.Context
 	if ctx == nil {
@@ -104,6 +162,8 @@
 
 // FromExpr creates an instance from an expression.
 // Any references must be resolved beforehand.
+//
+// Deprecated: use CompileExpr
 func (r *Runtime) FromExpr(expr ast.Expr) (*Instance, error) {
 	i := r.index().newInstance(nil)
 	err := i.insertFile(&ast.File{
diff --git a/cue/build/instance.go b/cue/build/instance.go
index c599e8f..f0c9ce9 100644
--- a/cue/build/instance.go
+++ b/cue/build/instance.go
@@ -171,16 +171,12 @@
 		return err
 	}
 
-	if err := inst.addSyntax(file); err != nil {
-		inst.ReportError(err)
-		return err
-	}
-	return nil
+	return inst.AddSyntax(file)
 }
 
-// addSyntax adds the given file to list of files for this instance. The package
+// AddSyntax adds the given file to list of files for this instance. The package
 // name of the file must match the package name of the instance.
-func (inst *Instance) addSyntax(file *ast.File) errors.Error {
+func (inst *Instance) AddSyntax(file *ast.File) errors.Error {
 	pkg := ""
 	pos := file.Pos()
 	if file.Name != nil {
@@ -188,9 +184,11 @@
 		pos = file.Name.Pos()
 	}
 	if !inst.setPkg(pkg) && pkg != inst.PkgName {
-		return errors.Newf(pos,
+		err := errors.Newf(pos,
 			"package name %q conflicts with previous package name %q",
 			pkg, inst.PkgName)
+		inst.ReportError(err)
+		return err
 	}
 	inst.Files = append(inst.Files, file)
 	return nil
diff --git a/cue/build_test.go b/cue/build_test.go
index 4369f43..da37549 100644
--- a/cue/build_test.go
+++ b/cue/build_test.go
@@ -41,7 +41,7 @@
 	for _, tc := range testCases {
 		t.Run("", func(t *testing.T) {
 			r := &Runtime{}
-			inst, err := r.FromExpr(tc.expr)
+			inst, err := r.CompileExpr(tc.expr)
 			if err != nil {
 				t.Fatal(err)
 			}
diff --git a/pkg/tool/file/file_test.go b/pkg/tool/file/file_test.go
index e82081f..b284445 100644
--- a/pkg/tool/file/file_test.go
+++ b/pkg/tool/file/file_test.go
@@ -35,7 +35,7 @@
 		t.Fatal(err)
 	}
 	var r cue.Runtime
-	i, err := r.FromExpr(x)
+	i, err := r.CompileExpr(x)
 	if err != nil {
 		t.Fatal(err)
 	}