...
Run Format

Source file src/go/importer/importer.go

     1	// Copyright 2015 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package importer provides access to export data importers.
     6	package importer
     7	
     8	import (
     9		"go/internal/gccgoimporter"
    10		"go/internal/gcimporter"
    11		"go/types"
    12		"io"
    13		"runtime"
    14	)
    15	
    16	// A Lookup function returns a reader to access package data for
    17	// a given import path, or an error if no matching package is found.
    18	type Lookup func(path string) (io.ReadCloser, error)
    19	
    20	// For returns an Importer for the given compiler and lookup interface,
    21	// or nil. Supported compilers are "gc", and "gccgo". If lookup is nil,
    22	// the default package lookup mechanism for the given compiler is used.
    23	// BUG(issue13847): For does not support non-nil lookup functions.
    24	func For(compiler string, lookup Lookup) types.Importer {
    25		switch compiler {
    26		case "gc":
    27			if lookup != nil {
    28				panic("gc importer for custom import path lookup not yet implemented")
    29			}
    30	
    31			return make(gcimports)
    32	
    33		case "gccgo":
    34			if lookup != nil {
    35				panic("gccgo importer for custom import path lookup not yet implemented")
    36			}
    37	
    38			var inst gccgoimporter.GccgoInstallation
    39			if err := inst.InitFromDriver("gccgo"); err != nil {
    40				return nil
    41			}
    42			return &gccgoimports{
    43				packages: make(map[string]*types.Package),
    44				importer: inst.GetImporter(nil, nil),
    45			}
    46		}
    47	
    48		// compiler not supported
    49		return nil
    50	}
    51	
    52	// Default returns an Importer for the compiler that built the running binary.
    53	// If available, the result implements types.ImporterFrom.
    54	func Default() types.Importer {
    55		return For(runtime.Compiler, nil)
    56	}
    57	
    58	// gc support
    59	
    60	type gcimports map[string]*types.Package
    61	
    62	func (m gcimports) Import(path string) (*types.Package, error) {
    63		return m.ImportFrom(path, "" /* no vendoring */, 0)
    64	}
    65	
    66	func (m gcimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
    67		if mode != 0 {
    68			panic("mode must be 0")
    69		}
    70		return gcimporter.Import(m, path, srcDir)
    71	}
    72	
    73	// gccgo support
    74	
    75	type gccgoimports struct {
    76		packages map[string]*types.Package
    77		importer gccgoimporter.Importer
    78	}
    79	
    80	func (m *gccgoimports) Import(path string) (*types.Package, error) {
    81		return m.ImportFrom(path, "" /* no vendoring */, 0)
    82	}
    83	
    84	func (m *gccgoimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
    85		if mode != 0 {
    86			panic("mode must be 0")
    87		}
    88		// TODO(gri) pass srcDir
    89		return m.importer(m.packages, path)
    90	}
    91	

View as plain text