Salome HOME
Initial Script version
[tools/hxx2salome.git] / scripts / parse3.awk
1 # This awk program contains the type mapping tables - and the treatments
2 # for code generation
3 #
4 BEGIN { 
5 #
6 # file name genaration
7   idl_file="code_idl"
8   hxx_file="code_hxx"
9   cxx_file="code_cxx"
10   class_i=class_name"_i"
11   print "\t// genetated part" > idl_file
12   printf "    // genetated part\n" > hxx_file
13   printf "//\n// genetated part\n//\n" > cxx_file
14   print "Functions parsing (for debug)" > "parse_result"
15 #
16 #
17 # type mapping from c++ componant to idl
18 #
19   idl_arg_type["int"]="in long"
20   idl_arg_type["double"]="in double"
21   idl_arg_type["float"]="in float"
22   idl_arg_type["long"]="in long"
23   idl_arg_type["short"]="in short"
24   idl_arg_type["unsigned"]="in unsigned long"
25   idl_arg_type["const char*"]="in string"
26   idl_arg_type["const std::string&"]="in string"
27   idl_arg_type["int&"]="out long"
28   idl_arg_type["double&"]="out double"
29   idl_arg_type["float&"]="out float"
30   idl_arg_type["long&"]="out long"
31   idl_arg_type["short&"]="out short"
32   idl_arg_type["unsigned&"]="out unsigned long"
33   idl_arg_type["std::string&"]="out string"
34   idl_arg_type["const MEDMEM::MESH&"]="in SALOME_MED::MESH"
35   idl_arg_type["const MEDMEM::MESH*"]="in SALOME_MED::MESH"
36   idl_arg_type["const MEDMEM::FIELD<double>*"]="in SALOME_MED::FIELDDOUBLE"
37   idl_arg_type["const MEDMEM::FIELD<double>&"]="in SALOME_MED::FIELDDOUBLE"
38   idl_arg_type["const std::vector<double>&"]="in SALOME::SenderDouble"
39   idl_arg_type["MEDMEM::FIELD<double>*&"]="out SALOME_MED::FIELDDOUBLE"
40   idl_arg_type["const MEDMEM::FIELD<int>*"]="in SALOME_MED::FIELDINT"
41   idl_arg_type["const MEDMEM::FIELD<int>&"]="in SALOME_MED::FIELDINT"
42   idl_arg_type["const std::vector<int>&"]="in SALOME::SenderInt"
43   idl_arg_type["MEDMEM::FIELD<int>*&"]="out SALOME_MED::FIELDINT"
44 #
45 #
46 # mapping for returned types
47 #
48   idl_rtn_type["void"]="void"
49   idl_rtn_type["int"]="long"
50   idl_rtn_type["double"]="double"
51   idl_rtn_type["float"]="float"
52   idl_rtn_type["long"]="long"
53   idl_rtn_type["short"]="short"
54   idl_rtn_type["unsigned"]="unsigned long"
55   idl_rtn_type["const char*"]="string"
56   idl_rtn_type["char*"]="string"
57   idl_rtn_type["std::string"]="string"
58   idl_rtn_type["const MEDMEM::MESH&"]="SALOME_MED::MESH"
59   idl_rtn_type["MEDMEM::MESH&"]="SALOME_MED::MESH"
60   idl_rtn_type["MEDMEM::MESH*"]="SALOME_MED::MESH"
61   idl_rtn_type["const MEDMEM::MESH*"]="SALOME_MED::MESH"
62   idl_rtn_type["const MEDMEM::FIELD<double>*"]="SALOME_MED::FIELDDOUBLE"
63   idl_rtn_type["MEDMEM::FIELD<double>*"]="SALOME_MED::FIELDDOUBLE"
64   idl_rtn_type["MEDMEM::FIELD<double>&"]="SALOME_MED::FIELDDOUBLE"
65   idl_rtn_type["const MEDMEM::FIELD<double>&"]="SALOME_MED::FIELDDOUBLE"
66   idl_rtn_type["std::vector<double>*"]="SALOME::SenderDouble"
67   idl_rtn_type["const MEDMEM::FIELD<int>*"]="SALOME_MED::FIELDINT"
68   idl_rtn_type["MEDMEM::FIELD<int>*"]="SALOME_MED::FIELDINT"
69   idl_rtn_type["MEDMEM::FIELD<int>&"]="SALOME_MED::FIELDINT"
70   idl_rtn_type["const MEDMEM::FIELD<int>&"]="SALOME_MED::FIELDINT"
71   idl_rtn_type["std::vector<int>*"]="SALOME::SenderInt"
72 #
73 #
74 # Corba mapping table (for argument's types and returned types)
75 #
76 # CNC commented types are not yet implemented
77   idl_impl_hxx["in long"]="CORBA::Long"
78   idl_impl_hxx["in double"]="CORBA::Double"
79   idl_impl_hxx["in float"]="CORBA::Float"
80   idl_impl_hxx["in short"]="CORBA::Short"
81   idl_impl_hxx["in unsigned long"]="CORBA::ULong"
82   idl_impl_hxx["in string"]="const char*"
83   idl_impl_hxx["out long"]="CORBA::Long_out"
84   idl_impl_hxx["out double"]="CORBA::Double_out"
85   idl_impl_hxx["out float"]="CORBA::Float_out"
86   idl_impl_hxx["out short"]="CORBA::Short_out"
87   idl_impl_hxx["out unsigned long"]="CORBA::ULong_out"
88   idl_impl_hxx["out string"]="CORBA::String_out"
89   idl_impl_hxx["in SALOME_MED::MESH"]="SALOME_MED::MESH_ptr"
90   idl_impl_hxx["in SALOME_MED::FIELDDOUBLE"]="SALOME_MED::FIELDDOUBLE_ptr"
91   idl_impl_hxx["in SALOME::SenderDouble"]="SALOME::SenderDouble_ptr"
92   idl_impl_hxx["out SALOME_MED::FIELDDOUBLE"]="SALOME_MED::FIELDDOUBLE_out"
93   idl_impl_hxx["in SALOME_MED::FIELDINT"]="SALOME_MED::FIELDINT_ptr"
94   idl_impl_hxx["in SALOME::SenderInt"]="SALOME::SenderInt_ptr"
95   idl_impl_hxx["out SALOME_MED::FIELDINT"]="SALOME_MED::FIELDINT_out"
96   idl_impl_hxx["void"]="void"
97   idl_impl_hxx["long"]="CORBA::Long"
98   idl_impl_hxx["double"]="CORBA::Double"
99   idl_impl_hxx["unsigned long"]="CORBA::ULong"
100   idl_impl_hxx["string"]="char*"
101   idl_impl_hxx["SALOME_MED::MESH"]="SALOME_MED::MESH_ptr"
102   idl_impl_hxx["SALOME_MED::FIELDDOUBLE"]="SALOME_MED::FIELDDOUBLE_ptr"
103   idl_impl_hxx["SALOME::SenderDouble"]="SALOME::SenderDouble_ptr"
104   idl_impl_hxx["SALOME_MED::FIELDINT"]="SALOME_MED::FIELDINT_ptr"
105   idl_impl_hxx["SALOME::SenderInt"]="SALOME::SenderInt_ptr"
106 #
107 #
108 # table for c++ code generation : argument's processing
109 #
110   cpp_impl_a["int"]="\tint _%s(%s);\n"
111   cpp_impl_a["double"]="\tdouble _%s(%s);\n"
112   cpp_impl_a["float"]="\tfloat _%s(%s);\n"
113   cpp_impl_a["long"]="\tlong _%s(%s);\n"
114   cpp_impl_a["short"]="\tshort _%s(%s);\n"
115   cpp_impl_a["unsigned"]="\tunsigned _%s(%s);\n"
116   cpp_impl_a["const char*"]="\tconst char* _%s(%s);\n"
117   cpp_impl_a["const std::string&"]="\tconst std::string _%s(%s);\n"
118   cpp_impl_a["int&"]="\tint _%s;\n"
119   cpp_impl_a["double&"]="\tdouble _%s;\n"
120   cpp_impl_a["float&"]="\tfloat _%s;\n"
121   cpp_impl_a["long&"]="\tlong _%s;\n"
122   cpp_impl_a["short&"]="\tshort _%s;\n"
123   cpp_impl_a["unsigned&"]="\tunsigned _%s;\n"
124   cpp_impl_a["std::string&"]="std::string _%s;\n"
125   cpp_impl_a["const MEDMEM::MESH&"]="\tMEDMEM::MESHClient* _%s = new MEDMEM::MESHClient(%s);\n" # MESHClient cannot be created on the stack (private constructor), so we create it on the heap and dereference it later (in treatment 4)
126   cpp_impl_a["const MEDMEM::MESH*"]="\tMEDMEM::MESHClient* _%s = new MEDMEM::MESHClient(%s);\n"
127   cpp_impl_a["const MEDMEM::FIELD<double>*"]="\tstd::auto_ptr<MEDMEM::FIELD<double> > _%s ( new MEDMEM::FIELDClient<double>(%s) );\n"
128   cpp_impl_a["const MEDMEM::FIELD<double>&"]="\tMEDMEM::FIELDClient<double> _%s(%s);\n"
129   cpp_impl_a["const std::vector<double>&"]="\tlong _size;\n\tdouble *_value = ReceiverFactory::getValue(%s,_size);\n"\
130              "\tstd::vector<double> _%s(_value,_value+_size);\n\tdelete [] _value;"
131   cpp_impl_a["MEDMEM::FIELD<int>*&"]="\tMEDMEM::FIELD<int>* _%s;\n"
132   cpp_impl_a["const MEDMEM::FIELD<int>*"]="\tstd::auto_ptr<MEDMEM::FIELD<int> > _%s ( new MEDMEM::FIELDClient<int>(%s) );\n"
133   cpp_impl_a["const MEDMEM::FIELD<int>&"]="\tMEDMEM::FIELDClient<int> _%s(%s);\n"
134   cpp_impl_a["const std::vector<int>&"]="\tlong _size;\n\tint *_value = ReceiverFactory::getValue(%s,_size);\n"\
135              "\tstd::vector<int> _%s(_value,_value+_size);\n\tdelete [] _value;"
136 #
137 #
138 # table for c++ code generation : returned value processing
139 #
140   cpp_impl_b["void"]=""
141   cpp_impl_b["int"]="\tCORBA::Long _rtn_ior(_rtn_cpp);\n"
142   cpp_impl_b["double"]="\tCORBA::Double _rtn_ior(_rtn_cpp);\n"
143   cpp_impl_b["float"]="\tCORBA::Float _rtn_ior(_rtn_cpp);\n"
144   cpp_impl_b["long"]="\tCORBA::Long _rtn_ior(_rtn_cpp);\n"
145   cpp_impl_b["short"]="\tCORBA::Short _rtn_ior(_rtn_cpp);\n"
146   cpp_impl_b["unsigned"]="\tCORBA::ULong _rtn_ior(_rtn_cpp);\n"
147   cpp_impl_b["const char*"]="\tchar* _rtn_ior = CORBA::string_dup(_rtn_cpp);\n"
148   cpp_impl_b["char*"]="\tchar* _rtn_ior(_rtn_cpp);\n"
149   cpp_impl_b["std::string"]="\tchar* _rtn_ior=CORBA::string_dup(_rtn_cpp.c_str());\n"
150              "\tstd::copy(_rtn_cpp.begin(),_rtn_cpp.end(),_rtn_ior);\n"    
151   cpp_impl_b["const MEDMEM::MESH&"]=\
152              "\tMEDMEM::MESH_i * _rtn_mesh_i = new MEDMEM::MESH_i(const_cast<MEDMEM::MESH*>(&_rtn_cpp));\n"\
153              "\tSALOME_MED::MESH_ptr _rtn_ior = _rtn_mesh_i->_this();\n"
154   cpp_impl_b["MEDMEM::MESH&"]=\
155              "\tMEDMEM::MESH_i * _rtn_mesh_i = new MEDMEM::MESH_i(&_rtn_cpp);\n"\
156              "\tSALOME_MED::MESH_ptr _rtn_ior = _rtn_mesh_i->_this();\n"
157   cpp_impl_b["MEDMEM::MESH*"]=\
158              "\tMEDMEM::MESH_i * _rtn_mesh_i = new MEDMEM::MESH_i(_rtn_cpp);\n"\
159              "\tSALOME_MED::MESH_ptr _rtn_ior = _rtn_mesh_i->_this();\n"
160   cpp_impl_b["const MEDMEM::MESH*"]=\
161              "\tMEDMEM::MESH_i * _rtn_mesh_i = new MEDMEM::MESH_i(const_cast<MEDMEM::MESH*>(_rtn_cpp));\n"\
162              "\tSALOME_MED::MESH_ptr _rtn_ior = _rtn_mesh_i->_this();\n"
163   cpp_impl_b["const MEDMEM::FIELD<double>*"]=\
164              "\tMEDMEM::FIELDDOUBLE_i * _rtn_field_i = new MEDMEM::FIELDDOUBLE_i(const_cast<MEDMEM::FIELD<double>*>(_rtn_cpp),false);\n"\
165              "\tSALOME_MED::FIELDDOUBLE_ptr _rtn_ior = _rtn_field_i->_this();\n"
166   cpp_impl_b["MEDMEM::FIELD<double>*"]=\
167              "\tMEDMEM::FIELDDOUBLE_i * _rtn_field_i = new MEDMEM::FIELDDOUBLE_i(_rtn_cpp,true);\n"\
168              "\tSALOME_MED::FIELDDOUBLE_ptr _rtn_ior = _rtn_field_i->_this();\n"
169   cpp_impl_b["MEDMEM::FIELD<double>&"]=\
170              "\tMEDMEM::FIELDDOUBLE_i * _rtn_field_i = new MEDMEM::FIELDDOUBLE_i(&_rtn_cpp,false);\n"\
171              "\tSALOME_MED::FIELDDOUBLE_ptr _rtn_ior = _rtn_field_i->_this();\n"
172   cpp_impl_b["const MEDMEM::FIELD<double>&"]=\
173              "\tMEDMEM::FIELDDOUBLE_i * _rtn_field_i = new MEDMEM::FIELDDOUBLE_i(const_cast<MEDMEM::FIELD<double>*>(&_rtn_cpp),false);\n"\
174              "\tSALOME_MED::FIELDDOUBLE_ptr _rtn_ior = _rtn_field_i->_this();\n"
175   cpp_impl_b["std::vector<double>*"]=\
176              "\tSALOME::SenderDouble_ptr _rtn_ior = SenderFactory::buildSender(*this,&(*_rtn_cpp)[0],(*_rtn_cpp).size(),true);\n"
177   cpp_impl_b["const MEDMEM::FIELD<int>*"]=\
178              "\tMEDMEM::FIELDINT_i * _rtn_field_i = new MEDMEM::FIELDINT_i(const_cast<MEDMEM::FIELD<int>*>(_rtn_cpp),false);\n"\
179              "\tSALOME_MED::FIELDINT_ptr _rtn_ior = _rtn_field_i->_this();\n"
180   cpp_impl_b["MEDMEM::FIELD<int>*"]=\
181              "\tMEDMEM::FIELDINT_i * _rtn_field_i = new MEDMEM::FIELDINT_i(_rtn_cpp,true);\n"\
182              "\tSALOME_MED::FIELDINT_ptr _rtn_ior = _rtn_field_i->_this();\n"
183   cpp_impl_b["MEDMEM::FIELD<int>&"]=\
184              "\tMEDMEM::FIELDINT_i * _rtn_field_i = new MEDMEM::FIELDINT_i(&_rtn_cpp,false);\n"\
185              "\tSALOME_MED::FIELDINT_ptr _rtn_ior = _rtn_field_i->_this();\n"
186   cpp_impl_b["const MEDMEM::FIELD<int>&"]=\
187              "\tMEDMEM::FIELDINT_i * _rtn_field_i = new MEDMEM::FIELDINT_i(const_cast<MEDMEM::FIELD<int>*>(&_rtn_cpp),false);\n"\
188              "\tSALOME_MED::FIELDINT_ptr _rtn_ior = _rtn_field_i->_this();\n"
189   cpp_impl_b["std::vector<int>*"]=\
190              "\tSALOME::SenderInt_ptr _rtn_ior = SenderFactory::buildSender(*this,&(*_rtn_cpp)[0],(*_rtn_cpp).size(),true);\n"
191
192 #
193 #
194 # table for c++ code generation : out parameters processing and removeRef for reference counted objects
195 #
196   cpp_impl_c["MEDMEM::FIELD<double>*&"]=\
197              "\tMEDMEM::FIELDDOUBLE_i * %s_ior = new MEDMEM::FIELDDOUBLE_i(_%s, true);\n"\
198              "\t%s = %s_ior->_this();\n"
199   cpp_impl_c["MEDMEM::FIELD<int>*&"]=\
200              "\tMEDMEM::FIELDINT_i * %s_ior = new MEDMEM::FIELDINT_i(_%s, true);\n"\
201              "\t%s = %s_ior->_this();\n"
202   cpp_impl_c["std::string&"]="\t%s = CORBA::string_dup(_%s.c_str());\n"
203   cpp_impl_c["int&"]="\t%s = _%s"
204   cpp_impl_c["double&"]="\t%s = _%s"
205   cpp_impl_c["float&"]="\t%s = _%s"
206   cpp_impl_c["long&"]="\t%s = _%s"
207   cpp_impl_c["short&"]="\t%s = _%s"
208   cpp_impl_c["unsigned&"]="\t%s = _%s"
209   cpp_impl_c["const MEDMEM::MESH&"]="\t_%s->removeReference();\n"
210   cpp_impl_c["const MEDMEM::MESH*"]="\t_%s->removeReference();\n"
211 #
212 #
213 # record sep is ");\n" whith blanks all around, and optional "(" at the beginning
214   RS="[(]?[ \t]*)[ \t]*;[ \t]*\n"  
215   FS="[ \t]*[(,][ \t]*"  # field sep is either "(" or "," surrounded by blanks 
216 }
217
218 # --------------------- treatment 1 ----------------------------------
219 #
220 #  extract from fields types, function name, and argument's names
221 #
222 {
223   print "Function : ",$0 >> "parse_result"  # print for debug
224   for (i=1; i<=NF; i++) {
225       print "\t-> ",i," : ",$i >> "parse_result"
226   }
227   ok1=0;ok=1
228   # check if returned type ($1) is one of the accepted types (idl_rtn_type)
229   for (cpptype in idl_rtn_type) {
230     if ( substr($1,1,length(cpptype)) == cpptype ) {
231       # if compatible, store returned type and function name
232       type[1]=cpptype
233       name[1]=substr($1,length(cpptype)+1)
234       sub("^[ \t]*","",name[1]) # get rid of leading blanks
235       ok1=1
236       break
237     }
238   }
239   ok*=ok1
240   # for each argument ($i), check if it is compatible (belongs to idl_arg_type)
241   for (i=2; i<=NF; i++) {
242     ok2=0
243     split($i,tab,"=") # get rid of default value
244     item=tab[1]
245     for (cpptype in idl_arg_type) {
246        if ( substr(item,1,length(cpptype)) == cpptype ) {
247           # if compatible, store argument type and name
248           type[i]=cpptype
249           name[i]=substr(item,length(cpptype)+1)
250           sub("^[ \t]*","",name[i]) # get rid of leading blanks
251           if ( length(name[i]) == 0 ) # automatic name if argument's name wasn't precised
252              name[i]=sprintf("_arg%d",i-1)
253           ok2=1
254           break
255        }
256     }
257     ok*=ok2 # ok=0 if one of the type is not compatible
258   }
259
260   # print compatibility 
261   if ( $0 !~ class_name ) { # constructor are not considered, but we don't print it
262       if ( ok == 0){ # if one of the c++ type is not compatible
263           printf "     [KO]     :  %s",$0
264       }
265       else
266           printf "     [OK]     :  %s",$0
267         
268       if ( $0 !~ /\(/  ) {
269           printf "(" # if there is no argument, parenthesis was suppressed, so we add it for printing
270       }
271       printf ");\n"
272   }    
273   if ( ok == 0) # pass to the next function if one of the c++ type is not compatible
274       next
275 }
276 #
277 # --------------------- treatment 2 ----------------------------------
278 #
279 #  generate the Corba interface (idl file)
280 #
281
282   printf "\t%s %s(", idl_rtn_type[type[1]],name[1] >> idl_file  # return type and name of function
283   if ( NF >= 2 ){  # if there is arguments, print them
284     for (i=2; i<=NF-1; i++)
285       printf "%s %s,",idl_arg_type[type[i]],name[i] >> idl_file
286     printf "%s %s", idl_arg_type[type[NF]],name[NF] >> idl_file
287   }
288   printf ");\n" >> idl_file
289 }  
290 #
291 # --------------------- treatment 3 ----------------------------------
292 #
293 #  generate the C++ implementation of component (hxx file)
294 #
295
296   printf "    %s %s(",idl_impl_hxx[idl_rtn_type[type[1]]],name[1] >> hxx_file
297   if ( NF >= 2 ){  # if there is arguments, print them
298       for (i=2; i<=NF-1; i++)
299           printf "%s %s,",idl_impl_hxx[idl_arg_type[type[i]]],name[i] >> hxx_file
300       printf "%s %s", idl_impl_hxx[idl_arg_type[type[NF]]],name[NF] >> hxx_file
301   }
302   printf ");\n" >> hxx_file
303 }
304 #
305 # --------------------- treatment 4 ----------------------------------
306 #
307 #  generate the C++ implementation of component (cxx file)
308 #
309 {
310
311
312   func_name=class_name"_i::"name[1]
313   printf "%s %s(",idl_impl_hxx[idl_rtn_type[type[1]]],func_name >> cxx_file
314   if ( NF >= 2 ){  # if there is arguments, print them
315       for (i=2; i<=NF-1; i++)
316           printf "%s %s,",idl_impl_hxx[idl_arg_type[type[i]]],name[i] >> cxx_file
317       printf "%s %s", idl_impl_hxx[idl_arg_type[type[NF]]],name[NF] >> cxx_file
318   }
319   printf ")\n{\n\tbeginService(\"%s\");\n\tBEGIN_OF(\"%s\");\n",func_name,func_name >> cxx_file
320
321   if ( NF >= 2 ){
322       printf "//\tArguments processing\n" >> cxx_file
323       for (i=2; i<=NF; i++)
324           printf cpp_impl_a[type[i]],name[i],name[i] >> cxx_file
325   }
326
327   if ( type[1] == "void" ) # if return type is void, the call syntax is different.
328       printf "//\tCall cpp component\n\tcppCompo_->%s(",name[1] >> cxx_file
329   else
330       printf "//\tCall cpp component\n\t%s _rtn_cpp = cppCompo_->%s(",type[1],name[1] >> cxx_file
331   if ( NF >= 2 ){  # if there is arguments, print them
332       for (i=2; i<=NF; i++) {
333           # special treatment for some arguments
334           post=""
335           pre=""
336           if ( cpp_impl_a[type[i]] ~ "auto_ptr" )
337              post=".get()" # for auto_ptr argument, retrieve the raw pointer behind
338           if ( type[i] == "const MEDMEM::MESH&" )
339              pre="*"  # we cannot create MESHClient on the stack (private constructor), so we create it on the heap and dereference it
340           if ( i < NF )
341              post=post"," # separator between arguments
342           printf " %s_%s%s",pre,name[i],post >> cxx_file
343       }
344   }
345   printf ");\n//\tPost-processing & return\n" >> cxx_file
346   for (i=2; i<=NF; i++)
347       printf cpp_impl_c[type[i]],name[i],name[i],name[i],name[i] >> cxx_file  # process for out parameters
348   printf cpp_impl_b[type[1]] >> cxx_file  # process for returned value
349   printf "\tendService(\"%s\");\n\tEND_OF(\"%s\");\n",func_name,func_name >> cxx_file
350   if ( type[1] != "void" )
351       printf "\treturn _rtn_ior;\n" >> cxx_file
352   printf "}\n\n" >> cxx_file
353 }
354 #
355 #
356 END {
357 # CNC peut ĂȘtre mis dans le template directement printf "\nprivate:\n    std::auto_ptr<%s> cppImpl_;\n",class_name >> hxx_file
358 }