registry.mx raw

   1  package types
   2  
   3  // Global bridge variables - DEPRECATED. These exist only for the legacy
   4  // compiler's codegen which reads them directly. Stage4 code accesses
   5  // these through cctx.universe.Registry / cctx.universe.DirectImports.
   6  var ImportRegistry map[string]*TCPackage
   7  var PkgDirectImports map[string][]string
   8  
   9  // ImportByName / LookupImportByName - DEPRECATED. Use UniverseState.LookupByName.
  10  var ImportByName map[string]*TCPackage
  11  
  12  func LookupImportByName(name string) (pkg *TCPackage) {
  13  	if ImportByName == nil {
  14  		ImportByName = map[string]*TCPackage{}
  15  		for _, p := range ImportRegistry {
  16  			if p != nil {
  17  				ImportByName[p.Name] = p
  18  			}
  19  		}
  20  	}
  21  	return ImportByName[name]
  22  }
  23  
  24  func EnsureImportRegistry() {}
  25  
  26  // DirectImportPaths on UniverseState - the correct path.
  27  func (u *UniverseState) DirectImportPaths(pkg *TCPackage) (paths []string) {
  28  	if pkg == nil || u == nil {
  29  		return nil
  30  	}
  31  	if ps, ok := u.DirectImports[pkg.Path]; ok {
  32  		return ps
  33  	}
  34  	var out []string
  35  	for _, ip := range pkg.Imports {
  36  		push(out, ip.Path)
  37  	}
  38  	if len(out) == 0 {
  39  		sc := pkg.Scope
  40  		if sc == nil {
  41  			return nil
  42  		}
  43  		for _, n := range sc.Names() {
  44  			if pn, ok := sc.Lookup(n).(*PkgName); ok && pn.Imported != nil {
  45  				push(out, pn.Imported.Path)
  46  			}
  47  		}
  48  	}
  49  	u.DirectImports[pkg.Path] = out
  50  	return out
  51  }
  52  
  53  // DirectImportPaths - DEPRECATED global version. Use UniverseState method.
  54  func DirectImportPaths(pkg *TCPackage) (paths []string) {
  55  	if pkg == nil {
  56  		return nil
  57  	}
  58  	if ps, ok := PkgDirectImports[pkg.Path]; ok {
  59  		return ps
  60  	}
  61  	var out []string
  62  	for _, ip := range pkg.Imports {
  63  		push(out, ip.Path)
  64  	}
  65  	if len(out) == 0 {
  66  		sc := pkg.Scope
  67  		if sc == nil {
  68  			return nil
  69  		}
  70  		for _, n := range sc.Names() {
  71  			if pn, ok := sc.Lookup(n).(*PkgName); ok && pn.Imported != nil {
  72  				push(out, pn.Imported.Path)
  73  			}
  74  		}
  75  	}
  76  	PkgDirectImports[pkg.Path] = out
  77  	return out
  78  }
  79