001/****************************************************************************
002 * Copyright/Copyleft:
003 *
004 * For this source the LGPL Lesser General Public License,
005 * published by the Free Software Foundation is valid.
006 * It means:
007 * 1) You can use this source without any restriction for any desired purpose.
008 * 2) You can redistribute copies of this source to everybody.
009 * 3) Every user of this source, also the user of redistribute copies
010 *    with or without payment, must accept this license for further using.
011 * 4) But the LPGL is not appropriate for a whole software product,
012 *    if this source is only a part of them. It means, the user
013 *    must publish this part of source,
014 *    but don't need to publish the whole source of the own product.
015 * 5) You can study and modify (improve) this source
016 *    for own using or for redistribution, but you have to license the
017 *    modified sources likewise under this LGPL Lesser General Public License.
018 *    You mustn't delete this Copyright/Copyleft inscription in this source file.
019 *
020 * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org
021 * @version 0.93 2011-01-05  (year-month-day)
022 *******************************************************************************/ 
023package org.vishia.java2C;
024
025/**This is a helper class to return a peace of c-code with associated type information.
026 * 
027 */
028public class CCodeData
029{
030  /**The generated code snippet for this expression. 
031   * This information is not important for further using, only as return string in gen_-routines.
032   * Therefore it may be deleted using removeCode();
033   */
034  String cCode;
035  
036  
037  /**If true, than the cCode represents a setting of a temporary reference. 
038   * The reference may be used as reference. It is the last used temporary reference.
039   * The result code should be consist of the setting of the temporary reference, with following
040   * comma separator of expressions. This strategy is used to disentangle the nesting of method calls
041   * in paramter for concatenated calls in Java.
042   * 
043   */
044  String sTempRef;
045  //boolean bUseTempRef;
046  
047  
048  /**If true, than the cCode represents a method call, which returned the same reference
049   * as used for call, it means the method returns this. This information can be used for concatenation,
050   * the reference needn't be taken from the return value. 
051   */
052  //final boolean bReturnThis;
053  private final char returnMode;
054  
055  boolean isReturnThis(){ return returnMode == 't'; }
056  boolean isReturnNew(){ return returnMode == 'n'; }
057  
058  char getReturnMode(){ return returnMode; }
059  
060  
061  /**The access mode may be changed in comparison with identInfos, if a array element is accessed
062   * in this code snippet. This information should be used for evaluating the kind of code snippet. 
063   */
064  char modeAccess;
065  
066  /**This is the identInfo of the basicly used variable or value type {@link ClassData#classTypeInfo}.
067   * Therefore the information about the type of the code snipped maybe alternating,
068   * <ul><li>at ex. <code>var</code> is an array variable with its identinfo here,
069   *         but the code snipped contains var[2], therefore the {@link LocalIdents.IdentInfos#dimensionArrayOrFixSize}
070   *         doesnot match to the codesnipped. See {@link #dimensionArrayOrFixSize} of this class.
071   *     <li>at ex. <code>{ 1, 2, 3}</code> is a result of a &lt;constArray> in Java2C.zbnf,
072   *         but its identInfo here is of the basicly type of the elements.     
073   */
074  final FieldData identInfo;
075  
076  /** 0 if the value in the code snipped is scalar, 1.. if it is an array. 
077   * The dimensionArrayOrFixSize may be changed in comparison with identInfos, if a array element is accessed
078   * in this code snippet. This information should be used for evaluating the kind of code snippet.
079   */
080  final int dimensionArrayOrFixSize;
081  
082  /**Initializes the detail information {@link #modeAccess} and {@link #dimensionArrayOrFixSize}
083   * with the same values as identInfo.
084   * @param cCode code snipped
085   * @param identInfo the type and access infos to the code snipped.
086   */
087  CCodeData(String cCode, FieldData identInfo)
088  { this(cCode, identInfo, identInfo.modeAccess);
089  }
090  
091  /**Initializes the detail information {@link #modeAccess} and {@link #dimensionArrayOrFixSize}
092   * with the same values as identInfo.
093   * @param cCode code snipped
094   * @param identInfo the type and access infos to the code snipped.
095   */
096  CCodeData(String cCode, FieldData identInfo, char modeAccess)
097  { this(cCode, identInfo, modeAccess
098        , identInfo.modeArrayElement=='B'  ? identInfo.getFixSizeStringBuffer(): identInfo.getDimensionArray()
099        , '.');
100  }
101  
102  
103  /**Initializes the detail information {@link #modeAccess} and {@link #dimensionArrayOrFixSize}
104   * with the same values as identInfo.
105   * @param cCode code snipped
106   * @param identInfo the type and access infos to the code snipped.
107   */
108  CCodeData(String cCode, FieldData identInfo, char modeAccess, char returnMode) //boolean bReturnThis)
109  { this(cCode, identInfo, modeAccess
110        , identInfo.modeArrayElement=='B'  ? identInfo.getFixSizeStringBuffer(): identInfo.getDimensionArray()
111        , returnMode);
112            //, bReturnThis);
113  }
114  
115  
116  /**Initializes the detail information {@link #modeAccess} and {@link #dimensionArrayOrFixSize}
117   * in a special kind.
118   * @param cCode code snipped
119   * @param identInfo the type and access infos to the code snipped.
120   * @param modeAccess
121   * @param dimensionArrayOrFixSize
122   */
123  CCodeData(String cCode, FieldData identInfo, char modeAccess, int dimensionArray )
124  { this(cCode, identInfo, modeAccess, dimensionArray, '.');
125  }
126  
127  
128  /**Initializes the detail information {@link #modeAccess} and {@link #dimensionArrayOrFixSize}
129   * in a special kind.
130   * @param cCode code snipped
131   * @param identInfo the type and access infos to the code snipped.
132   * @param modeAccess
133   * @param dimensionArrayOrFixSize
134   */
135  CCodeData(String cCode, FieldData identInfo, char modeAccess, int dimensionArray, char returnMode) //boolean bReturnThis )
136  { //super(cCode, identInfo == null ? null : identInfo.typeClazz);
137    this.cCode = cCode; 
138    //this.type = identInfo == null ? null : identInfo.typeClazz; 
139    this.modeAccess = modeAccess; 
140    this.dimensionArrayOrFixSize = (byte)dimensionArray; 
141    this.identInfo = identInfo;
142    this.returnMode = returnMode;
143    //this.bReturnThis = bReturnThis;
144  }
145  
146  
147  /**Tests and generates a cast if a cast to the destination form is necessary.
148   * <br>
149   * If it is a static array, a <code>{ CONST_ObjectJc(...), fixArraySize, 0, cCode }</code> is generated
150   * to initialize a static array.
151   * <br>
152   * Otherwise {@link ClassData#testAndcast(ClassData, String, char)} is called 
153   * for the given {@link FieldData#typeClazz} from parameter <code>dstIdentInfo</code>
154   *  
155   * @param dstIdentInfo The type of necessity appearance of the code snipped, 
156   *        enclosing the representation of the type. 
157   * @param intension see {@link ClassData#testAndcast(ClassData, String, char)}.
158   * @return The original {@link cCode} or the casted form.
159   */
160  private String xxxtestAndCast(FieldData dstIdentInfo, char intension)
161  { String ret = "";
162    if("Ss".indexOf(dstIdentInfo.modeStatic) >=0)
163    { if(dimensionArrayOrFixSize ==1 && modeAccess == '%' && dstIdentInfo.modeAccess == '$')
164      { //given is a simple immediate array, but requested is a embedded array struct:
165        ret = "{ CONST_ObjectJc(0, &" + dstIdentInfo.getTypeName() + "_" + dstIdentInfo.getTypeName() + ", null), " 
166            + dstIdentInfo.fixArraySizes[0] 
167            + ", sizeof(" + dstIdentInfo.getTypeName() + "), 0, " 
168            + cCode + "}";  
169        
170      }
171      else ret = dstIdentInfo.typeClazz.xxxtestAndcast(identInfo.typeClazz, cCode, intension); //no casting      
172    }
173    else
174    {
175      ret = dstIdentInfo.typeClazz.xxxtestAndcast(identInfo.typeClazz, cCode, intension);
176    }
177    return ret;
178  }
179  
180  /**Returns the String representation of the type of the code snipped, 
181   * detected from the {@link #identInfo} and its {@link FieldData#typeClazz}. */
182  public String getTypeName(){ return identInfo.typeClazz.getClassIdentName(); }
183  
184  
185  /**Returns all class level idents, if the code snipped is a reference of a class type.
186   * Especially it able to use, if the code snipped is a reference to a method or field
187   * of another class.
188   */
189  public LocalIdents getClassLevelIdents()
190  { return identInfo.typeClazz.classLevelIdents;
191  }
192
193  /**Returns the name of the Headerfile, which defines the class, 
194   * if the code snipped is a reference of a class type.
195   * Especially it able to use, if the code snipped is a reference to a method or field
196   * of another class.
197   */
198  public String getTypeHeaderfilename()
199  { return identInfo.typeClazz.sFileName;
200  }
201  
202  
203  public String toString()
204  { return cCode + ":" + modeAccess + identInfo.toString(); }
205  
206}
207