This patch adds support for function definitions to appear in slicc structs.
This is required for supporting functional accesses for different types of
messages. Subsequent patches will use this to development.
external_type(NodeID, default="0", primitive="yes");
external_type(MachineID);
-MessageBuffer getMandatoryQueue(int core_id);
-
structure (Set, external = "yes", non_obj="yes") {
void setSize(int);
void add(NodeID);
pairs = { "external" : "yes" }
func = Func(self.symtab, func_id, self.location,
self.symtab.find("std::string", Type), [ t ], [], "",
- pairs, None)
+ pairs)
self.symtab.newSymbol(func)
def files(self, parent=None):
return set()
- def generate(self):
+ def generate(self, parent = None):
types = []
params = []
void_type = self.symtab.find("void", Type)
machine = self.state_machine
func = Func(self.symtab, self.ident, self.location, return_type,
- types, params, str(body), self.pairs, machine)
+ types, params, str(body), self.pairs)
- if machine is not None:
+ if parent is not None:
+ if not parent.addFunc(func):
+ self.error("Duplicate method: %s:%s()" % (parent, self.ident))
+ func.class_name = parent.c_ident
+
+ elif machine is not None:
machine.addFunc(func)
+ func.isInternalMachineFunc = True
+ func.class_name = "%s_Controller" % machine
else:
self.symtab.newSymbol(func)
# Add the trigger method - FIXME, this is a bit dirty
pairs = { "external" : "yes" }
func = Func(self.symtab, "trigger", self.location, void_type,
- param_types, [], "", pairs, None)
+ param_types, [], "", pairs)
symtab.newSymbol(func)
param_types = []
# checks before calling double trigger to ensure that won't
# happen
func = Func(self.symtab, "doubleTrigger", self.location, void_type,
- param_types, [], "", pairs, None)
+ param_types, [], "", pairs)
symtab.newSymbol(func)
# Add the continueProcessing method - this hack supports
# messages that don't trigger events
func = Func(self.symtab, "continueProcessing", self.location,
- void_type, [], [], "", pairs, None)
+ void_type, [], [], "", pairs)
symtab.newSymbol(func)
if self.statements is not None:
def findMachines(self):
# Add to MachineType enumeration
machine_type = self.symtab.find("MachineType", Type)
- if not machine_type.enumAdd(self.ident, self.pairs_ast.pairs):
+ if not machine_type.addEnum(self.ident, self.pairs_ast.pairs):
self.error("Duplicate machine name: %s:%s" % (machine_type,
self.ident))
pairs = { "external" : "yes" }
func = Func(self.symtab, func_id, self.location,
self.symtab.find("std::string", Type), [ t ], [], "",
- pairs, None)
+ pairs)
self.symtab.newSymbol(func)
# Add the State_to_permission method
pairs = { "external" : "yes" }
func = Func(self.symtab, func_id, self.location,
self.symtab.find("AccessPermission", Type), [ t ], [], "",
- pairs, None)
+ pairs)
self.symtab.newSymbol(func)
self.error("States must in a State Declaration, not a normal enum.")
# Add enumeration
- if not type.enumAdd(self.field_id, self.pairs_ast.pairs):
+ if not type.addEnum(self.field_id, self.pairs_ast.pairs):
self.error("Duplicate enumeration: %s:%s" % (type, self.field_id))
# Fill machine info
(field_type, rvalue_type))
# Add data member to the parent type
- if not type.dataMemberAdd(self.field_id, field_type, self.pairs,
+ if not type.addDataMember(self.field_id, field_type, self.pairs,
init_code):
self.error("Duplicate data member: %s:%s" % (type_ptr, field_id))
from slicc.ast.TypeFieldAST import TypeFieldAST
class TypeFieldMethodAST(TypeFieldAST):
- def __init__(self, slicc, return_type_ast, ident, type_asts, pairs):
+ def __init__(self, slicc, return_type_ast, ident, type_asts, pairs,
+ statements = None):
super(TypeFieldMethodAST, self).__init__(slicc, pairs)
self.return_type_ast = return_type_ast
self.ident = ident
self.type_asts = type_asts
+ self.statements = statements
def __repr__(self):
return ""
types = [ t.type for t in self.type_asts ]
# Add method
- if not type.methodAdd(self.ident, return_type, types):
+ if not type.addMethod(self.ident, return_type, types):
self.error("Duplicate method: %s:%s()" % (type, self.ident))
self.error("State Declaration must be of type State.")
# Add enumeration
- if not type.enumAdd(self.field_id, self.pairs_ast.pairs):
+ if not type.addEnum(self.field_id, self.pairs_ast.pairs):
self.error("Duplicate enumeration: %s:%s" % (type, self.field_id))
# Fill machine info
"decl : type ident pairs SEMI"
p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
+ # Function definition and declaration
def p_decl__func_decl(self, p):
- """decl : void ident '(' params ')' pairs SEMI
+ "decl : func_decl"
+ p[0] = p[1]
+
+ def p_func_decl__0(self, p):
+ """func_decl : void ident '(' params ')' pairs SEMI
| type ident '(' params ')' pairs SEMI"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
def p_decl__func_def(self, p):
- """decl : void ident '(' params ')' pairs statements
- | type ident '(' params ')' pairs statements"""
+ "decl : func_def"
+ p[0] = p[1]
+
+ def p_func_def__0(self, p):
+ """func_def : void ident '(' params ')' pairs statements
+ | type ident '(' params ')' pairs statements"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
# Type fields
"type_member : type_or_void ident '(' types ')' pairs SEMI"
p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
+ def p_type_method__1(self, p):
+ "type_member : type_or_void ident '(' params ')' pairs statements"
+ p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
+
def p_type_member__1(self, p):
"type_member : type_or_void ident pairs SEMI"
p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
class Func(Symbol):
def __init__(self, table, ident, location, return_type, param_types,
- param_strings, body, pairs, machine):
+ param_strings, body, pairs):
super(Func, self).__init__(table, ident, location, pairs)
self.return_type = return_type
self.param_types = param_types
self.body = body
self.isInternalMachineFunc = False
self.c_ident = ident
-
- if machine is None or "external" in self or "primitive" in self:
- pass
- else:
- self.machineStr = str(machine)
- self.isInternalMachineFunc = True
+ self.class_name = ""
def __repr__(self):
return ""
if "return_by_pointer" in self and self.return_type != void_type:
return_type += "*"
- if self.isInternalMachineFunc:
- klass = "%s_Controller" % self.machineStr
- else:
- self.error("No class found for the function %s" % self.ident)
-
params = ', '.join(self.param_strings)
code('''
$return_type
-${klass}::${{self.c_ident}}($params)
+${{self.class_name}}::${{self.c_ident}}($params)
{
${{self.body}}
}
# Methods
self.methods = {}
+ self.functions = {}
# Enums
self.enums = orderdict()
return "interface" in self
# Return false on error
- def dataMemberAdd(self, ident, type, pairs, init_code):
+ def addDataMember(self, ident, type, pairs, init_code):
if ident in self.data_members:
return False
def statePermPairAdd(self, state_name, perm_name):
self.statePermPairs.append([state_name, perm_name])
- def methodAdd(self, name, return_type, param_type_vec):
+ def addMethod(self, name, return_type, param_type_vec):
ident = self.methodId(name, param_type_vec)
if ident in self.methods:
return False
self.methods[ident] = Method(return_type, param_type_vec)
return True
- def enumAdd(self, ident, pairs):
+ # Ideally either this function or the one above should exist. But
+ # methods and functions have different structures right now.
+ # Hence, these are different, at least for the time being.
+ def addFunc(self, func):
+ ident = self.methodId(func.ident, func.param_types)
+ if ident in self.functions:
+ return False
+
+ self.functions[ident] = func
+ return True
+
+ def addEnum(self, ident, pairs):
if ident in self.enums:
return False
code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;')
+ # Prototypes for functions defined for the Type
+ for item in self.functions:
+ proto = self.functions[item].prototype
+ if proto:
+ code('$proto')
+
code.dedent()
code('};')
out << "]";
}''')
+ # print the code for the functions in the type
+ for item in self.functions:
+ code(self.functions[item].generateCode())
+
code.write(path, "%s.cc" % self.c_ident)
def printEnumHH(self, path):