org.vishia.java2C
Class Docu.F_Translation_Secondpass

java.lang.Object
  extended by org.vishia.java2C.Docu.F_Translation_Secondpass
Enclosing class:
Docu

public class Docu.F_Translation_Secondpass
extends java.lang.Object

This chapter describes details of translation.


Constructor Summary
Docu.F_Translation_Secondpass()
           
 
Method Summary
 void f1_referencesOrEnvironmentTypes()
          In Java a environment-type for a static method and a reference of a dynamic method are not differenced.
 void f2_casting()
          There are four types of castings, it is the combination of implicitly or explicitly casts, casts of scalar types (int etc) or reference types.
 void f3_methodCall_WithParameterSensitivity()
          Methods in Java are parameter sensitive.
 void f4_methodCallWithVariableArguments()
          Variable arguments are known both in Java and in C.
 void f5_methodCallFromSuperClasses()
           
 void f6_methodCallFromOuterClasses()
           
 void f7_constructorCall()
          A constructor is either a static method of the associated class, or it is understand as a non-static method of the outer class, if the class is an non-static inner class.
 void f9_intension_of_call()
          The translation process of details should know informations about its environment of call.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Docu.F_Translation_Secondpass

public Docu.F_Translation_Secondpass()
Method Detail

f1_referencesOrEnvironmentTypes

public void f1_referencesOrEnvironmentTypes()
In Java a environment-type for a static method and a reference of a dynamic method are not differenced. Both are written with a dot as separator:
 Arrays.binarySearch(..) //an example of static method of class Arrays
 myInstance.method(...)  //a normal class method call.
 
The difference is detectable only searching the identifier in translator tables. Therefore the identifier is tested in the method in StatementBlock#gen_reference(String[], ZbnfParseResultItem, LocalIdents, char, IdentInfos[] retIdentInfo). The got association String from ZBNF-parse-result is tested calling LocalIdents.getType(String, LocalIdents) in the given local environment. It includes especially standard types, but also local types in the class definition and the file-level types (package and import). If the association String is recognized as Type, the associated type is used in the next reference levels respectively it is returned in parameter retIdent. Because that type is the base type info for searching the referenced identifier, the method calls in C are translated with the name_Type-info.


f2_casting

public void f2_casting()
There are four types of castings, it is the combination of The casts of scalar types is equal in C and Java: Cast to a type with higher precision may be implicitly, the cast to a type with less precision need s a explicitly cast. In Java it is necessary, in C it is a warning of compiler mostly, but it should done.

The casts of reference types may be separated in two cases: The downcast may be implicit or explicit written in Java, both is correct. The compiler knows all possibilities of down-casting, tests and accepts it. In C a downcast have to be programmed with an access to the correct and given super class or interface. A simple pointer cast is the second properly variant. A pointer cast is always a possible sourse of errors. Therefore it should be avoid to use if it isn't necessary. An access to a super class can be write with &ref->base.super.base.super instead an equivalent (SuperType*)(ref). The machine code is equal for both variants.

A upcast is written explicitly also in Java. A upcast is correct if a reference given as super-class or interface reference references really a instance which is from the casted type. In Java a runtime check is execute. The demand of upcast from programmer is an assumption. Therefore it should be done explicitely. In C the adequate construct should be a cast also. Another way of solution isn't able. But the C runtime doesn't check the legitimacy of that cast. A goal to explain the cast in a safety software check session is, the code is tested in Java. Another possibility may be, also execute a instance type test. The second decision needs some calculation time, it's the same problem as check the array indices for IndexOutOfBoundsException. The decision in Java2C for this problem is: If a failed access is able to assume, the programmer should be write a instance type test in Java explicitly. It is assert(ref instance of Type);. It will be translated and executed in C too, and it is conspicuously and should sufficing for a safety software check.

For Java2C-translation the check of cast-ability and the production of a cast expression is necessary for some cases: Another problem is the adaption of access. In Java only either primitive types or references are addressed in a variable or with an expression. In C at least the embedded instances needs a special handling, the reference operator &ref->embeddedInstance. Additionally there are some special cases like a StringJc or OS_TimeStamp, which are taken as value arguments and returned as values in methods.

The method FieldData#testAndcast(CCodeData) produces the correct expression with given C-code including type and access mode of it in respect to a given field-type-description. It is used at any assignments: method arguments, return value preparation and value assignments. At example in Java it is coded ref-embeddedInstance, but a parameter or destination variable is of an base or interface type. Than the method returns the necessary adaption for C language in form &(ref->embeddedInstance.base.IfcType).

The methods ClassData.addCastFromType(ClassData, String, String) and ClassData.addCastToType(ClassData, String, String) adds a possibility of cast to the a class. It is used explicitely for standard types, but also for declaring the possibility of access super and interface types, see Docu.SuperClassesAndInterfaces. It is called inside ClassData.fillMethodsOverrideable(ClassData.InheritanceInfo inheritanceInfo, String sPathToMtbl, String sPathToBase)


f3_methodCall_WithParameterSensitivity

public void f3_methodCall_WithParameterSensitivity()
Methods in Java are parameter sensitive. This feature is known also as overloading of methods. It means, that an method of class StringBuilder append(int value) is another method as append(String value). It is clear for Java users. Okay. In C++ it is the same concept.

But in C, the methods should have an unambiguously name. Therefore, the same steps have to be done which were done by a javac-compiler: This steps are done in Java2C-translation time. The result is visible in the generated C-code. This steps are done only if ambiguously method with the same names are exist. If only unambiguously method names in one class are given, a simple rule is used to build an unambiguously method name common spanned: methodname_classname.


For some lang and util-classes the methods have their fix names in the [[CRuntimeJavalike]]-C-Library, that is also useable for C-level-programming. Thats why, and also to support a testing at C-level the names of methods considering parameter types should be shortly, readable and understandable

The translation of Java method name plus parameter types to that C- method names are given in a translation table. There is no common fix rule to built it. But for method names of the users Java code to translate to C a rule is given.

The following things are implemented in the Java2C-Translator:


f4_methodCallWithVariableArguments

public void f4_methodCallWithVariableArguments()
Variable arguments are known both in Java and in C. In C the handling of variable arguments is more simple-primitive. The type of the arguments should be described somewhere else. The typical known application of them is printf. Hereby the type of arguments are described in the printf-text combined with the format specification, at example printf("values: %3.3f, %4d\n", floatValue, intValue);. It is classic. Because the type of values may not correct (float or double, long, short), the compiler may convert a float value in double etc. to prevent errors. The originally idea was to determine the fine specification of the type with modifiers such as %3hd for short. But already float and double are not possible to difference. Some compilers operates variedly. That is C/C++.

In Java the problem of a printf-like formatting is some more separated from the variable argument handling. Variable arguments are accepted as array of the same types. Because any complex type can downcast to the base Type Object, and any simple type has a representation as Object (int as class Integer etc.), any argument can be provided as an Object:
   method(Object ...){...} //declaration of a method
   method(5, 3.4, intVal, classInstance); //call of method.
 
Java2C generated a type String as argument before a variable argument list in any case. The type String contains the detect type of any actual argument as one letter. Especially the primitive and some Standard types are detect in this way. All complex types are supplied as ObjectJc. Therefore the method from example above has the following representation in C:
   method(char const* typeArgs, ...);   //declaration of method
   method("IDIL", 5, 3.4, intVal, classInstance); //call
 
...TODO


f5_methodCallFromSuperClasses

public void f5_methodCallFromSuperClasses()

f6_methodCallFromOuterClasses

public void f6_methodCallFromOuterClasses()

f7_constructorCall

public void f7_constructorCall()
A constructor is either a static method of the associated class, or it is understand as a non-static method of the outer class, if the class is an non-static inner class. The constructor is searched calling ClassData#searchMethod(String, java.util.List, boolean), where the name is To difference whether or not the associated class is a non-static-inner one, the property ClassData.isNonStaticInner is checked. This rules are regarded in The correct constructor is found depending on its parameter, which are given as second param while calling ClassData#searchMethod(String, java.util.List, boolean).


f9_intension_of_call

public void f9_intension_of_call()
The translation process of details should know informations about its environment of call. At example it is a difference whether a expression is evaluated for an assignment or to provide an actual parameter. Therefore a parameter named 'intension' is used often.
Ccmbzf=RrelaAtisPY-method
-------------------SecondPass.gen_statementBlock(org.vishia.zbnf.ZbnfParseResultItem, int, org.vishia.java2C.StatementBlock, FieldData, char)
-cmb-f-----aA------SecondPass#gen_variableDefinition(org.vishia.zbnf.ZbnfParseResultItem, org.vishia.zbnf.ZbnfParseResultItem, LocalIdents, java.util.List, char)
C--------------s---GenerateClass#gen_AnonymousClass(org.vishia.zbnf.ZbnfParseResultItem, LocalIdents, String, char, GenerateFile)
Ccmb------------P-- FirstPass.buildType(StringBuilder, String, GenerateFile, String, String, String, String, org.vishia.zbnf.ZbnfParseResultItem, boolean, String, ClassData, java.util.List, boolean, ClassData, char)
Ccmb------------PYx ClassData#ClassData(String, GenerateFile, String, String, String, String, String, char, ClassData, ClassData, ClassData[], org.vishia.zbnf.ZbnfParseResultItem, String, char, LocalIdents)