From 9648c05db19292ddd285a80914593cc0631403ff Mon Sep 17 00:00:00 2001 From: Nilay Vaish Date: Fri, 14 Aug 2015 19:28:43 -0500 Subject: ruby: slicc: use default argument value Before this patch, while one could declare / define a function with default argument values, but the actual function call would require one to specify all the arguments. This patch changes the check for function arguments. Now a function call needs to specify arguments that are at least as much as those with default values and at most the total number of arguments taken as input by the function. --- src/mem/slicc/ast/EnumDeclAST.py | 2 +- src/mem/slicc/ast/FormalParamAST.py | 22 ++++++++++++++++++++-- src/mem/slicc/ast/FuncDeclAST.py | 17 ++++++++++++----- src/mem/slicc/ast/InPortDeclAST.py | 4 ++-- src/mem/slicc/ast/StateDeclAST.py | 4 ++-- 5 files changed, 37 insertions(+), 12 deletions(-) (limited to 'src/mem/slicc/ast') diff --git a/src/mem/slicc/ast/EnumDeclAST.py b/src/mem/slicc/ast/EnumDeclAST.py index d97c13483..bc0c1c224 100644 --- a/src/mem/slicc/ast/EnumDeclAST.py +++ b/src/mem/slicc/ast/EnumDeclAST.py @@ -67,6 +67,6 @@ class EnumDeclAST(DeclAST): pairs = { "external" : "yes" } func = Func(self.symtab, func_id + "_" + t.c_ident, func_id, self.location, - self.symtab.find("std::string", Type), [ t ], [], "", + self.symtab.find("std::string", Type), [ t ], [], [], "", pairs) self.symtab.newSymbol(func) diff --git a/src/mem/slicc/ast/FormalParamAST.py b/src/mem/slicc/ast/FormalParamAST.py index ce73304f1..ef39b40f0 100644 --- a/src/mem/slicc/ast/FormalParamAST.py +++ b/src/mem/slicc/ast/FormalParamAST.py @@ -46,6 +46,9 @@ class FormalParamAST(AST): def generate(self): type = self.type_ast.type param = "param_%s" % self.ident + proto = "" + body = "" + default = False # Add to symbol table v = Var(self.symtab, self.ident, self.location, type, param, @@ -56,6 +59,21 @@ class FormalParamAST(AST): "interface" in type and ( type["interface"] == "AbstractCacheEntry" or type["interface"] == "AbstractEntry")): - return type, "%s* %s" % (type.c_ident, param) + proto = "%s* %s" % (type.c_ident, param) + body = proto + elif self.default != None: + value = "" + if self.default == True: + value = "true" + elif self.default == False: + value = "false" + else: + value = "%s" % self.default + proto = "const %s& %s = %s" % (type.c_ident, param, value) + body = "const %s& %s" % (type.c_ident, param) + default = True else: - return type, "const %s& %s" % (type.c_ident, param) + proto = "const %s& %s" % (type.c_ident, param) + body = proto + + return type, proto, body, default diff --git a/src/mem/slicc/ast/FuncDeclAST.py b/src/mem/slicc/ast/FuncDeclAST.py index 47ae7076e..4e64c0ba5 100644 --- a/src/mem/slicc/ast/FuncDeclAST.py +++ b/src/mem/slicc/ast/FuncDeclAST.py @@ -45,7 +45,9 @@ class FuncDeclAST(DeclAST): def generate(self, parent = None): types = [] - params = [] + proto_params = [] + body_params = [] + default_count = 0 void_type = self.symtab.find("void", Type) # Generate definition code @@ -58,13 +60,17 @@ class FuncDeclAST(DeclAST): for formal in self.formals: # Lookup parameter types try: - type, ident = formal.generate() + type, proto, body, default = formal.generate() types.append(type) - params.append(ident) + proto_params.append(proto) + body_params.append(body) + if default: + default_count += 1 except AttributeError: types.append(formal.type) - params.append(None) + proto_params.append(None) + body_params.append(None) body = self.slicc.codeFormatter() if self.statements is None: @@ -87,7 +93,8 @@ class FuncDeclAST(DeclAST): machine = self.state_machine func = Func(self.symtab, func_name_args, self.ident, self.location, - return_type, types, params, str(body), self.pairs) + return_type, types, proto_params, + body_params, str(body), self.pairs, default_count) if parent is not None: if not parent.addFunc(func): diff --git a/src/mem/slicc/ast/InPortDeclAST.py b/src/mem/slicc/ast/InPortDeclAST.py index 7a019a0e0..2ef043151 100644 --- a/src/mem/slicc/ast/InPortDeclAST.py +++ b/src/mem/slicc/ast/InPortDeclAST.py @@ -89,13 +89,13 @@ class InPortDeclAST(DeclAST): for param in param_types: trigger_func_name += "_" + param.ident func = Func(self.symtab, trigger_func_name, "trigger", self.location, - void_type, param_types, [], "", pairs) + void_type, param_types, [], [], "", pairs) symtab.newSymbol(func) # Add the stallPort method - this hacks reschedules the controller # for stalled messages that don't trigger events func = Func(self.symtab, "stallPort", "stallPort", self.location, - void_type, [], [], "", pairs) + void_type, [], [], [], "", pairs) symtab.newSymbol(func) param_types = [] diff --git a/src/mem/slicc/ast/StateDeclAST.py b/src/mem/slicc/ast/StateDeclAST.py index f0a0b97d3..a33ea9245 100644 --- a/src/mem/slicc/ast/StateDeclAST.py +++ b/src/mem/slicc/ast/StateDeclAST.py @@ -66,7 +66,7 @@ class StateDeclAST(DeclAST): pairs = { "external" : "yes" } func = Func(self.symtab, func_id + "_" + t.ident, func_id, self.location, - self.symtab.find("std::string", Type), [ t ], [], "", + self.symtab.find("std::string", Type), [ t ], [], [], "", pairs) self.symtab.newSymbol(func) @@ -76,6 +76,6 @@ class StateDeclAST(DeclAST): pairs = { "external" : "yes" } func = Func(self.symtab, func_id + "_" + t.ident, func_id, self.location, - self.symtab.find("AccessPermission", Type), [ t ], [], "", + self.symtab.find("AccessPermission", Type), [ t ], [], [], "", pairs) self.symtab.newSymbol(func) -- cgit v1.2.3