Skip to content

wake.ir.expressions.member_access module #

MemberAccess class #

Bases: ExpressionAbc

Represents a member access using the dot notation.

Source code in wake/ir/expressions/member_access.py
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
class MemberAccess(ExpressionAbc):
    """
    Represents a member access using the dot notation.
    """

    _ast_node: SolcMemberAccess
    _parent: weakref.ReferenceType[SolidityAbc]  # TODO: make this more specific

    _expression: ExpressionAbc
    _member_name: str
    _referenced_declaration_id: Optional[AstNodeId]

    def __init__(
        self, init: IrInitTuple, member_access: SolcMemberAccess, parent: SolidityAbc
    ):
        super().__init__(init, member_access, parent)
        self._expression = ExpressionAbc.from_ast(init, member_access.expression, self)
        assert self._expression.byte_location[0] == self.byte_location[0]
        assert self._expression.byte_location[1] < self.byte_location[1]

        self._member_name = member_access.member_name
        self._referenced_declaration_id = member_access.referenced_declaration

        self._reference_resolver.register_post_process_callback(
            self._post_process, priority=-1
        )

    def __iter__(self) -> Iterator[IrAbc]:
        yield self
        yield from self._expression

    def _post_process(self, callback_params: CallbackParams):
        # workaround for enum value bug in Solidity versions prior to 0.8.2
        if self._referenced_declaration_id is None:
            if isinstance(self._expression, Identifier) or (
                isinstance(self._expression, MemberAccess)
                and self._expression._referenced_declaration_id is not None
            ):
                referenced_declaration = self._expression.referenced_declaration
                if isinstance(referenced_declaration, EnumDefinition):
                    for enum_value in referenced_declaration.values:
                        if enum_value.name == self._member_name:
                            node_path_order = (
                                self._reference_resolver.get_node_path_order(
                                    AstNodeId(enum_value.ast_node_id),
                                    enum_value.source_unit.cu_hash,
                                )
                            )
                            this_cu_id = self._reference_resolver.get_ast_id_from_cu_node_path_order(
                                node_path_order, self.source_unit.cu_hash
                            )
                            self._referenced_declaration_id = this_cu_id
                            break

        if self._referenced_declaration_id is None:
            expr_type = self.expression.type

            if isinstance(expr_type, Address):
                if self.member_name == "balance":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_BALANCE
                    )
                elif self.member_name == "code":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_CODE
                    )
                elif self.member_name == "codehash":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_CODEHASH
                    )
                elif self.member_name == "transfer":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_TRANSFER
                    )
                elif self.member_name == "send":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_SEND
                    )
                elif self.member_name == "call":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_CALL
                    )
                elif self.member_name == "delegatecall":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_DELEGATECALL
                    )
                elif self.member_name == "staticcall":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ADDRESS_STATICCALL
                    )
                else:
                    assert False, f"Unknown address member: {self.member_name}"
            elif isinstance(expr_type, Array):
                if self.member_name == "length":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.ARRAY_LENGTH
                    )
                elif self.member_name == "push":
                    self._referenced_declaration_id = AstNodeId(GlobalSymbol.ARRAY_PUSH)
                elif self.member_name == "pop":
                    self._referenced_declaration_id = AstNodeId(GlobalSymbol.ARRAY_POP)
                else:
                    assert False, f"Unknown array member: {self.member_name}"
            elif isinstance(expr_type, (Bytes, FixedBytes)):
                if self.member_name == "length":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.BYTES_LENGTH
                    )
                elif self.member_name == "push":
                    self._referenced_declaration_id = AstNodeId(GlobalSymbol.BYTES_PUSH)
                elif self.member_name == "pop":
                    self._referenced_declaration_id = AstNodeId(GlobalSymbol.BYTES_POP)
                else:
                    assert False, f"Unknown bytes member: {self.member_name}"
            elif isinstance(expr_type, Function):
                if self.member_name == "selector":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.FUNCTION_SELECTOR
                    )
                elif self.member_name == "value":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.FUNCTION_VALUE
                    )
                elif self.member_name == "gas":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.FUNCTION_GAS
                    )
                elif self.member_name == "address":
                    self._referenced_declaration_id = AstNodeId(
                        GlobalSymbol.FUNCTION_ADDRESS
                    )
                else:
                    assert False, f"Unknown function member: {self.member_name}"
            elif isinstance(expr_type, Magic):
                if expr_type.kind == MagicTypeKind.BLOCK:
                    if self.member_name == "basefee":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_BASEFEE
                        )
                    elif self.member_name == "chainid":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_CHAINID
                        )
                    elif self.member_name == "coinbase":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_COINBASE
                        )
                    elif self.member_name == "difficulty":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_DIFFICULTY
                        )
                    elif self.member_name == "gaslimit":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_GASLIMIT
                        )
                    elif self.member_name == "number":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_NUMBER
                        )
                    elif self.member_name == "timestamp":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_TIMESTAMP
                        )
                    elif self.member_name == "prevrandao":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_PREVRANDAO
                        )
                    elif self.member_name == "blobbasefee":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BLOCK_BLOBBASEFEE
                        )
                    else:
                        assert False, f"Unknown block member {self.member_name}"
                elif expr_type.kind == MagicTypeKind.MESSAGE:
                    if self.member_name == "data":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.MSG_DATA
                        )
                    elif self.member_name == "sender":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.MSG_SENDER
                        )
                    elif self.member_name == "sig":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.MSG_SIG
                        )
                    elif self.member_name == "value":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.MSG_VALUE
                        )
                    else:
                        assert False, f"Unknown msg member {self.member_name}"
                elif expr_type.kind == MagicTypeKind.TRANSACTION:
                    if self.member_name == "gasprice":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TX_GASPRICE
                        )
                    elif self.member_name == "origin":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TX_ORIGIN
                        )
                    else:
                        assert False, f"Unknown tx member {self.member_name}"
                elif expr_type.kind == MagicTypeKind.ABI:
                    if self.member_name == "decode":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_DECODE
                        )
                    elif self.member_name == "encode":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_ENCODE
                        )
                    elif self.member_name == "encodePacked":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_ENCODE_PACKED
                        )
                    elif self.member_name == "encodeWithSelector":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_ENCODE_WITH_SELECTOR
                        )
                    elif self.member_name == "encodeWithSignature":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_ENCODE_WITH_SIGNATURE
                        )
                    elif self.member_name == "encodeCall":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.ABI_ENCODE_CALL
                        )
                    else:
                        assert False, f"Unknown abi member {self.member_name}"
                elif expr_type.kind == MagicTypeKind.META_TYPE:
                    if self.member_name == "name":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_NAME
                        )
                    elif self.member_name == "creationCode":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_CREATION_CODE
                        )
                    elif self.member_name == "runtimeCode":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_RUNTIME_CODE
                        )
                    elif self.member_name == "interfaceId":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_INTERFACE_ID
                        )
                    elif self.member_name == "min":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_MIN
                        )
                    elif self.member_name == "max":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.TYPE_MAX
                        )
                    else:
                        assert False, f"Unknown type member {self.member_name}"
            elif isinstance(expr_type, Type):
                if isinstance(expr_type.actual_type, Bytes):
                    if self.member_name == "concat":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.BYTES_CONCAT
                        )
                    else:
                        assert False, f"Unknown bytes member {self.member_name}"
                elif isinstance(expr_type.actual_type, String):
                    if self.member_name == "concat":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.STRING_CONCAT
                        )
                    else:
                        assert False, f"Unknown string member {self.member_name}"
                elif isinstance(expr_type.actual_type, UserDefinedValueType):
                    if self.member_name == "wrap":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.USER_DEFINED_VALUE_TYPE_WRAP
                        )
                    elif self.member_name == "unwrap":
                        self._referenced_declaration_id = AstNodeId(
                            GlobalSymbol.USER_DEFINED_VALUE_TYPE_UNWRAP
                        )
                    else:
                        assert (
                            False
                        ), f"Unknown user defined value type member {self.member_name}"
                else:
                    assert (
                        False
                    ), f"Unknown type member {self.member_name} {expr_type.actual_type}"

        assert (
            self._referenced_declaration_id is not None
        ), f"Unknown member {self.member_name}"

        if self._referenced_declaration_id < 0:
            global_symbol = GlobalSymbol(self._referenced_declaration_id)
            self._reference_resolver.register_global_symbol_reference(
                global_symbol, self
            )
            self._reference_resolver.register_destroy_callback(
                self.source_unit.file, partial(self._destroy, global_symbol)
            )
        else:
            node = self._reference_resolver.resolve_node(
                self._referenced_declaration_id, self.source_unit.cu_hash
            )

            if isinstance(node, DeclarationAbc):
                node.register_reference(self)
                self._reference_resolver.register_destroy_callback(
                    self.source_unit.file, partial(self._destroy, node)
                )
            elif isinstance(node, ImportDirective):
                # make this node to reference the source unit directly
                assert node.unit_alias is not None
                source_unit = callback_params.source_units[node.imported_file]
                node_path_order = self._reference_resolver.get_node_path_order(
                    AstNodeId(source_unit.ast_node_id),
                    source_unit.cu_hash,
                )
                self._referenced_declaration_id = (
                    self._reference_resolver.get_ast_id_from_cu_node_path_order(
                        node_path_order, self.source_unit.cu_hash
                    )
                )
            else:
                raise TypeError(f"Unexpected type: {type(node)}")

    def _destroy(
        self, referenced_declaration: Union[GlobalSymbol, DeclarationAbc]
    ) -> None:
        if isinstance(referenced_declaration, GlobalSymbol):
            self._reference_resolver.unregister_global_symbol_reference(
                referenced_declaration, self
            )
        elif isinstance(referenced_declaration, DeclarationAbc):
            referenced_declaration.unregister_reference(self)
        else:
            raise TypeError(f"Unexpected type: {type(referenced_declaration)}")

    @property
    def parent(self) -> SolidityAbc:
        return self._parent()

    @property
    def children(self) -> Iterator[ExpressionAbc]:
        """
        Yields:
            Direct children of this node.
        """
        yield self._expression

    @property
    def expression(self) -> ExpressionAbc:
        """
        Returns:
            Expression, whose member is accessed.
        """
        return self._expression

    @property
    def member_name(self) -> str:
        """
        Returns:
            Name of the member being accessed.
        """
        return self._member_name

    @property
    @weak_self_lru_cache(maxsize=2048)
    def member_location(self) -> Tuple[int, int]:
        """
        In the case of [MemberAccess][wake.ir.expressions.member_access.MemberAccess], [byte_location][wake.ir.abc.IrAbc.byte_location] returns the byte location including the expression, whose member is accessed.
        This property returns the byte location of the member name only.

        Returns:
            Byte location of the member name.
        """
        relative_expression_end = (
            self._expression.byte_location[1] - self.byte_location[0]
        )
        source = bytearray(self._source[relative_expression_end:])
        _, stripped_sums = SoliditySourceParser.strip_comments(source)

        match = MEMBER_RE.match(source)
        assert match

        if len(stripped_sums) == 0:
            stripped = 0
        else:
            index = bisect([s[0] for s in stripped_sums], match.start("member"))
            if index == 0:
                stripped = 0
            else:
                stripped = stripped_sums[index - 1][1]

        return (
            self._expression.byte_location[1] + match.start("member") + stripped,
            self._expression.byte_location[1] + match.end("member") + stripped,
        )

    @property
    def referenced_declaration(
        self,
    ) -> Union[DeclarationAbc, GlobalSymbol, SourceUnit]:
        """
        Returns:
            Referenced declaration.
        """
        assert self._referenced_declaration_id is not None
        if self._referenced_declaration_id < 0:
            return GlobalSymbol(self._referenced_declaration_id)

        node = self._reference_resolver.resolve_node(
            self._referenced_declaration_id, self.source_unit.cu_hash
        )
        assert isinstance(node, (DeclarationAbc, SourceUnit))
        return node

    @property
    @weak_self_lru_cache(maxsize=2048)
    def is_ref_to_state_variable(self) -> bool:
        referenced_declaration = self.referenced_declaration
        return (
            isinstance(referenced_declaration, VariableDeclaration)
            and referenced_declaration.is_state_variable
            or self.expression.is_ref_to_state_variable
        )

    @property
    @weak_self_lru_cache(maxsize=2048)
    def modifies_state(
        self,
    ) -> Set[Tuple[Union[ExpressionAbc, StatementAbc, YulAbc], ModifiesStateFlag]]:
        return self.expression.modifies_state

children: Iterator[ExpressionAbc] property #

Yields:

Type Description
ExpressionAbc

Direct children of this node.

expression: ExpressionAbc property #

Returns:

Type Description
ExpressionAbc

Expression, whose member is accessed.

member_location: Tuple[int, int] property #

In the case of MemberAccess, byte_location returns the byte location including the expression, whose member is accessed. This property returns the byte location of the member name only.

Returns:

Type Description
Tuple[int, int]

Byte location of the member name.

member_name: str property #

Returns:

Type Description
str

Name of the member being accessed.

referenced_declaration: Union[DeclarationAbc, GlobalSymbol, SourceUnit] property #

Returns:

Type Description
Union[DeclarationAbc, GlobalSymbol, SourceUnit]

Referenced declaration.