__author__ = 'CwT'
from AbstractAnalysis import AbstractFlowAnalysis

class GlobalAnalysis(AbstractFlowAnalysis):
    def __init__(self):
        self._globalvars = {}
        AbstractFlowAnalysis.__init__(self)

    @property
    def GlobalSet(self):
        return self._globalvars

    def flowTransfer(self, block):
        for instruction in block.insns:
            handler, var = instruction.isGlobal()
            if handler == 'register':
                if var not in self._globalvars.keys():
                    self._globalvars[var] = []
            elif handler == 'check':
                for each in var:
                    start = each.find('<$')
                    name = each[start+2:-1]
                    if name in self._globalvars.keys() and \
                            each not in self._globalvars[name]:
                        self._globalvars[name].append(each)

    def IgnoreFunc(self):
        if self._graph._name == 'main':
            return True
        return False

    def printData(self):
        print(self._globalvars)
