nld.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for load/assignment nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nld;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. {$ifdef state_tracking}
  23. nstate,
  24. {$endif}
  25. symconst,symbase,symtype,symsym,symdef;
  26. type
  27. tloadnode = class(tunarynode)
  28. symtableentry : tsym;
  29. symtableentryderef : tderef;
  30. symtable : tsymtable;
  31. procdef : tprocdef;
  32. procdefderef : tderef;
  33. constructor create(v : tsym;st : tsymtable);virtual;
  34. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. procedure set_mp(p:tnode);
  40. function is_addr_param_load:boolean;
  41. function getcopy : tnode;override;
  42. function pass_1 : tnode;override;
  43. function det_resulttype:tnode;override;
  44. procedure mark_write;override;
  45. function docompare(p: tnode): boolean; override;
  46. procedure printnodedata(var t:text);override;
  47. end;
  48. tloadnodeclass = class of tloadnode;
  49. { different assignment types }
  50. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  51. tassignmentnode = class(tbinarynode)
  52. assigntype : tassigntype;
  53. constructor create(l,r : tnode);virtual;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. function getcopy : tnode;override;
  57. function pass_1 : tnode;override;
  58. function det_resulttype:tnode;override;
  59. {$ifdef state_tracking}
  60. function track_state_pass(exec_known:boolean):boolean;override;
  61. {$endif state_tracking}
  62. function docompare(p: tnode): boolean; override;
  63. end;
  64. tassignmentnodeclass = class of tassignmentnode;
  65. tarrayconstructorrangenode = class(tbinarynode)
  66. constructor create(l,r : tnode);virtual;
  67. function pass_1 : tnode;override;
  68. function det_resulttype:tnode;override;
  69. end;
  70. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  71. tarrayconstructornode = class(tbinarynode)
  72. constructor create(l,r : tnode);virtual;
  73. function getcopy : tnode;override;
  74. function pass_1 : tnode;override;
  75. function det_resulttype:tnode;override;
  76. function docompare(p: tnode): boolean; override;
  77. procedure force_type(tt:ttype);
  78. procedure insert_typeconvs;
  79. end;
  80. tarrayconstructornodeclass = class of tarrayconstructornode;
  81. ttypenode = class(tnode)
  82. allowed : boolean;
  83. restype : ttype;
  84. constructor create(t : ttype);virtual;
  85. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure buildderefimpl;override;
  88. procedure derefimpl;override;
  89. function pass_1 : tnode;override;
  90. function det_resulttype:tnode;override;
  91. function docompare(p: tnode): boolean; override;
  92. end;
  93. ttypenodeclass = class of ttypenode;
  94. trttinode = class(tnode)
  95. l1,l2 : longint;
  96. rttitype : trttitype;
  97. rttidef : tstoreddef;
  98. rttidefderef : tderef;
  99. constructor create(def:tstoreddef;rt:trttitype);virtual;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function getcopy : tnode;override;
  105. function pass_1 : tnode;override;
  106. function det_resulttype:tnode;override;
  107. function docompare(p: tnode): boolean; override;
  108. end;
  109. trttinodeclass = class of trttinode;
  110. var
  111. cloadnode : tloadnodeclass;
  112. cassignmentnode : tassignmentnodeclass;
  113. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  114. carrayconstructornode : tarrayconstructornodeclass;
  115. ctypenode : ttypenodeclass;
  116. crttinode : trttinodeclass;
  117. implementation
  118. uses
  119. cutils,verbose,globtype,globals,systems,
  120. symnot,
  121. defutil,defcmp,
  122. htypechk,pass_1,procinfo,paramgr,
  123. ncon,ninl,ncnv,nmem,ncal,nutils,
  124. cgobj,cgbase
  125. ;
  126. {*****************************************************************************
  127. TLOADNODE
  128. *****************************************************************************}
  129. constructor tloadnode.create(v : tsym;st : tsymtable);
  130. begin
  131. inherited create(loadn,nil);
  132. if not assigned(v) then
  133. internalerror(200108121);
  134. symtableentry:=v;
  135. symtable:=st;
  136. procdef:=nil;
  137. end;
  138. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  139. begin
  140. inherited create(loadn,nil);
  141. if not assigned(v) then
  142. internalerror(200108121);
  143. symtableentry:=v;
  144. symtable:=st;
  145. procdef:=d;
  146. end;
  147. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  148. begin
  149. inherited ppuload(t,ppufile);
  150. ppufile.getderef(symtableentryderef);
  151. symtable:=nil;
  152. ppufile.getderef(procdefderef);
  153. end;
  154. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  155. begin
  156. inherited ppuwrite(ppufile);
  157. ppufile.putderef(symtableentryderef);
  158. ppufile.putderef(procdefderef);
  159. end;
  160. procedure tloadnode.buildderefimpl;
  161. begin
  162. inherited buildderefimpl;
  163. symtableentryderef.build(symtableentry);
  164. procdefderef.build(procdef);
  165. end;
  166. procedure tloadnode.derefimpl;
  167. begin
  168. inherited derefimpl;
  169. symtableentry:=tsym(symtableentryderef.resolve);
  170. symtable:=symtableentry.owner;
  171. procdef:=tprocdef(procdefderef.resolve);
  172. end;
  173. procedure tloadnode.set_mp(p:tnode);
  174. begin
  175. { typen nodes should not be set }
  176. if p.nodetype=typen then
  177. internalerror(200301042);
  178. left:=p;
  179. end;
  180. function tloadnode.getcopy : tnode;
  181. var
  182. n : tloadnode;
  183. begin
  184. n:=tloadnode(inherited getcopy);
  185. n.symtable:=symtable;
  186. n.symtableentry:=symtableentry;
  187. n.procdef:=procdef;
  188. result:=n;
  189. end;
  190. function tloadnode.is_addr_param_load:boolean;
  191. begin
  192. result:=(symtable.symtabletype=parasymtable) and
  193. (symtableentry.typ=paravarsym) and
  194. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  195. not(nf_load_self_pointer in flags) and
  196. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  197. end;
  198. function tloadnode.det_resulttype:tnode;
  199. begin
  200. result:=nil;
  201. case symtableentry.typ of
  202. absolutevarsym :
  203. resulttype:=tabsolutevarsym(symtableentry).vartype;
  204. constsym:
  205. begin
  206. if tconstsym(symtableentry).consttyp=constresourcestring then
  207. begin
  208. {$ifdef ansistring_bits}
  209. case aktansistring_bits of
  210. sb_16:
  211. resulttype:=cansistringtype16;
  212. sb_32:
  213. resulttype:=cansistringtype32;
  214. sb_64:
  215. resulttype:=cansistringtype64;
  216. end;
  217. {$else}
  218. resulttype:=cansistringtype
  219. {$endif}
  220. end
  221. else
  222. internalerror(22799);
  223. end;
  224. globalvarsym,
  225. paravarsym,
  226. localvarsym :
  227. begin
  228. inc(tabstractvarsym(symtableentry).refs);
  229. { Nested variable? The we need to load the framepointer of
  230. the parent procedure }
  231. if assigned(current_procinfo) then
  232. begin
  233. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  234. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  235. begin
  236. if assigned(left) then
  237. internalerror(200309289);
  238. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  239. { we can't inline the referenced parent procedure }
  240. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  241. { reference in nested procedures, variable needs to be in memory }
  242. make_not_regable(self);
  243. end;
  244. { static variables referenced in procedures or from finalization,
  245. variable needs to be in memory.
  246. It is too hard and the benefit is too small to detect whether a
  247. variable is only used in the finalization to add support for it (PFV) }
  248. if (symtable.symtabletype=staticsymtable) and
  249. (
  250. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  251. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  252. ) then
  253. make_not_regable(self);
  254. end;
  255. { fix self type which is declared as voidpointer in the
  256. definition }
  257. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  258. begin
  259. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  260. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  261. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  262. resulttype.setdef(tclassrefdef.create(resulttype))
  263. else if is_object(resulttype.def) and
  264. (nf_load_self_pointer in flags) then
  265. resulttype.setdef(tpointerdef.create(resulttype));
  266. end
  267. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  268. begin
  269. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  270. resulttype.setdef(tclassrefdef.create(resulttype));
  271. end
  272. else
  273. resulttype:=tabstractvarsym(symtableentry).vartype;
  274. end;
  275. typedconstsym :
  276. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  277. procsym :
  278. begin
  279. if not assigned(procdef) then
  280. begin
  281. if Tprocsym(symtableentry).procdef_count>1 then
  282. CGMessage(parser_e_no_overloaded_procvars);
  283. procdef:=tprocsym(symtableentry).first_procdef;
  284. end;
  285. { the result is a procdef, addrn and proc_to_procvar
  286. typeconvn need this as resulttype so they know
  287. that the address needs to be returned }
  288. resulttype.setdef(procdef);
  289. { process methodpointer }
  290. if assigned(left) then
  291. resulttypepass(left);
  292. end;
  293. labelsym:
  294. resulttype:=voidtype;
  295. else
  296. internalerror(200104141);
  297. end;
  298. end;
  299. procedure Tloadnode.mark_write;
  300. begin
  301. include(flags,nf_write);
  302. end;
  303. function tloadnode.pass_1 : tnode;
  304. begin
  305. result:=nil;
  306. expectloc:=LOC_REFERENCE;
  307. registersint:=0;
  308. registersfpu:=0;
  309. {$ifdef SUPPORT_MMX}
  310. registersmmx:=0;
  311. {$endif SUPPORT_MMX}
  312. if (cs_create_pic in aktmoduleswitches) and
  313. not(symtableentry.typ in [paravarsym,localvarsym]) then
  314. include(current_procinfo.flags,pi_needs_got);
  315. case symtableentry.typ of
  316. absolutevarsym :
  317. ;
  318. constsym:
  319. begin
  320. if tconstsym(symtableentry).consttyp=constresourcestring then
  321. expectloc:=LOC_CREFERENCE;
  322. end;
  323. globalvarsym,
  324. localvarsym,
  325. paravarsym :
  326. begin
  327. if assigned(left) then
  328. firstpass(left);
  329. if not is_addr_param_load and
  330. tabstractvarsym(symtableentry).is_regvar then
  331. begin
  332. case tabstractvarsym(symtableentry).varregable of
  333. vr_intreg :
  334. expectloc:=LOC_CREGISTER;
  335. vr_fpureg :
  336. expectloc:=LOC_CFPUREGISTER;
  337. vr_mmreg :
  338. expectloc:=LOC_CMMREGISTER;
  339. end
  340. end
  341. else
  342. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  343. expectloc:=LOC_CREFERENCE;
  344. { we need a register for call by reference parameters }
  345. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  346. registersint:=1;
  347. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  348. registersint:=1;
  349. if (target_info.system=system_powerpc_darwin) and
  350. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  351. include(current_procinfo.flags,pi_needs_got);
  352. { call to get address of threadvar }
  353. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  354. include(current_procinfo.flags,pi_do_call);
  355. if nf_write in flags then
  356. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  357. else
  358. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  359. { count variable references }
  360. if cg.t_times>1 then
  361. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  362. end;
  363. typedconstsym :
  364. ;
  365. procsym :
  366. begin
  367. { method pointer ? }
  368. if assigned(left) then
  369. begin
  370. expectloc:=LOC_CREFERENCE;
  371. firstpass(left);
  372. registersint:=max(registersint,left.registersint);
  373. registersfpu:=max(registersfpu,left.registersfpu);
  374. {$ifdef SUPPORT_MMX}
  375. registersmmx:=max(registersmmx,left.registersmmx);
  376. {$endif SUPPORT_MMX}
  377. end;
  378. end;
  379. labelsym :
  380. ;
  381. else
  382. internalerror(200104143);
  383. end;
  384. end;
  385. function tloadnode.docompare(p: tnode): boolean;
  386. begin
  387. docompare :=
  388. inherited docompare(p) and
  389. (symtableentry = tloadnode(p).symtableentry) and
  390. (procdef = tloadnode(p).procdef) and
  391. (symtable = tloadnode(p).symtable);
  392. end;
  393. procedure Tloadnode.printnodedata(var t:text);
  394. begin
  395. inherited printnodedata(t);
  396. write(t,printnodeindention,'symbol = ',symtableentry.name);
  397. if symtableentry.typ=procsym then
  398. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  399. writeln(t,'');
  400. end;
  401. {*****************************************************************************
  402. TASSIGNMENTNODE
  403. *****************************************************************************}
  404. constructor tassignmentnode.create(l,r : tnode);
  405. begin
  406. inherited create(assignn,l,r);
  407. l.mark_write;
  408. assigntype:=at_normal;
  409. end;
  410. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  411. begin
  412. inherited ppuload(t,ppufile);
  413. assigntype:=tassigntype(ppufile.getbyte);
  414. end;
  415. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  416. begin
  417. inherited ppuwrite(ppufile);
  418. ppufile.putbyte(byte(assigntype));
  419. end;
  420. function tassignmentnode.getcopy : tnode;
  421. var
  422. n : tassignmentnode;
  423. begin
  424. n:=tassignmentnode(inherited getcopy);
  425. n.assigntype:=assigntype;
  426. getcopy:=n;
  427. end;
  428. function tassignmentnode.det_resulttype:tnode;
  429. var
  430. hp : tnode;
  431. useshelper : boolean;
  432. begin
  433. result:=nil;
  434. resulttype:=voidtype;
  435. { must be made unique }
  436. set_unique(left);
  437. resulttypepass(left);
  438. if is_ansistring(left.resulttype.def) then
  439. begin
  440. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  441. if (right.nodetype=addn) and
  442. left.isequal(tbinarynode(right).left) and
  443. { don't fold multiple concatenations else we could get trouble
  444. with multiple uses of s
  445. }
  446. (tbinarynode(right).left.nodetype<>addn) and
  447. (tbinarynode(right).right.nodetype<>addn) then
  448. begin
  449. { don't do a resulttypepass(right), since then the addnode }
  450. { may insert typeconversions that make this optimization }
  451. { opportunity quite difficult to detect (JM) }
  452. resulttypepass(tbinarynode(right).left);
  453. resulttypepass(tbinarynode(right).right);
  454. if (is_char(tbinarynode(right).right.resulttype.def) or
  455. is_shortstring(tbinarynode(right).right.resulttype.def) or
  456. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  457. begin
  458. { remove property flag so it'll not trigger an error }
  459. exclude(left.flags,nf_isproperty);
  460. { generate call to helper }
  461. hp:=ccallparanode.create(tbinarynode(right).right,
  462. ccallparanode.create(left,nil));
  463. if is_char(tbinarynode(right).right.resulttype.def) then
  464. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  465. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  466. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  467. else
  468. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  469. tbinarynode(right).right:=nil;
  470. left:=nil;
  471. exit;
  472. end;
  473. end;
  474. end
  475. else
  476. if is_shortstring(left.resulttype.def) then
  477. begin
  478. { fold <shortstring>:=<shortstring>+<shortstring>,
  479. <shortstring>+<char> is handled by an optimized node }
  480. if (right.nodetype=addn) and
  481. left.isequal(tbinarynode(right).left) and
  482. { don't fold multiple concatenations else we could get trouble
  483. with multiple uses of s }
  484. (tbinarynode(right).left.nodetype<>addn) and
  485. (tbinarynode(right).right.nodetype<>addn) then
  486. begin
  487. { don't do a resulttypepass(right), since then the addnode }
  488. { may insert typeconversions that make this optimization }
  489. { opportunity quite difficult to detect (JM) }
  490. resulttypepass(tbinarynode(right).left);
  491. resulttypepass(tbinarynode(right).right);
  492. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  493. begin
  494. { remove property flag so it'll not trigger an error }
  495. exclude(left.flags,nf_isproperty);
  496. { generate call to helper }
  497. hp:=ccallparanode.create(tbinarynode(right).right,
  498. ccallparanode.create(left,nil));
  499. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  500. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  501. tbinarynode(right).right:=nil;
  502. left:=nil;
  503. exit;
  504. end;
  505. end;
  506. end;
  507. resulttypepass(right);
  508. set_varstate(right,vs_read,[vsf_must_be_valid]);
  509. set_varstate(left,vs_written,[]);
  510. if codegenerror then
  511. exit;
  512. { tp procvar support, when we don't expect a procvar
  513. then we need to call the procvar }
  514. if (left.resulttype.def.deftype<>procvardef) then
  515. maybe_call_procvar(right,true);
  516. { assignments to formaldefs and open arrays aren't allowed }
  517. if (left.resulttype.def.deftype=formaldef) or
  518. is_open_array(left.resulttype.def) then
  519. CGMessage(type_e_operator_not_allowed);
  520. { test if node can be assigned, properties are allowed }
  521. valid_for_assignment(left,true);
  522. { assigning nil to a dynamic array clears the array }
  523. if is_dynamic_array(left.resulttype.def) and
  524. (right.nodetype=niln) then
  525. begin
  526. hp:=ccallparanode.create(caddrnode.create_internal
  527. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  528. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  529. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  530. left:=nil;
  531. exit;
  532. end;
  533. { shortstring helpers can do the conversion directly,
  534. so treat them separatly }
  535. if (is_shortstring(left.resulttype.def)) then
  536. begin
  537. { insert typeconv, except for chars that are handled in
  538. secondpass and except for ansi/wide string that can
  539. be converted immediatly }
  540. if not(is_char(right.resulttype.def) or
  541. (right.resulttype.def.deftype=stringdef)) then
  542. inserttypeconv(right,left.resulttype);
  543. if right.resulttype.def.deftype=stringdef then
  544. begin
  545. useshelper:=true;
  546. { convert constant strings to shortstrings. But
  547. skip empty constant strings, that will be handled
  548. in secondpass }
  549. if (right.nodetype=stringconstn) then
  550. begin
  551. { verify if range fits within shortstring }
  552. { just emit a warning, delphi gives an }
  553. { error, only if the type definition of }
  554. { of the string is less < 255 characters }
  555. if not is_open_string(left.resulttype.def) and
  556. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  557. cgmessage(type_w_string_too_long);
  558. inserttypeconv(right,left.resulttype);
  559. if (tstringconstnode(right).len=0) then
  560. useshelper:=false;
  561. end;
  562. { rest is done in pass 1 (JM) }
  563. if useshelper then
  564. exit;
  565. end
  566. end
  567. else
  568. begin
  569. { check if the assignment may cause a range check error }
  570. check_ranges(fileinfo,right,left.resulttype.def);
  571. inserttypeconv(right,left.resulttype);
  572. end;
  573. { call helpers for interface }
  574. if is_interfacecom(left.resulttype.def) then
  575. begin
  576. hp:=ccallparanode.create(ctypeconvnode.create_internal
  577. (right,voidpointertype),
  578. ccallparanode.create(ctypeconvnode.create_internal
  579. (left,voidpointertype),nil));
  580. result:=ccallnode.createintern('fpc_intf_assign',hp);
  581. left:=nil;
  582. right:=nil;
  583. exit;
  584. end;
  585. { call helpers for variant, they can contain non ref. counted types like
  586. vararrays which must be really copied }
  587. if left.resulttype.def.deftype=variantdef then
  588. begin
  589. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  590. caddrnode.create_internal(right),voidpointertype),
  591. ccallparanode.create(ctypeconvnode.create_internal(
  592. caddrnode.create_internal(left),voidpointertype),
  593. nil));
  594. result:=ccallnode.createintern('fpc_variant_copy',hp);
  595. left:=nil;
  596. right:=nil;
  597. exit;
  598. end;
  599. { check if local proc/func is assigned to procvar }
  600. if right.resulttype.def.deftype=procvardef then
  601. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  602. end;
  603. function tassignmentnode.pass_1 : tnode;
  604. var
  605. hp: tnode;
  606. begin
  607. result:=nil;
  608. expectloc:=LOC_VOID;
  609. firstpass(left);
  610. firstpass(right);
  611. { assignment to refcounted variable -> inc/decref }
  612. if (not is_class(left.resulttype.def) and
  613. left.resulttype.def.needs_inittable) then
  614. include(current_procinfo.flags,pi_do_call);
  615. if codegenerror then
  616. exit;
  617. if (is_shortstring(left.resulttype.def)) then
  618. begin
  619. if right.resulttype.def.deftype=stringdef then
  620. begin
  621. if (right.nodetype<>stringconstn) or
  622. (tstringconstnode(right).len<>0) then
  623. begin
  624. if (cs_optimize in aktglobalswitches) and
  625. (right.nodetype in [calln,blockn]) and
  626. (left.nodetype = temprefn) and
  627. is_shortstring(right.resulttype.def) and
  628. not is_open_string(left.resulttype.def) and
  629. (tstringdef(left.resulttype.def).len = 255) then
  630. begin
  631. { the blocknode case is handled in pass_2 at the temp }
  632. { reference level (mainly for callparatemp) (JM) }
  633. if (right.nodetype = calln) then
  634. begin
  635. tcallnode(right).funcretnode := left;
  636. result := right;
  637. end
  638. else
  639. exit;
  640. end
  641. else
  642. begin
  643. hp:=ccallparanode.create
  644. (right,
  645. ccallparanode.create(cinlinenode.create
  646. (in_high_x,false,left.getcopy),nil));
  647. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  648. firstpass(result);
  649. end;
  650. left:=nil;
  651. right:=nil;
  652. exit;
  653. end;
  654. end;
  655. end;
  656. if (cs_optimize in aktglobalswitches) and
  657. (right.nodetype = calln) and
  658. { left must be a temp, since otherwise as soon as you modify the }
  659. { result, the current left node is modified and that one may }
  660. { still be an argument to the function or even accessed in the }
  661. { function }
  662. (((left.nodetype = temprefn) and
  663. paramanager.ret_in_param(right.resulttype.def,
  664. tcallnode(right).procdefinition.proccalloption)) or
  665. { there's special support for ansi/widestrings in the callnode }
  666. is_ansistring(right.resulttype.def) or
  667. is_widestring(right.resulttype.def)) then
  668. begin
  669. tcallnode(right).funcretnode := left;
  670. result := right;
  671. left := nil;
  672. right := nil;
  673. exit;
  674. end;
  675. registersint:=left.registersint+right.registersint;
  676. registersfpu:=max(left.registersfpu,right.registersfpu);
  677. {$ifdef SUPPORT_MMX}
  678. registersmmx:=max(left.registersmmx,right.registersmmx);
  679. {$endif SUPPORT_MMX}
  680. end;
  681. function tassignmentnode.docompare(p: tnode): boolean;
  682. begin
  683. docompare :=
  684. inherited docompare(p) and
  685. (assigntype = tassignmentnode(p).assigntype);
  686. end;
  687. {$ifdef state_tracking}
  688. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  689. var se:Tstate_entry;
  690. begin
  691. track_state_pass:=false;
  692. if exec_known then
  693. begin
  694. track_state_pass:=right.track_state_pass(exec_known);
  695. {Force a new resulttype pass.}
  696. right.resulttype.def:=nil;
  697. do_resulttypepass(right);
  698. resulttypepass(right);
  699. aktstate.store_fact(left.getcopy,right.getcopy);
  700. end
  701. else
  702. aktstate.delete_fact(left);
  703. end;
  704. {$endif}
  705. {*****************************************************************************
  706. TARRAYCONSTRUCTORRANGENODE
  707. *****************************************************************************}
  708. constructor tarrayconstructorrangenode.create(l,r : tnode);
  709. begin
  710. inherited create(arrayconstructorrangen,l,r);
  711. end;
  712. function tarrayconstructorrangenode.det_resulttype:tnode;
  713. begin
  714. result:=nil;
  715. resulttypepass(left);
  716. resulttypepass(right);
  717. set_varstate(left,vs_read,[vsf_must_be_valid]);
  718. set_varstate(right,vs_read,[vsf_must_be_valid]);
  719. if codegenerror then
  720. exit;
  721. resulttype:=left.resulttype;
  722. end;
  723. function tarrayconstructorrangenode.pass_1 : tnode;
  724. begin
  725. firstpass(left);
  726. firstpass(right);
  727. expectloc:=LOC_CREFERENCE;
  728. calcregisters(self,0,0,0);
  729. result:=nil;
  730. end;
  731. {****************************************************************************
  732. TARRAYCONSTRUCTORNODE
  733. *****************************************************************************}
  734. constructor tarrayconstructornode.create(l,r : tnode);
  735. begin
  736. inherited create(arrayconstructorn,l,r);
  737. end;
  738. function tarrayconstructornode.getcopy : tnode;
  739. var
  740. n : tarrayconstructornode;
  741. begin
  742. n:=tarrayconstructornode(inherited getcopy);
  743. result:=n;
  744. end;
  745. function tarrayconstructornode.det_resulttype:tnode;
  746. var
  747. htype : ttype;
  748. hp : tarrayconstructornode;
  749. len : longint;
  750. varia : boolean;
  751. begin
  752. result:=nil;
  753. { are we allowing array constructor? Then convert it to a set }
  754. if not allow_array_constructor then
  755. begin
  756. hp:=tarrayconstructornode(getcopy);
  757. arrayconstructor_to_set(tnode(hp));
  758. result:=hp;
  759. exit;
  760. end;
  761. { only pass left tree, right tree contains next construct if any }
  762. htype.reset;
  763. len:=0;
  764. varia:=false;
  765. if assigned(left) then
  766. begin
  767. hp:=self;
  768. while assigned(hp) do
  769. begin
  770. resulttypepass(hp.left);
  771. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  772. if (htype.def=nil) then
  773. htype:=hp.left.resulttype
  774. else
  775. begin
  776. if ((nf_novariaallowed in flags) or (not varia)) and
  777. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  778. begin
  779. varia:=true;
  780. end;
  781. end;
  782. inc(len);
  783. hp:=tarrayconstructornode(hp.right);
  784. end;
  785. end;
  786. { Set the type of empty or varia arrays to void. Also
  787. do this if the type is array of const/open array
  788. because those can't be used with setelementtype }
  789. if not assigned(htype.def) or
  790. varia or
  791. is_array_of_const(htype.def) or
  792. is_open_array(htype.def) then
  793. htype:=voidtype;
  794. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  795. tarraydef(resulttype.def).setelementtype(htype);
  796. tarraydef(resulttype.def).IsConstructor:=true;
  797. tarraydef(resulttype.def).IsVariant:=varia;
  798. end;
  799. procedure tarrayconstructornode.force_type(tt:ttype);
  800. var
  801. hp : tarrayconstructornode;
  802. begin
  803. tarraydef(resulttype.def).setelementtype(tt);
  804. tarraydef(resulttype.def).IsConstructor:=true;
  805. tarraydef(resulttype.def).IsVariant:=false;
  806. if assigned(left) then
  807. begin
  808. hp:=self;
  809. while assigned(hp) do
  810. begin
  811. inserttypeconv(hp.left,tt);
  812. hp:=tarrayconstructornode(hp.right);
  813. end;
  814. end;
  815. end;
  816. procedure tarrayconstructornode.insert_typeconvs;
  817. var
  818. hp : tarrayconstructornode;
  819. dovariant : boolean;
  820. begin
  821. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  822. { only pass left tree, right tree contains next construct if any }
  823. if assigned(left) then
  824. begin
  825. hp:=self;
  826. while assigned(hp) do
  827. begin
  828. resulttypepass(hp.left);
  829. { Insert typeconvs for array of const }
  830. if dovariant then
  831. begin
  832. case hp.left.resulttype.def.deftype of
  833. enumdef :
  834. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  835. arraydef :
  836. begin
  837. if is_chararray(hp.left.resulttype.def) then
  838. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  839. else
  840. if is_widechararray(hp.left.resulttype.def) then
  841. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  842. else
  843. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  844. end;
  845. orddef :
  846. begin
  847. if is_integer(hp.left.resulttype.def) and
  848. not(is_64bitint(hp.left.resulttype.def)) then
  849. hp.left:=ctypeconvnode.create(hp.left,s32inttype)
  850. else if is_void(hp.left.resulttype.def) then
  851. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  852. end;
  853. floatdef :
  854. if not(is_currency(hp.left.resulttype.def)) then
  855. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  856. procvardef :
  857. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  858. stringdef,
  859. variantdef,
  860. pointerdef,
  861. classrefdef:
  862. ;
  863. objectdef :
  864. if is_object(hp.left.resulttype.def) then
  865. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  866. else
  867. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  868. end;
  869. end;
  870. resulttypepass(hp.left);
  871. hp:=tarrayconstructornode(hp.right);
  872. end;
  873. end;
  874. end;
  875. function tarrayconstructornode.pass_1 : tnode;
  876. var
  877. hp : tarrayconstructornode;
  878. do_variant:boolean;
  879. begin
  880. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  881. result:=nil;
  882. { Insert required type convs, this must be
  883. done in pass 1, because the call must be
  884. resulttypepassed already }
  885. if assigned(left) then
  886. begin
  887. insert_typeconvs;
  888. { call firstpass for all nodes }
  889. hp:=self;
  890. while assigned(hp) do
  891. begin
  892. if hp.left<>nil then
  893. begin
  894. {This check is pessimistic; a call will happen depending
  895. on the location in which the elements will be found in
  896. pass 2.}
  897. if not do_variant then
  898. include(current_procinfo.flags,pi_do_call);
  899. firstpass(hp.left);
  900. end;
  901. hp:=tarrayconstructornode(hp.right);
  902. end;
  903. end;
  904. expectloc:=LOC_CREFERENCE;
  905. calcregisters(self,0,0,0);
  906. end;
  907. function tarrayconstructornode.docompare(p: tnode): boolean;
  908. begin
  909. docompare:=inherited docompare(p);
  910. end;
  911. {*****************************************************************************
  912. TTYPENODE
  913. *****************************************************************************}
  914. constructor ttypenode.create(t : ttype);
  915. begin
  916. inherited create(typen);
  917. restype:=t;
  918. allowed:=false;
  919. end;
  920. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  921. begin
  922. inherited ppuload(t,ppufile);
  923. ppufile.gettype(restype);
  924. allowed:=boolean(ppufile.getbyte);
  925. end;
  926. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  927. begin
  928. inherited ppuwrite(ppufile);
  929. ppufile.puttype(restype);
  930. ppufile.putbyte(byte(allowed));
  931. end;
  932. procedure ttypenode.buildderefimpl;
  933. begin
  934. inherited buildderefimpl;
  935. restype.buildderef;
  936. end;
  937. procedure ttypenode.derefimpl;
  938. begin
  939. inherited derefimpl;
  940. restype.resolve;
  941. end;
  942. function ttypenode.det_resulttype:tnode;
  943. begin
  944. result:=nil;
  945. resulttype:=restype;
  946. { check if it's valid }
  947. if restype.def.deftype = errordef then
  948. CGMessage(parser_e_illegal_expression);
  949. end;
  950. function ttypenode.pass_1 : tnode;
  951. begin
  952. result:=nil;
  953. expectloc:=LOC_VOID;
  954. { a typenode can't generate code, so we give here
  955. an error. Else it'll be an abstract error in pass_2.
  956. Only when the allowed flag is set we don't generate
  957. an error }
  958. if not allowed then
  959. Message(parser_e_no_type_not_allowed_here);
  960. end;
  961. function ttypenode.docompare(p: tnode): boolean;
  962. begin
  963. docompare :=
  964. inherited docompare(p);
  965. end;
  966. {*****************************************************************************
  967. TRTTINODE
  968. *****************************************************************************}
  969. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  970. begin
  971. inherited create(rttin);
  972. rttidef:=def;
  973. rttitype:=rt;
  974. end;
  975. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  976. begin
  977. inherited ppuload(t,ppufile);
  978. ppufile.getderef(rttidefderef);
  979. rttitype:=trttitype(ppufile.getbyte);
  980. end;
  981. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  982. begin
  983. inherited ppuwrite(ppufile);
  984. ppufile.putderef(rttidefderef);
  985. ppufile.putbyte(byte(rttitype));
  986. end;
  987. procedure trttinode.buildderefimpl;
  988. begin
  989. inherited buildderefimpl;
  990. rttidefderef.build(rttidef);
  991. end;
  992. procedure trttinode.derefimpl;
  993. begin
  994. inherited derefimpl;
  995. rttidef:=tstoreddef(rttidefderef.resolve);
  996. end;
  997. function trttinode.getcopy : tnode;
  998. var
  999. n : trttinode;
  1000. begin
  1001. n:=trttinode(inherited getcopy);
  1002. n.rttidef:=rttidef;
  1003. n.rttitype:=rttitype;
  1004. result:=n;
  1005. end;
  1006. function trttinode.det_resulttype:tnode;
  1007. begin
  1008. { rtti information will be returned as a void pointer }
  1009. result:=nil;
  1010. resulttype:=voidpointertype;
  1011. end;
  1012. function trttinode.pass_1 : tnode;
  1013. begin
  1014. result:=nil;
  1015. expectloc:=LOC_CREFERENCE;
  1016. end;
  1017. function trttinode.docompare(p: tnode): boolean;
  1018. begin
  1019. docompare :=
  1020. inherited docompare(p) and
  1021. (rttidef = trttinode(p).rttidef) and
  1022. (rttitype = trttinode(p).rttitype);
  1023. end;
  1024. begin
  1025. cloadnode:=tloadnode;
  1026. cassignmentnode:=tassignmentnode;
  1027. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1028. carrayconstructornode:=tarrayconstructornode;
  1029. ctypenode:=ttypenode;
  1030. crttinode:=trttinode;
  1031. end.