def cacheName_ClassDef(self, cls): # self.logger.debug('cacheName_ClassDef', cls, cls.owner, cls.instantiator, cls.instantiation) owner = cls.owner assert not owner.hasSymbol(cls.name) assert not isinstance(owner, ast.StatementBlock) if cls.isProtoType(): owner.addSymbol(cls) for p in cls.genericParams[:]: p.visit(self) return if cls.isSimpleClass(): owner.addSymbol(cls) cls.destructor = None for d in cls.definitions: # self.logger.debug('cacheName_ClassDef def', cls.name, d, cls) assert not cls.isProtoType() or cls.isInstantiation(), (cls.name, cls, d) d.visit(self) for script in cls.scripts: script.visit(self) if len(cls.constructors) == 0: cons = ast.FuncDef([], cls.name, ast.FuncSpec([], None), ast.StatementBody([])) # self.logger.debug('cacheName_ClassDef add constructor', cls.name, cons, cls) cons.info.type = ast.FuncType.constructor cls.constructors.append(cons) cls.definitions.append(cons) self.setupNewItem(cons, cls, True)
def insertToString(self, cls, basecls, visitor): # visitor.logger.debug('insertToString', cls, basecls) name = 'toString' if name not in cls.symbols: # visitor.logger.debug('insertToString do', cls, basecls) clsname = ast.Call(ast.Identifier('getClassName'), []) thisptr = ast.Call(ast.Identifier('formatId'), []) expr = None if 'name' in cls.symbols: # visitor.logger.debug('insertToString with name', cls, basecls, clsname, thisptr) expr = ast.BinaryOp( '%', ast.makeLiteral('%s(name=%s,id=%s)'), ast.TupleLiteral( [clsname, ast.Identifier('name'), thisptr])) else: # visitor.logger.debug('insertToString without name', cls, basecls, clsname, thisptr) expr = ast.BinaryOp('%', ast.makeLiteral('%s(id=%s)'), ast.TupleLiteral([clsname, thisptr])) func = ast.FuncDef([], name, ast.FuncSpec([], ast.makePrimitiveType('string')), ast.StatementBody([ast.Return(expr)])) # visitor.logger.debug('insertFunc ok', name, cls, basecls, cls.primaryVars, func) cls.definitions.append(func) func.setOwner(cls) visitor.visitNewItem(func)
def addMixinFunc(self, visitor, script, f, owner, mixincls): # visitor.logger.debug('addMixinFunc', f.name, owner, f, mixincls) if not owner.hasSymbol(f.name): callinfo = ast.Call(ast.AttrRef(script.args[1].clone(), f.name), [ast.Identifier(param.name) for param in f.spec.params]) # visitor.logger.debug('addMixinFunc callinfo', f.name, owner, f, mixincls, callinfo, callinfo.caller) newfunc = ast.FuncDef([], f.name, f.spec.clone(), ast.StatementBody([ast.Return(callinfo)])) owner.definitions.append(newfunc) newfunc.setOwner(owner) visitor.visitNewItem(newfunc)
def _build_function_definition(self, spec, decl, param_decls, body): """ Builds a function definition. """ declaration = self._build_declarations( spec=spec, decls=[dict(decl=decl, init=None)])[0] return ast.FuncDef(spec=spec, decl=declaration, param_decls=param_decls, body=body, coord=decl.coord)
def processScript(self, visitor, script): owner = script.owner assert isinstance(owner, ast.ClassDef) cls = owner cls.singleton = True if not cls.hasSymbol('instance'): proto = parseFuncProto('instance() => %s' % owner.name) func = ast.FuncDef([], proto.name, proto.spec, ast.StatementBody([])) func.spec.static = True cls.definitions.append(func) func.setOwner(cls) visitor.visitNewItem(func) visitor.logger.debug('SingletonScript.processScript func', cls.name, func.name, cls, func)
def processScript(self, visitor, script): owner = script.owner assert isinstance(owner, ast.ClassDef), (script, script.owner) # visitor.logger.debug('NameScript.processScript', self, script, owner) cls = owner if not cls.hasSymbol('getClassName'): # visitor.logger.debug('NameScript.processScript add getClassName', self, script, owner) proto = parseFuncProto('getClassName() => string') stmts = [ast.Return(ast.makeLiteral(cls.name))] func = ast.FuncDef([], proto.name, proto.spec, ast.StatementBody(stmts)) cls.definitions.append(func) func.setOwner(cls) visitor.visitNewItem(func)
def insertFunc(self, cls, basecls, visitor, name, fieldExprGen, stmtsGen): basefunc = basecls.symbols[name] if name not in cls.symbols: # visitor.logger.debug('insertFunc', name, cls, basecls, cls.primaryVars) stmts = [ self.generateFieldExpr(cls, basecls, basefunc, fieldExprGen, visitor, field) for field in cls.primaryVars ] stmts = [stmt for stmt in stmts if stmt is not None] stmts = stmtsGen(cls, basecls, visitor, stmts) func = ast.FuncDef([], name, basefunc.spec.clone(), ast.StatementBody(stmts)) # visitor.logger.debug('insertFunc ok', name, cls, basecls, cls.primaryVars, func, stmts) cls.definitions.append(func) func.setOwner(cls) visitor.visitNewItem(func)
def processScript(self, visitor, script): owner = script.owner # visitor.logger.debug('MixinScript.processScript', script, owner.bases, script.args) assert isinstance(owner, ast.ClassDef) script.mixin_var_type = ast.UserType(getScriptQuialifiedName(script.args[0])) script.mixin_var = ast.SingleVarDef(script.mixin_var_name, script.mixin_var_type, ast.Call(script.args[0].clone(), script.args[3:], script.namedArgs)) proto = parseFuncProto('%s() => %s' % (script.mixin_propget_name, script.mixin_var_type.fullpath)) script.mixin_propget = ast.FuncDef([], script.mixin_propget_name, proto.spec, ast.StatementBody([ast.Return(ast.Identifier(script.mixin_var_name))])) script.mixin_class = owner.resolveSymbol(script.mixin_var_type.path) # visitor.logger.debug('MixinScript.processScript add var', owner, script.owner, script, owner.bases, script.args, script.mixin_var) owner.definitions.append(script.mixin_var) script.mixin_var.setOwner(script.owner) owner.definitions.append(script.mixin_propget) script.mixin_propget.setOwner(script.owner) # visitor.logger.debug('MixinScript.processScript add var', owner, script.owner, script, owner.bases, script.args, script.mixin_var, script.mixin_var.owner) visitor.visitNewItem(script.mixin_var) visitor.visitNewItem(script.mixin_propget) self.addMixinFuncs(visitor, script, script.mixin_class, owner)
def p_function_definition_2(self, p): """ function_definition : declaration_specifiers declarator declaration_list_opt compound_statement """ decl_spec = p[1] struct = None if isinstance(decl_spec['spec'][0], ast.Struct): struct = decl_spec['spec'][0] type = p[2] if struct is not None: if isinstance(type, ast.IdentifierType): declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=struct, init=None) else: while not isinstance(type.type, ast.IdentifierType): type = type.type declname = type.type.name type.type = struct declaration = ast.Decl(name=declname, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) else: while not isinstance(type, ast.IdentifierType): type = type.type type.spec = decl_spec['spec'] declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) p[0] = ast.FuncDef(decl=declaration, param_decls=p[3], body=p[4])
def p_function_definition(self, p): """ function_definition : type_specifier declarator declaration_list_opt compound_statement """ declarations = self._build_declarations(spec=[p[1]], decls=[{"decl": p[2]}]) p[4].coord = p[1].coord p[0] = ast.FuncDef(p[1], declarations[0], p[3], p[4])