UtilFortran.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /******************************************************************************/
  2. /* */
  3. /* CONV (converter) for Agrif (Adaptive Grid Refinement In Fortran) */
  4. /* */
  5. /* Copyright or or Copr. Laurent Debreu (Laurent.Debreu@imag.fr) */
  6. /* Cyril Mazauric (Cyril_Mazauric@yahoo.fr) */
  7. /* This software is governed by the CeCILL-C license under French law and */
  8. /* abiding by the rules of distribution of free software. You can use, */
  9. /* modify and/ or redistribute the software under the terms of the CeCILL-C */
  10. /* license as circulated by CEA, CNRS and INRIA at the following URL */
  11. /* "http://www.cecill.info". */
  12. /* */
  13. /* As a counterpart to the access to the source code and rights to copy, */
  14. /* modify and redistribute granted by the license, users are provided only */
  15. /* with a limited warranty and the software's author, the holder of the */
  16. /* economic rights, and the successive licensors have only limited */
  17. /* liability. */
  18. /* */
  19. /* In this respect, the user's attention is drawn to the risks associated */
  20. /* with loading, using, modifying and/or developing or reproducing the */
  21. /* software by the user in light of its specific status of free software, */
  22. /* that may mean that it is complicated to manipulate, and that also */
  23. /* therefore means that it is reserved for developers and experienced */
  24. /* professionals having in-depth computer knowledge. Users are therefore */
  25. /* encouraged to load and test the software's suitability as regards their */
  26. /* requirements in conditions enabling the security of their systems and/or */
  27. /* data to be ensured and, more generally, to use and operate it in the */
  28. /* same conditions as regards security. */
  29. /* */
  30. /* The fact that you are presently reading this means that you have had */
  31. /* knowledge of the CeCILL-C license and that you accept its terms. */
  32. /******************************************************************************/
  33. /* version 1.7 */
  34. /******************************************************************************/
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "decl.h"
  39. /******************************************************************************/
  40. /* initdimprob */
  41. /******************************************************************************/
  42. /* This subroutine is used to initialized grid dimension variable */
  43. /******************************************************************************/
  44. void initdimprob(int dimprobmod, const char * nx, const char * ny, const char* nz)
  45. {
  46. dimprob = dimprobmod;
  47. strcpy(nbmaillesX, nx);
  48. strcpy(nbmaillesY, ny);
  49. strcpy(nbmaillesZ, nz);
  50. }
  51. /******************************************************************************/
  52. /* Variableshouldberemoved */
  53. /******************************************************************************/
  54. /* Firstpass 0 */
  55. /******************************************************************************/
  56. /* */
  57. /* Agrif_<toto>(variable) ====> Agrif_<toto>(variable) */
  58. /* */
  59. /******************************************************************************/
  60. int Variableshouldberemoved(const char *nom)
  61. {
  62. return Agrif_in_Tok_NAME(nom);
  63. }
  64. /******************************************************************************/
  65. /* variableisglobal */
  66. /******************************************************************************/
  67. /* This subroutine is to know if a variable is global */
  68. /******************************************************************************/
  69. int variableisglobal(listvar *curvar, listvar *listin)
  70. {
  71. int Globalite;
  72. listvar *newvar;
  73. Globalite = 0;
  74. newvar = listin;
  75. while ( newvar && Globalite == 0 )
  76. {
  77. if ( !strcasecmp(newvar->var->v_nomvar,curvar->var->v_nomvar) )
  78. {
  79. Globalite = 1;
  80. /* Now we should give the definition of the variable in the */
  81. /* table List_UsedInSubroutine_Var */
  82. strcpy(curvar->var->v_typevar, newvar->var->v_typevar);
  83. strcpy(curvar->var->v_dimchar, newvar->var->v_dimchar);
  84. curvar->var->v_nbdim = newvar->var->v_nbdim;
  85. curvar->var->v_dimensiongiven = newvar->var->v_dimensiongiven;
  86. curvar->var->v_allocatable = newvar->var->v_allocatable;
  87. curvar->var->v_target = newvar->var->v_target;
  88. curvar->var->v_catvar = newvar->var->v_catvar;
  89. curvar->var->v_pointerdeclare = newvar->var->v_pointerdeclare;
  90. curvar->var->v_indicetabvars = newvar->var->v_indicetabvars;
  91. strcpy(curvar->var->v_nameinttypename, newvar->var->v_nameinttypename);
  92. strcpy(curvar->var->v_precision, newvar->var->v_precision);
  93. strcpy(curvar->var->v_readedlistdimension, newvar->var->v_readedlistdimension);
  94. strcpy(curvar->var->v_commoninfile, newvar->var->v_commoninfile);
  95. }
  96. else
  97. {
  98. newvar = newvar->suiv;
  99. }
  100. }
  101. return Globalite ;
  102. }
  103. int VariableIsInListCommon(listvar *curvar,listvar *listin)
  104. {
  105. int present;
  106. listvar *newvar;
  107. present = 0;
  108. newvar = listin;
  109. while ( newvar && present == 0 )
  110. {
  111. if ( !strcasecmp(newvar->var->v_nomvar,curvar->var->v_nomvar) &&
  112. !strcasecmp(newvar->var->v_subroutinename, curvar->var->v_subroutinename) )
  113. {
  114. strcpy(curvar->var->v_commoninfile,newvar->var->v_commoninfile);
  115. Merge_Variables(curvar->var,newvar->var);
  116. present = 1;
  117. }
  118. else newvar = newvar->suiv;
  119. }
  120. return present;
  121. }
  122. int VariableIsInList(listvar *curvar,listvar *listin)
  123. {
  124. int present;
  125. listvar *newvar;
  126. present = 0;
  127. newvar = listin;
  128. while ( newvar && present == 0 )
  129. {
  130. if ( !strcasecmp(newvar->var->v_nomvar,curvar->var->v_nomvar) )
  131. {
  132. Merge_Variables(curvar->var,newvar->var);
  133. present = 1;
  134. }
  135. else newvar = newvar->suiv;
  136. }
  137. return present;
  138. }
  139. /******************************************************************************/
  140. /* variableisglobalinmodule */
  141. /******************************************************************************/
  142. /* This subroutine is to know if a variable is global */
  143. /******************************************************************************/
  144. void variableisglobalinmodule(listcouple *listin, const char *module, FILE *fileout, long int oldposcuruse)
  145. {
  146. int Globalite;
  147. listcouple *newvar;
  148. listcouple *newvarprec;
  149. listvar *tempo;
  150. listvar *newvar2;
  151. int out;
  152. char truename[LONG_VNAME];
  153. Globalite = 1;
  154. newvarprec = (listcouple *)NULL;
  155. tempo = (listvar *)NULL;
  156. tempo = Readthedependfile(module,tempo);
  157. newvar = listin;
  158. while ( newvar )
  159. {
  160. if (!strcmp(newvar->c_namepointedvar,"")) {
  161. strcpy(truename,newvar->c_namevar);
  162. }
  163. else
  164. {
  165. strcpy(truename,newvar->c_namepointedvar);
  166. }
  167. out = 0;
  168. newvar2 = tempo;
  169. while ( newvar2 && out == 0 )
  170. {
  171. if ( !strcasecmp(newvar2->var->v_nomvar,truename) ) out = 1;
  172. else newvar2 = newvar2 ->suiv;
  173. }
  174. if ( out == 1 )
  175. {
  176. /* remove from the listin */
  177. if ( newvar == listin )
  178. {
  179. listin = listin->suiv;
  180. newvar = listin;
  181. }
  182. else
  183. {
  184. newvarprec->suiv = newvar->suiv;
  185. newvar = newvar->suiv;
  186. }
  187. }
  188. else
  189. {
  190. newvarprec = newvar;
  191. newvar = newvar->suiv;
  192. Globalite = 0;
  193. }
  194. }
  195. if ( Globalite == 0 || !newvar)
  196. {
  197. pos_end = setposcurname(fileout);
  198. RemoveWordSET_0(fileout,oldposcuruse,pos_end-oldposcuruse);
  199. newvar = listin;
  200. while ( newvar )
  201. {
  202. fprintf(fileout," use %s, only : %s \n",module,newvar->c_namevar);
  203. newvar = newvar->suiv;
  204. }
  205. }
  206. }
  207. void Write_Word_end_module_0()
  208. {
  209. if ( firstpass == 0 )
  210. {
  211. fprintf(fortran_out,"\n end module %s",curmodulename);
  212. }
  213. }
  214. void Add_Subroutine_For_Alloc(const char *nom)
  215. {
  216. listnom *parcours;
  217. listnom *newvar;
  218. int out;
  219. newvar = (listnom*) calloc(1, sizeof(listnom));
  220. strcpy(newvar->o_nom,nom);
  221. newvar->suiv = NULL;
  222. if ( !List_Subroutine_For_Alloc )
  223. {
  224. List_Subroutine_For_Alloc = newvar;
  225. }
  226. else
  227. {
  228. parcours = List_Subroutine_For_Alloc;
  229. out = 0 ;
  230. while ( parcours->suiv && out == 0 )
  231. {
  232. if ( !strcasecmp(parcours->o_nom,nom) ) out = 1 ;
  233. else parcours = parcours ->suiv;
  234. }
  235. /* */
  236. if ( out == 0 )
  237. {
  238. if ( strcasecmp(parcours->o_nom,nom) ) parcours->suiv = newvar;
  239. }
  240. }
  241. }
  242. void Write_Closing_Module(int forend)
  243. {
  244. listvar *parcours;
  245. listnom *parcours_nom;
  246. listnom *parcours_nomprec;
  247. variable *v;
  248. int out = 0;
  249. int headtypewritten = 0;
  250. char ligne[LONG_M];
  251. int changeval;
  252. // Write Global Parameter Declaration
  253. parcours = List_GlobalParameter_Var;
  254. while( parcours )
  255. {
  256. if ( !strcasecmp(parcours->var->v_modulename, curmodulename) )
  257. {
  258. WriteVarDeclaration(parcours->var, module_declar, 0, 1);
  259. }
  260. parcours = parcours -> suiv;
  261. }
  262. // Write Global Type declaration
  263. parcours = List_Global_Var;
  264. while( parcours )
  265. {
  266. v = parcours->var;
  267. if ( !strcasecmp(v->v_modulename, curmodulename) &&
  268. !strcasecmp(v->v_typevar, "type") )
  269. {
  270. if ( headtypewritten == 0 )
  271. {
  272. fprintf(fortran_out, "\n type Agrif_%s\n", curmodulename);
  273. headtypewritten = 1;
  274. }
  275. changeval = 0;
  276. if ( v->v_allocatable )
  277. {
  278. changeval = 1;
  279. v->v_allocatable = 0;
  280. v->v_pointerdeclare = 1;
  281. }
  282. WriteVarDeclaration(v, fortran_out, 0, 0);
  283. if ( changeval )
  284. {
  285. v->v_allocatable = 1;
  286. v->v_pointerdeclare = 0;
  287. }
  288. out = 1;
  289. }
  290. parcours = parcours -> suiv;
  291. }
  292. if (out == 1)
  293. {
  294. fprintf(fortran_out, " end type Agrif_%s\n", curmodulename);
  295. sprintf(ligne, "type(Agrif_%s), dimension(:), allocatable :: Agrif_%s_var",curmodulename, curmodulename);
  296. tofich(fortran_out,ligne,1);
  297. fprintf(fortran_out, " public :: Agrif_%s\n", curmodulename);
  298. fprintf(fortran_out, " public :: Agrif_%s_var\n", curmodulename);
  299. }
  300. // Write NotGridDepend declaration
  301. parcours = List_NotGridDepend_Var;
  302. while( parcours )
  303. {
  304. if ( !strcasecmp(parcours->var->v_modulename,curmodulename) )
  305. {
  306. WriteVarDeclaration(parcours->var, fortran_out, 0, 1);
  307. }
  308. parcours = parcours -> suiv;
  309. }
  310. // Write Alloc_agrif_'modulename' subroutine
  311. parcours_nomprec = (listnom*) NULL;
  312. parcours_nom = List_NameOfModule;
  313. out = 0 ;
  314. while ( parcours_nom && out == 0 )
  315. {
  316. if ( !strcasecmp(curmodulename,parcours_nom->o_nom) ) out = 1;
  317. else parcours_nom = parcours_nom -> suiv;
  318. }
  319. if ( ! out )
  320. {
  321. printf("#\n# Write_Closing_Module : OUT == 0 *** /!\\ ***\n");
  322. printf("# FIXME: POSSIBLE BUG in CONV !!!\n#\n");
  323. }
  324. if ( out )
  325. {
  326. if ( parcours_nom->o_val == 1 )
  327. {
  328. fprintf(fortran_out,"\n public :: Alloc_agrif_%s\n",curmodulename);
  329. }
  330. if ( (forend == 0) || (parcours_nom->o_val == 1) )
  331. {
  332. fprintf(fortran_out,"\n contains\n");
  333. }
  334. if ( parcours_nom->o_val == 1 )
  335. {
  336. fprintf(fortran_out, " subroutine Alloc_agrif_%s(Agrif_Gr)\n", curmodulename);
  337. fprintf(fortran_out, " use Agrif_Util\n");
  338. fprintf(fortran_out, " type(Agrif_grid), pointer :: Agrif_Gr\n");
  339. fprintf(fortran_out, " integer :: i\n");
  340. fprintf(fortran_out, "\n#include \"alloc_agrif_%s.h\"\n", curmodulename);
  341. fprintf(fortran_out, " end subroutine Alloc_agrif_%s\n", curmodulename);
  342. Add_Subroutine_For_Alloc(curmodulename);
  343. }
  344. else
  345. {
  346. parcours_nom = List_Subroutine_For_Alloc;
  347. out = 0;
  348. while ( parcours_nom && out == 0 )
  349. {
  350. if ( !strcasecmp(parcours_nom->o_nom, curmodulename) ) out = 1;
  351. else
  352. {
  353. parcours_nomprec = parcours_nom;
  354. parcours_nom = parcours_nom->suiv;
  355. }
  356. }
  357. if ( out )
  358. {
  359. if ( parcours_nom == List_Subroutine_For_Alloc)
  360. {
  361. List_Subroutine_For_Alloc = List_Subroutine_For_Alloc->suiv;
  362. }
  363. else
  364. {
  365. parcours_nomprec->suiv = parcours_nom->suiv;
  366. parcours_nom = parcours_nomprec->suiv ;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. /******************************************************************************/
  373. /* IsTabvarsUseInArgument_0 */
  374. /******************************************************************************/
  375. /* Firstpass 1 */
  376. /******************************************************************************/
  377. /* */
  378. /******************************************************************************/
  379. int IsTabvarsUseInArgument_0()
  380. {
  381. int out;
  382. int doloopout;
  383. listvar *parcours;
  384. out=1;
  385. if ( List_UsedInSubroutine_Var )
  386. {
  387. doloopout = 0;
  388. parcours = List_UsedInSubroutine_Var;
  389. while ( parcours && doloopout == 0 )
  390. {
  391. if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) )
  392. doloopout = 1;
  393. else parcours = parcours->suiv;
  394. }
  395. if ( doloopout == 0 ) out = 0;
  396. else out = 1 ;
  397. }
  398. else out = 0;
  399. return out;
  400. }
  401. /******************************************************************************/
  402. /* ImplicitNoneInSubroutine */
  403. /******************************************************************************/
  404. /* Firstpass 0 */
  405. /******************************************************************************/
  406. /* */
  407. /******************************************************************************/
  408. int ImplicitNoneInSubroutine()
  409. {
  410. listname *parcours;
  411. int out;
  412. parcours= List_ImplicitNoneSubroutine;
  413. out = 0 ;
  414. while ( parcours && out == 0 )
  415. {
  416. if ( !strcasecmp(parcours->n_name,subroutinename) ) out = 1;
  417. else parcours = parcours->suiv;
  418. }
  419. return out;
  420. }
  421. /******************************************************************************/
  422. /* Add_Pointer_Var_From_List_1 */
  423. /******************************************************************************/
  424. /* Firstpass 1 */
  425. /******************************************************************************/
  426. /* */
  427. /******************************************************************************/
  428. void Add_Pointer_Var_From_List_1(listvar *listin)
  429. {
  430. listvar *parcours;
  431. if ( firstpass == 1 )
  432. {
  433. parcours = listin;
  434. while ( parcours )
  435. {
  436. Add_Pointer_Var_1(parcours->var->v_nomvar);
  437. parcours = parcours -> suiv ;
  438. }
  439. }
  440. }
  441. /******************************************************************************/
  442. /* Add_Pointer_Var_1 */
  443. /******************************************************************************/
  444. /* Firstpass 1 */
  445. /******************************************************************************/
  446. /* */
  447. /******************************************************************************/
  448. void Add_Pointer_Var_1(char *nom)
  449. {
  450. listname *newvar;
  451. listname *parcours;
  452. int out;
  453. if ( firstpass == 1 )
  454. {
  455. if ( !List_Pointer_Var )
  456. {
  457. newvar = (listname*) calloc(1, sizeof(listname));
  458. strcpy(newvar->n_name, nom);
  459. newvar->suiv = NULL;
  460. List_Pointer_Var = newvar;
  461. }
  462. else
  463. {
  464. parcours = List_Pointer_Var;
  465. out = 0 ;
  466. while ( parcours->suiv && out == 0 )
  467. {
  468. if ( !strcasecmp(parcours->n_name,nom) ) out = 1;
  469. else
  470. parcours=parcours->suiv;
  471. }
  472. if ( out == 0 )
  473. {
  474. if ( !strcasecmp(parcours->n_name,nom) ) out = 1;
  475. else
  476. {
  477. /* add the record */
  478. newvar = (listname*) calloc(1, sizeof(listname));
  479. strcpy(newvar->n_name,nom);
  480. newvar->suiv = NULL;
  481. parcours->suiv = newvar;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. /******************************************************************************/
  488. /* varispointer_0 */
  489. /******************************************************************************/
  490. /* Firstpass 0 */
  491. /******************************************************************************/
  492. /* */
  493. /******************************************************************************/
  494. int varispointer_0(char *ident)
  495. {
  496. listname *newname;
  497. int out;
  498. out =0;
  499. if ( firstpass == 0 )
  500. {
  501. newname = List_Pointer_Var;
  502. while( newname && out == 0 )
  503. {
  504. if ( !strcasecmp(ident,newname->n_name) ) out = 1 ;
  505. else newname = newname->suiv;
  506. }
  507. }
  508. return out;
  509. }
  510. /******************************************************************************/
  511. /* varistyped_0 */
  512. /******************************************************************************/
  513. /* Firstpass 0 */
  514. /******************************************************************************/
  515. /* */
  516. /******************************************************************************/
  517. int varistyped_0(char *ident)
  518. {
  519. listvar *parcours;
  520. int out;
  521. out =0;
  522. if ( firstpass == 0 )
  523. {
  524. parcours = List_Global_Var;
  525. while( parcours && out == 0 )
  526. {
  527. if ( !strcasecmp(ident,parcours->var->v_nomvar) )
  528. {
  529. if (!strcasecmp(parcours->var->v_typevar,"type")) out = 1;
  530. }
  531. parcours = parcours->suiv;
  532. }
  533. }
  534. return out;
  535. }
  536. /******************************************************************************/
  537. /* VariableIsFunction */
  538. /******************************************************************************/
  539. /* */
  540. /******************************************************************************/
  541. int VariableIsFunction(const char *ident)
  542. {
  543. int out;
  544. listvar *newvar;
  545. out = 0;
  546. if ( !strcasecmp(ident,"size") ||
  547. !strcasecmp(ident,"if") ||
  548. !strcasecmp(ident,"max") ||
  549. !strcasecmp(ident,"min") )
  550. {
  551. newvar = List_SubroutineDeclaration_Var;
  552. while ( newvar && out == 0 )
  553. {
  554. if ( !strcasecmp(subroutinename, newvar->var->v_subroutinename) &&
  555. !strcasecmp(ident, newvar->var->v_nomvar) )
  556. {
  557. out = 1;
  558. }
  559. newvar = newvar -> suiv ;
  560. }
  561. if ( out == 0 ) /* if it has not been found */
  562. {
  563. newvar = List_Global_Var;
  564. while ( newvar && out == 0 )
  565. {
  566. if ( !strcasecmp(ident, newvar->var->v_nomvar) ) out = 1;
  567. newvar = newvar -> suiv ;
  568. }
  569. }
  570. }
  571. return (out == 0);
  572. }
  573. void dump_var(const variable* var)
  574. {
  575. fprintf(stderr, " var->v_nomvar : %s\n",var->v_nomvar);
  576. fprintf(stderr, " var->v_indice : %d\n",var->v_indicetabvars);
  577. fprintf(stderr, " var->v_typevar: %s\n",var->v_typevar);
  578. fprintf(stderr, " var->v_catvar : %d\n",var->v_catvar);
  579. fprintf(stderr, " var->v_modulename: %s\n",var->v_modulename);
  580. fprintf(stderr, " var->v_subroutinename: %s\n",var->v_subroutinename);
  581. fprintf(stderr, " var->v_commonname: %s\n",var->v_commonname);
  582. fprintf(stderr, " var->v_commoninfile: %s\n",var->v_commoninfile);
  583. fprintf(stderr, " var->v_nbdim: %d\n",var->v_nbdim);
  584. fprintf(stderr, " var->v_common: %d\n",var->v_common);
  585. fprintf(stderr, " var->v_module: %d\n",var->v_module);
  586. fprintf(stderr, " var->v_initialvalue: %s\n",var->v_initialvalue);
  587. }