Lichen

Annotated results.py

649:7c8a7ad8dc50
2017-03-03 Paul Boddie Permit the parameterisation of the special instance indicator, making it match the position of the __class__ attribute, thus being different to any class type attribute position.
paul@11 1
#!/usr/bin/env python
paul@11 2
paul@11 3
"""
paul@11 4
Result abstractions.
paul@11 5
paul@510 6
Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@11 7
paul@11 8
This program is free software; you can redistribute it and/or modify it under
paul@11 9
the terms of the GNU General Public License as published by the Free Software
paul@11 10
Foundation; either version 3 of the License, or (at your option) any later
paul@11 11
version.
paul@11 12
paul@11 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@11 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@11 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@11 16
details.
paul@11 17
paul@11 18
You should have received a copy of the GNU General Public License along with
paul@11 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@11 20
"""
paul@11 21
paul@27 22
from referencing import Reference
paul@27 23
paul@11 24
# Classes representing inspection and translation observations.
paul@11 25
paul@11 26
class Result:
paul@11 27
paul@11 28
    "An abstract expression result."
paul@11 29
paul@11 30
    def is_name(self):
paul@11 31
        return False
paul@118 32
paul@603 33
    def is_global_name(self):
paul@603 34
        return False
paul@603 35
paul@226 36
    def reference(self):
paul@226 37
        return None
paul@226 38
paul@552 39
    def references(self):
paul@552 40
        return None
paul@552 41
paul@554 42
    def access_location(self):
paul@553 43
        return None
paul@553 44
paul@618 45
    def context_identity(self):
paul@618 46
        return None
paul@618 47
paul@637 48
    def discards_temporary(self, test=True):
paul@637 49
        return None
paul@637 50
paul@226 51
    def get_name(self):
paul@226 52
        return None
paul@226 53
paul@11 54
    def get_origin(self):
paul@11 55
        return None
paul@11 56
paul@226 57
    def static(self):
paul@118 58
        return None
paul@118 59
paul@226 60
    def final(self):
paul@226 61
        return None
paul@226 62
paul@226 63
    def has_kind(self, kinds):
paul@226 64
        return False
paul@226 65
paul@11 66
class AccessRef(Result):
paul@11 67
paul@11 68
    """
paul@11 69
    A reference to an attribute access that is generally only returned from a
paul@11 70
    processed access for possible initialiser resolution for assignments.
paul@11 71
    """
paul@11 72
paul@11 73
    def __init__(self, original_name, attrnames, number):
paul@11 74
        self.original_name = original_name
paul@11 75
        self.attrnames = attrnames
paul@11 76
        self.number = number
paul@11 77
paul@11 78
    def __repr__(self):
paul@11 79
        return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number)
paul@11 80
paul@11 81
class InvocationRef(Result):
paul@11 82
paul@11 83
    "An invocation of a name reference."
paul@11 84
paul@11 85
    def __init__(self, name_ref):
paul@11 86
        self.name_ref = name_ref
paul@11 87
paul@27 88
    def reference(self):
paul@27 89
        origin = self.name_ref.get_origin()
paul@27 90
        if origin:
paul@27 91
            return Reference("<invoke>", origin)
paul@27 92
        else:
paul@27 93
            return Reference("<var>")
paul@27 94
paul@11 95
    def __repr__(self):
paul@11 96
        return "InvocationRef(%r)" % self.name_ref
paul@11 97
paul@11 98
class NameRef(Result):
paul@11 99
paul@11 100
    "A reference to a name."
paul@11 101
paul@603 102
    def __init__(self, name, expr=None, is_global=False):
paul@11 103
        self.name = name
paul@11 104
        self.expr = expr
paul@603 105
        self.is_global = is_global
paul@11 106
paul@11 107
    def is_name(self):
paul@11 108
        return True
paul@11 109
paul@603 110
    def is_global_name(self):
paul@603 111
        return self.is_global
paul@603 112
paul@11 113
    def final(self):
paul@11 114
        return None
paul@11 115
paul@11 116
    def __repr__(self):
paul@603 117
        return "NameRef(%r, %r, %r)" % (self.name, self.expr, self.is_global)
paul@11 118
paul@11 119
class LocalNameRef(NameRef):
paul@11 120
paul@11 121
    "A reference to a local name."
paul@11 122
paul@11 123
    def __init__(self, name, number):
paul@603 124
        NameRef.__init__(self, name, is_global=False)
paul@11 125
        self.number = number
paul@11 126
paul@11 127
    def __repr__(self):
paul@11 128
        return "LocalNameRef(%r, %r)" % (self.name, self.number)
paul@11 129
paul@317 130
class ResolvedRef:
paul@11 131
paul@317 132
    "A resolved reference mix-in."
paul@11 133
paul@552 134
    def __init__(self, ref):
paul@552 135
        self.ref = ref
paul@552 136
paul@11 137
    def reference(self):
paul@11 138
        return self.ref
paul@11 139
paul@552 140
    def references(self):
paul@552 141
        return [self.ref]
paul@552 142
paul@11 143
    def get_name(self):
paul@11 144
        return self.ref and self.ref.get_name() or None
paul@11 145
paul@11 146
    def get_origin(self):
paul@11 147
        return self.ref and self.ref.get_origin() or None
paul@11 148
paul@11 149
    def static(self):
paul@11 150
        return self.ref and self.ref.static() or None
paul@11 151
paul@11 152
    def final(self):
paul@11 153
        return self.ref and self.ref.final() or None
paul@11 154
paul@11 155
    def has_kind(self, kinds):
paul@11 156
        return self.ref and self.ref.has_kind(kinds)
paul@11 157
paul@338 158
    def is_constant_alias(self):
paul@338 159
        return self.ref and self.ref.is_constant_alias()
paul@338 160
paul@317 161
class ResolvedNameRef(ResolvedRef, NameRef):
paul@317 162
paul@317 163
    "A resolved name-based reference."
paul@317 164
paul@603 165
    def __init__(self, name, ref, expr=None, is_global=False):
paul@603 166
        NameRef.__init__(self, name, expr, is_global)
paul@552 167
        ResolvedRef.__init__(self, ref)
paul@317 168
paul@11 169
    def __repr__(self):
paul@603 170
        return "ResolvedNameRef(%r, %r, %r, %r)" % (self.name, self.ref, self.expr, self.is_global)
paul@11 171
paul@11 172
class ConstantValueRef(ResolvedNameRef):
paul@11 173
paul@11 174
    "A constant reference representing a single literal value."
paul@11 175
paul@11 176
    def __init__(self, name, ref, value, number=None):
paul@11 177
        ResolvedNameRef.__init__(self, name, ref)
paul@11 178
        self.value = value
paul@11 179
        self.number = number
paul@11 180
paul@11 181
    def __repr__(self):
paul@11 182
        return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)
paul@11 183
paul@317 184
class InstanceRef(ResolvedRef, Result):
paul@11 185
paul@11 186
    "An instance reference."
paul@11 187
paul@11 188
    def reference(self):
paul@11 189
        return self.ref
paul@11 190
paul@11 191
    def __repr__(self):
paul@11 192
        return "InstanceRef(%r)" % self.ref
paul@11 193
paul@11 194
class LiteralSequenceRef(ResolvedNameRef):
paul@11 195
paul@11 196
    "A reference representing a sequence of values."
paul@11 197
paul@11 198
    def __init__(self, name, ref, node, items=None):
paul@11 199
        ResolvedNameRef.__init__(self, name, ref)
paul@11 200
        self.node = node
paul@11 201
        self.items = items
paul@11 202
paul@11 203
    def __repr__(self):
paul@11 204
        return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)
paul@11 205
paul@226 206
class VariableRef(Result):
paul@226 207
paul@226 208
    "A variable reference."
paul@226 209
paul@226 210
    def __repr__(self):
paul@226 211
        return "VariableRef()"
paul@226 212
paul@11 213
# vim: tabstop=4 expandtab shiftwidth=4