Skip to content
← Back

src/syntax/trees/expressions/variable.ghul

1
namespace Syntax.Trees.Expressions is
2
3
use Source
4
5
class VARIABLE: Expression is
6
name: Identifiers.Identifier
7
initializer: Expression?
8
type_expression: TypeExpressions.TypeExpression
9
10
// Attribute pragmas (`@Foo() name: T`) written directly before a
11
// lambda-literal parameter. Only ever populated by
12
// Expressions.PRIMARY's `@` handler; null everywhere else this
13
// node shows up (rewritten from a tuple element, or synthesised
14
// for an untyped lambda parameter).
15
pragmas: Collections.LIST[Pragmas.PRAGMA]? public
16
17
// Set when this parameter is written as a destructure pattern
18
// (`((a, b)) => …`) rather than a plain name. The parameter is
19
// still one physical argument, under the synthesised `name`
20
// above; `left` says how to unpack it into the names the body
21
// actually uses. Null for an ordinary named parameter, which
22
// is every other place this node appears.
23
left: Variables.VariableLeft? public
24
25
is_destructuring: bool => left?
26
27
// Set when this node's name was synthesised rather than written
28
// by the user (a destructured-parameter placeholder, currently
29
// "$destructured_argument" from TUPLE.try_copy_as_variable) - the
30
// node still carries a real user source location, so
31
// declare-members needs this to mark the resulting symbol
32
// synthesised.
33
is_synthesized: bool
34
35
mark_synthesized() is
36
is_synthesized = true
37
si
38
39
could_be_formal_argument: bool => true
40
41
init(
42
location: LOCATION,
43
name: Identifiers.Identifier,
44
type_expression: TypeExpressions.TypeExpression,
45
initializer: Expression?
46
)
47
is
48
super.init(location)
49
50
self.name = name
51
self.type_expression = type_expression
52
self.initializer = initializer
53
si
54
55
set_pragmas(pragmas: Collections.LIST[Pragmas.PRAGMA]) is
56
self.pragmas = pragmas
57
si
58
59
set_left(left: Variables.VariableLeft) is
60
self.left = left
61
si
62
63
// An element written `name: T` inside a pattern is a leaf with
64
// a per-element type ascription, exactly as in a `let`.
65
try_copy_as_variable_left() -> Variables.VariableLeft? is
66
let result = Variables.SIMPLE_VARIABLE_LEFT(location, name.copy())
67
68
if !isa TypeExpressions.INFER(type_expression) then
69
result.set_type_expression(type_expression.copy())
70
fi
71
72
return result
73
si
74
75
try_copy_as_tuple_element() -> TUPLE_ELEMENT is
76
let result = TUPLE_ELEMENT(location, name, type_expression, initializer)
77
78
if let self_pragmas = pragmas then
79
result.set_pragmas(self_pragmas)
80
fi
81
82
return result
83
si
84
85
accept(visitor: Visitor) is
86
visitor.visit(self)
87
si
88
89
walk(visitor: Visitor) is
90
if !visitor.pre(self) then
91
if pragmas? then
92
for pragma in pragmas do
93
pragma.walk(visitor)
94
od
95
fi
96
97
name.walk(visitor)
98
99
if let self.left? then
100
left.walk(visitor)
101
fi
102
103
type_expression.walk(visitor)
104
105
if initializer? then
106
initializer.walk(visitor)
107
fi
108
fi
109
110
accept(visitor)
111
si
112
si
113
si