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):
307839Snilay@cs.wisc.edu    def __init__(self, slicc, type_ast, type_modifier, 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
357839Snilay@cs.wisc.edu        self.type_modifier = type_modifier
366882SBrad.Beckmann@amd.com
376882SBrad.Beckmann@amd.com    def __repr__(self):
386882SBrad.Beckmann@amd.com        return "[StaticCastAST: %r]" % self.expr_ast
396882SBrad.Beckmann@amd.com
406882SBrad.Beckmann@amd.com    def generate(self, code):
416882SBrad.Beckmann@amd.com        actual_type, ecode = self.expr_ast.inline(True)
427839Snilay@cs.wisc.edu        if self.type_modifier == "pointer":
437839Snilay@cs.wisc.edu            code('static_cast<${{self.type_ast.type.c_ident}} *>($ecode)')
447839Snilay@cs.wisc.edu        else:
457839Snilay@cs.wisc.edu            code('static_cast<${{self.type_ast.type.c_ident}} &>($ecode)')
466882SBrad.Beckmann@amd.com
476882SBrad.Beckmann@amd.com        if not "interface" in self.type_ast.type:
486882SBrad.Beckmann@amd.com            self.expr_ast.error("static cast only premitted for those types " \
496882SBrad.Beckmann@amd.com                                "that implement inherit an interface")
506882SBrad.Beckmann@amd.com
516882SBrad.Beckmann@amd.com        # The interface type should match
526882SBrad.Beckmann@amd.com        if str(actual_type) != str(self.type_ast.type["interface"]):
536882SBrad.Beckmann@amd.com            self.expr_ast.error("static cast miss-match, type is '%s'," \
546882SBrad.Beckmann@amd.com                                "but inherited type is '%s'",
556882SBrad.Beckmann@amd.com                                actual_type, self.type_ast.type["interface"])
566882SBrad.Beckmann@amd.com
576882SBrad.Beckmann@amd.com        return self.type_ast.type
586882SBrad.Beckmann@amd.com
59