[WIP] Create an on-demand augmentor for overriding natives #1357
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WIP
I'm not sure if this idea (see below) will work yet. I'm building it up and will take this out of WIP when it is in a good state that we think will work. I'm going to switch to a different update and work on this in the background until I can finish the concatenation of AST files correctly.
Please comment anything that you think might help or you think would work better. Any feedback will help me refine this idea and the code.
Problem
While working on generics, one task is to share generics across packages so that correct instances can be created. This means that the AST files themselves need to be in memory at the same time and we have to delay converting them to Archives until after the instantiation of types. (This seemed way simpler than adding all the information to instantiate generics into the Archives and doing a second pass of analysis after filling in the missing instances.)
Since we need all the AST files at the same time, we can upgrade to use the
Load
method fromx/tools/go/packages
(packages
) that will read all the packages in memory before returning. However, to perform our file augmentation and native overrides whilst usingpackages
, we have come up with a way to perform the augmentation with the fields exposed inpackages.Config
.Solution
This code change creates an on-demand augmentor that will fit into the
ParseFile
function pointer field ofpackages.Config
. The idea is that whenParseFile
goes to parse the file, we read and parse the file (probably withparser.ParseFile
), then we augment it, before returning fromParseFile
.How we do that is to look at the package path of the file via the file's AST. If we haven't seen the package before we call it the "first file" for that package. If the package is one of the ones that we have overrides for, then the on-demand augmentor reads the overrides into additional file ASTs. We process additional ASTs to get the overrides. We apply those overrides to the first file and store them in the augmentor for later. We then append all the additional ASTs to the end of the first file. The Pos for the additional ASTs will still point to the virtual native override files in the FileSet so it will still report the correct location on a parse error. When another file from the same package is loaded, the augmentor will look up the overrides for that package and apply the overrides to that file.
This code change will not switch us over to use
packages
since that would require a lot of work in the builder and contexts. This is simply to get the augmentation to be performed on-demand like we will need it to be forpackages
, even though without usingpackages
it seems like an odd move.