nld.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. { reference in nested procedures, variable needs to be in memory }
  240. make_not_regable(self);
  241. end;
  242. { static variables referenced in procedures or from finalization,
  243. variable needs to be in memory.
  244. It is too hard and the benefit is too small to detect whether a
  245. variable is only used in the finalization to add support for it (PFV) }
  246. if (symtable.symtabletype=staticsymtable) and
  247. (
  248. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  249. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  250. ) then
  251. make_not_regable(self);
  252. end;
  253. { fix self type which is declared as voidpointer in the
  254. definition }
  255. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  256. begin
  257. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  258. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  259. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  260. resulttype.setdef(tclassrefdef.create(resulttype))
  261. else if is_object(resulttype.def) and
  262. (nf_load_self_pointer in flags) then
  263. resulttype.setdef(tpointerdef.create(resulttype));
  264. end
  265. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  266. begin
  267. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  268. resulttype.setdef(tclassrefdef.create(resulttype));
  269. end
  270. else
  271. resulttype:=tabstractvarsym(symtableentry).vartype;
  272. end;
  273. typedconstsym :
  274. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  275. procsym :
  276. begin
  277. if not assigned(procdef) then
  278. begin
  279. if Tprocsym(symtableentry).procdef_count>1 then
  280. CGMessage(parser_e_no_overloaded_procvars);
  281. procdef:=tprocsym(symtableentry).first_procdef;
  282. end;
  283. { the result is a procdef, addrn and proc_to_procvar
  284. typeconvn need this as resulttype so they know
  285. that the address needs to be returned }
  286. resulttype.setdef(procdef);
  287. { process methodpointer }
  288. if assigned(left) then
  289. resulttypepass(left);
  290. end;
  291. labelsym:
  292. resulttype:=voidtype;
  293. else
  294. internalerror(200104141);
  295. end;
  296. end;
  297. procedure Tloadnode.mark_write;
  298. begin
  299. include(flags,nf_write);
  300. end;
  301. function tloadnode.pass_1 : tnode;
  302. begin
  303. result:=nil;
  304. expectloc:=LOC_REFERENCE;
  305. registersint:=0;
  306. registersfpu:=0;
  307. {$ifdef SUPPORT_MMX}
  308. registersmmx:=0;
  309. {$endif SUPPORT_MMX}
  310. if (cs_create_pic in aktmoduleswitches) and
  311. not(symtableentry.typ in [paravarsym,localvarsym]) then
  312. include(current_procinfo.flags,pi_needs_got);
  313. case symtableentry.typ of
  314. absolutevarsym :
  315. ;
  316. constsym:
  317. begin
  318. if tconstsym(symtableentry).consttyp=constresourcestring then
  319. expectloc:=LOC_CREFERENCE;
  320. end;
  321. globalvarsym,
  322. localvarsym,
  323. paravarsym :
  324. begin
  325. if assigned(left) then
  326. firstpass(left);
  327. if not is_addr_param_load and
  328. tabstractvarsym(symtableentry).is_regvar then
  329. begin
  330. case tabstractvarsym(symtableentry).varregable of
  331. vr_intreg :
  332. expectloc:=LOC_CREGISTER;
  333. vr_fpureg :
  334. expectloc:=LOC_CFPUREGISTER;
  335. vr_mmreg :
  336. expectloc:=LOC_CMMREGISTER;
  337. end
  338. end
  339. else
  340. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  341. expectloc:=LOC_CREFERENCE;
  342. { we need a register for call by reference parameters }
  343. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  344. registersint:=1;
  345. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  346. registersint:=1;
  347. if (target_info.system=system_powerpc_darwin) and (vo_is_dll_var in tabstractvarsym(symtableentry).varoptions) then
  348. include(current_procinfo.flags,pi_needs_got);
  349. { call to get address of threadvar }
  350. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  351. include(current_procinfo.flags,pi_do_call);
  352. if nf_write in flags then
  353. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  354. else
  355. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  356. { count variable references }
  357. if cg.t_times>1 then
  358. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  359. end;
  360. typedconstsym :
  361. ;
  362. procsym :
  363. begin
  364. { method pointer ? }
  365. if assigned(left) then
  366. begin
  367. expectloc:=LOC_CREFERENCE;
  368. firstpass(left);
  369. registersint:=max(registersint,left.registersint);
  370. registersfpu:=max(registersfpu,left.registersfpu);
  371. {$ifdef SUPPORT_MMX}
  372. registersmmx:=max(registersmmx,left.registersmmx);
  373. {$endif SUPPORT_MMX}
  374. end;
  375. end;
  376. labelsym :
  377. ;
  378. else
  379. internalerror(200104143);
  380. end;
  381. end;
  382. function tloadnode.docompare(p: tnode): boolean;
  383. begin
  384. docompare :=
  385. inherited docompare(p) and
  386. (symtableentry = tloadnode(p).symtableentry) and
  387. (procdef = tloadnode(p).procdef) and
  388. (symtable = tloadnode(p).symtable);
  389. end;
  390. procedure Tloadnode.printnodedata(var t:text);
  391. begin
  392. inherited printnodedata(t);
  393. write(t,printnodeindention,'symbol = ',symtableentry.name);
  394. if symtableentry.typ=procsym then
  395. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  396. writeln(t,'');
  397. end;
  398. {*****************************************************************************
  399. TASSIGNMENTNODE
  400. *****************************************************************************}
  401. constructor tassignmentnode.create(l,r : tnode);
  402. begin
  403. inherited create(assignn,l,r);
  404. l.mark_write;
  405. assigntype:=at_normal;
  406. end;
  407. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  408. begin
  409. inherited ppuload(t,ppufile);
  410. assigntype:=tassigntype(ppufile.getbyte);
  411. end;
  412. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  413. begin
  414. inherited ppuwrite(ppufile);
  415. ppufile.putbyte(byte(assigntype));
  416. end;
  417. function tassignmentnode._getcopy : tnode;
  418. var
  419. n : tassignmentnode;
  420. begin
  421. n:=tassignmentnode(inherited _getcopy);
  422. n.assigntype:=assigntype;
  423. result:=n;
  424. end;
  425. function tassignmentnode.det_resulttype:tnode;
  426. var
  427. hp : tnode;
  428. useshelper : boolean;
  429. original_size : longint;
  430. begin
  431. result:=nil;
  432. resulttype:=voidtype;
  433. original_size := 0;
  434. { must be made unique }
  435. set_unique(left);
  436. resulttypepass(left);
  437. if is_ansistring(left.resulttype.def) then
  438. begin
  439. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  440. if (right.nodetype=addn) and
  441. left.isequal(tbinarynode(right).left) and
  442. { don't fold multiple concatenations else we could get trouble
  443. with multiple uses of s
  444. }
  445. (tbinarynode(right).left.nodetype<>addn) and
  446. (tbinarynode(right).right.nodetype<>addn) then
  447. begin
  448. { don't do a resulttypepass(right), since then the addnode }
  449. { may insert typeconversions that make this optimization }
  450. { opportunity quite difficult to detect (JM) }
  451. resulttypepass(tbinarynode(right).left);
  452. resulttypepass(tbinarynode(right).right);
  453. if (is_char(tbinarynode(right).right.resulttype.def) or
  454. is_shortstring(tbinarynode(right).right.resulttype.def) or
  455. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  456. begin
  457. { remove property flag so it'll not trigger an error }
  458. exclude(left.flags,nf_isproperty);
  459. { generate call to helper }
  460. hp:=ccallparanode.create(tbinarynode(right).right,
  461. ccallparanode.create(left,nil));
  462. if is_char(tbinarynode(right).right.resulttype.def) then
  463. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  464. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  465. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  466. else
  467. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  468. tbinarynode(right).right:=nil;
  469. left:=nil;
  470. exit;
  471. end;
  472. end;
  473. end
  474. else
  475. if is_shortstring(left.resulttype.def) then
  476. begin
  477. { fold <shortstring>:=<shortstring>+<shortstring>,
  478. <shortstring>+<char> is handled by an optimized node }
  479. if (right.nodetype=addn) and
  480. left.isequal(tbinarynode(right).left) and
  481. { don't fold multiple concatenations else we could get trouble
  482. with multiple uses of s }
  483. (tbinarynode(right).left.nodetype<>addn) and
  484. (tbinarynode(right).right.nodetype<>addn) then
  485. begin
  486. { don't do a resulttypepass(right), since then the addnode }
  487. { may insert typeconversions that make this optimization }
  488. { opportunity quite difficult to detect (JM) }
  489. resulttypepass(tbinarynode(right).left);
  490. resulttypepass(tbinarynode(right).right);
  491. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  492. begin
  493. { remove property flag so it'll not trigger an error }
  494. exclude(left.flags,nf_isproperty);
  495. { generate call to helper }
  496. hp:=ccallparanode.create(tbinarynode(right).right,
  497. ccallparanode.create(left,nil));
  498. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  499. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  500. tbinarynode(right).right:=nil;
  501. left:=nil;
  502. exit;
  503. end;
  504. end;
  505. end;
  506. resulttypepass(right);
  507. set_varstate(left,vs_assigned,[]);
  508. set_varstate(right,vs_used,[vsf_must_be_valid]);
  509. if codegenerror then
  510. exit;
  511. { tp procvar support, when we don't expect a procvar
  512. then we need to call the procvar }
  513. if (left.resulttype.def.deftype<>procvardef) then
  514. maybe_call_procvar(right,true);
  515. { assignments to formaldefs and open arrays aren't allowed }
  516. if (left.resulttype.def.deftype=formaldef) or
  517. is_open_array(left.resulttype.def) then
  518. CGMessage(type_e_operator_not_allowed);
  519. { test if node can be assigned, properties are allowed }
  520. valid_for_assignment(left);
  521. { assigning nil to a dynamic array clears the array }
  522. if is_dynamic_array(left.resulttype.def) and
  523. (right.nodetype=niln) then
  524. begin
  525. hp:=ccallparanode.create(caddrnode.create_internal
  526. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  527. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  528. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  529. left:=nil;
  530. exit;
  531. end;
  532. { shortstring helpers can do the conversion directly,
  533. so treat them separatly }
  534. if (is_shortstring(left.resulttype.def)) then
  535. begin
  536. { insert typeconv, except for chars that are handled in
  537. secondpass and except for ansi/wide string that can
  538. be converted immediatly }
  539. if not(is_char(right.resulttype.def) or
  540. (right.resulttype.def.deftype=stringdef)) then
  541. inserttypeconv(right,left.resulttype);
  542. if right.resulttype.def.deftype=stringdef then
  543. begin
  544. useshelper:=true;
  545. { convert constant strings to shortstrings. But
  546. skip empty constant strings, that will be handled
  547. in secondpass }
  548. if (right.nodetype=stringconstn) then
  549. begin
  550. { verify if range fits within shortstring }
  551. { just emit a warning, delphi gives an }
  552. { error, only if the type definition of }
  553. { of the string is less < 255 characters }
  554. if not is_open_string(left.resulttype.def) and
  555. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  556. cgmessage(type_w_string_too_long);
  557. inserttypeconv(right,left.resulttype);
  558. if (tstringconstnode(right).len=0) then
  559. useshelper:=false;
  560. end;
  561. { rest is done in pass 1 (JM) }
  562. if useshelper then
  563. exit;
  564. end
  565. end
  566. else
  567. begin
  568. { get the size before the type conversion - check for all nodes }
  569. if assigned(right.resulttype.def) and
  570. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  571. (right.nodetype in [loadn,vecn,calln]) then
  572. original_size := right.resulttype.def.size;
  573. inserttypeconv(right,left.resulttype);
  574. end;
  575. { check if the assignment may cause a range check error }
  576. { if its not explicit, and only if the values are }
  577. { ordinals, enumdef and floatdef }
  578. if (right.nodetype = typeconvn) and
  579. not (nf_explicit in ttypeconvnode(right).flags) then
  580. begin
  581. if assigned(left.resulttype.def) and
  582. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  583. not is_boolean(left.resulttype.def) then
  584. begin
  585. if (original_size <> 0) and
  586. (left.resulttype.def.size < original_size) then
  587. begin
  588. if (cs_check_range in aktlocalswitches) then
  589. Message(type_w_smaller_possible_range_check)
  590. else
  591. Message(type_h_smaller_possible_range_check);
  592. end;
  593. end;
  594. end;
  595. { call helpers for interface }
  596. if is_interfacecom(left.resulttype.def) then
  597. begin
  598. hp:=ccallparanode.create(ctypeconvnode.create_internal
  599. (right,voidpointertype),
  600. ccallparanode.create(ctypeconvnode.create_internal
  601. (left,voidpointertype),nil));
  602. result:=ccallnode.createintern('fpc_intf_assign',hp);
  603. left:=nil;
  604. right:=nil;
  605. exit;
  606. end;
  607. { call helpers for variant, they can contain non ref. counted types like
  608. vararrays which must be really copied }
  609. if left.resulttype.def.deftype=variantdef then
  610. begin
  611. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  612. caddrnode.create_internal(right),voidpointertype),
  613. ccallparanode.create(ctypeconvnode.create_internal(
  614. caddrnode.create_internal(left),voidpointertype),
  615. nil));
  616. result:=ccallnode.createintern('fpc_variant_copy',hp);
  617. left:=nil;
  618. right:=nil;
  619. exit;
  620. end;
  621. { check if local proc/func is assigned to procvar }
  622. if right.resulttype.def.deftype=procvardef then
  623. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  624. end;
  625. function tassignmentnode.pass_1 : tnode;
  626. var
  627. hp: tnode;
  628. begin
  629. result:=nil;
  630. expectloc:=LOC_VOID;
  631. firstpass(left);
  632. firstpass(right);
  633. { assignment to refcounted variable -> inc/decref }
  634. if (not is_class(left.resulttype.def) and
  635. left.resulttype.def.needs_inittable) then
  636. include(current_procinfo.flags,pi_do_call);
  637. if codegenerror then
  638. exit;
  639. if (is_shortstring(left.resulttype.def)) then
  640. begin
  641. if right.resulttype.def.deftype=stringdef then
  642. begin
  643. if (right.nodetype<>stringconstn) or
  644. (tstringconstnode(right).len<>0) then
  645. begin
  646. if (cs_optimize in aktglobalswitches) and
  647. (right.nodetype in [calln,blockn]) and
  648. (left.nodetype = temprefn) and
  649. is_shortstring(right.resulttype.def) and
  650. not is_open_string(left.resulttype.def) and
  651. (tstringdef(left.resulttype.def).len = 255) then
  652. begin
  653. { the blocknode case is handled in pass_2 at the temp }
  654. { reference level (mainly for callparatemp) (JM) }
  655. if (right.nodetype = calln) then
  656. begin
  657. tcallnode(right).funcretnode := left;
  658. result := right;
  659. end
  660. else
  661. exit;
  662. end
  663. else
  664. begin
  665. hp:=ccallparanode.create
  666. (right,
  667. ccallparanode.create(cinlinenode.create
  668. (in_high_x,false,left.getcopy),nil));
  669. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  670. firstpass(result);
  671. end;
  672. left:=nil;
  673. right:=nil;
  674. exit;
  675. end;
  676. end;
  677. end;
  678. if (cs_optimize in aktglobalswitches) and
  679. (right.nodetype = calln) and
  680. { left must be a temp, since otherwise as soon as you modify the }
  681. { result, the current left node is modified and that one may }
  682. { still be an argument to the function or even accessed in the }
  683. { function }
  684. (((left.nodetype = temprefn) and
  685. paramanager.ret_in_param(right.resulttype.def,
  686. tcallnode(right).procdefinition.proccalloption)) or
  687. { there's special support for ansi/widestrings in the callnode }
  688. is_ansistring(right.resulttype.def) or
  689. is_widestring(right.resulttype.def)) then
  690. begin
  691. tcallnode(right).funcretnode := left;
  692. result := right;
  693. left := nil;
  694. right := nil;
  695. exit;
  696. end;
  697. registersint:=left.registersint+right.registersint;
  698. registersfpu:=max(left.registersfpu,right.registersfpu);
  699. {$ifdef SUPPORT_MMX}
  700. registersmmx:=max(left.registersmmx,right.registersmmx);
  701. {$endif SUPPORT_MMX}
  702. end;
  703. function tassignmentnode.docompare(p: tnode): boolean;
  704. begin
  705. docompare :=
  706. inherited docompare(p) and
  707. (assigntype = tassignmentnode(p).assigntype);
  708. end;
  709. {$ifdef state_tracking}
  710. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  711. var se:Tstate_entry;
  712. begin
  713. track_state_pass:=false;
  714. if exec_known then
  715. begin
  716. track_state_pass:=right.track_state_pass(exec_known);
  717. {Force a new resulttype pass.}
  718. right.resulttype.def:=nil;
  719. do_resulttypepass(right);
  720. resulttypepass(right);
  721. aktstate.store_fact(left.getcopy,right.getcopy);
  722. end
  723. else
  724. aktstate.delete_fact(left);
  725. end;
  726. {$endif}
  727. {*****************************************************************************
  728. TARRAYCONSTRUCTORRANGENODE
  729. *****************************************************************************}
  730. constructor tarrayconstructorrangenode.create(l,r : tnode);
  731. begin
  732. inherited create(arrayconstructorrangen,l,r);
  733. end;
  734. function tarrayconstructorrangenode.det_resulttype:tnode;
  735. begin
  736. result:=nil;
  737. resulttypepass(left);
  738. resulttypepass(right);
  739. set_varstate(left,vs_used,[vsf_must_be_valid]);
  740. set_varstate(right,vs_used,[vsf_must_be_valid]);
  741. if codegenerror then
  742. exit;
  743. resulttype:=left.resulttype;
  744. end;
  745. function tarrayconstructorrangenode.pass_1 : tnode;
  746. begin
  747. firstpass(left);
  748. firstpass(right);
  749. expectloc:=LOC_CREFERENCE;
  750. calcregisters(self,0,0,0);
  751. result:=nil;
  752. end;
  753. {****************************************************************************
  754. TARRAYCONSTRUCTORNODE
  755. *****************************************************************************}
  756. constructor tarrayconstructornode.create(l,r : tnode);
  757. begin
  758. inherited create(arrayconstructorn,l,r);
  759. end;
  760. function tarrayconstructornode._getcopy : tnode;
  761. var
  762. n : tarrayconstructornode;
  763. begin
  764. n:=tarrayconstructornode(inherited _getcopy);
  765. result:=n;
  766. end;
  767. function tarrayconstructornode.det_resulttype:tnode;
  768. var
  769. htype : ttype;
  770. hp : tarrayconstructornode;
  771. len : longint;
  772. varia : boolean;
  773. begin
  774. result:=nil;
  775. { are we allowing array constructor? Then convert it to a set }
  776. if not allow_array_constructor then
  777. begin
  778. hp:=tarrayconstructornode(getcopy);
  779. arrayconstructor_to_set(tnode(hp));
  780. result:=hp;
  781. exit;
  782. end;
  783. { only pass left tree, right tree contains next construct if any }
  784. htype.reset;
  785. len:=0;
  786. varia:=false;
  787. if assigned(left) then
  788. begin
  789. hp:=self;
  790. while assigned(hp) do
  791. begin
  792. resulttypepass(hp.left);
  793. set_varstate(hp.left,vs_used,[vsf_must_be_valid]);
  794. if (htype.def=nil) then
  795. htype:=hp.left.resulttype
  796. else
  797. begin
  798. if ((nf_novariaallowed in flags) or (not varia)) and
  799. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  800. begin
  801. varia:=true;
  802. end;
  803. end;
  804. inc(len);
  805. hp:=tarrayconstructornode(hp.right);
  806. end;
  807. end;
  808. { Set the type of empty or varia arrays to void. Also
  809. do this if the type is array of const/open array
  810. because those can't be used with setelementtype }
  811. if not assigned(htype.def) or
  812. varia or
  813. is_array_of_const(htype.def) or
  814. is_open_array(htype.def) then
  815. htype:=voidtype;
  816. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  817. tarraydef(resulttype.def).setelementtype(htype);
  818. tarraydef(resulttype.def).IsConstructor:=true;
  819. tarraydef(resulttype.def).IsVariant:=varia;
  820. end;
  821. procedure tarrayconstructornode.force_type(tt:ttype);
  822. var
  823. hp : tarrayconstructornode;
  824. begin
  825. tarraydef(resulttype.def).setelementtype(tt);
  826. tarraydef(resulttype.def).IsConstructor:=true;
  827. tarraydef(resulttype.def).IsVariant:=false;
  828. if assigned(left) then
  829. begin
  830. hp:=self;
  831. while assigned(hp) do
  832. begin
  833. inserttypeconv(hp.left,tt);
  834. hp:=tarrayconstructornode(hp.right);
  835. end;
  836. end;
  837. end;
  838. procedure tarrayconstructornode.insert_typeconvs;
  839. var
  840. hp : tarrayconstructornode;
  841. dovariant : boolean;
  842. begin
  843. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  844. { only pass left tree, right tree contains next construct if any }
  845. if assigned(left) then
  846. begin
  847. hp:=self;
  848. while assigned(hp) do
  849. begin
  850. resulttypepass(hp.left);
  851. { Insert typeconvs for array of const }
  852. if dovariant then
  853. begin
  854. case hp.left.resulttype.def.deftype of
  855. enumdef :
  856. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  857. arraydef :
  858. begin
  859. if is_chararray(hp.left.resulttype.def) then
  860. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  861. else
  862. if is_widechararray(hp.left.resulttype.def) then
  863. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  864. else
  865. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  866. end;
  867. orddef :
  868. begin
  869. if is_integer(hp.left.resulttype.def) and
  870. not(is_64bitint(hp.left.resulttype.def)) then
  871. hp.left:=ctypeconvnode.create(hp.left,s32inttype);
  872. end;
  873. floatdef :
  874. if not(is_currency(hp.left.resulttype.def)) then
  875. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  876. procvardef :
  877. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  878. stringdef,
  879. variantdef,
  880. pointerdef,
  881. classrefdef:
  882. ;
  883. objectdef :
  884. if is_object(hp.left.resulttype.def) then
  885. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  886. else
  887. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  888. end;
  889. end;
  890. resulttypepass(hp.left);
  891. hp:=tarrayconstructornode(hp.right);
  892. end;
  893. end;
  894. end;
  895. function tarrayconstructornode.pass_1 : tnode;
  896. var
  897. hp : tarrayconstructornode;
  898. do_variant:boolean;
  899. begin
  900. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  901. result:=nil;
  902. { Insert required type convs, this must be
  903. done in pass 1, because the call must be
  904. resulttypepassed already }
  905. if assigned(left) then
  906. begin
  907. insert_typeconvs;
  908. { call firstpass for all nodes }
  909. hp:=self;
  910. while assigned(hp) do
  911. begin
  912. if hp.left<>nil then
  913. begin
  914. {This check is pessimistic; a call will happen depending
  915. on the location in which the elements will be found in
  916. pass 2.}
  917. if not do_variant then
  918. include(current_procinfo.flags,pi_do_call);
  919. firstpass(hp.left);
  920. end;
  921. hp:=tarrayconstructornode(hp.right);
  922. end;
  923. end;
  924. expectloc:=LOC_CREFERENCE;
  925. calcregisters(self,0,0,0);
  926. end;
  927. function tarrayconstructornode.docompare(p: tnode): boolean;
  928. begin
  929. docompare:=inherited docompare(p);
  930. end;
  931. {*****************************************************************************
  932. TTYPENODE
  933. *****************************************************************************}
  934. constructor ttypenode.create(t : ttype);
  935. begin
  936. inherited create(typen);
  937. restype:=t;
  938. allowed:=false;
  939. end;
  940. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  941. begin
  942. inherited ppuload(t,ppufile);
  943. ppufile.gettype(restype);
  944. allowed:=boolean(ppufile.getbyte);
  945. end;
  946. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  947. begin
  948. inherited ppuwrite(ppufile);
  949. ppufile.puttype(restype);
  950. ppufile.putbyte(byte(allowed));
  951. end;
  952. procedure ttypenode.buildderefimpl;
  953. begin
  954. inherited buildderefimpl;
  955. restype.buildderef;
  956. end;
  957. procedure ttypenode.derefimpl;
  958. begin
  959. inherited derefimpl;
  960. restype.resolve;
  961. end;
  962. function ttypenode.det_resulttype:tnode;
  963. begin
  964. result:=nil;
  965. resulttype:=restype;
  966. { check if it's valid }
  967. if restype.def.deftype = errordef then
  968. CGMessage(parser_e_illegal_expression);
  969. end;
  970. function ttypenode.pass_1 : tnode;
  971. begin
  972. result:=nil;
  973. expectloc:=LOC_VOID;
  974. { a typenode can't generate code, so we give here
  975. an error. Else it'll be an abstract error in pass_2.
  976. Only when the allowed flag is set we don't generate
  977. an error }
  978. if not allowed then
  979. Message(parser_e_no_type_not_allowed_here);
  980. end;
  981. function ttypenode.docompare(p: tnode): boolean;
  982. begin
  983. docompare :=
  984. inherited docompare(p);
  985. end;
  986. {*****************************************************************************
  987. TRTTINODE
  988. *****************************************************************************}
  989. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  990. begin
  991. inherited create(rttin);
  992. rttidef:=def;
  993. rttitype:=rt;
  994. end;
  995. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  996. begin
  997. inherited ppuload(t,ppufile);
  998. ppufile.getderef(rttidefderef);
  999. rttitype:=trttitype(ppufile.getbyte);
  1000. end;
  1001. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1002. begin
  1003. inherited ppuwrite(ppufile);
  1004. ppufile.putderef(rttidefderef);
  1005. ppufile.putbyte(byte(rttitype));
  1006. end;
  1007. procedure trttinode.buildderefimpl;
  1008. begin
  1009. inherited buildderefimpl;
  1010. rttidefderef.build(rttidef);
  1011. end;
  1012. procedure trttinode.derefimpl;
  1013. begin
  1014. inherited derefimpl;
  1015. rttidef:=tstoreddef(rttidefderef.resolve);
  1016. end;
  1017. function trttinode._getcopy : tnode;
  1018. var
  1019. n : trttinode;
  1020. begin
  1021. n:=trttinode(inherited _getcopy);
  1022. n.rttidef:=rttidef;
  1023. n.rttitype:=rttitype;
  1024. result:=n;
  1025. end;
  1026. function trttinode.det_resulttype:tnode;
  1027. begin
  1028. { rtti information will be returned as a void pointer }
  1029. result:=nil;
  1030. resulttype:=voidpointertype;
  1031. end;
  1032. function trttinode.pass_1 : tnode;
  1033. begin
  1034. result:=nil;
  1035. expectloc:=LOC_CREFERENCE;
  1036. end;
  1037. function trttinode.docompare(p: tnode): boolean;
  1038. begin
  1039. docompare :=
  1040. inherited docompare(p) and
  1041. (rttidef = trttinode(p).rttidef) and
  1042. (rttitype = trttinode(p).rttitype);
  1043. end;
  1044. begin
  1045. cloadnode:=tloadnode;
  1046. cassignmentnode:=tassignmentnode;
  1047. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1048. carrayconstructornode:=tarrayconstructornode;
  1049. ctypenode:=ttypenode;
  1050. crttinode:=trttinode;
  1051. end.