public void channel(STEntry entry) { IDeclarationContainer container = findEnclosingDelcarationContainer(); if (currentCode instanceof Component) { // Usual case if (entry.getType() instanceof ChannelType) { ((IComponent) currentCode).addChannel(entry.getName(), (ChannelType) entry.getType()); } else if (entry.getType() instanceof InterfaceType) { ((IComponent) currentCode).addInterface(entry.getName(), (InterfaceType) entry.getType()); } else { throw new RuntimeException( "unknown type encountered in channel definition" + entry.getType().getClass().getName()); } } else if (container instanceof Component) { if (entry.getType() instanceof ChannelType) { ((IComponent) container).addChannel(entry.getName(), (ChannelType) entry.getType()); } else if (entry.getType() instanceof InterfaceType) { ((IComponent) container).addInterface(entry.getName(), (InterfaceType) entry.getType()); } else { throw new RuntimeException( "unknown type encountered in channel definition" + entry.getType().getClass().getName()); } } else if (currentCode instanceof CompilationUnit) { // this is a top level interface definition - no code generation needed // do nothing } else { // error situation throw new RuntimeException( "unknown context for channel definition" + currentCode.getClass().getName()); } }
public void addIncludeToCurrentContext(String s) { // look back the stack until we find a CompilationUnit or a Component ICode thisone = null; // not sure if needed thisone = currentCode; if (thisone instanceof Component) { ((IComponent) thisone) .addExternalIncludes(Code.HASH_INCLUDE_ + Code.DQUOTE + s + Code.DQUOTE); return; } else if (thisone instanceof CompilationUnit) { ((ICompilationUnit) thisone) .addExternalInclude(Code.HASH_INCLUDE_ + Code.DQUOTE + s + Code.DQUOTE); // System.out.println("adding '" + s + "' to " + ((ICompilationUnit) thisone )); return; } int lastIndex = bodyStack.size() - 1; for (int i = lastIndex; i > 0; i--) { thisone = bodyStack.get(i); if (thisone instanceof StructDeclaration) { ((StructDeclaration) thisone) .addExternalIncludes(Code.HASH_INCLUDE_ + Code.DQUOTE + s + Code.DQUOTE); return; } else if (thisone instanceof Component) { ((IComponent) thisone) .addExternalIncludes(Code.HASH_INCLUDE_ + Code.DQUOTE + s + Code.DQUOTE); return; } else if (thisone instanceof CompilationUnit) { ((ICompilationUnit) thisone) .addExternalInclude(Code.HASH_INCLUDE_ + Code.DQUOTE + s + Code.DQUOTE); return; } } throw new RuntimeException("Didn't find Compilation Unit or Component when adding includes"); }
public IBehaviour behaviourBody() { Behaviour newbody = new Behaviour(findEnclosingDelcarationContainer()); if (currentCode instanceof IComponent) { // may not be if an error has occurred ((IComponent) currentCode).addBehaviour(newbody); newCodeBody(newbody); } return newbody; }
// JL should we rename addhoistedCodeToCurrentContext to ...ComponentContext // or add further contexts here? public void addHoistedCodeToComponentContext(String s) { // look back the stack until we find a Component int lastIndex = bodyStack.size() - 1; for (int i = lastIndex; i > 0; i--) { ICode thisone = bodyStack.get(i); if (thisone instanceof Component) { ((IComponent) thisone).addHoistedCode(s); return; } } throw new RuntimeException("Didn't find Component when adding hoisted code"); }
public ICode constructorBody(FunctionType ft, List<String> names, int scope_level) { IConstructor newbody = new Constructor( ((IProcedureContainer) findEnclosingDelcarationContainer()), ft, names, findEnclosingDelcarationContainer(), scope_level); if (currentCode instanceof IComponent) { // it may not be if an error has occurred. ((IComponent) currentCode).addConstructor(newbody); newCodeBody(newbody); } return newbody; }
// JL should we look back on stack for next valid context for code hoisting e.g. // hoisting makes sense for ?expression?, procedure, behaviour, component, sequence ... public void addHoistedCodeToCurrentContext(String s) { // look back the stack until we find valid context for hoisting int lastIndex = bodyStack.size() - 1; for (int i = lastIndex; i >= 0; i--) { ICode thisone = bodyStack.get(i); if (thisone instanceof Sequence) { ((ISequence) thisone).addHoistedCode(s); return; } else if (thisone instanceof Function) { ((IFunction) thisone).addHoistedCode(s); return; } else if (thisone instanceof Behaviour) { ((IBehaviour) thisone).addHoistedCode(s); return; } else if (thisone instanceof Component) { ((IComponent) thisone).addHoistedCode(s); return; } } throw new RuntimeException("Didn't find suitable context for code hoisting"); }