tccal.pas 50 KB

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