1*4947cdc7SCole Faust// Copyright 2019 The Bazel Authors. All rights reserved. 2*4947cdc7SCole Faust// Use of this source code is governed by a BSD-style 3*4947cdc7SCole Faust// license that can be found in the LICENSE file. 4*4947cdc7SCole Faust 5*4947cdc7SCole Faustpackage resolve 6*4947cdc7SCole Faust 7*4947cdc7SCole Faustimport "go.starlark.net/syntax" 8*4947cdc7SCole Faust 9*4947cdc7SCole Faust// This file defines resolver data types saved in the syntax tree. 10*4947cdc7SCole Faust// We cannot guarantee API stability for these types 11*4947cdc7SCole Faust// as they are closely tied to the implementation. 12*4947cdc7SCole Faust 13*4947cdc7SCole Faust// A Binding contains resolver information about an identifer. 14*4947cdc7SCole Faust// The resolver populates the Binding field of each syntax.Identifier. 15*4947cdc7SCole Faust// The Binding ties together all identifiers that denote the same variable. 16*4947cdc7SCole Fausttype Binding struct { 17*4947cdc7SCole Faust Scope Scope 18*4947cdc7SCole Faust 19*4947cdc7SCole Faust // Index records the index into the enclosing 20*4947cdc7SCole Faust // - {DefStmt,File}.Locals, if Scope==Local 21*4947cdc7SCole Faust // - DefStmt.FreeVars, if Scope==Free 22*4947cdc7SCole Faust // - File.Globals, if Scope==Global. 23*4947cdc7SCole Faust // It is zero if Scope is Predeclared, Universal, or Undefined. 24*4947cdc7SCole Faust Index int 25*4947cdc7SCole Faust 26*4947cdc7SCole Faust First *syntax.Ident // first binding use (iff Scope==Local/Free/Global) 27*4947cdc7SCole Faust} 28*4947cdc7SCole Faust 29*4947cdc7SCole Faust// The Scope of Binding indicates what kind of scope it has. 30*4947cdc7SCole Fausttype Scope uint8 31*4947cdc7SCole Faust 32*4947cdc7SCole Faustconst ( 33*4947cdc7SCole Faust Undefined Scope = iota // name is not defined 34*4947cdc7SCole Faust Local // name is local to its function or file 35*4947cdc7SCole Faust Cell // name is function-local but shared with a nested function 36*4947cdc7SCole Faust Free // name is cell of some enclosing function 37*4947cdc7SCole Faust Global // name is global to module 38*4947cdc7SCole Faust Predeclared // name is predeclared for this module (e.g. glob) 39*4947cdc7SCole Faust Universal // name is universal (e.g. len) 40*4947cdc7SCole Faust) 41*4947cdc7SCole Faust 42*4947cdc7SCole Faustvar scopeNames = [...]string{ 43*4947cdc7SCole Faust Undefined: "undefined", 44*4947cdc7SCole Faust Local: "local", 45*4947cdc7SCole Faust Cell: "cell", 46*4947cdc7SCole Faust Free: "free", 47*4947cdc7SCole Faust Global: "global", 48*4947cdc7SCole Faust Predeclared: "predeclared", 49*4947cdc7SCole Faust Universal: "universal", 50*4947cdc7SCole Faust} 51*4947cdc7SCole Faust 52*4947cdc7SCole Faustfunc (scope Scope) String() string { return scopeNames[scope] } 53*4947cdc7SCole Faust 54*4947cdc7SCole Faust// A Module contains resolver information about a file. 55*4947cdc7SCole Faust// The resolver populates the Module field of each syntax.File. 56*4947cdc7SCole Fausttype Module struct { 57*4947cdc7SCole Faust Locals []*Binding // the file's (comprehension-)local variables 58*4947cdc7SCole Faust Globals []*Binding // the file's global variables 59*4947cdc7SCole Faust} 60*4947cdc7SCole Faust 61*4947cdc7SCole Faust// A Function contains resolver information about a named or anonymous function. 62*4947cdc7SCole Faust// The resolver populates the Function field of each syntax.DefStmt and syntax.LambdaExpr. 63*4947cdc7SCole Fausttype Function struct { 64*4947cdc7SCole Faust Pos syntax.Position // of DEF or LAMBDA 65*4947cdc7SCole Faust Name string // name of def, or "lambda" 66*4947cdc7SCole Faust Params []syntax.Expr // param = ident | ident=expr | * | *ident | **ident 67*4947cdc7SCole Faust Body []syntax.Stmt // contains synthetic 'return expr' for lambda 68*4947cdc7SCole Faust 69*4947cdc7SCole Faust HasVarargs bool // whether params includes *args (convenience) 70*4947cdc7SCole Faust HasKwargs bool // whether params includes **kwargs (convenience) 71*4947cdc7SCole Faust NumKwonlyParams int // number of keyword-only optional parameters 72*4947cdc7SCole Faust Locals []*Binding // this function's local/cell variables, parameters first 73*4947cdc7SCole Faust FreeVars []*Binding // enclosing cells to capture in closure 74*4947cdc7SCole Faust} 75