18086SBrad.Beckmann@amd.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
26657SN/A# All rights reserved.
36657SN/A#
46657SN/A# Redistribution and use in source and binary forms, with or without
56657SN/A# modification, are permitted provided that the following conditions are
66657SN/A# met: redistributions of source code must retain the above copyright
76657SN/A# notice, this list of conditions and the following disclaimer;
86657SN/A# redistributions in binary form must reproduce the above copyright
96657SN/A# notice, this list of conditions and the following disclaimer in the
106657SN/A# documentation and/or other materials provided with the distribution;
116657SN/A# neither the name of the copyright holders nor the names of its
126657SN/A# contributors may be used to endorse or promote products derived from
136657SN/A# this software without specific prior written permission.
146657SN/A#
156657SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166657SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176657SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186657SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196657SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206657SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216657SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226657SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236657SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246657SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256657SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266657SN/A
276657SN/Afrom slicc.ast.DeclAST import DeclAST
286657SN/Afrom slicc.symbols import Func, Type
296657SN/A
308086SBrad.Beckmann@amd.comclass StateDeclAST(DeclAST):
318086SBrad.Beckmann@amd.com    def __init__(self, slicc, type_ast, pairs, states):
328086SBrad.Beckmann@amd.com        super(StateDeclAST, self).__init__(slicc, pairs)
336657SN/A
346657SN/A        self.type_ast = type_ast
358086SBrad.Beckmann@amd.com        self.states = states
366657SN/A
376657SN/A    def __repr__(self):
388086SBrad.Beckmann@amd.com        return "[StateDecl: %s]" % (self.type_ast)
396657SN/A
406714SN/A    def files(self, parent=None):
416657SN/A        if "external" in self:
426714SN/A            return set()
436657SN/A
446657SN/A        if parent:
456657SN/A            ident = "%s_%s" % (parent, self.type_ast.ident)
466657SN/A        else:
476657SN/A            ident = self.type_ast.ident
486714SN/A        s = set(("%s.hh" % ident, "%s.cc" % ident))
496714SN/A        return s
506657SN/A
516657SN/A    def generate(self):
526657SN/A        ident = str(self.type_ast)
536657SN/A
546657SN/A        # Make the new type
556657SN/A        t = Type(self.symtab, ident, self.location, self.pairs,
566657SN/A                 self.state_machine)
576657SN/A        self.symtab.newSymbol(t)
586657SN/A
598086SBrad.Beckmann@amd.com        # Add all of the states of the type to it
608086SBrad.Beckmann@amd.com        for state in self.states:
618086SBrad.Beckmann@amd.com            state.generate(t)
626657SN/A
636657SN/A        # Add the implicit State_to_string method - FIXME, this is a bit dirty
646657SN/A        func_id = "%s_to_string" % t.c_ident
656657SN/A
666657SN/A        pairs = { "external" : "yes" }
6710984SBrad.Beckmann@amd.com        func = Func(self.symtab, func_id + "_" +
6810984SBrad.Beckmann@amd.com                    t.ident, func_id, self.location,
6911049Snilay@cs.wisc.edu                    self.symtab.find("std::string", Type), [ t ], [], "",
709298Snilay@cs.wisc.edu                    pairs)
716657SN/A        self.symtab.newSymbol(func)
728086SBrad.Beckmann@amd.com
738086SBrad.Beckmann@amd.com        # Add the State_to_permission method
748086SBrad.Beckmann@amd.com        func_id = "%s_to_permission" % t.c_ident
758086SBrad.Beckmann@amd.com
768086SBrad.Beckmann@amd.com        pairs = { "external" : "yes" }
7710984SBrad.Beckmann@amd.com        func = Func(self.symtab, func_id + "_" +
7810984SBrad.Beckmann@amd.com                    t.ident, func_id, self.location,
7911049Snilay@cs.wisc.edu                    self.symtab.find("AccessPermission", Type), [ t ], [], "",
809298Snilay@cs.wisc.edu                    pairs)
818086SBrad.Beckmann@amd.com        self.symtab.newSymbol(func)
82