MethodCallExprAST.py (6882:898047a3672c) MethodCallExprAST.py (6999:f226c098c393)
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
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):
28from slicc.ast.ExprAST import ExprAST
29
30class MethodCallExprAST(ExprAST):
31 def __init__(self, slicc, proc_name, expr_ast_vec):
32 super(MethodCallExprAST, self).__init__(slicc)
33 self.proc_name = proc_name
34 self.expr_ast_vec = expr_ast_vec
35
36 def generate(self, code):
39 tmp = code_formatter()
37 tmp = self.slicc.codeFormatter()
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 and \
72 str(actual_type["interface"]) != str(expected_type):
73 self.error("Type mismatch: expected: %s actual: %s",
74 expected_type, actual_type)
75
76 # Return the return type of the method
77 return obj_type.methods[methodId].return_type
78
79 def findResources(self, resources):
80 pass
81
82class MemberMethodCallExprAST(MethodCallExprAST):
83 def __init__(self, slicc, obj_expr_ast, proc_name, expr_ast_vec):
84 s = super(MemberMethodCallExprAST, self)
85 s.__init__(slicc, proc_name, expr_ast_vec)
86
87 self.obj_expr_ast = obj_expr_ast
88
89 def __repr__(self):
90 return "[MethodCallExpr: %r%r %r]" % (self.proc_name,
91 self.obj_expr_ast,
92 self.expr_ast_vec)
93 def generate_prefix(self, paramTypes):
38 paramTypes = []
39 for expr_ast in self.expr_ast_vec:
40 return_type = expr_ast.generate(tmp)
41 paramTypes.append(return_type)
42
43 obj_type, methodId, prefix = self.generate_prefix(paramTypes)
44
45 # generate code
46 params = []
47 for expr_ast in self.expr_ast_vec:
48 return_type,tcode = expr_ast.inline(True)
49 params.append(str(tcode))
50 fix = code.nofix()
51 code("$prefix${{self.proc_name}}(${{', '.join(params)}}))")
52 code.fix(fix)
53
54 # Verify that this is a method of the object
55 if methodId not in obj_type.methods:
56 self.error("Invalid method call: Type '%s' does not have a method '%s'",
57 obj_type, methodId)
58
59 if len(self.expr_ast_vec) != \
60 len(obj_type.methods[methodId].param_types):
61 # Right number of parameters
62 self.error("Wrong number of parameters for function name: '%s', " + \
63 "expected: , actual: ", proc_name,
64 len(obj_type.methods[methodId].param_types),
65 len(self.expr_ast_vec))
66
67 for actual_type, expected_type in \
68 zip(paramTypes, obj_type.methods[methodId].param_types):
69 if actual_type != expected_type and \
70 str(actual_type["interface"]) != str(expected_type):
71 self.error("Type mismatch: expected: %s actual: %s",
72 expected_type, actual_type)
73
74 # Return the return type of the method
75 return obj_type.methods[methodId].return_type
76
77 def findResources(self, resources):
78 pass
79
80class MemberMethodCallExprAST(MethodCallExprAST):
81 def __init__(self, slicc, obj_expr_ast, proc_name, expr_ast_vec):
82 s = super(MemberMethodCallExprAST, self)
83 s.__init__(slicc, proc_name, expr_ast_vec)
84
85 self.obj_expr_ast = obj_expr_ast
86
87 def __repr__(self):
88 return "[MethodCallExpr: %r%r %r]" % (self.proc_name,
89 self.obj_expr_ast,
90 self.expr_ast_vec)
91 def generate_prefix(self, paramTypes):
94 code = code_formatter()
92 code = self.slicc.codeFormatter()
95
96 # member method call
97 obj_type = self.obj_expr_ast.generate(code)
98 methodId = obj_type.methodId(self.proc_name, paramTypes)
99
100 prefix = ""
101 implements_interface = False
102 if methodId not in obj_type.methods:
103 #
104 # The initial method check has failed, but before generating an
105 # error we must check whether any of the paramTypes implement
106 # an interface. If so, we must check if the method ids using
107 # the inherited types exist.
108 #
109 # This code is a temporary fix and only checks for the methodId
110 # where all paramTypes are converted to their inherited type. The
111 # right way to do this is to replace slicc's simple string
112 # comparison for determining the correct overloaded method, with a
113 # more robust param by param check.
114 #
115 implemented_paramTypes = []
116 for paramType in paramTypes:
117 implemented_paramType = paramType
118 if paramType.isInterface:
119 implements_interface = True
120 implemented_paramType.abstract_ident = paramType["interface"]
121 else:
122 implemented_paramType.abstract_ident = paramType.c_ident
123
124 implemented_paramTypes.append(implemented_paramType)
125
126 if implements_interface:
127 implementedMethodId = obj_type.methodIdAbstract(self.proc_name,
128 implemented_paramTypes)
129 else:
130 implementedMethodId = ""
131
132 if implementedMethodId not in obj_type.methods:
133 self.error("Invalid method call: " \
134 "Type '%s' does not have a method '%s' nor '%s'",
135 obj_type, methodId, implementedMethodId)
136 else:
137 #
138 # Replace the methodId with the implementedMethodId found in
139 # the method list.
140 #
141 methodId = implementedMethodId
142
143 return_type = obj_type.methods[methodId].return_type
144 if return_type.isInterface:
145 prefix = "static_cast<%s &>" % return_type.c_ident
146 prefix = "%s((%s)." % (prefix, code)
147
148 return obj_type, methodId, prefix
149
150
151class ClassMethodCallExprAST(MethodCallExprAST):
152 def __init__(self, slicc, type_ast, proc_name, expr_ast_vec):
153 s = super(ClassMethodCallExprAST, self)
154 s.__init__(slicc, proc_name, expr_ast_vec)
155
156 self.type_ast = type_ast
157
158 def __repr__(self):
159 return "[MethodCallExpr: %r %r]" % (self.proc_name, self.expr_ast_vec)
160
161 def generate_prefix(self, paramTypes):
162
163 # class method call
164 prefix = "(%s::" % self.type_ast
165 obj_type = self.type_ast.type
166 methodId = obj_type.methodId(self.proc_name, paramTypes)
167
168 return obj_type, methodId, prefix
169
170__all__ = [ "MemberMethodCallExprAST", "ClassMethodCallExprAST" ]
93
94 # member method call
95 obj_type = self.obj_expr_ast.generate(code)
96 methodId = obj_type.methodId(self.proc_name, paramTypes)
97
98 prefix = ""
99 implements_interface = False
100 if methodId not in obj_type.methods:
101 #
102 # The initial method check has failed, but before generating an
103 # error we must check whether any of the paramTypes implement
104 # an interface. If so, we must check if the method ids using
105 # the inherited types exist.
106 #
107 # This code is a temporary fix and only checks for the methodId
108 # where all paramTypes are converted to their inherited type. The
109 # right way to do this is to replace slicc's simple string
110 # comparison for determining the correct overloaded method, with a
111 # more robust param by param check.
112 #
113 implemented_paramTypes = []
114 for paramType in paramTypes:
115 implemented_paramType = paramType
116 if paramType.isInterface:
117 implements_interface = True
118 implemented_paramType.abstract_ident = paramType["interface"]
119 else:
120 implemented_paramType.abstract_ident = paramType.c_ident
121
122 implemented_paramTypes.append(implemented_paramType)
123
124 if implements_interface:
125 implementedMethodId = obj_type.methodIdAbstract(self.proc_name,
126 implemented_paramTypes)
127 else:
128 implementedMethodId = ""
129
130 if implementedMethodId not in obj_type.methods:
131 self.error("Invalid method call: " \
132 "Type '%s' does not have a method '%s' nor '%s'",
133 obj_type, methodId, implementedMethodId)
134 else:
135 #
136 # Replace the methodId with the implementedMethodId found in
137 # the method list.
138 #
139 methodId = implementedMethodId
140
141 return_type = obj_type.methods[methodId].return_type
142 if return_type.isInterface:
143 prefix = "static_cast<%s &>" % return_type.c_ident
144 prefix = "%s((%s)." % (prefix, code)
145
146 return obj_type, methodId, prefix
147
148
149class ClassMethodCallExprAST(MethodCallExprAST):
150 def __init__(self, slicc, type_ast, proc_name, expr_ast_vec):
151 s = super(ClassMethodCallExprAST, self)
152 s.__init__(slicc, proc_name, expr_ast_vec)
153
154 self.type_ast = type_ast
155
156 def __repr__(self):
157 return "[MethodCallExpr: %r %r]" % (self.proc_name, self.expr_ast_vec)
158
159 def generate_prefix(self, paramTypes):
160
161 # class method call
162 prefix = "(%s::" % self.type_ast
163 obj_type = self.type_ast.type
164 methodId = obj_type.methodId(self.proc_name, paramTypes)
165
166 return obj_type, methodId, prefix
167
168__all__ = [ "MemberMethodCallExprAST", "ClassMethodCallExprAST" ]