nld.pas 42 KB

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