tccal.pas 49 KB

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