tccal.pas 51 KB

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