StateDeclAST.py revision 6657
14202Sbinkertn@umich.edu# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
24202Sbinkertn@umich.edu# Copyright (c) 2009 The Hewlett-Packard Development Company
34202Sbinkertn@umich.edu# All rights reserved.
44202Sbinkertn@umich.edu#
54202Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
64202Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
74202Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
84202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
94202Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
104202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
114202Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
124202Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
134202Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
144202Sbinkertn@umich.edu# this software without specific prior written permission.
154202Sbinkertn@umich.edu#
164202Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174202Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184202Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194202Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204202Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214202Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224202Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234202Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244202Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254202Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264202Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274202Sbinkertn@umich.edu
284202Sbinkertn@umich.edufrom slicc.ast.DeclAST import DeclAST
294202Sbinkertn@umich.edufrom slicc.symbols import Func, Type
304202Sbinkertn@umich.edu
314202Sbinkertn@umich.educlass EnumDeclAST(DeclAST):
324202Sbinkertn@umich.edu    def __init__(self, slicc, type_ast, pairs, fields):
3310996Sandreas.sandberg@arm.com        super(EnumDeclAST, self).__init__(slicc, pairs)
3410996Sandreas.sandberg@arm.com
359398Sandreas.hansson@arm.com        self.type_ast = type_ast
369850Sandreas.hansson@arm.com        self.fields = fields
379259SAli.Saidi@ARM.com
384486Sbinkertn@umich.edu    def __repr__(self):
3910146Sandreas.hansson@arm.com        return "[EnumDecl: %s]" % (self.type_ast)
4010478SAndrew.Bardsley@arm.com
4110478SAndrew.Bardsley@arm.com    def files(self, hh, cc, parent=None):
426165Ssanchezd@stanford.edu        if "external" in self:
439850Sandreas.hansson@arm.com            return
4410405Sandreas.hansson@arm.com
4511184Serfan.azarkhish@unibo.it        if parent:
4611185Serfan.azarkhish@unibo.it            ident = "%s_%s" % (parent, self.type_ast.ident)
4712802Sandreas.sandberg@arm.com        else:
486168Snate@binkert.org            ident = self.type_ast.ident
499850Sandreas.hansson@arm.com        hh.add("%s.hh" % ident)
509259SAli.Saidi@ARM.com        cc.add("%s.cc" % ident)
514202Sbinkertn@umich.edu
5210405Sandreas.hansson@arm.com    def generate(self):
5310431SOmar.Naji@arm.com        ident = str(self.type_ast)
5410146Sandreas.hansson@arm.com
5510478SAndrew.Bardsley@arm.com        # Make the new type
5610478SAndrew.Bardsley@arm.com        t = Type(self.symtab, ident, self.location, self.pairs,
574202Sbinkertn@umich.edu                 self.state_machine)
588761Sgblack@eecs.umich.edu        self.symtab.newSymbol(t)
5910405Sandreas.hansson@arm.com
604202Sbinkertn@umich.edu        # Add all of the fields of the type to it
614202Sbinkertn@umich.edu        for field in self.fields:
628914Sandreas.hansson@arm.com            field.generate(t)
6310405Sandreas.hansson@arm.com
6410405Sandreas.hansson@arm.com        # Add the implicit State_to_string method - FIXME, this is a bit dirty
6510405Sandreas.hansson@arm.com        func_id = "%s_to_string" % t.c_ident
6610405Sandreas.hansson@arm.com
6710614Skanishk.sugand@arm.com        pairs = { "external" : "yes" }
684202Sbinkertn@umich.edu        func = Func(self.symtab, func_id, self.location,
6910405Sandreas.hansson@arm.com                    self.symtab.find("string", Type), [ t ], [], "",
7011184Serfan.azarkhish@unibo.it                    pairs, None)
7111185Serfan.azarkhish@unibo.it        self.symtab.newSymbol(func)
7212802Sandreas.sandberg@arm.com