| 1 |
// Copyright 2023 The Bazel 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 syntax |
| 6 |
|
| 7 |
import _ "unsafe" // for linkname |
| 8 |
|
| 9 |
// FileOptions specifies various per-file options that affect static |
| 10 |
// aspects of an individual file such as parsing, name resolution, and |
| 11 |
// code generation. (Options that affect global dynamics are typically |
| 12 |
// controlled through [go.starlark.net/starlark.Thread].) |
| 13 |
// |
| 14 |
// The zero value of FileOptions is the default behavior. |
| 15 |
// |
| 16 |
// Many functions in this package come in two versions: the legacy |
| 17 |
// standalone function (such as [Parse]) uses [LegacyFileOptions], |
| 18 |
// whereas the more recent method (such as [FileOptions.Parse]) honors the |
| 19 |
// provided options. The second form is preferred. In other packages, |
| 20 |
// the modern version is a standalone function with a leading |
| 21 |
// FileOptions parameter and the name suffix "Options", such as |
| 22 |
// [go.starlark.net/starlark.ExecFileOptions]. |
| 23 |
type FileOptions struct { |
| 24 |
// resolver |
| 25 |
Set bool // allow references to the 'set' built-in function |
| 26 |
While bool // allow 'while' statements |
| 27 |
TopLevelControl bool // allow if/for/while statements at top-level |
| 28 |
GlobalReassign bool // allow reassignment to top-level names |
| 29 |
LoadBindsGlobally bool // load creates global not file-local bindings (deprecated) |
| 30 |
|
| 31 |
// compiler |
| 32 |
Recursion bool // disable recursion check for functions in this file |
| 33 |
} |
| 34 |
|
| 35 |
// TODO(adonovan): provide a canonical flag parser for FileOptions. |
| 36 |
// (And use it in the testdata "options:" strings.) |
| 37 |
|
| 38 |
// LegacyFileOptions returns a new FileOptions containing the current |
| 39 |
// values of the resolver package's legacy global variables such as |
| 40 |
// [resolve.AllowRecursion], etc. |
| 41 |
// These variables may be associated with command-line flags. |
| 42 |
func LegacyFileOptions() *FileOptions { |
| 43 |
return &FileOptions{ |
| 44 |
Set: resolverAllowSet, |
| 45 |
While: resolverAllowGlobalReassign, |
| 46 |
TopLevelControl: resolverAllowGlobalReassign, |
| 47 |
GlobalReassign: resolverAllowGlobalReassign, |
| 48 |
Recursion: resolverAllowRecursion, |
| 49 |
LoadBindsGlobally: resolverLoadBindsGlobally, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Access resolver (legacy) flags, if they are linked in; false otherwise. |
| 54 |
var ( |
| 55 |
//go:linkname resolverAllowSet go.starlark.net/resolve.AllowSet |
| 56 |
resolverAllowSet bool |
| 57 |
//go:linkname resolverAllowGlobalReassign go.starlark.net/resolve.AllowGlobalReassign |
| 58 |
resolverAllowGlobalReassign bool |
| 59 |
//go:linkname resolverAllowRecursion go.starlark.net/resolve.AllowRecursion |
| 60 |
resolverAllowRecursion bool |
| 61 |
//go:linkname resolverLoadBindsGlobally go.starlark.net/resolve.LoadBindsGlobally |
| 62 |
resolverLoadBindsGlobally bool |
| 63 |
) |