StaticCastAST.py revision 6882
16882SBrad.Beckmann@amd.com# Copyright (c) 2009 Advanced Micro Devices, Inc.
26882SBrad.Beckmann@amd.com# All rights reserved.
36882SBrad.Beckmann@amd.com#
46882SBrad.Beckmann@amd.com# Redistribution and use in source and binary forms, with or without
56882SBrad.Beckmann@amd.com# modification, are permitted provided that the following conditions are
66882SBrad.Beckmann@amd.com# met: redistributions of source code must retain the above copyright
76882SBrad.Beckmann@amd.com# notice, this list of conditions and the following disclaimer;
86882SBrad.Beckmann@amd.com# redistributions in binary form must reproduce the above copyright
96882SBrad.Beckmann@amd.com# notice, this list of conditions and the following disclaimer in the
106882SBrad.Beckmann@amd.com# documentation and/or other materials provided with the distribution;
116882SBrad.Beckmann@amd.com# neither the name of the copyright holders nor the names of its
126882SBrad.Beckmann@amd.com# contributors may be used to endorse or promote products derived from
136882SBrad.Beckmann@amd.com# this software without specific prior written permission.
146882SBrad.Beckmann@amd.com#
156882SBrad.Beckmann@amd.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166882SBrad.Beckmann@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176882SBrad.Beckmann@amd.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186882SBrad.Beckmann@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196882SBrad.Beckmann@amd.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206882SBrad.Beckmann@amd.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216882SBrad.Beckmann@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226882SBrad.Beckmann@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236882SBrad.Beckmann@amd.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246882SBrad.Beckmann@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256882SBrad.Beckmann@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266882SBrad.Beckmann@amd.com
276882SBrad.Beckmann@amd.comfrom slicc.ast.ExprAST import ExprAST
286882SBrad.Beckmann@amd.com
296882SBrad.Beckmann@amd.comclass StaticCastAST(ExprAST):
306882SBrad.Beckmann@amd.com    def __init__(self, slicc, type_ast, expr_ast):
316882SBrad.Beckmann@amd.com        super(StaticCastAST, self).__init__(slicc)
326882SBrad.Beckmann@amd.com
336882SBrad.Beckmann@amd.com        self.type_ast = type_ast
346882SBrad.Beckmann@amd.com        self.expr_ast = expr_ast
356882SBrad.Beckmann@amd.com
366882SBrad.Beckmann@amd.com    def __repr__(self):
376882SBrad.Beckmann@amd.com        return "[StaticCastAST: %r]" % self.expr_ast
386882SBrad.Beckmann@amd.com
396882SBrad.Beckmann@amd.com    def generate(self, code):
406882SBrad.Beckmann@amd.com        actual_type, ecode = self.expr_ast.inline(True)
416882SBrad.Beckmann@amd.com        code('static_cast<${{self.type_ast.type.c_ident}} &>($ecode)')
426882SBrad.Beckmann@amd.com
436882SBrad.Beckmann@amd.com        if not "interface" in self.type_ast.type:
446882SBrad.Beckmann@amd.com            self.expr_ast.error("static cast only premitted for those types " \
456882SBrad.Beckmann@amd.com                                "that implement inherit an interface")
466882SBrad.Beckmann@amd.com
476882SBrad.Beckmann@amd.com        # The interface type should match
486882SBrad.Beckmann@amd.com        if str(actual_type) != str(self.type_ast.type["interface"]):
496882SBrad.Beckmann@amd.com            self.expr_ast.error("static cast miss-match, type is '%s'," \
506882SBrad.Beckmann@amd.com                                "but inherited type is '%s'",
516882SBrad.Beckmann@amd.com                                actual_type, self.type_ast.type["interface"])
526882SBrad.Beckmann@amd.com
536882SBrad.Beckmann@amd.com        return self.type_ast.type
546882SBrad.Beckmann@amd.com
55