-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49939: execgen: introduce // execgen:template directive for bools r=jordanlewis a=jordanlewis Based on #49728. This PR adds the // execgen:template<arg1, arg2> syntax for concrete boolean template variables. For example: ``` // execgen:inline // execgen:template<templateParam> func a (runtimeParam int, templateParam bool) int { if templateParam { return runtimeParam + 1 } else { return runtimeParam - 1 } } func main() { x := a(0, true) y := a(0, false) fmt.Println(x, y) } ``` This code will print `1, -1` when eventually executed, but the templateParam boolean will be evaluated at code generation time and not appear in the generated code at all. Two variants of `a()` will be inlined into their callsites - one with templateParam true, and one with templateParam false. Release note: None Co-authored-by: Jordan Lewis <[email protected]>
- Loading branch information
Showing
10 changed files
with
799 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package execgen | ||
|
||
import ( | ||
"go/parser" | ||
"go/token" | ||
"strings" | ||
|
||
"github.com/dave/dst/decorator" | ||
) | ||
|
||
// Generate transforms the string contents of an input execgen template by | ||
// processing all supported // execgen annotations. | ||
func Generate(inputFileContents string) (string, error) { | ||
f, err := decorator.ParseFile(token.NewFileSet(), "", inputFileContents, parser.ParseComments) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Generate template variants: // execgen:template | ||
expandTemplates(f) | ||
|
||
// Inline functions: // execgen:inline | ||
inlineFuncs(f) | ||
|
||
// Produce output string. | ||
var sb strings.Builder | ||
_ = decorator.Fprint(&sb, f) | ||
return sb.String(), nil | ||
} |
Oops, something went wrong.