IfStatementAST.py revision 9628
12131SN/A# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
22131SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
32131SN/A# All rights reserved.
42131SN/A#
52131SN/A# Redistribution and use in source and binary forms, with or without
62131SN/A# modification, are permitted provided that the following conditions are
72131SN/A# met: redistributions of source code must retain the above copyright
82131SN/A# notice, this list of conditions and the following disclaimer;
92131SN/A# redistributions in binary form must reproduce the above copyright
102131SN/A# notice, this list of conditions and the following disclaimer in the
112131SN/A# documentation and/or other materials provided with the distribution;
122131SN/A# neither the name of the copyright holders nor the names of its
132131SN/A# contributors may be used to endorse or promote products derived from
142131SN/A# this software without specific prior written permission.
152131SN/A#
162131SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172131SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182131SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192131SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202131SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212131SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222131SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232131SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242131SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252131SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262131SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu
282665Ssaidi@eecs.umich.edufrom slicc.ast.StatementAST import StatementAST
292131SN/Afrom slicc.symbols import Type
302131SN/A
312239SN/Aclass IfStatementAST(StatementAST):
322680Sktlim@umich.edu    def __init__(self, slicc, cond, then, else_):
332447SN/A        super(IfStatementAST, self).__init__(slicc)
342447SN/A
352800Ssaidi@eecs.umich.edu        assert cond is not None
362800Ssaidi@eecs.umich.edu        assert then is not None
372800Ssaidi@eecs.umich.edu
382800Ssaidi@eecs.umich.edu        self.cond = cond
392131SN/A        self.then = then
402447SN/A        self.else_ = else_
412447SN/A
422131SN/A    def __repr__(self):
432479SN/A        return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
442447SN/A
452447SN/A    def generate(self, code, return_type):
462131SN/A        cond_code = self.slicc.codeFormatter()
472479SN/A        cond_type = self.cond.generate(cond_code)
482447SN/A
492447SN/A        if cond_type != self.symtab.find("bool", Type):
502447SN/A            self.cond.error("Condition of if stmt must be bool, type was '%s'",
512447SN/A                            cond_type)
522447SN/A
532447SN/A        # Conditional
542447SN/A        code.indent()
552447SN/A        code('if ($cond_code) {')
562447SN/A        # Then part
572447SN/A        code.indent()
582447SN/A        self.symtab.pushFrame()
592800Ssaidi@eecs.umich.edu        self.then.generate(code, return_type)
602800Ssaidi@eecs.umich.edu        self.symtab.popFrame()
612800Ssaidi@eecs.umich.edu        code.dedent()
622800Ssaidi@eecs.umich.edu        # Else part
632800Ssaidi@eecs.umich.edu        if self.else_:
642800Ssaidi@eecs.umich.edu            code('} else {')
652447SN/A            code.indent()
662447SN/A            self.symtab.pushFrame()
672447SN/A            self.else_.generate(code, return_type)
682447SN/A            self.symtab.popFrame()
692447SN/A            code.dedent()
702447SN/A        code('}') # End scope
712447SN/A
722447SN/A    def findResources(self, resources):
732447SN/A        # Take a worse case look at both paths
742447SN/A        self.then.findResources(resources)
752447SN/A        if self.else_ is not None:
762447SN/A            self.else_.findResources(resources)
772447SN/A