tccal.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for call nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit tccal;
  19. interface
  20. uses
  21. symtable,tree;
  22. {$ifndef OLDHIGH}
  23. procedure gen_high_tree(p:ptree;openstring:boolean);
  24. {$endif}
  25. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  26. procedure firstcalln(var p : ptree);
  27. procedure firstprocinline(var p : ptree);
  28. implementation
  29. uses
  30. globtype,systems,
  31. cobjects,verbose,globals,
  32. aasm,types,
  33. hcodegen,htypechk,pass_1
  34. {$ifdef i386}
  35. {$ifdef ag386bin}
  36. ,i386base
  37. {$else}
  38. ,i386
  39. {$endif}
  40. ,tgeni386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k,tgen68k
  44. {$endif}
  45. ;
  46. {*****************************************************************************
  47. FirstCallParaN
  48. *****************************************************************************}
  49. {$ifndef OLDHIGH}
  50. procedure gen_high_tree(p:ptree;openstring:boolean);
  51. var
  52. len : longint;
  53. st : psymtable;
  54. begin
  55. if assigned(p^.hightree) then
  56. exit;
  57. len:=-1;
  58. case p^.left^.resulttype^.deftype of
  59. arraydef :
  60. begin
  61. if is_open_array(p^.left^.resulttype) then
  62. begin
  63. st:=p^.left^.symtable;
  64. getsymonlyin(st,'high'+pvarsym(p^.left^.symtableentry)^.name);
  65. p^.hightree:=genloadnode(pvarsym(srsym),st);
  66. end
  67. else
  68. len:=parraydef(p^.left^.resulttype)^.highrange-
  69. parraydef(p^.left^.resulttype)^.lowrange;
  70. end;
  71. stringdef :
  72. begin
  73. if openstring then
  74. begin
  75. if is_open_string(p^.left^.resulttype) then
  76. begin
  77. st:=p^.left^.symtable;
  78. getsymonlyin(st,'high'+pvarsym(p^.left^.symtableentry)^.name);
  79. p^.hightree:=genloadnode(pvarsym(srsym),st);
  80. end
  81. else
  82. len:=pstringdef(p^.left^.resulttype)^.len;
  83. end
  84. else
  85. { passing a string to an array of char }
  86. begin
  87. if (p^.left^.treetype=stringconstn) then
  88. begin
  89. len:=str_length(p^.left);
  90. if len>0 then
  91. dec(len);
  92. end
  93. else
  94. begin
  95. p^.hightree:=gennode(subn,geninlinenode(in_length_string,false,getcopy(p^.left)),
  96. genordinalconstnode(1,s32bitdef));
  97. firstpass(p^.hightree);
  98. p^.hightree:=gentypeconvnode(p^.hightree,s32bitdef);
  99. end;
  100. end;
  101. end;
  102. else
  103. len:=0;
  104. end;
  105. if len>=0 then
  106. p^.hightree:=genordinalconstnode(len,s32bitdef);
  107. firstpass(p^.hightree);
  108. end;
  109. {$endif OLDHIGH}
  110. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  111. var
  112. old_array_constructor : boolean;
  113. store_valid : boolean;
  114. oldtype : pdef;
  115. {convtyp : tconverttype;}
  116. begin
  117. inc(parsing_para_level);
  118. if assigned(p^.right) then
  119. begin
  120. if defcoll=nil then
  121. firstcallparan(p^.right,nil)
  122. else
  123. firstcallparan(p^.right,defcoll^.next);
  124. p^.registers32:=p^.right^.registers32;
  125. p^.registersfpu:=p^.right^.registersfpu;
  126. {$ifdef SUPPORT_MMX}
  127. p^.registersmmx:=p^.right^.registersmmx;
  128. {$endif}
  129. end;
  130. if defcoll=nil then
  131. begin
  132. old_array_constructor:=allow_array_constructor;
  133. allow_array_constructor:=true;
  134. if not(assigned(p^.resulttype)) or
  135. (p^.left^.treetype=typeconvn) then
  136. firstpass(p^.left);
  137. allow_array_constructor:=old_array_constructor;
  138. if codegenerror then
  139. begin
  140. dec(parsing_para_level);
  141. exit;
  142. end;
  143. p^.resulttype:=p^.left^.resulttype;
  144. end
  145. { if we know the routine which is called, then the type }
  146. { conversions are inserted }
  147. else
  148. begin
  149. if count_ref then
  150. begin
  151. { not completly proper, but avoids some warnings }
  152. if (p^.left^.treetype=funcretn) and (defcoll^.paratyp=vs_var) then
  153. procinfo.funcret_is_valid:=true;
  154. store_valid:=must_be_valid;
  155. if (defcoll^.paratyp=vs_var) then
  156. test_protected(p^.left);
  157. must_be_valid:=(defcoll^.paratyp<>vs_var);
  158. { only process typeconvn, else it will break other trees }
  159. old_array_constructor:=allow_array_constructor;
  160. allow_array_constructor:=true;
  161. if (p^.left^.treetype=typeconvn) then
  162. firstpass(p^.left);
  163. allow_array_constructor:=old_array_constructor;
  164. must_be_valid:=store_valid;
  165. end;
  166. { generate the high() value tree }
  167. if push_high_param(defcoll^.data) then
  168. {$ifndef OLDHIGH}
  169. gen_high_tree(p,is_open_string(defcoll^.data));
  170. {$endif}
  171. if not(is_shortstring(p^.left^.resulttype) and
  172. is_shortstring(defcoll^.data)) and
  173. (defcoll^.data^.deftype<>formaldef) then
  174. begin
  175. if (defcoll^.paratyp=vs_var) and
  176. { allows conversion from word to integer and
  177. byte to shortint }
  178. (not(
  179. (p^.left^.resulttype^.deftype=orddef) and
  180. (defcoll^.data^.deftype=orddef) and
  181. (p^.left^.resulttype^.size=defcoll^.data^.size)
  182. ) and
  183. { an implicit pointer conversion is allowed }
  184. not(
  185. (p^.left^.resulttype^.deftype=pointerdef) and
  186. (defcoll^.data^.deftype=pointerdef)
  187. ) and
  188. { child classes can be also passed }
  189. not(
  190. (p^.left^.resulttype^.deftype=objectdef) and
  191. (defcoll^.data^.deftype=objectdef) and
  192. pobjectdef(p^.left^.resulttype)^.isrelated(pobjectdef(defcoll^.data))
  193. ) and
  194. { passing a single element to a openarray of the same type }
  195. not(
  196. (is_open_array(defcoll^.data) and
  197. is_equal(parraydef(defcoll^.data)^.definition,p^.left^.resulttype))
  198. ) and
  199. { an implicit file conversion is also allowed }
  200. { from a typed file to an untyped one }
  201. not(
  202. (p^.left^.resulttype^.deftype=filedef) and
  203. (defcoll^.data^.deftype=filedef) and
  204. (pfiledef(defcoll^.data)^.filetype = ft_untyped) and
  205. (pfiledef(p^.left^.resulttype)^.filetype = ft_typed)
  206. ) and
  207. not(is_equal(p^.left^.resulttype,defcoll^.data))) then
  208. CGMessage(parser_e_call_by_ref_without_typeconv);
  209. { process cargs arrayconstructor }
  210. if is_array_constructor(p^.left^.resulttype) and
  211. (aktcallprocsym^.definition^.options and pocdecl<>0) and
  212. (aktcallprocsym^.definition^.options and poexternal<>0) then
  213. begin
  214. p^.left^.cargs:=true;
  215. old_array_constructor:=allow_array_constructor;
  216. allow_array_constructor:=true;
  217. firstpass(p^.left);
  218. allow_array_constructor:=old_array_constructor;
  219. end;
  220. { process open parameters }
  221. if push_high_param(defcoll^.data) then
  222. begin
  223. { insert type conv but hold the ranges of the array }
  224. oldtype:=p^.left^.resulttype;
  225. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  226. firstpass(p^.left);
  227. p^.left^.resulttype:=oldtype;
  228. end
  229. else
  230. begin
  231. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  232. firstpass(p^.left);
  233. { this is necessary if an arrayconstruct -> set is done
  234. first, then the set generation tree needs to be passed
  235. to get the end resulttype (PFV) }
  236. if not assigned(p^.left^.resulttype) then
  237. firstpass(p^.left);
  238. end;
  239. if codegenerror then
  240. begin
  241. dec(parsing_para_level);
  242. exit;
  243. end;
  244. end;
  245. { check var strings }
  246. if (cs_strict_var_strings in aktlocalswitches) and
  247. is_shortstring(p^.left^.resulttype) and
  248. is_shortstring(defcoll^.data) and
  249. (defcoll^.paratyp=vs_var) and
  250. not(is_open_string(defcoll^.data)) and
  251. not(is_equal(p^.left^.resulttype,defcoll^.data)) then
  252. CGMessage(type_e_strict_var_string_violation);
  253. { Variablen for call by reference may not be copied }
  254. { into a register }
  255. { is this usefull here ? }
  256. { this was missing in formal parameter list }
  257. if defcoll^.paratyp=vs_var then
  258. begin
  259. set_unique(p^.left);
  260. make_not_regable(p^.left);
  261. end;
  262. p^.resulttype:=defcoll^.data;
  263. end;
  264. if p^.left^.registers32>p^.registers32 then
  265. p^.registers32:=p^.left^.registers32;
  266. if p^.left^.registersfpu>p^.registersfpu then
  267. p^.registersfpu:=p^.left^.registersfpu;
  268. {$ifdef SUPPORT_MMX}
  269. if p^.left^.registersmmx>p^.registersmmx then
  270. p^.registersmmx:=p^.left^.registersmmx;
  271. {$endif SUPPORT_MMX}
  272. dec(parsing_para_level);
  273. end;
  274. {*****************************************************************************
  275. FirstCallN
  276. *****************************************************************************}
  277. procedure firstcalln(var p : ptree);
  278. type
  279. pprocdefcoll = ^tprocdefcoll;
  280. tprocdefcoll = record
  281. data : pprocdef;
  282. nextpara : pdefcoll;
  283. firstpara : pdefcoll;
  284. next : pprocdefcoll;
  285. end;
  286. var
  287. hp,procs,hp2 : pprocdefcoll;
  288. pd : pprocdef;
  289. oldcallprocsym : pprocsym;
  290. nextprocsym : pprocsym;
  291. def_from,def_to,conv_to : pdef;
  292. pt,inlinecode : ptree;
  293. exactmatch,inlined : boolean;
  294. paralength,l : longint;
  295. pdc : pdefcoll;
  296. {$ifdef TEST_PROCSYMS}
  297. symt : psymtable;
  298. {$endif TEST_PROCSYMS}
  299. { only Dummy }
  300. hcvt : tconverttype;
  301. regi : tregister;
  302. store_valid, old_count_ref : boolean;
  303. { check if the resulttype from tree p is equal with def, needed
  304. for stringconstn and formaldef }
  305. function is_equal(p:ptree;def:pdef) : boolean;
  306. begin
  307. { safety check }
  308. if not (assigned(def) or assigned(p^.resulttype)) then
  309. begin
  310. is_equal:=false;
  311. exit;
  312. end;
  313. { all types can be passed to a formaldef }
  314. is_equal:=(def^.deftype=formaldef) or
  315. (types.is_equal(p^.resulttype,def))
  316. { to support ansi/long/wide strings in a proper way }
  317. { string and string[10] are assumed as equal }
  318. { when searching the correct overloaded procedure }
  319. or
  320. (
  321. (def^.deftype=stringdef) and (p^.resulttype^.deftype=stringdef) and
  322. (pstringdef(def)^.string_typ=pstringdef(p^.resulttype)^.string_typ)
  323. )
  324. or
  325. (
  326. (p^.left^.treetype=stringconstn) and
  327. (is_ansistring(p^.resulttype) and is_pchar(def))
  328. )
  329. or
  330. (
  331. (p^.left^.treetype=ordconstn) and
  332. (is_char(p^.resulttype) and (is_shortstring(def) or is_ansistring(def)))
  333. )
  334. { set can also be a not yet converted array constructor }
  335. or
  336. (
  337. (def^.deftype=setdef) and (p^.resulttype^.deftype=arraydef) and
  338. (parraydef(p^.resulttype)^.IsConstructor) and not(parraydef(p^.resulttype)^.IsVariant)
  339. )
  340. ;
  341. end;
  342. function is_in_limit(def_from,def_to : pdef) : boolean;
  343. begin
  344. is_in_limit:=(def_from^.deftype = orddef) and
  345. (def_to^.deftype = orddef) and
  346. (porddef(def_from)^.low>porddef(def_to)^.low) and
  347. (porddef(def_from)^.high<porddef(def_to)^.high);
  348. end;
  349. var
  350. is_const : boolean;
  351. begin
  352. { release registers! }
  353. { if procdefinition<>nil then we called firstpass already }
  354. { it seems to be bad because of the registers }
  355. { at least we can avoid the overloaded search !! }
  356. procs:=nil;
  357. { made this global for disposing !! }
  358. store_valid:=must_be_valid;
  359. must_be_valid:=false;
  360. oldcallprocsym:=aktcallprocsym;
  361. aktcallprocsym:=nil;
  362. inlined:=false;
  363. if assigned(p^.procdefinition) and
  364. ((p^.procdefinition^.options and poinline)<>0) then
  365. begin
  366. inlinecode:=p^.right;
  367. if assigned(inlinecode) then
  368. begin
  369. inlined:=true;
  370. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  371. end;
  372. p^.right:=nil;
  373. end;
  374. { procedure variable ? }
  375. if assigned(p^.right) then
  376. begin
  377. { procedure does a call }
  378. procinfo.flags:=procinfo.flags or pi_do_call;
  379. { calc the correture value for the register }
  380. {$ifdef i386}
  381. for regi:=R_EAX to R_EDI do
  382. inc(reg_pushes[regi],t_times*2);
  383. {$endif}
  384. {$ifdef m68k}
  385. for regi:=R_D0 to R_A6 do
  386. inc(reg_pushes[regi],t_times*2);
  387. {$endif}
  388. { calculate the type of the parameters }
  389. if assigned(p^.left) then
  390. begin
  391. old_count_ref:=count_ref;
  392. count_ref:=false;
  393. firstcallparan(p^.left,nil);
  394. count_ref:=old_count_ref;
  395. if codegenerror then
  396. exit;
  397. end;
  398. firstpass(p^.right);
  399. { check the parameters }
  400. pdc:=pprocvardef(p^.right^.resulttype)^.para1;
  401. pt:=p^.left;
  402. while assigned(pdc) and assigned(pt) do
  403. begin
  404. pt:=pt^.right;
  405. pdc:=pdc^.next;
  406. end;
  407. if assigned(pt) or assigned(pdc) then
  408. CGMessage(parser_e_illegal_parameter_list);
  409. { insert type conversions }
  410. if assigned(p^.left) then
  411. begin
  412. old_count_ref:=count_ref;
  413. count_ref:=true;
  414. firstcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1);
  415. count_ref:=old_count_ref;
  416. if codegenerror then
  417. exit;
  418. end;
  419. p^.resulttype:=pprocvardef(p^.right^.resulttype)^.retdef;
  420. { this was missing, leads to a bug below if
  421. the procvar is a function }
  422. p^.procdefinition:=pprocdef(p^.right^.resulttype);
  423. end
  424. else
  425. { not a procedure variable }
  426. begin
  427. { determine the type of the parameters }
  428. if assigned(p^.left) then
  429. begin
  430. old_count_ref:=count_ref;
  431. count_ref:=false;
  432. store_valid:=must_be_valid;
  433. must_be_valid:=false;
  434. firstcallparan(p^.left,nil);
  435. count_ref:=old_count_ref;
  436. must_be_valid:=store_valid;
  437. if codegenerror then
  438. exit;
  439. end;
  440. aktcallprocsym:=pprocsym(p^.symtableprocentry);
  441. { do we know the procedure to call ? }
  442. if not(assigned(p^.procdefinition)) then
  443. begin
  444. {$ifdef TEST_PROCSYMS}
  445. if (p^.unit_specific) or
  446. assigned(p^.methodpointer) then
  447. nextprocsym:=nil
  448. else while not assigned(procs) do
  449. begin
  450. symt:=p^.symtableproc;
  451. srsym:=nil;
  452. while assigned(symt^.next) and not assigned(srsym) do
  453. begin
  454. symt:=symt^.next;
  455. getsymonlyin(symt,actprocsym^.name);
  456. if assigned(srsym) then
  457. if srsym^.typ<>procsym then
  458. begin
  459. { reject all that is not a procedure }
  460. srsym:=nil;
  461. { don't search elsewhere }
  462. while assigned(symt^.next) do
  463. symt:=symt^.next;
  464. end;
  465. end;
  466. nextprocsym:=srsym;
  467. end;
  468. {$else TEST_PROCSYMS}
  469. nextprocsym:=nil;
  470. {$endif TEST_PROCSYMS}
  471. { determine length of parameter list }
  472. pt:=p^.left;
  473. paralength:=0;
  474. while assigned(pt) do
  475. begin
  476. inc(paralength);
  477. pt:=pt^.right;
  478. end;
  479. { link all procedures which have the same # of parameters }
  480. pd:=aktcallprocsym^.definition;
  481. while assigned(pd) do
  482. begin
  483. { we should also check that the overloaded function
  484. has been declared in a unit that is in the uses !! }
  485. { pd^.owner should be in the symtablestack !! }
  486. { Laenge der deklarierten Parameterliste feststellen: }
  487. { not necessary why nextprocsym field }
  488. {st:=symtablestack;
  489. if (pd^.owner^.symtabletype<>objectsymtable) then
  490. while assigned(st) do
  491. begin
  492. if (st=pd^.owner) then break;
  493. st:=st^.next;
  494. end;
  495. if assigned(st) then }
  496. begin
  497. pdc:=pd^.para1;
  498. l:=0;
  499. while assigned(pdc) do
  500. begin
  501. inc(l);
  502. pdc:=pdc^.next;
  503. end;
  504. { only when the # of parameter are equal }
  505. if (l=paralength) then
  506. begin
  507. new(hp);
  508. hp^.data:=pd;
  509. hp^.next:=procs;
  510. hp^.nextpara:=pd^.para1;
  511. hp^.firstpara:=pd^.para1;
  512. procs:=hp;
  513. end;
  514. end;
  515. pd:=pd^.nextoverloaded;
  516. end;
  517. { no procedures found? then there is something wrong
  518. with the parameter size }
  519. if not assigned(procs) and
  520. ((parsing_para_level=0) or assigned(p^.left)) and
  521. (nextprocsym=nil) then
  522. begin
  523. CGMessage(parser_e_wrong_parameter_size);
  524. aktcallprocsym^.write_parameter_lists;
  525. exit;
  526. end;
  527. { now we can compare parameter after parameter }
  528. pt:=p^.left;
  529. { we start with the last parameter }
  530. l:=paralength+1;
  531. while assigned(pt) do
  532. begin
  533. dec(l);
  534. { matches a parameter of one procedure exact ? }
  535. exactmatch:=false;
  536. hp:=procs;
  537. while assigned(hp) do
  538. begin
  539. if is_equal(pt,hp^.nextpara^.data) then
  540. begin
  541. if hp^.nextpara^.data=pt^.resulttype then
  542. begin
  543. pt^.exact_match_found:=true;
  544. hp^.nextpara^.argconvtyp:=act_exact;
  545. end
  546. else
  547. hp^.nextpara^.argconvtyp:=act_equal;
  548. exactmatch:=true;
  549. end
  550. else
  551. hp^.nextpara^.argconvtyp:=act_convertable;
  552. hp:=hp^.next;
  553. end;
  554. { .... if yes, del all the other procedures }
  555. if exactmatch then
  556. begin
  557. { the first .... }
  558. while (assigned(procs)) and not(is_equal(pt,procs^.nextpara^.data)) do
  559. begin
  560. hp:=procs^.next;
  561. dispose(procs);
  562. procs:=hp;
  563. end;
  564. { and the others }
  565. hp:=procs;
  566. while (assigned(hp)) and assigned(hp^.next) do
  567. begin
  568. if not(is_equal(pt,hp^.next^.nextpara^.data)) then
  569. begin
  570. hp2:=hp^.next^.next;
  571. dispose(hp^.next);
  572. hp^.next:=hp2;
  573. end
  574. else
  575. hp:=hp^.next;
  576. end;
  577. end
  578. { when a parameter matches exact, remove all procs
  579. which need typeconvs }
  580. else
  581. begin
  582. { the first... }
  583. while (assigned(procs)) and
  584. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  585. hcvt,pt^.left^.treetype,false)) do
  586. begin
  587. hp:=procs^.next;
  588. dispose(procs);
  589. procs:=hp;
  590. end;
  591. { and the others }
  592. hp:=procs;
  593. while (assigned(hp)) and assigned(hp^.next) do
  594. begin
  595. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  596. hcvt,pt^.left^.treetype,false)) then
  597. begin
  598. hp2:=hp^.next^.next;
  599. dispose(hp^.next);
  600. hp^.next:=hp2;
  601. end
  602. else
  603. hp:=hp^.next;
  604. end;
  605. end;
  606. { update nextpara for all procedures }
  607. hp:=procs;
  608. while assigned(hp) do
  609. begin
  610. hp^.nextpara:=hp^.nextpara^.next;
  611. hp:=hp^.next;
  612. end;
  613. { load next parameter }
  614. if assigned(procs) then
  615. pt:=pt^.right
  616. else
  617. pt:=nil;
  618. end;
  619. if not assigned(procs) then
  620. begin
  621. { there is an error, must be wrong type, because
  622. wrong size is already checked (PFV) }
  623. if ((parsing_para_level=0) or (p^.left<>nil)) and
  624. (nextprocsym=nil) then
  625. begin
  626. CGMessage1(parser_e_wrong_parameter_type,tostr(l));
  627. aktcallprocsym^.write_parameter_lists;
  628. exit;
  629. end
  630. else
  631. begin
  632. { try to convert to procvar }
  633. p^.treetype:=loadn;
  634. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  635. p^.symtableentry:=p^.symtableprocentry;
  636. p^.is_first:=false;
  637. p^.disposetyp:=dt_nothing;
  638. firstpass(p);
  639. exit;
  640. end;
  641. end;
  642. { if there are several choices left then for orddef }
  643. { if a type is totally included in the other }
  644. { we don't fear an overflow , }
  645. { so we can do as if it is an exact match }
  646. { this will convert integer to longint }
  647. { rather than to words }
  648. { conversion of byte to integer or longint }
  649. {would still not be solved }
  650. if assigned(procs) and assigned(procs^.next) then
  651. begin
  652. hp:=procs;
  653. while assigned(hp) do
  654. begin
  655. hp^.nextpara:=hp^.firstpara;
  656. hp:=hp^.next;
  657. end;
  658. pt:=p^.left;
  659. while assigned(pt) do
  660. begin
  661. { matches a parameter of one procedure exact ? }
  662. exactmatch:=false;
  663. def_from:=pt^.resulttype;
  664. hp:=procs;
  665. while assigned(hp) do
  666. begin
  667. if not is_equal(pt,hp^.nextpara^.data) then
  668. begin
  669. def_to:=hp^.nextpara^.data;
  670. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  671. (is_in_limit(def_from,def_to) or
  672. ((hp^.nextpara^.paratyp=vs_var) and
  673. (def_from^.size=def_to^.size))) then
  674. begin
  675. exactmatch:=true;
  676. conv_to:=def_to;
  677. end;
  678. end;
  679. hp:=hp^.next;
  680. end;
  681. { .... if yes, del all the other procedures }
  682. if exactmatch then
  683. begin
  684. { the first .... }
  685. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  686. begin
  687. hp:=procs^.next;
  688. dispose(procs);
  689. procs:=hp;
  690. end;
  691. { and the others }
  692. hp:=procs;
  693. while (assigned(hp)) and assigned(hp^.next) do
  694. begin
  695. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  696. begin
  697. hp2:=hp^.next^.next;
  698. dispose(hp^.next);
  699. hp^.next:=hp2;
  700. end
  701. else
  702. begin
  703. def_to:=hp^.next^.nextpara^.data;
  704. if (conv_to^.size>def_to^.size) or
  705. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  706. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  707. begin
  708. hp2:=procs;
  709. procs:=hp;
  710. conv_to:=def_to;
  711. dispose(hp2);
  712. end
  713. else
  714. hp:=hp^.next;
  715. end;
  716. end;
  717. end;
  718. { update nextpara for all procedures }
  719. hp:=procs;
  720. while assigned(hp) do
  721. begin
  722. hp^.nextpara:=hp^.nextpara^.next;
  723. hp:=hp^.next;
  724. end;
  725. pt:=pt^.right;
  726. end;
  727. end;
  728. { reset nextpara for all procs left }
  729. hp:=procs;
  730. while assigned(hp) do
  731. begin
  732. hp^.nextpara:=hp^.firstpara;
  733. hp:=hp^.next;
  734. end;
  735. { let's try to eliminate equal is exact is there }
  736. if assigned(procs^.next) then
  737. begin
  738. pt:=p^.left;
  739. while assigned(pt) do
  740. begin
  741. if pt^.exact_match_found then
  742. begin
  743. hp:=procs;
  744. procs:=nil;
  745. while assigned(hp) do
  746. begin
  747. hp2:=hp^.next;
  748. { keep the exact matches, dispose the others }
  749. if (hp^.nextpara^.data=pt^.resulttype) then
  750. begin
  751. hp^.next:=procs;
  752. procs:=hp;
  753. end
  754. else
  755. begin
  756. dispose(hp);
  757. end;
  758. hp:=hp2;
  759. end;
  760. end;
  761. { update nextpara for all procedures }
  762. hp:=procs;
  763. while assigned(hp) do
  764. begin
  765. hp^.nextpara:=hp^.nextpara^.next;
  766. hp:=hp^.next;
  767. end;
  768. pt:=pt^.right;
  769. end;
  770. end;
  771. if assigned(procs^.next) then
  772. begin
  773. CGMessage(cg_e_cant_choose_overload_function);
  774. aktcallprocsym^.write_parameter_lists;
  775. end;
  776. {$ifdef TEST_PROCSYMS}
  777. if (procs=nil) and assigned(nextprocsym) then
  778. begin
  779. p^.symtableprocentry:=nextprocsym;
  780. p^.symtableproc:=symt;
  781. end;
  782. end ; { of while assigned(p^.symtableprocentry) do }
  783. {$endif TEST_PROCSYMS}
  784. if make_ref then
  785. begin
  786. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@p^.fileinfo));
  787. if procs^.data^.defref=nil then
  788. procs^.data^.defref:=procs^.data^.lastref;
  789. end;
  790. p^.procdefinition:=procs^.data;
  791. p^.resulttype:=procs^.data^.retdef;
  792. { big error for with statements
  793. p^.symtableproc:=p^.procdefinition^.owner;
  794. but neede for overloaded operators !! }
  795. if p^.symtableproc=nil then
  796. p^.symtableproc:=p^.procdefinition^.owner;
  797. p^.location.loc:=LOC_MEM;
  798. {$ifdef CHAINPROCSYMS}
  799. { object with method read;
  800. call to read(x) will be a usual procedure call }
  801. if assigned(p^.methodpointer) and
  802. (p^.procdefinition^._class=nil) then
  803. begin
  804. { not ok for extended }
  805. case p^.methodpointer^.treetype of
  806. typen,hnewn : fatalerror(no_para_match);
  807. end;
  808. disposetree(p^.methodpointer);
  809. p^.methodpointer:=nil;
  810. end;
  811. {$endif CHAINPROCSYMS}
  812. end; { end of procedure to call determination }
  813. is_const:=((p^.procdefinition^.options and pointernconst)<>0) and
  814. ((block_type=bt_const) or
  815. (assigned(p^.left) and (p^.left^.left^.treetype in [realconstn,ordconstn])));
  816. { handle predefined procedures }
  817. if ((p^.procdefinition^.options and pointernproc)<>0) or is_const then
  818. begin
  819. if assigned(p^.left) then
  820. begin
  821. { settextbuf needs two args }
  822. if assigned(p^.left^.right) then
  823. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left)
  824. else
  825. begin
  826. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left^.left);
  827. putnode(p^.left);
  828. end;
  829. end
  830. else
  831. begin
  832. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,nil);
  833. end;
  834. putnode(p);
  835. firstpass(pt);
  836. p:=pt;
  837. must_be_valid:=store_valid;
  838. if codegenerror then
  839. exit;
  840. dispose(procs);
  841. exit;
  842. end
  843. else
  844. { no intern procedure => we do a call }
  845. { calc the correture value for the register }
  846. { handle predefined procedures }
  847. if (p^.procdefinition^.options and poinline)<>0 then
  848. begin
  849. if assigned(p^.methodpointer) then
  850. CGMessage(cg_e_unable_inline_object_methods);
  851. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  852. CGMessage(cg_e_unable_inline_procvar);
  853. { p^.treetype:=procinlinen; }
  854. if not assigned(p^.right) then
  855. begin
  856. if assigned(p^.procdefinition^.code) then
  857. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  858. else
  859. CGMessage(cg_e_no_code_for_inline_stored);
  860. if assigned(inlinecode) then
  861. begin
  862. { consider it has not inlined if called
  863. again inside the args }
  864. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  865. firstpass(inlinecode);
  866. inlined:=true;
  867. end;
  868. end;
  869. end
  870. else
  871. procinfo.flags:=procinfo.flags or pi_do_call;
  872. { work trough all parameters to insert the type conversions }
  873. { !!! done now after internproc !! (PM) }
  874. if assigned(p^.left) then
  875. begin
  876. old_count_ref:=count_ref;
  877. count_ref:=true;
  878. firstcallparan(p^.left,p^.procdefinition^.para1);
  879. count_ref:=old_count_ref;
  880. end;
  881. {$ifdef i386}
  882. for regi:=R_EAX to R_EDI do
  883. begin
  884. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  885. inc(reg_pushes[regi],t_times*2);
  886. end;
  887. {$endif}
  888. {$ifdef m68k}
  889. for regi:=R_D0 to R_A6 do
  890. begin
  891. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  892. inc(reg_pushes[regi],t_times*2);
  893. end;
  894. {$endif}
  895. end;
  896. { ensure that the result type is set }
  897. p^.resulttype:=p^.procdefinition^.retdef;
  898. { get a register for the return value }
  899. if (p^.resulttype<>pdef(voiddef)) then
  900. begin
  901. if (p^.procdefinition^.options and poconstructor)<>0 then
  902. begin
  903. { extra handling of classes }
  904. { p^.methodpointer should be assigned! }
  905. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  906. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  907. begin
  908. p^.location.loc:=LOC_REGISTER;
  909. p^.registers32:=1;
  910. { the result type depends on the classref }
  911. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  912. end
  913. { a object constructor returns the result with the flags }
  914. else
  915. p^.location.loc:=LOC_FLAGS;
  916. end
  917. else
  918. begin
  919. {$ifdef SUPPORT_MMX}
  920. if (cs_mmx in aktlocalswitches) and
  921. is_mmx_able_array(p^.resulttype) then
  922. begin
  923. p^.location.loc:=LOC_MMXREGISTER;
  924. p^.registersmmx:=1;
  925. end
  926. else
  927. {$endif SUPPORT_MMX}
  928. if ret_in_acc(p^.resulttype) then
  929. begin
  930. p^.location.loc:=LOC_REGISTER;
  931. if is_64bitint(p^.resulttype) then
  932. p^.registers32:=2
  933. else
  934. p^.registers32:=1;
  935. end
  936. else if (p^.resulttype^.deftype=floatdef) then
  937. begin
  938. p^.location.loc:=LOC_FPU;
  939. p^.registersfpu:=1;
  940. end
  941. end;
  942. end;
  943. { a fpu can be used in any procedure !! }
  944. p^.registersfpu:=p^.procdefinition^.fpu_used;
  945. { if this is a call to a method calc the registers }
  946. if (p^.methodpointer<>nil) then
  947. begin
  948. case p^.methodpointer^.treetype of
  949. { but only, if this is not a supporting node }
  950. typen,hnewn : ;
  951. else
  952. begin
  953. {$ifndef NODIRECTWITH}
  954. if ((p^.procdefinition^.options and (poconstructor or podestructor)) <> 0) and
  955. assigned(p^.symtable) and (p^.symtable^.symtabletype=withsymtable) and
  956. not pwithsymtable(p^.symtable)^.direct_with then
  957. begin
  958. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  959. end; { Is accepted by Delphi !! }
  960. { this is not a good reason to accept it in FPC if we produce
  961. wrong code for it !!! (PM) }
  962. {$endif ndef NODIRECTWITH}
  963. { R.Assign is not a constructor !!! }
  964. { but for R^.Assign, R must be valid !! }
  965. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  966. ((p^.methodpointer^.treetype=loadn) and
  967. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  968. must_be_valid:=false
  969. else
  970. must_be_valid:=true;
  971. firstpass(p^.methodpointer);
  972. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  973. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  974. {$ifdef SUPPORT_MMX}
  975. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  976. {$endif SUPPORT_MMX}
  977. end;
  978. end;
  979. end;
  980. if inlined then
  981. begin
  982. p^.right:=inlinecode;
  983. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  984. end;
  985. { determine the registers of the procedure variable }
  986. { is this OK for inlined procs also ?? (PM) }
  987. if assigned(p^.right) then
  988. begin
  989. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  990. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  991. {$ifdef SUPPORT_MMX}
  992. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  993. {$endif SUPPORT_MMX}
  994. end;
  995. { determine the registers of the procedure }
  996. if assigned(p^.left) then
  997. begin
  998. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  999. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  1000. {$ifdef SUPPORT_MMX}
  1001. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  1002. {$endif SUPPORT_MMX}
  1003. end;
  1004. if assigned(procs) then
  1005. dispose(procs);
  1006. aktcallprocsym:=oldcallprocsym;
  1007. must_be_valid:=store_valid;
  1008. end;
  1009. {*****************************************************************************
  1010. FirstProcInlineN
  1011. *****************************************************************************}
  1012. procedure firstprocinline(var p : ptree);
  1013. begin
  1014. { left contains the code in tree form }
  1015. { but it has already been firstpassed }
  1016. { so firstpass(p^.left); does not seem required }
  1017. { might be required later if we change the arg handling !! }
  1018. end;
  1019. end.
  1020. {
  1021. $Log$
  1022. Revision 1.24 1999-02-22 02:15:45 peter
  1023. * updates for ag386bin
  1024. Revision 1.23 1999/02/09 17:15:52 florian
  1025. * some false warnings "function result doesn't seems to be set" are
  1026. avoided
  1027. Revision 1.22 1999/01/29 11:34:55 pierre
  1028. + better info for impossible type conversion in calln
  1029. Revision 1.21 1999/01/21 22:10:49 peter
  1030. * fixed array of const
  1031. * generic platform independent high() support
  1032. Revision 1.20 1999/01/21 16:41:06 pierre
  1033. * fix for constructor inside with statements
  1034. Revision 1.19 1999/01/19 14:20:16 peter
  1035. * fixed [char] crash
  1036. Revision 1.18 1999/01/12 14:25:40 peter
  1037. + BrowserLog for browser.log generation
  1038. + BrowserCol for browser info in TCollections
  1039. * released all other UseBrowser
  1040. Revision 1.17 1998/12/11 00:03:52 peter
  1041. + globtype,tokens,version unit splitted from globals
  1042. Revision 1.16 1998/12/10 14:57:52 pierre
  1043. * fix for operators
  1044. Revision 1.15 1998/12/10 09:47:32 florian
  1045. + basic operations with int64/qord (compiler with -dint64)
  1046. + rtti of enumerations extended: names are now written
  1047. Revision 1.14 1998/11/27 14:50:52 peter
  1048. + open strings, $P switch support
  1049. Revision 1.13 1998/11/24 17:03:51 peter
  1050. * fixed exactmatch removings
  1051. Revision 1.12 1998/11/16 10:18:10 peter
  1052. * fixes for ansistrings
  1053. Revision 1.11 1998/11/10 10:09:17 peter
  1054. * va_list -> array of const
  1055. Revision 1.10 1998/11/09 11:44:41 peter
  1056. + va_list for printf support
  1057. Revision 1.9 1998/10/28 18:26:22 pierre
  1058. * removed some erros after other errors (introduced by useexcept)
  1059. * stabs works again correctly (for how long !)
  1060. Revision 1.8 1998/10/09 16:36:09 pierre
  1061. * some memory leaks specific to usebrowser define fixed
  1062. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  1063. Revision 1.7 1998/10/06 20:49:09 peter
  1064. * m68k compiler compiles again
  1065. Revision 1.6 1998/10/02 09:24:22 peter
  1066. * more constant expression evaluators
  1067. Revision 1.5 1998/09/28 11:22:17 pierre
  1068. * did not compile for browser
  1069. * merge from fixes
  1070. Revision 1.4 1998/09/27 10:16:24 florian
  1071. * type casts pchar<->ansistring fixed
  1072. * ansistring[..] calls does now an unique call
  1073. Revision 1.3 1998/09/24 14:27:40 peter
  1074. * some better support for openarray
  1075. Revision 1.2 1998/09/24 09:02:16 peter
  1076. * rewritten isconvertable to use case
  1077. * array of .. and single variable are compatible
  1078. Revision 1.1 1998/09/23 20:42:24 peter
  1079. * splitted pass_1
  1080. }