MethodCallExprAST.py (11029:32604f9e190b) MethodCallExprAST.py (11049:dfb0aa3f0649)
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 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):
37 tmp = self.slicc.codeFormatter()
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
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 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):
37 tmp = self.slicc.codeFormatter()
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 func = obj_type.methods[methodId]
60 func.checkArguments(self.expr_ast_vec)
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))
61
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
62 # Return the return type of the method
63 return obj_type.methods[methodId].return_type
64
65 def findResources(self, resources):
66 pass
67
68class MemberMethodCallExprAST(MethodCallExprAST):
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):
69 def __init__(self, slicc, obj_expr_ast, func_call):
81 def __init__(self, slicc, obj_expr_ast, proc_name, expr_ast_vec):
70 s = super(MemberMethodCallExprAST, self)
82 s = super(MemberMethodCallExprAST, self)
71 s.__init__(slicc, func_call.proc_name, func_call.exprs)
83 s.__init__(slicc, proc_name, expr_ast_vec)
84
72 self.obj_expr_ast = obj_expr_ast
73
74 def __repr__(self):
75 return "[MethodCallExpr: %r%r %r]" % (self.proc_name,
76 self.obj_expr_ast,
77 self.expr_ast_vec)
78 def generate_prefix(self, paramTypes):
79 code = self.slicc.codeFormatter()
80
81 # member method call
82 obj_type = self.obj_expr_ast.generate(code)
83 methodId = obj_type.methodId(self.proc_name, paramTypes)
84
85 prefix = ""
86 implements_interface = False
87
88 if methodId in obj_type.methods:
89 return_type = obj_type.methods[methodId].return_type
90
91 else:
92 #
93 # Check whether the method is implemented by the super class
94 if "interface" in obj_type:
95 interface_type = self.symtab.find(obj_type["interface"]);
96
97 if methodId in interface_type.methods:
98 return_type = interface_type.methods[methodId].return_type
99 obj_type = interface_type
100
101 else:
102 self.error("Invalid method call: " \
103 "Type '%s' does not have a method %s, '%s'",
104 obj_type, self.proc_name, methodId)
105
106 else:
107 #
108 # The initial method check has failed, but before generating an
109 # error we must check whether any of the paramTypes implement
110 # an interface. If so, we must check if the method ids using
111 # the inherited types exist.
112 #
113 # This code is a temporary fix and only checks for the methodId
114 # where all paramTypes are converted to their inherited type. The
115 # right way to do this is to replace slicc's simple string
116 # comparison for determining the correct overloaded method, with a
117 # more robust param by param check.
118 #
119 implemented_paramTypes = []
120 for paramType in paramTypes:
121 implemented_paramType = paramType
122 if paramType.isInterface:
123 implements_interface = True
124 implemented_paramType.abstract_ident = paramType["interface"]
125 else:
126 implemented_paramType.abstract_ident = paramType.c_ident
127
128 implemented_paramTypes.append(implemented_paramType)
129
130 implementedMethodId = ""
131 if implements_interface:
132 implementedMethodId = obj_type.methodIdAbstract(
133 self.proc_name, implemented_paramTypes)
134
135 if implementedMethodId not in obj_type.methods:
136 self.error("Invalid method call: Type '%s' " \
137 "does not have a method %s, '%s' nor '%s'",
138 obj_type, self.proc_name, methodId,
139 implementedMethodId)
140
141 # Replace the methodId with the implementedMethodId
142 # found in the method list.
143 methodId = implementedMethodId
144 return_type = obj_type.methods[methodId].return_type
145
146 if str(obj_type) == "AbstractCacheEntry" or \
147 str(obj_type) == "AbstractEntry" or \
148 ("interface" in obj_type and (
149 obj_type["interface"] == "AbstractCacheEntry" or
150 obj_type["interface"] == "AbstractEntry")):
151 prefix = "%s((*(%s))." % (prefix, code)
152 else:
153 prefix = "%s((%s)." % (prefix, code)
154
155 return obj_type, methodId, prefix
156
157
158class ClassMethodCallExprAST(MethodCallExprAST):
159 def __init__(self, slicc, type_ast, proc_name, expr_ast_vec):
160 s = super(ClassMethodCallExprAST, self)
161 s.__init__(slicc, proc_name, expr_ast_vec)
162
163 self.type_ast = type_ast
164
165 def __repr__(self):
166 return "[MethodCallExpr: %r %r]" % (self.proc_name, self.expr_ast_vec)
167
168 def generate_prefix(self, paramTypes):
169
170 # class method call
171 prefix = "(%s::" % self.type_ast
172 obj_type = self.type_ast.type
173 methodId = obj_type.methodId(self.proc_name, paramTypes)
174
175 return obj_type, methodId, prefix
176
177__all__ = [ "MemberMethodCallExprAST", "ClassMethodCallExprAST" ]
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):
92 code = self.slicc.codeFormatter()
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
101 if methodId in obj_type.methods:
102 return_type = obj_type.methods[methodId].return_type
103
104 else:
105 #
106 # Check whether the method is implemented by the super class
107 if "interface" in obj_type:
108 interface_type = self.symtab.find(obj_type["interface"]);
109
110 if methodId in interface_type.methods:
111 return_type = interface_type.methods[methodId].return_type
112 obj_type = interface_type
113
114 else:
115 self.error("Invalid method call: " \
116 "Type '%s' does not have a method %s, '%s'",
117 obj_type, self.proc_name, methodId)
118
119 else:
120 #
121 # The initial method check has failed, but before generating an
122 # error we must check whether any of the paramTypes implement
123 # an interface. If so, we must check if the method ids using
124 # the inherited types exist.
125 #
126 # This code is a temporary fix and only checks for the methodId
127 # where all paramTypes are converted to their inherited type. The
128 # right way to do this is to replace slicc's simple string
129 # comparison for determining the correct overloaded method, with a
130 # more robust param by param check.
131 #
132 implemented_paramTypes = []
133 for paramType in paramTypes:
134 implemented_paramType = paramType
135 if paramType.isInterface:
136 implements_interface = True
137 implemented_paramType.abstract_ident = paramType["interface"]
138 else:
139 implemented_paramType.abstract_ident = paramType.c_ident
140
141 implemented_paramTypes.append(implemented_paramType)
142
143 implementedMethodId = ""
144 if implements_interface:
145 implementedMethodId = obj_type.methodIdAbstract(
146 self.proc_name, implemented_paramTypes)
147
148 if implementedMethodId not in obj_type.methods:
149 self.error("Invalid method call: Type '%s' " \
150 "does not have a method %s, '%s' nor '%s'",
151 obj_type, self.proc_name, methodId,
152 implementedMethodId)
153
154 # Replace the methodId with the implementedMethodId
155 # found in the method list.
156 methodId = implementedMethodId
157 return_type = obj_type.methods[methodId].return_type
158
159 if str(obj_type) == "AbstractCacheEntry" or \
160 str(obj_type) == "AbstractEntry" or \
161 ("interface" in obj_type and (
162 obj_type["interface"] == "AbstractCacheEntry" or
163 obj_type["interface"] == "AbstractEntry")):
164 prefix = "%s((*(%s))." % (prefix, code)
165 else:
166 prefix = "%s((%s)." % (prefix, code)
167
168 return obj_type, methodId, prefix
169
170
171class ClassMethodCallExprAST(MethodCallExprAST):
172 def __init__(self, slicc, type_ast, proc_name, expr_ast_vec):
173 s = super(ClassMethodCallExprAST, self)
174 s.__init__(slicc, proc_name, expr_ast_vec)
175
176 self.type_ast = type_ast
177
178 def __repr__(self):
179 return "[MethodCallExpr: %r %r]" % (self.proc_name, self.expr_ast_vec)
180
181 def generate_prefix(self, paramTypes):
182
183 # class method call
184 prefix = "(%s::" % self.type_ast
185 obj_type = self.type_ast.type
186 methodId = obj_type.methodId(self.proc_name, paramTypes)
187
188 return obj_type, methodId, prefix
189
190__all__ = [ "MemberMethodCallExprAST", "ClassMethodCallExprAST" ]