tccal.pas 50 KB

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