Commit Graph

3 Commits

Author SHA1 Message Date
Ben Clayton 0b930237ec ast: Clone symbols instead of ever-growing
The interface for cloning a module was made significantly more complex in https://dawn-review.googlesource.com/c/tint/+/35502/ as some of the transforms required constructing symbols before the clone. This was temporary solution in 35502 was to copy the symbol table, then construct the new types, then perform the clone. This lead to a really messy callback interface, that was extremely error prone (e.g. lamda-capturing stack variables from the initializer callback that had been unwound).

Instead, clone the symbols as they're encountered. This may produce an entirely different set of identifiers, but no longer ever-grows the symbol list, and keeps the interface clean.

Bug: tint:396
Bug: tint:390
Change-Id: I54affd68ac3b730b649af9b47eba685c8a1d784a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35663
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2020-12-15 12:32:18 +00:00
Ben Clayton ce33d42b41 ast: Remove no-arg constructor from Node
Bug: tint:396
Bug: tint:390
Change-Id: I730738dd6bafa946dc66ee8a15c48b3a3f73e5fc
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35164
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
2020-12-12 13:00:34 +00:00
Ben Clayton 2bc6840d3c Add ast::CloneContext::ReplaceAll()
ReplaceAll() registers `replacer` to be called whenever the Clone() method is called with a type that matches (or derives from) the type of the first parameter of `replacer`.

`replacer` must be function-like with the signature: `T* (T*)`, where `T` is a type deriving from CastableBase.

If `replacer` returns a nullptr then Clone() will attempt the next registered replacer function that matches the object type. If no replacers match the object type, or all returned nullptr then Clone() will call `T::Clone()` to clone the object.

Example:

```
  // Replace all ast::UintLiterals with the number 42
  CloneCtx ctx(mod);
  ctx.ReplaceAll([&] (ast::UintLiteral* in) {
    return ctx.mod->create<ast::UintLiteral>(ctx.Clone(in->type()), 42);
  });
  auto* out = ctx.Clone(tree);
```

This is to be used by Transforms that want to replace parts of the AST on clone.

Bug: tint:390
Change-Id: I80a0e58aa3711f309f58a504f6b6a06f6c546ea1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34568
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2020-12-03 18:10:39 +00:00