cuelang.org/go@v0.10.1/internal/core/runtime/build.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "strings" 19 20 "cuelang.org/go/cue/ast" 21 "cuelang.org/go/cue/ast/astutil" 22 "cuelang.org/go/cue/build" 23 "cuelang.org/go/cue/errors" 24 "cuelang.org/go/cue/stats" 25 "cuelang.org/go/cue/token" 26 "cuelang.org/go/internal/core/adt" 27 "cuelang.org/go/internal/core/compile" 28 ) 29 30 type Config struct { 31 Runtime *Runtime 32 Filename string 33 ImportPath string 34 35 Counts *stats.Counts 36 37 compile.Config 38 } 39 40 // Build builds b and all its transitive dependencies, insofar they have not 41 // been build yet. 42 func (x *Runtime) Build(cfg *Config, b *build.Instance) (v *adt.Vertex, errs errors.Error) { 43 if err := b.Complete(); err != nil { 44 return nil, b.Err 45 } 46 if v := x.getNodeFromInstance(b); v != nil { 47 return v, b.Err 48 } 49 // TODO: clear cache of old implementation. 50 // if s := b.ImportPath; s != "" { 51 // // Use cached result, if available. 52 // if v, err := x.LoadImport(s); v != nil || err != nil { 53 // return v, err 54 // } 55 // } 56 57 errs = b.Err 58 59 // Build transitive dependencies. 60 for _, file := range b.Files { 61 file.VisitImports(func(d *ast.ImportDecl) { 62 for _, s := range d.Specs { 63 errs = errors.Append(errs, x.buildSpec(cfg, b, s)) 64 } 65 }) 66 } 67 68 err := x.ResolveFiles(b) 69 errs = errors.Append(errs, err) 70 71 var cc *compile.Config 72 if cfg != nil { 73 cc = &cfg.Config 74 } 75 if cfg != nil && cfg.ImportPath != "" { 76 b.ImportPath = cfg.ImportPath 77 b.PkgName = astutil.ImportPathName(b.ImportPath) 78 } 79 v, err = compile.Files(cc, x, b.ID(), b.Files...) 80 errs = errors.Append(errs, err) 81 82 errs = errors.Append(errs, x.injectImplementations(b, v)) 83 84 if errs != nil { 85 v = adt.ToVertex(&adt.Bottom{Err: errs}) 86 b.Err = errs 87 } 88 89 x.AddInst(b.ImportPath, v, b) 90 91 return v, errs 92 } 93 94 func dummyLoad(token.Pos, string) *build.Instance { return nil } 95 96 func (r *Runtime) Compile(cfg *Config, source interface{}) (*adt.Vertex, *build.Instance) { 97 ctx := build.NewContext() 98 var filename string 99 if cfg != nil && cfg.Filename != "" { 100 filename = cfg.Filename 101 } 102 p := ctx.NewInstance(filename, dummyLoad) 103 if err := p.AddFile(filename, source); err != nil { 104 return nil, p 105 } 106 v, _ := r.Build(cfg, p) 107 return v, p 108 } 109 110 func (r *Runtime) CompileFile(cfg *Config, file *ast.File) (*adt.Vertex, *build.Instance) { 111 ctx := build.NewContext() 112 filename := file.Filename 113 if cfg != nil && cfg.Filename != "" { 114 filename = cfg.Filename 115 } 116 p := ctx.NewInstance(filename, dummyLoad) 117 err := p.AddSyntax(file) 118 if err != nil { 119 return nil, p 120 } 121 p.PkgName = file.PackageName() 122 v, _ := r.Build(cfg, p) 123 return v, p 124 } 125 126 func (x *Runtime) buildSpec(cfg *Config, b *build.Instance, spec *ast.ImportSpec) (errs errors.Error) { 127 info, err := astutil.ParseImportSpec(spec) 128 if err != nil { 129 return errors.Promote(err, "invalid import path") 130 } 131 132 pkg := b.LookupImport(info.ID) 133 if pkg == nil { 134 if strings.Contains(info.ID, ".") { 135 return errors.Newf(spec.Pos(), 136 "package %q imported but not defined in %s", 137 info.ID, b.ImportPath) 138 } else if x.index.builtinPaths[info.ID] == nil { 139 return errors.Newf(spec.Pos(), 140 "builtin package %q undefined", info.ID) 141 } 142 return nil 143 } 144 145 if v := x.getNodeFromInstance(pkg); v != nil { 146 return pkg.Err 147 } 148 149 if _, err := x.Build(cfg, pkg); err != nil { 150 return err 151 } 152 153 return nil 154 }