MethodCallExprAST.py revision 6780
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 2# Copyright (c) 2009 The Hewlett-Packard Development Company 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer; 9# redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution; 12# neither the name of the copyright holders nor the names of its 13# contributors may be used to endorse or promote products derived from 14# this software without specific prior written permission. 15# 16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28from m5.util import code_formatter 29 30from slicc.ast.ExprAST import ExprAST 31 32class MethodCallExprAST(ExprAST): 33 def __init__(self, slicc, proc_name, expr_ast_vec): 34 super(MethodCallExprAST, self).__init__(slicc) 35 self.proc_name = proc_name 36 self.expr_ast_vec = expr_ast_vec 37 38 def generate(self, code): 39 tmp = code_formatter() 40 paramTypes = [] 41 for expr_ast in self.expr_ast_vec: 42 return_type = expr_ast.generate(tmp) 43 paramTypes.append(return_type) 44 45 obj_type, methodId, prefix = self.generate_prefix(paramTypes) 46 47 # generate code 48 params = [] 49 for expr_ast in self.expr_ast_vec: 50 return_type,tcode = expr_ast.inline(True) 51 params.append(str(tcode)) 52 fix = code.nofix() 53 code("$prefix${{self.proc_name}}(${{', '.join(params)}}))") 54 code.fix(fix) 55 56 # Verify that this is a method of the object 57 if methodId not in obj_type.methods: 58 self.error("Invalid method call: Type '%s' does not have a method '%s'", 59 obj_type, methodId) 60 61 if len(self.expr_ast_vec) != \ 62 len(obj_type.methods[methodId].param_types): 63 # Right number of parameters 64 self.error("Wrong number of parameters for function name: '%s', " + \ 65 "expected: , actual: ", proc_name, 66 len(obj_type.methods[methodId].param_types), 67 len(self.expr_ast_vec)) 68 69 for actual_type, expected_type in \ 70 zip(paramTypes, obj_type.methods[methodId].param_types): 71 if actual_type != expected_type: 72 self.error("Type mismatch: expected: %s actual: %s", 73 expected_type, actual_type) 74 75 # Return the return type of the method 76 return obj_type.methods[methodId].return_type 77 78 def findResources(self, resources): 79 pass 80 81class MemberMethodCallExprAST(MethodCallExprAST): 82 def __init__(self, slicc, obj_expr_ast, proc_name, expr_ast_vec): 83 s = super(MemberMethodCallExprAST, self) 84 s.__init__(slicc, proc_name, expr_ast_vec) 85 86 self.obj_expr_ast = obj_expr_ast 87 88 def __repr__(self): 89 return "[MethodCallExpr: %r%r %r]" % (self.proc_name, 90 self.obj_expr_ast, 91 self.expr_ast_vec) 92 def generate_prefix(self, paramTypes): 93 code = code_formatter() 94 95 # member method call 96 obj_type = self.obj_expr_ast.generate(code) 97 methodId = obj_type.methodId(self.proc_name, paramTypes) 98 99 prefix = "" 100 if methodId not in obj_type.methods: 101 self.error("Invalid method call: Type '%s' does not have a method '%s'", 102 obj_type, methodId) 103 return_type = obj_type.methods[methodId].return_type 104 if return_type.isInterface: 105 prefix = "static_cast<%s &>" % return_type.c_ident 106 prefix = "%s((%s)." % (prefix, code) 107 108 return obj_type, methodId, prefix 109 110 111class ClassMethodCallExprAST(MethodCallExprAST): 112 def __init__(self, slicc, type_ast, proc_name, expr_ast_vec): 113 s = super(ClassMethodCallExprAST, self) 114 s.__init__(slicc, proc_name, expr_ast_vec) 115 116 self.type_ast = type_ast 117 118 def __repr__(self): 119 return "[MethodCallExpr: %r %r]" % (self.proc_name, self.expr_ast_vec) 120 121 def generate_prefix(self, paramTypes): 122 123 # class method call 124 prefix = "(%s::" % self.type_ast 125 obj_type = self.type_ast.type 126 methodId = obj_type.methodId(self.proc_name, paramTypes) 127 128 return obj_type, methodId, prefix 129 130__all__ = [ "MemberMethodCallExprAST", "ClassMethodCallExprAST" ] 131