summaryrefslogtreecommitdiff
path: root/Tools/c-analyzer/c_analyzer/symbols/info.py
blob: 96a251abb7c7fdfe625b9eed2cba3f65d768d36d (plain)
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
from collections import namedtuple

from c_analyzer.common.info import ID
from c_analyzer.common.util import classonly, _NTBase


class Symbol(_NTBase, namedtuple('Symbol', 'id kind external')):
    """Info for a single compilation symbol."""

    __slots__ = ()

    class KIND:
        VARIABLE = 'variable'
        FUNCTION = 'function'
        OTHER = 'other'

    @classonly
    def from_name(cls, name, filename=None, kind=KIND.VARIABLE, external=None):
        """Return a new symbol based on the given name."""
        id = ID(filename, None, name)
        return cls(id, kind, external)

    def __new__(cls, id, kind=KIND.VARIABLE, external=None):
        self = super().__new__(
                cls,
                id=ID.from_raw(id),
                kind=str(kind) if kind else None,
                external=bool(external) if external is not None else None,
                )
        return self

    def __hash__(self):
        return hash(self.id)

    def __getattr__(self, name):
        return getattr(self.id, name)

    def validate(self):
        """Fail if the object is invalid (i.e. init with bad data)."""
        if not self.id:
            raise TypeError('missing id')
        else:
            self.id.validate()

        if not self.kind:
            raise TypeError('missing kind')
        elif self.kind not in vars(self.KIND).values():
            raise ValueError(f'unsupported kind {self.kind}')

        if self.external is None:
            raise TypeError('missing external')