tccal.pas 49 KB

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