tccal.pas 47 KB

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