VarExprAST.py revision 6657:ef5fae93a3b2
110478SAndrew.Bardsley@arm.com# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 210478SAndrew.Bardsley@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company 310478SAndrew.Bardsley@arm.com# All rights reserved. 410478SAndrew.Bardsley@arm.com# 510478SAndrew.Bardsley@arm.com# Redistribution and use in source and binary forms, with or without 610478SAndrew.Bardsley@arm.com# modification, are permitted provided that the following conditions are 710478SAndrew.Bardsley@arm.com# met: redistributions of source code must retain the above copyright 810478SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer; 910478SAndrew.Bardsley@arm.com# redistributions in binary form must reproduce the above copyright 1010478SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer in the 1110478SAndrew.Bardsley@arm.com# documentation and/or other materials provided with the distribution; 1210478SAndrew.Bardsley@arm.com# neither the name of the copyright holders nor the names of its 1310478SAndrew.Bardsley@arm.com# contributors may be used to endorse or promote products derived from 1410478SAndrew.Bardsley@arm.com# this software without specific prior written permission. 1510478SAndrew.Bardsley@arm.com# 1610478SAndrew.Bardsley@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1710478SAndrew.Bardsley@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1810478SAndrew.Bardsley@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1910478SAndrew.Bardsley@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2010478SAndrew.Bardsley@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2110478SAndrew.Bardsley@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2210478SAndrew.Bardsley@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2310478SAndrew.Bardsley@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2410478SAndrew.Bardsley@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2510478SAndrew.Bardsley@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2610478SAndrew.Bardsley@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2710478SAndrew.Bardsley@arm.com 2810478SAndrew.Bardsley@arm.comfrom slicc.ast.ExprAST import ExprAST 2910478SAndrew.Bardsley@arm.comfrom slicc.symbols import Type, Var 3010478SAndrew.Bardsley@arm.com 3110478SAndrew.Bardsley@arm.comclass VarExprAST(ExprAST): 3210478SAndrew.Bardsley@arm.com def __init__(self, slicc, var): 3310478SAndrew.Bardsley@arm.com super(VarExprAST, self).__init__(slicc) 3410478SAndrew.Bardsley@arm.com self._var = var 3510478SAndrew.Bardsley@arm.com 3610478SAndrew.Bardsley@arm.com def __repr__(self): 3710478SAndrew.Bardsley@arm.com return "[VarExprAST: %r]" % self._var 3810478SAndrew.Bardsley@arm.com 3911817SChristian.Menard@tu-dresden.de @property 4010478SAndrew.Bardsley@arm.com def name(self): 4110478SAndrew.Bardsley@arm.com return str(self._var) 4211793Sbrandon.potter@amd.com 4311793Sbrandon.potter@amd.com @property 4410478SAndrew.Bardsley@arm.com def var(self): 4510478SAndrew.Bardsley@arm.com var = self.symtab.find(self._var, Var) 4610478SAndrew.Bardsley@arm.com if not var: 4711800Sbrandon.potter@amd.com self.error("Unrecognized variable: %s", self._var) 4810478SAndrew.Bardsley@arm.com return var 4911817SChristian.Menard@tu-dresden.de 5010478SAndrew.Bardsley@arm.com def assertType(self, type_ident): 5110478SAndrew.Bardsley@arm.com expected_type = self.symtab.find(type_ident, Type) 5210478SAndrew.Bardsley@arm.com 5310478SAndrew.Bardsley@arm.com if not expected_type: 5410478SAndrew.Bardsley@arm.com self.error("There must be a type '%s' declared in this scope", 5510478SAndrew.Bardsley@arm.com type_ident) 5610478SAndrew.Bardsley@arm.com 5710478SAndrew.Bardsley@arm.com if self.var.type != expected_type: 5810478SAndrew.Bardsley@arm.com self.error("Incorrect type: " + \ 5911817SChristian.Menard@tu-dresden.de "'%s' is expected to be type '%s' not '%s'", 6012680Sgiacomo.travaglini@arm.com self.var.ident, expected_type, self.var.type) 6110478SAndrew.Bardsley@arm.com 6210478SAndrew.Bardsley@arm.com def generate(self, code): 6313784Sgabeblack@google.com fix = code.nofix() 6413784Sgabeblack@google.com code("${{self.var.code}}") 6510478SAndrew.Bardsley@arm.com code.fix(fix) 6610478SAndrew.Bardsley@arm.com return self.var.type 6710478SAndrew.Bardsley@arm.com