functiondeclaration.java

来自「plugin for eclipse」· Java 代码 · 共 205 行

JAVA
205 行
字号
/*
 * Created on May 9, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package isis.anp.nesc.ot;

import isis.anp.common.CodeLocation;
import isis.anp.common.ObjectTreeBuilderContext;
import isis.anp.common.ParserMessage;
import isis.anp.nesc.ot.scope.Symbol;
import isis.anp.nesc.ot.types.FunctionType;
import isis.anp.nesc.tinydoc.TinyDoc;

import java.util.Iterator;


/**
 * @author sallai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FunctionDeclaration extends Declaration {
	// constraint: type must be a FunctionType
	// list of parameters are part of type
	// list of interface parameters are part of type
	
	InterfacePort interfacePort;	// can be null
	FunctionPort functionPort;		// can be null
	
	public FunctionDeclaration() {
		super();
	}
	
	/**
	 * @param declnObj
	 */
	public FunctionDeclaration(Declaration d) {
		super();
		this.defNode = d.defNode;
		this.name = d.name;
		this.nameNode = d.nameNode;
		this.storageClassSpecifiers = d.storageClassSpecifiers;
		this.type = d.type;
	}

	/**
	 * @param ssObj
	 */
	public void addSynchronySpecifier(SynchronySpecifier ssObj) {
		storageClassSpecifiers.add(ssObj);
	}

	/**
	 * @param dsObj
	 */
	public void addDirectionSpecifier(DirectionSpecifier dsObj) {
		storageClassSpecifiers.add(dsObj);
		
	}
	

	public void outline(Outline o) {
//		String doc = getTinyDocText();
	    String doc = null;
	    TinyDoc tinyDoc = getTinyDoc();
	    if(tinyDoc !=null) doc = tinyDoc.toString();
		if(doc!=null) { 
		    o.addTabs();
		    o.append(doc);
		    o.addNewLine();
		}

		o.addTabs();
		
		if(getDefNode()!=null) {
//		    o.append("/* "+getDefNode().getFileName()+":"+getDefNode().getLine()+":"+getDefNode().getColumn()+" */>");
		} else {
		    o.append("/* -*-*-*-*-*-*-*-* DEFNODE IS NULL  */");
		}
		o.ensureSpace();
		
		Iterator i = storageClassSpecifiers.iterator();
		while (i.hasNext()) {
			StorageClassSpecifier scs = (StorageClassSpecifier) i.next();
			o.append(scs.getDefNode().getText());
			o.append(" ");
		}
		
		if(getType()!=null && getType() instanceof FunctionType) {
			FunctionType ft = (FunctionType)getType();
			ft.getEnclosedType().outline(o);
			o.ensureSpace();
			o.append(getName());
			if(interfacePort!=null) {
				o.append(".");
				o.append(interfacePort.getName());
			}
			o.ensureSpace();
			o.append("(");	
			o.ensureSpace();
			ft.getParameterTypeList().outline(o);
			o.ensureSpace();
			o.append(")");			
		} else {
			o.append("/* Error: not a function */");
		}
	}

	/**
	 * @return Returns the interfacePort.
	 */
	public InterfacePort getInterfacePort() {
		return interfacePort;
	}
	/**
	 * @param interfacePort The interfaceAlias to set.
	 */
	public void setInterfacePort(InterfacePort interfacePort) {
		this.interfacePort = interfacePort;
	}

	
	public void addToScope(ObjectTreeBuilderContext ctx) {
		Symbol prevSymbol = ctx.getCurrentScope().findSymbolInCurrentScope(getName());
		
		String functionName = getName();
		if(interfacePort!=null) {
			// TODO: check compliance with prototype
			functionName = interfacePort.getName()+"."+functionName;								
		}

		Symbol newSymbol = this;
		
		if(prevSymbol!=null && !(prevSymbol instanceof FunctionDeclaration)) {
			NameConflictException ex = new NameConflictException(prevSymbol, newSymbol);
			ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(newSymbol.getNameNode()), ex)) ;	
		} else {
			ctx.getCurrentScope().addSymbol(newSymbol);
		}
	}
	
	
	public void addParametersToScope(ObjectTreeBuilderContext ctx) {
		// add parameters to scope
		FunctionType ft = (FunctionType) getType();
		Iterator i = ft.getParameterTypeList().getParameterDeclarations().iterator();
		while (i.hasNext()) {
			ParameterDeclaration pd = (ParameterDeclaration) i.next();
			
			if(pd.getNameNode() == null) continue;
			
			Symbol prevSymbol = ctx.getCurrentScope().findSymbolInCurrentScope(getName());		
			Symbol newSymbol = pd;

			if(prevSymbol==null) {
				ctx.getCurrentScope().addSymbol(newSymbol);
			} else {
				NameConflictException ex = new NameConflictException(prevSymbol, newSymbol);
				ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(newSymbol.getNameNode()), ex)) ;
			}
		}
	}
	/**
	 * @return Returns the functionPort.
	 */
	public FunctionPort getFunctionPort() {
		return functionPort;
	}
	/**
	 * @param functionPort The functionPort to set.
	 */
	public void setFunctionPort(FunctionPort functionPort) {
		this.functionPort = functionPort;
	}
	
	public boolean hasStorageClassSpecifier(String scsText) {
		Iterator i = this.storageClassSpecifiers.iterator();
		while (i.hasNext()) {
			StorageClassSpecifier scs = (StorageClassSpecifier) i.next();
			if(scs.getDefNode().getText().equals(scsText)) return true;
		}
		return false;
	}
	
	public boolean isEvent() {
		return hasStorageClassSpecifier("event");
	}

	public boolean isCommand() {
		return hasStorageClassSpecifier("command");
	}
	
	public boolean isTask() {
		return hasStorageClassSpecifier("task");
	}
	
	public boolean isAsync() {
		return hasStorageClassSpecifier("async");
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?