@@ -44,19 +44,19 @@ type Sources struct {
4444 JSFiles []jsFile.JSFile
4545
4646 // TypeInfo is the type information this package.
47- // This is nil until PrepareInfo is called.
47+ // This is nil until Prepare is called.
4848 TypeInfo * analysis.Info
4949
5050 // Instances is the type parameters instances for this package.
51- // This is nil until PrepareInfo is called.
51+ // This is nil until Prepare is called.
5252 Instances * typeparams.PackageInstanceSets
5353
5454 // Package is the type-PrepareInfo package.
55- // This is nil until PrepareInfo is called.
55+ // This is nil until Prepare is called.
5656 Package * types.Package
5757
5858 // GoLinknames is the set of Go linknames for this package.
59- // This is nil until PrepareInfo is called.
59+ // This is nil until Prepare is called.
6060 GoLinknames []linkname.GoLinkname
6161}
6262
@@ -67,14 +67,13 @@ type Importer func(path, srcDir string) (*Sources, error)
6767// determining the type information, go linknames, etc.
6868//
6969// The importer function is used to import the sources of other packages
70- // that are imported by this package being prepared. If the other sources
71- // are not prepared when returned by the importer, that package will be
72- // prepared as well before continuing on with the current package.
70+ // that the package being prepared depends on . If the other sources
71+ // are not prepared when returned by the importer, then that package
72+ // will be prepared before continuing on with the current package.
7373// This is where the recursive nature of the Prepare function comes in.
7474//
7575// Note that at the end of this call the analysis information
76- // has NOT been propagated across packages yet
77- // and the source files have not been simplified yet.
76+ // has NOT been propagated across packages yet.
7877func (s * Sources ) Prepare (importer Importer , sizes types.Sizes , tContext * types.Context ) error {
7978 // Skip if the sources have already been prepared.
8079 if s .isPrepared () {
@@ -113,6 +112,12 @@ func (s *Sources) Prepare(importer Importer, sizes types.Sizes, tContext *types.
113112 return nil
114113}
115114
115+ // isPrepared returns true if this sources have had Prepare called on it.
116+ //
117+ // This can not determine if the type information has been propagated
118+ // across packages yet, but usually would only be called prior to that.
119+ // For the source to be fully prepared for compilation, the type information
120+ // must be propagated across packages as well.
116121func (s * Sources ) isPrepared () bool {
117122 return s .TypeInfo != nil && s .Package != nil
118123}
@@ -132,8 +137,8 @@ func (s *Sources) sort() {
132137// Note this function mutates the original Files slice.
133138// This must be called after TypeCheck and before analyze since
134139// this will change the pointers in the AST, for example the pointers
135- // to function literals will change making it impossible to find them
136- // in the type information if analyze is called first.
140+ // to function literals will change, making it impossible to find them
141+ // in the type information, if analyze is called first.
137142func (s * Sources ) simplify (typesInfo * types.Info ) {
138143 for i , file := range s .Files {
139144 s .Files [i ] = astrewrite .Simplify (file , typesInfo , false )
@@ -143,7 +148,7 @@ func (s *Sources) simplify(typesInfo *types.Info) {
143148// typeCheck the sources. Returns information about declared package types and
144149// type information for the supplied AST.
145150//
146- // This must be called prior to simplify.
151+ // This must be called prior to simplify to get the types.Info used by simplify .
147152func (s * Sources ) typeCheck (importer Importer , sizes types.Sizes , tContext * types.Context ) (* types.Info , error ) {
148153 const errLimit = 10 // Max number of type checking errors to return.
149154
@@ -195,7 +200,9 @@ func (s *Sources) typeCheck(importer Importer, sizes types.Sizes, tContext *type
195200// analyze will determine the type parameters instances, blocking,
196201// and other type information for the package.
197202//
198- // This must be called after to simplify.
203+ // This must be called after to simplify to ensure the pointers
204+ // in the AST are still valid.
205+ //
199206// Note that at the end of this call the analysis information
200207// has NOT been propagated across packages yet.
201208func (s * Sources ) analyze (typesInfo * types.Info , importer Importer , tContext * types.Context ) {
@@ -222,7 +229,6 @@ func (s *Sources) analyze(typesInfo *types.Info, importer Importer, tContext *ty
222229// parseGoLinknames extracts all //go:linkname compiler directive from the sources.
223230//
224231// This will set the GoLinknames field on the Sources struct.
225- // This must be called prior to simplify.
226232func (s * Sources ) parseGoLinknames () error {
227233 goLinknames := []linkname.GoLinkname {}
228234 var errs errorList.ErrorList
@@ -290,13 +296,22 @@ func (pi *packageImporter) Import(path string) (*types.Package, error) {
290296 return nil , err
291297 }
292298
293- if ! srcs .isPrepared () {
294- err := srcs .Prepare (pi .importer , pi .sizes , pi .tContext )
295- if err != nil {
296- pi .Errors = pi .Errors .AppendDistinct (err )
297- return nil , err
298- }
299+ // If the source hasn't been prepared yet, prepare it now.
300+ // This will recursively prepare all of it's dependencies too.
301+ // If the source is already prepared, this will be a no-op.
302+ err = srcs .Prepare (pi .importer , pi .sizes , pi .tContext )
303+ if err != nil {
304+ pi .Errors = pi .Errors .AppendDistinct (err )
305+ return nil , err
299306 }
300307
301308 return srcs .Package , nil
302309}
310+
311+ // SortedSourcesSlice in place sorts the given slice of Sources by ImportPath.
312+ // This will not change the order of the files within any Sources.
313+ func SortedSourcesSlice (sourcesSlice []* Sources ) {
314+ sort .Slice (sourcesSlice , func (i , j int ) bool {
315+ return sourcesSlice [i ].ImportPath < sourcesSlice [j ].ImportPath
316+ })
317+ }
0 commit comments