~mwhudson/pypy/imported-annotation-no-rev-num

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
"""
Type inference for user-defined classes.
"""

from __future__ import generators
from types import FunctionType
from pypy.annotation.model import SomeImpossibleValue, unionof


class Attribute:
    # readonly-ness
    # SomeThing-ness
    # more potential sources (pbcs or classes) of information

    def __init__(self, name, bookkeeper):
        self.name = name
        self.bookkeeper = bookkeeper
        self.sources = {} # source -> None or ClassDef
        # XXX a SomeImpossibleValue() constant?  later!!
        self.s_value = SomeImpossibleValue()
        self.readonly = True

    def getvalue(self):
        while self.sources:
            source, classdef = self.sources.popitem()
            s_value = self.bookkeeper.immutablevalue(
                source.__dict__[self.name])
            if classdef:
                s_value = s_value.bindcallables(classdef)
            self.s_value = unionof(self.s_value, s_value)
        return self.s_value

    def merge(self, other):
        assert self.name == other.name
        self.sources.update(other.sources)
        self.s_value = unionof(self.s_value, other.s_value)
        self.readonly = self.readonly and other.readonly


class ClassDef:
    "Wraps a user class."

    def __init__(self, cls, bookkeeper):
        self.bookkeeper = bookkeeper
        self.attrs = {}          # {name: Attribute}
        self.revision = 0        # which increases the revision number
        self.instantiation_locations = {}
        self.cls = cls
        self.subdefs = {}
        assert (len(cls.__bases__) <= 1 or
                cls.__bases__[1:] == (object,),   # for baseobjspace.Wrappable
                "single inheritance only right now: %r" % (cls,))
        if cls.__bases__:
            base = cls.__bases__[0]
        else:
            base = object
        self.basedef = bookkeeper.getclassdef(base)
        if self.basedef:
            self.basedef.subdefs[cls] = self

        # collect the (supposed constant) class attributes
        for name, value in cls.__dict__.items():
            # ignore some special attributes
            if name.startswith('_') and not isinstance(value, FunctionType):
                continue
            if isinstance(value, FunctionType):
                value.class_ = cls # remember that this is really a method
            self.add_source_for_attribute(name, cls, self)

    def add_source_for_attribute(self, attr, source, clsdef=None):
        self.find_attribute(attr).sources[source] = clsdef

    def locate_attribute(self, attr):
        for cdef in self.getmro():
            if attr in cdef.attrs:
                return cdef
        self.generalize_attr(attr)
        return self

    def find_attribute(self, attr):
        return self.locate_attribute(attr).attrs[attr]
    
    def __repr__(self):
        return "<ClassDef '%s.%s'>" % (self.cls.__module__, self.cls.__name__)

    def commonbase(self, other):
        while other is not None and not issubclass(self.cls, other.cls):
            other = other.basedef
        return other

    def superdef_containing(self, cls):
        clsdef = self
        while clsdef is not None and not issubclass(cls, clsdef.cls):
            clsdef = clsdef.basedef
        return clsdef

    def getmro(self):
        while self is not None:
            yield self
            self = self.basedef

    def getallsubdefs(self):
        pending = [self]
        seen = {}
        for clsdef in pending:
            yield clsdef
            for sub in clsdef.subdefs.values():
                if sub not in seen:
                    pending.append(sub)
                    seen[sub] = True

    def getallinstantiations(self):
        locations = {}
        for clsdef in self.getallsubdefs():
            locations.update(clsdef.instantiation_locations)
        return locations

    def _generalize_attr(self, attr, s_value):
        # first remove the attribute from subclasses -- including us!
        subclass_attrs = []
        for subdef in self.getallsubdefs():
            if attr in subdef.attrs:
                subclass_attrs.append(subdef.attrs[attr])
                del subdef.attrs[attr]
            # bump the revision number of this class and all subclasses
            subdef.revision += 1

        # do the generalization
        newattr = Attribute(attr, self.bookkeeper)
        if s_value:
            newattr.s_value = s_value
            
        for subattr in subclass_attrs:
            newattr.merge(subattr)
        self.attrs[attr] = newattr

        # reflow from all factories
        for position in self.getallinstantiations():
            self.bookkeeper.annotator.reflowfromposition(position)

    def generalize_attr(self, attr, s_value=None):
        # if the attribute exists in a superclass, generalize there.
        for clsdef in self.getmro():
            if attr in clsdef.attrs:
                clsdef._generalize_attr(attr, s_value)
        else:
            self._generalize_attr(attr, s_value)

    def about_attribute(self, name):
        for cdef in self.getmro():
            if name in cdef.attrs:
                s_result = cdef.attrs[name].s_value
                if s_result != SomeImpossibleValue():
                    return s_result
                else:
                    return None
        return None


def isclassdef(x):
    return isinstance(x, ClassDef)