tccal.pas 47 KB

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