tccal.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. { we should also check that the overloaded function
  486. has been declared in a unit that is in the uses !! }
  487. { pd^.owner should be in the symtablestack !! }
  488. { Laenge der deklarierten Parameterliste feststellen: }
  489. { not necessary why nextprocsym field }
  490. {st:=symtablestack;
  491. if (pd^.owner^.symtabletype<>objectsymtable) then
  492. while assigned(st) do
  493. begin
  494. if (st=pd^.owner) then break;
  495. st:=st^.next;
  496. end;
  497. if assigned(st) then }
  498. begin
  499. pdc:=pd^.para1;
  500. l:=0;
  501. while assigned(pdc) do
  502. begin
  503. inc(l);
  504. pdc:=pdc^.next;
  505. end;
  506. { only when the # of parameter are equal }
  507. if (l=paralength) then
  508. begin
  509. new(hp);
  510. hp^.data:=pd;
  511. hp^.next:=procs;
  512. hp^.nextpara:=pd^.para1;
  513. hp^.firstpara:=pd^.para1;
  514. procs:=hp;
  515. end;
  516. end;
  517. pd:=pd^.nextoverloaded;
  518. end;
  519. { no procedures found? then there is something wrong
  520. with the parameter size }
  521. if not assigned(procs) and
  522. ((parsing_para_level=0) or assigned(p^.left)) and
  523. (nextprocsym=nil) then
  524. begin
  525. CGMessage(parser_e_wrong_parameter_size);
  526. aktcallprocsym^.write_parameter_lists;
  527. exit;
  528. end;
  529. { now we can compare parameter after parameter }
  530. pt:=p^.left;
  531. { we start with the last parameter }
  532. l:=paralength+1;
  533. while assigned(pt) do
  534. begin
  535. dec(l);
  536. { matches a parameter of one procedure exact ? }
  537. exactmatch:=false;
  538. hp:=procs;
  539. while assigned(hp) do
  540. begin
  541. if is_equal(pt,hp^.nextpara^.data) then
  542. begin
  543. if hp^.nextpara^.data=pt^.resulttype then
  544. begin
  545. pt^.exact_match_found:=true;
  546. hp^.nextpara^.argconvtyp:=act_exact;
  547. end
  548. else
  549. hp^.nextpara^.argconvtyp:=act_equal;
  550. exactmatch:=true;
  551. end
  552. else
  553. hp^.nextpara^.argconvtyp:=act_convertable;
  554. hp:=hp^.next;
  555. end;
  556. { .... if yes, del all the other procedures }
  557. if exactmatch then
  558. begin
  559. { the first .... }
  560. while (assigned(procs)) and not(is_equal(pt,procs^.nextpara^.data)) do
  561. begin
  562. hp:=procs^.next;
  563. dispose(procs);
  564. procs:=hp;
  565. end;
  566. { and the others }
  567. hp:=procs;
  568. while (assigned(hp)) and assigned(hp^.next) do
  569. begin
  570. if not(is_equal(pt,hp^.next^.nextpara^.data)) then
  571. begin
  572. hp2:=hp^.next^.next;
  573. dispose(hp^.next);
  574. hp^.next:=hp2;
  575. end
  576. else
  577. hp:=hp^.next;
  578. end;
  579. end
  580. { when a parameter matches exact, remove all procs
  581. which need typeconvs }
  582. else
  583. begin
  584. { the first... }
  585. while (assigned(procs)) and
  586. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  587. hcvt,pt^.left^.treetype,false)) do
  588. begin
  589. hp:=procs^.next;
  590. dispose(procs);
  591. procs:=hp;
  592. end;
  593. { and the others }
  594. hp:=procs;
  595. while (assigned(hp)) and assigned(hp^.next) do
  596. begin
  597. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  598. hcvt,pt^.left^.treetype,false)) then
  599. begin
  600. hp2:=hp^.next^.next;
  601. dispose(hp^.next);
  602. hp^.next:=hp2;
  603. end
  604. else
  605. hp:=hp^.next;
  606. end;
  607. end;
  608. { update nextpara for all procedures }
  609. hp:=procs;
  610. while assigned(hp) do
  611. begin
  612. hp^.nextpara:=hp^.nextpara^.next;
  613. hp:=hp^.next;
  614. end;
  615. { load next parameter }
  616. if assigned(procs) then
  617. pt:=pt^.right
  618. else
  619. pt:=nil;
  620. end;
  621. if not assigned(procs) then
  622. begin
  623. { there is an error, must be wrong type, because
  624. wrong size is already checked (PFV) }
  625. if ((parsing_para_level=0) or (p^.left<>nil)) and
  626. (nextprocsym=nil) then
  627. begin
  628. CGMessage1(parser_e_wrong_parameter_type,tostr(l));
  629. aktcallprocsym^.write_parameter_lists;
  630. exit;
  631. end
  632. else
  633. begin
  634. { try to convert to procvar }
  635. p^.treetype:=loadn;
  636. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  637. p^.symtableentry:=p^.symtableprocentry;
  638. p^.is_first:=false;
  639. p^.disposetyp:=dt_nothing;
  640. firstpass(p);
  641. exit;
  642. end;
  643. end;
  644. { if there are several choices left then for orddef }
  645. { if a type is totally included in the other }
  646. { we don't fear an overflow , }
  647. { so we can do as if it is an exact match }
  648. { this will convert integer to longint }
  649. { rather than to words }
  650. { conversion of byte to integer or longint }
  651. {would still not be solved }
  652. if assigned(procs) and assigned(procs^.next) then
  653. begin
  654. hp:=procs;
  655. while assigned(hp) do
  656. begin
  657. hp^.nextpara:=hp^.firstpara;
  658. hp:=hp^.next;
  659. end;
  660. pt:=p^.left;
  661. while assigned(pt) do
  662. begin
  663. { matches a parameter of one procedure exact ? }
  664. exactmatch:=false;
  665. def_from:=pt^.resulttype;
  666. hp:=procs;
  667. while assigned(hp) do
  668. begin
  669. if not is_equal(pt,hp^.nextpara^.data) then
  670. begin
  671. def_to:=hp^.nextpara^.data;
  672. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  673. (is_in_limit(def_from,def_to) or
  674. ((hp^.nextpara^.paratyp=vs_var) and
  675. (def_from^.size=def_to^.size))) then
  676. begin
  677. exactmatch:=true;
  678. conv_to:=def_to;
  679. end;
  680. end;
  681. hp:=hp^.next;
  682. end;
  683. { .... if yes, del all the other procedures }
  684. if exactmatch then
  685. begin
  686. { the first .... }
  687. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  688. begin
  689. hp:=procs^.next;
  690. dispose(procs);
  691. procs:=hp;
  692. end;
  693. { and the others }
  694. hp:=procs;
  695. while (assigned(hp)) and assigned(hp^.next) do
  696. begin
  697. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  698. begin
  699. hp2:=hp^.next^.next;
  700. dispose(hp^.next);
  701. hp^.next:=hp2;
  702. end
  703. else
  704. begin
  705. def_to:=hp^.next^.nextpara^.data;
  706. if (conv_to^.size>def_to^.size) or
  707. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  708. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  709. begin
  710. hp2:=procs;
  711. procs:=hp;
  712. conv_to:=def_to;
  713. dispose(hp2);
  714. end
  715. else
  716. hp:=hp^.next;
  717. end;
  718. end;
  719. end;
  720. { update nextpara for all procedures }
  721. hp:=procs;
  722. while assigned(hp) do
  723. begin
  724. hp^.nextpara:=hp^.nextpara^.next;
  725. hp:=hp^.next;
  726. end;
  727. pt:=pt^.right;
  728. end;
  729. end;
  730. { reset nextpara for all procs left }
  731. hp:=procs;
  732. while assigned(hp) do
  733. begin
  734. hp^.nextpara:=hp^.firstpara;
  735. hp:=hp^.next;
  736. end;
  737. { let's try to eliminate equal is exact is there }
  738. if assigned(procs^.next) then
  739. begin
  740. pt:=p^.left;
  741. while assigned(pt) do
  742. begin
  743. if pt^.exact_match_found then
  744. begin
  745. hp:=procs;
  746. procs:=nil;
  747. while assigned(hp) do
  748. begin
  749. hp2:=hp^.next;
  750. { keep the exact matches, dispose the others }
  751. if (hp^.nextpara^.data=pt^.resulttype) then
  752. begin
  753. hp^.next:=procs;
  754. procs:=hp;
  755. end
  756. else
  757. begin
  758. dispose(hp);
  759. end;
  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. if assigned(procs^.next) then
  774. begin
  775. CGMessage(cg_e_cant_choose_overload_function);
  776. aktcallprocsym^.write_parameter_lists;
  777. end;
  778. {$ifdef TEST_PROCSYMS}
  779. if (procs=nil) and assigned(nextprocsym) then
  780. begin
  781. p^.symtableprocentry:=nextprocsym;
  782. p^.symtableproc:=symt;
  783. end;
  784. end ; { of while assigned(p^.symtableprocentry) do }
  785. {$endif TEST_PROCSYMS}
  786. if make_ref then
  787. begin
  788. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@p^.fileinfo));
  789. if procs^.data^.defref=nil then
  790. procs^.data^.defref:=procs^.data^.lastref;
  791. end;
  792. p^.procdefinition:=procs^.data;
  793. p^.resulttype:=procs^.data^.retdef;
  794. { big error for with statements
  795. p^.symtableproc:=p^.procdefinition^.owner;
  796. but neede for overloaded operators !! }
  797. if p^.symtableproc=nil then
  798. p^.symtableproc:=p^.procdefinition^.owner;
  799. p^.location.loc:=LOC_MEM;
  800. {$ifdef CHAINPROCSYMS}
  801. { object with method read;
  802. call to read(x) will be a usual procedure call }
  803. if assigned(p^.methodpointer) and
  804. (p^.procdefinition^._class=nil) then
  805. begin
  806. { not ok for extended }
  807. case p^.methodpointer^.treetype of
  808. typen,hnewn : fatalerror(no_para_match);
  809. end;
  810. disposetree(p^.methodpointer);
  811. p^.methodpointer:=nil;
  812. end;
  813. {$endif CHAINPROCSYMS}
  814. end; { end of procedure to call determination }
  815. is_const:=((p^.procdefinition^.options and pointernconst)<>0) and
  816. ((block_type=bt_const) or
  817. (assigned(p^.left) and (p^.left^.left^.treetype in [realconstn,ordconstn])));
  818. { handle predefined procedures }
  819. if ((p^.procdefinition^.options and pointernproc)<>0) or is_const then
  820. begin
  821. if assigned(p^.left) then
  822. begin
  823. { settextbuf needs two args }
  824. if assigned(p^.left^.right) then
  825. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left)
  826. else
  827. begin
  828. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left^.left);
  829. putnode(p^.left);
  830. end;
  831. end
  832. else
  833. begin
  834. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,nil);
  835. end;
  836. putnode(p);
  837. firstpass(pt);
  838. p:=pt;
  839. must_be_valid:=store_valid;
  840. if codegenerror then
  841. exit;
  842. dispose(procs);
  843. exit;
  844. end
  845. else
  846. { no intern procedure => we do a call }
  847. { calc the correture value for the register }
  848. { handle predefined procedures }
  849. if (p^.procdefinition^.options and poinline)<>0 then
  850. begin
  851. if assigned(p^.methodpointer) then
  852. CGMessage(cg_e_unable_inline_object_methods);
  853. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  854. CGMessage(cg_e_unable_inline_procvar);
  855. { p^.treetype:=procinlinen; }
  856. if not assigned(p^.right) then
  857. begin
  858. if assigned(p^.procdefinition^.code) then
  859. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  860. else
  861. CGMessage(cg_e_no_code_for_inline_stored);
  862. if assigned(inlinecode) then
  863. begin
  864. { consider it has not inlined if called
  865. again inside the args }
  866. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  867. firstpass(inlinecode);
  868. inlined:=true;
  869. end;
  870. end;
  871. end
  872. else
  873. procinfo.flags:=procinfo.flags or pi_do_call;
  874. { work trough all parameters to insert the type conversions }
  875. { !!! done now after internproc !! (PM) }
  876. if assigned(p^.left) then
  877. begin
  878. old_count_ref:=count_ref;
  879. count_ref:=true;
  880. firstcallparan(p^.left,p^.procdefinition^.para1);
  881. count_ref:=old_count_ref;
  882. end;
  883. {$ifdef i386}
  884. for regi:=R_EAX to R_EDI do
  885. begin
  886. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  887. inc(reg_pushes[regi],t_times*2);
  888. end;
  889. {$endif}
  890. {$ifdef m68k}
  891. for regi:=R_D0 to R_A6 do
  892. begin
  893. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  894. inc(reg_pushes[regi],t_times*2);
  895. end;
  896. {$endif}
  897. end;
  898. { ensure that the result type is set }
  899. p^.resulttype:=p^.procdefinition^.retdef;
  900. { get a register for the return value }
  901. if (p^.resulttype<>pdef(voiddef)) then
  902. begin
  903. if (p^.procdefinition^.options and poconstructor)<>0 then
  904. begin
  905. { extra handling of classes }
  906. { p^.methodpointer should be assigned! }
  907. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  908. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  909. begin
  910. p^.location.loc:=LOC_REGISTER;
  911. p^.registers32:=1;
  912. { the result type depends on the classref }
  913. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  914. end
  915. { a object constructor returns the result with the flags }
  916. else
  917. p^.location.loc:=LOC_FLAGS;
  918. end
  919. else
  920. begin
  921. {$ifdef SUPPORT_MMX}
  922. if (cs_mmx in aktlocalswitches) and
  923. is_mmx_able_array(p^.resulttype) then
  924. begin
  925. p^.location.loc:=LOC_MMXREGISTER;
  926. p^.registersmmx:=1;
  927. end
  928. else
  929. {$endif SUPPORT_MMX}
  930. if ret_in_acc(p^.resulttype) then
  931. begin
  932. p^.location.loc:=LOC_REGISTER;
  933. if is_64bitint(p^.resulttype) then
  934. p^.registers32:=2
  935. else
  936. p^.registers32:=1;
  937. end
  938. else if (p^.resulttype^.deftype=floatdef) then
  939. begin
  940. p^.location.loc:=LOC_FPU;
  941. p^.registersfpu:=1;
  942. end
  943. end;
  944. end;
  945. { a fpu can be used in any procedure !! }
  946. p^.registersfpu:=p^.procdefinition^.fpu_used;
  947. { if this is a call to a method calc the registers }
  948. if (p^.methodpointer<>nil) then
  949. begin
  950. case p^.methodpointer^.treetype of
  951. { but only, if this is not a supporting node }
  952. typen,hnewn : ;
  953. else
  954. begin
  955. {$ifndef NODIRECTWITH}
  956. if ((p^.procdefinition^.options and (poconstructor or podestructor)) <> 0) and
  957. assigned(p^.symtable) and (p^.symtable^.symtabletype=withsymtable) and
  958. not pwithsymtable(p^.symtable)^.direct_with then
  959. begin
  960. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  961. end; { Is accepted by Delphi !! }
  962. { this is not a good reason to accept it in FPC if we produce
  963. wrong code for it !!! (PM) }
  964. {$endif ndef NODIRECTWITH}
  965. { R.Assign is not a constructor !!! }
  966. { but for R^.Assign, R must be valid !! }
  967. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  968. ((p^.methodpointer^.treetype=loadn) and
  969. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  970. must_be_valid:=false
  971. else
  972. must_be_valid:=true;
  973. firstpass(p^.methodpointer);
  974. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  975. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  976. {$ifdef SUPPORT_MMX}
  977. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  978. {$endif SUPPORT_MMX}
  979. end;
  980. end;
  981. end;
  982. if inlined then
  983. begin
  984. p^.right:=inlinecode;
  985. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  986. end;
  987. { determine the registers of the procedure variable }
  988. { is this OK for inlined procs also ?? (PM) }
  989. if assigned(p^.right) then
  990. begin
  991. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  992. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  993. {$ifdef SUPPORT_MMX}
  994. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  995. {$endif SUPPORT_MMX}
  996. end;
  997. { determine the registers of the procedure }
  998. if assigned(p^.left) then
  999. begin
  1000. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  1001. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  1002. {$ifdef SUPPORT_MMX}
  1003. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  1004. {$endif SUPPORT_MMX}
  1005. end;
  1006. if assigned(procs) then
  1007. dispose(procs);
  1008. aktcallprocsym:=oldcallprocsym;
  1009. must_be_valid:=store_valid;
  1010. end;
  1011. {*****************************************************************************
  1012. FirstProcInlineN
  1013. *****************************************************************************}
  1014. procedure firstprocinline(var p : ptree);
  1015. begin
  1016. { left contains the code in tree form }
  1017. { but it has already been firstpassed }
  1018. { so firstpass(p^.left); does not seem required }
  1019. { might be required later if we change the arg handling !! }
  1020. end;
  1021. end.
  1022. {
  1023. $Log$
  1024. Revision 1.25 1999-02-22 15:09:44 florian
  1025. * behaviaor of PROTECTED and PRIVATE fixed, works now like TP/Delphi
  1026. Revision 1.24 1999/02/22 02:15:45 peter
  1027. * updates for ag386bin
  1028. Revision 1.23 1999/02/09 17:15:52 florian
  1029. * some false warnings "function result doesn't seems to be set" are
  1030. avoided
  1031. Revision 1.22 1999/01/29 11:34:55 pierre
  1032. + better info for impossible type conversion in calln
  1033. Revision 1.21 1999/01/21 22:10:49 peter
  1034. * fixed array of const
  1035. * generic platform independent high() support
  1036. Revision 1.20 1999/01/21 16:41:06 pierre
  1037. * fix for constructor inside with statements
  1038. Revision 1.19 1999/01/19 14:20:16 peter
  1039. * fixed [char] crash
  1040. Revision 1.18 1999/01/12 14:25:40 peter
  1041. + BrowserLog for browser.log generation
  1042. + BrowserCol for browser info in TCollections
  1043. * released all other UseBrowser
  1044. Revision 1.17 1998/12/11 00:03:52 peter
  1045. + globtype,tokens,version unit splitted from globals
  1046. Revision 1.16 1998/12/10 14:57:52 pierre
  1047. * fix for operators
  1048. Revision 1.15 1998/12/10 09:47:32 florian
  1049. + basic operations with int64/qord (compiler with -dint64)
  1050. + rtti of enumerations extended: names are now written
  1051. Revision 1.14 1998/11/27 14:50:52 peter
  1052. + open strings, $P switch support
  1053. Revision 1.13 1998/11/24 17:03:51 peter
  1054. * fixed exactmatch removings
  1055. Revision 1.12 1998/11/16 10:18:10 peter
  1056. * fixes for ansistrings
  1057. Revision 1.11 1998/11/10 10:09:17 peter
  1058. * va_list -> array of const
  1059. Revision 1.10 1998/11/09 11:44:41 peter
  1060. + va_list for printf support
  1061. Revision 1.9 1998/10/28 18:26:22 pierre
  1062. * removed some erros after other errors (introduced by useexcept)
  1063. * stabs works again correctly (for how long !)
  1064. Revision 1.8 1998/10/09 16:36:09 pierre
  1065. * some memory leaks specific to usebrowser define fixed
  1066. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  1067. Revision 1.7 1998/10/06 20:49:09 peter
  1068. * m68k compiler compiles again
  1069. Revision 1.6 1998/10/02 09:24:22 peter
  1070. * more constant expression evaluators
  1071. Revision 1.5 1998/09/28 11:22:17 pierre
  1072. * did not compile for browser
  1073. * merge from fixes
  1074. Revision 1.4 1998/09/27 10:16:24 florian
  1075. * type casts pchar<->ansistring fixed
  1076. * ansistring[..] calls does now an unique call
  1077. Revision 1.3 1998/09/24 14:27:40 peter
  1078. * some better support for openarray
  1079. Revision 1.2 1998/09/24 09:02:16 peter
  1080. * rewritten isconvertable to use case
  1081. * array of .. and single variable are compatible
  1082. Revision 1.1 1998/09/23 20:42:24 peter
  1083. * splitted pass_1
  1084. }