Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

SpinTypeInit.java

00001 package edu.ksu.cis.bandera.spin;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.edu)     *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import edu.ksu.cis.bandera.bir.Collection;
00036 
00037 import java.io.*;
00038 import java.util.*;
00039 
00040 import edu.ksu.cis.bandera.bir.*;
00041 
00042 /**
00043  * BIR TypeSwitch to initialize a variable of a BIR type in PROMELA.
00044  * <p>
00045  * The Object parameter to the type switch case methods is a context
00046  * indicating whether the variable being initialized is top-level.
00047  * If the context is a StateVar, the variable is top-level and the
00048  * name and initial value for the variable should be retrieved from
00049  * that StateVar object.  If the context is a String, then the variable
00050  * is a component of a collection, record, or array, and the String
00051  * is the fully qualified variable name.  In this case, the initial
00052  * value comes from the type of the variable.
00053  * <p>
00054  * SPIN claims to initialize most types to 0 automatically, so
00055  * we omit such initializations.
00056  */
00057 
00058 public class SpinTypeInit extends AbstractTypeSwitch {
00059 
00060     SpinTrans spinTrans;
00061     PrintWriter out;
00062 
00063     public SpinTypeInit(SpinTrans spinTrans, PrintWriter out) {
00064     this.spinTrans = spinTrans;
00065     this.out = out;
00066     }
00067     public void caseArray(Array type, Object o)
00068     {
00069     String name = varName(o);
00070     int size = type.getSize().getValue();
00071     spinTrans.println(name + ".length = " + size + ";");
00072     for (int i = 0; i < size; i++) 
00073         type.getBaseType().apply(this, name + ".element[" + i + "]");
00074     }
00075     public void caseBool(Bool type, Object o) 
00076     {
00077     simpleInit(type, o);
00078     }
00079     public void caseCollection(Collection type, Object o)
00080     {
00081     String name = varName(o);
00082     int size = type.getSize().getValue();
00083     for (int i = 0; i < size; i++) {
00084         // spinTrans.println(name + ".inuse[" + i + "] = 0;");
00085         type.getBaseType().apply(this, name + ".instance[" + i + "]");
00086     }
00087     }
00088     public void caseEnumerated(Enumerated type, Object o) 
00089     {
00090     simpleInit(type, o);
00091     }
00092     public void caseField(Field type, Object o)
00093     {
00094     type.getType().apply(this, varName(o) + "." + type.getName());
00095     }
00096     public void caseLock(Lock type, Object o)
00097     {   
00098     String name = varName(o);
00099     spinTrans.println(name + ".lock!LOCK;");
00100     // We'll trust SPIN to set vars to 0 initially
00101     // spinTrans.println(name + ".owner = 0;");
00102     // if (type.isWaiting())
00103     //    spinTrans.println(name + ".waiting = 0;");
00104     // if (type.isReentrant())
00105     //    spinTrans.println(name + ".count = 0;");
00106     }
00107     public void caseRange(Range type, Object o) 
00108     {
00109     simpleInit(type, o);
00110     }
00111     public void caseRecord(Record type, Object o)
00112     {
00113     Vector fields = type.getFields();
00114     for (int i = 0; i < fields.size(); i++) 
00115         ((Field)fields.elementAt(i)).apply(this, o);
00116     }
00117     public void caseRef(Ref type, Object o)
00118     {
00119     String name = varName(o);
00120     // spinTrans.println(name + " = 0;");
00121     }
00122     public void defaultCase(Object obj) {
00123     throw new RuntimeException("Construct not handled: " + obj);
00124     }
00125     Expr initValue(Type type, Object context) {
00126     return (context instanceof String) ?
00127         type.defaultVal() : ((StateVar)context).getInitVal();
00128     }
00129     void simpleInit(Type type, Object o) 
00130     {
00131     initValue(type,o).apply(spinTrans);
00132     CaseNode value = (CaseNode)spinTrans.getResult();
00133     ExprNode leaf = (ExprNode) value.getCase(SpinTrans.normal);
00134     if (! leaf.expr1.equals("0"))
00135         spinTrans.println(varName(o) + " = " + leaf.expr1 + ";");
00136     }
00137     String varName(Object context) {
00138     return (context instanceof String) ? 
00139         (String) context : ((StateVar)context).getName();
00140     }
00141 }

Generated at Thu Feb 7 06:55:32 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001