nld.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. original_size : longint;
  433. begin
  434. result:=nil;
  435. resulttype:=voidtype;
  436. original_size := 0;
  437. { must be made unique }
  438. set_unique(left);
  439. resulttypepass(left);
  440. if is_ansistring(left.resulttype.def) then
  441. begin
  442. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  443. if (right.nodetype=addn) and
  444. left.isequal(tbinarynode(right).left) and
  445. { don't fold multiple concatenations else we could get trouble
  446. with multiple uses of s
  447. }
  448. (tbinarynode(right).left.nodetype<>addn) and
  449. (tbinarynode(right).right.nodetype<>addn) then
  450. begin
  451. { don't do a resulttypepass(right), since then the addnode }
  452. { may insert typeconversions that make this optimization }
  453. { opportunity quite difficult to detect (JM) }
  454. resulttypepass(tbinarynode(right).left);
  455. resulttypepass(tbinarynode(right).right);
  456. if (is_char(tbinarynode(right).right.resulttype.def) or
  457. is_shortstring(tbinarynode(right).right.resulttype.def) or
  458. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  459. begin
  460. { remove property flag so it'll not trigger an error }
  461. exclude(left.flags,nf_isproperty);
  462. { generate call to helper }
  463. hp:=ccallparanode.create(tbinarynode(right).right,
  464. ccallparanode.create(left,nil));
  465. if is_char(tbinarynode(right).right.resulttype.def) then
  466. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  467. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  468. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  469. else
  470. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  471. tbinarynode(right).right:=nil;
  472. left:=nil;
  473. exit;
  474. end;
  475. end;
  476. end
  477. else
  478. if is_shortstring(left.resulttype.def) then
  479. begin
  480. { fold <shortstring>:=<shortstring>+<shortstring>,
  481. <shortstring>+<char> is handled by an optimized node }
  482. if (right.nodetype=addn) and
  483. left.isequal(tbinarynode(right).left) and
  484. { don't fold multiple concatenations else we could get trouble
  485. with multiple uses of s }
  486. (tbinarynode(right).left.nodetype<>addn) and
  487. (tbinarynode(right).right.nodetype<>addn) then
  488. begin
  489. { don't do a resulttypepass(right), since then the addnode }
  490. { may insert typeconversions that make this optimization }
  491. { opportunity quite difficult to detect (JM) }
  492. resulttypepass(tbinarynode(right).left);
  493. resulttypepass(tbinarynode(right).right);
  494. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  495. begin
  496. { remove property flag so it'll not trigger an error }
  497. exclude(left.flags,nf_isproperty);
  498. { generate call to helper }
  499. hp:=ccallparanode.create(tbinarynode(right).right,
  500. ccallparanode.create(left,nil));
  501. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  502. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  503. tbinarynode(right).right:=nil;
  504. left:=nil;
  505. exit;
  506. end;
  507. end;
  508. end;
  509. resulttypepass(right);
  510. set_varstate(right,vs_read,[vsf_must_be_valid]);
  511. set_varstate(left,vs_written,[]);
  512. if codegenerror then
  513. exit;
  514. { tp procvar support, when we don't expect a procvar
  515. then we need to call the procvar }
  516. if (left.resulttype.def.deftype<>procvardef) then
  517. maybe_call_procvar(right,true);
  518. { assignments to formaldefs and open arrays aren't allowed }
  519. if (left.resulttype.def.deftype=formaldef) or
  520. is_open_array(left.resulttype.def) then
  521. CGMessage(type_e_operator_not_allowed);
  522. { test if node can be assigned, properties are allowed }
  523. valid_for_assignment(left,true);
  524. { assigning nil to a dynamic array clears the array }
  525. if is_dynamic_array(left.resulttype.def) and
  526. (right.nodetype=niln) then
  527. begin
  528. hp:=ccallparanode.create(caddrnode.create_internal
  529. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  530. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  531. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  532. left:=nil;
  533. exit;
  534. end;
  535. { shortstring helpers can do the conversion directly,
  536. so treat them separatly }
  537. if (is_shortstring(left.resulttype.def)) then
  538. begin
  539. { insert typeconv, except for chars that are handled in
  540. secondpass and except for ansi/wide string that can
  541. be converted immediatly }
  542. if not(is_char(right.resulttype.def) or
  543. (right.resulttype.def.deftype=stringdef)) then
  544. inserttypeconv(right,left.resulttype);
  545. if right.resulttype.def.deftype=stringdef then
  546. begin
  547. useshelper:=true;
  548. { convert constant strings to shortstrings. But
  549. skip empty constant strings, that will be handled
  550. in secondpass }
  551. if (right.nodetype=stringconstn) then
  552. begin
  553. { verify if range fits within shortstring }
  554. { just emit a warning, delphi gives an }
  555. { error, only if the type definition of }
  556. { of the string is less < 255 characters }
  557. if not is_open_string(left.resulttype.def) and
  558. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  559. cgmessage(type_w_string_too_long);
  560. inserttypeconv(right,left.resulttype);
  561. if (tstringconstnode(right).len=0) then
  562. useshelper:=false;
  563. end;
  564. { rest is done in pass 1 (JM) }
  565. if useshelper then
  566. exit;
  567. end
  568. end
  569. else
  570. begin
  571. { get the size before the type conversion - check for all nodes }
  572. if assigned(right.resulttype.def) and
  573. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  574. (right.nodetype in [loadn,vecn,calln]) then
  575. original_size := right.resulttype.def.size;
  576. inserttypeconv(right,left.resulttype);
  577. end;
  578. { check if the assignment may cause a range check error }
  579. { if its not explicit, and only if the values are }
  580. { ordinals, enumdef and floatdef }
  581. if (right.nodetype = typeconvn) and
  582. not (nf_explicit in ttypeconvnode(right).flags) then
  583. begin
  584. if assigned(left.resulttype.def) and
  585. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  586. not is_boolean(left.resulttype.def) then
  587. begin
  588. if (original_size <> 0) and
  589. (left.resulttype.def.size < original_size) then
  590. begin
  591. if (cs_check_range in aktlocalswitches) then
  592. Message(type_w_smaller_possible_range_check)
  593. else
  594. Message(type_h_smaller_possible_range_check);
  595. end;
  596. end;
  597. end;
  598. { call helpers for interface }
  599. if is_interfacecom(left.resulttype.def) then
  600. begin
  601. hp:=ccallparanode.create(ctypeconvnode.create_internal
  602. (right,voidpointertype),
  603. ccallparanode.create(ctypeconvnode.create_internal
  604. (left,voidpointertype),nil));
  605. result:=ccallnode.createintern('fpc_intf_assign',hp);
  606. left:=nil;
  607. right:=nil;
  608. exit;
  609. end;
  610. { call helpers for variant, they can contain non ref. counted types like
  611. vararrays which must be really copied }
  612. if left.resulttype.def.deftype=variantdef then
  613. begin
  614. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  615. caddrnode.create_internal(right),voidpointertype),
  616. ccallparanode.create(ctypeconvnode.create_internal(
  617. caddrnode.create_internal(left),voidpointertype),
  618. nil));
  619. result:=ccallnode.createintern('fpc_variant_copy',hp);
  620. left:=nil;
  621. right:=nil;
  622. exit;
  623. end;
  624. { check if local proc/func is assigned to procvar }
  625. if right.resulttype.def.deftype=procvardef then
  626. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  627. end;
  628. function tassignmentnode.pass_1 : tnode;
  629. var
  630. hp: tnode;
  631. begin
  632. result:=nil;
  633. expectloc:=LOC_VOID;
  634. firstpass(left);
  635. firstpass(right);
  636. { assignment to refcounted variable -> inc/decref }
  637. if (not is_class(left.resulttype.def) and
  638. left.resulttype.def.needs_inittable) then
  639. include(current_procinfo.flags,pi_do_call);
  640. if codegenerror then
  641. exit;
  642. if (is_shortstring(left.resulttype.def)) then
  643. begin
  644. if right.resulttype.def.deftype=stringdef then
  645. begin
  646. if (right.nodetype<>stringconstn) or
  647. (tstringconstnode(right).len<>0) then
  648. begin
  649. if (cs_optimize in aktglobalswitches) and
  650. (right.nodetype in [calln,blockn]) and
  651. (left.nodetype = temprefn) and
  652. is_shortstring(right.resulttype.def) and
  653. not is_open_string(left.resulttype.def) and
  654. (tstringdef(left.resulttype.def).len = 255) then
  655. begin
  656. { the blocknode case is handled in pass_2 at the temp }
  657. { reference level (mainly for callparatemp) (JM) }
  658. if (right.nodetype = calln) then
  659. begin
  660. tcallnode(right).funcretnode := left;
  661. result := right;
  662. end
  663. else
  664. exit;
  665. end
  666. else
  667. begin
  668. hp:=ccallparanode.create
  669. (right,
  670. ccallparanode.create(cinlinenode.create
  671. (in_high_x,false,left.getcopy),nil));
  672. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  673. firstpass(result);
  674. end;
  675. left:=nil;
  676. right:=nil;
  677. exit;
  678. end;
  679. end;
  680. end;
  681. if (cs_optimize in aktglobalswitches) and
  682. (right.nodetype = calln) and
  683. { left must be a temp, since otherwise as soon as you modify the }
  684. { result, the current left node is modified and that one may }
  685. { still be an argument to the function or even accessed in the }
  686. { function }
  687. (((left.nodetype = temprefn) and
  688. paramanager.ret_in_param(right.resulttype.def,
  689. tcallnode(right).procdefinition.proccalloption)) or
  690. { there's special support for ansi/widestrings in the callnode }
  691. is_ansistring(right.resulttype.def) or
  692. is_widestring(right.resulttype.def)) then
  693. begin
  694. tcallnode(right).funcretnode := left;
  695. result := right;
  696. left := nil;
  697. right := nil;
  698. exit;
  699. end;
  700. registersint:=left.registersint+right.registersint;
  701. registersfpu:=max(left.registersfpu,right.registersfpu);
  702. {$ifdef SUPPORT_MMX}
  703. registersmmx:=max(left.registersmmx,right.registersmmx);
  704. {$endif SUPPORT_MMX}
  705. end;
  706. function tassignmentnode.docompare(p: tnode): boolean;
  707. begin
  708. docompare :=
  709. inherited docompare(p) and
  710. (assigntype = tassignmentnode(p).assigntype);
  711. end;
  712. {$ifdef state_tracking}
  713. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  714. var se:Tstate_entry;
  715. begin
  716. track_state_pass:=false;
  717. if exec_known then
  718. begin
  719. track_state_pass:=right.track_state_pass(exec_known);
  720. {Force a new resulttype pass.}
  721. right.resulttype.def:=nil;
  722. do_resulttypepass(right);
  723. resulttypepass(right);
  724. aktstate.store_fact(left.getcopy,right.getcopy);
  725. end
  726. else
  727. aktstate.delete_fact(left);
  728. end;
  729. {$endif}
  730. {*****************************************************************************
  731. TARRAYCONSTRUCTORRANGENODE
  732. *****************************************************************************}
  733. constructor tarrayconstructorrangenode.create(l,r : tnode);
  734. begin
  735. inherited create(arrayconstructorrangen,l,r);
  736. end;
  737. function tarrayconstructorrangenode.det_resulttype:tnode;
  738. begin
  739. result:=nil;
  740. resulttypepass(left);
  741. resulttypepass(right);
  742. set_varstate(left,vs_read,[vsf_must_be_valid]);
  743. set_varstate(right,vs_read,[vsf_must_be_valid]);
  744. if codegenerror then
  745. exit;
  746. resulttype:=left.resulttype;
  747. end;
  748. function tarrayconstructorrangenode.pass_1 : tnode;
  749. begin
  750. firstpass(left);
  751. firstpass(right);
  752. expectloc:=LOC_CREFERENCE;
  753. calcregisters(self,0,0,0);
  754. result:=nil;
  755. end;
  756. {****************************************************************************
  757. TARRAYCONSTRUCTORNODE
  758. *****************************************************************************}
  759. constructor tarrayconstructornode.create(l,r : tnode);
  760. begin
  761. inherited create(arrayconstructorn,l,r);
  762. end;
  763. function tarrayconstructornode.getcopy : tnode;
  764. var
  765. n : tarrayconstructornode;
  766. begin
  767. n:=tarrayconstructornode(inherited getcopy);
  768. result:=n;
  769. end;
  770. function tarrayconstructornode.det_resulttype:tnode;
  771. var
  772. htype : ttype;
  773. hp : tarrayconstructornode;
  774. len : longint;
  775. varia : boolean;
  776. begin
  777. result:=nil;
  778. { are we allowing array constructor? Then convert it to a set }
  779. if not allow_array_constructor then
  780. begin
  781. hp:=tarrayconstructornode(getcopy);
  782. arrayconstructor_to_set(tnode(hp));
  783. result:=hp;
  784. exit;
  785. end;
  786. { only pass left tree, right tree contains next construct if any }
  787. htype.reset;
  788. len:=0;
  789. varia:=false;
  790. if assigned(left) then
  791. begin
  792. hp:=self;
  793. while assigned(hp) do
  794. begin
  795. resulttypepass(hp.left);
  796. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  797. if (htype.def=nil) then
  798. htype:=hp.left.resulttype
  799. else
  800. begin
  801. if ((nf_novariaallowed in flags) or (not varia)) and
  802. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  803. begin
  804. varia:=true;
  805. end;
  806. end;
  807. inc(len);
  808. hp:=tarrayconstructornode(hp.right);
  809. end;
  810. end;
  811. { Set the type of empty or varia arrays to void. Also
  812. do this if the type is array of const/open array
  813. because those can't be used with setelementtype }
  814. if not assigned(htype.def) or
  815. varia or
  816. is_array_of_const(htype.def) or
  817. is_open_array(htype.def) then
  818. htype:=voidtype;
  819. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  820. tarraydef(resulttype.def).setelementtype(htype);
  821. tarraydef(resulttype.def).IsConstructor:=true;
  822. tarraydef(resulttype.def).IsVariant:=varia;
  823. end;
  824. procedure tarrayconstructornode.force_type(tt:ttype);
  825. var
  826. hp : tarrayconstructornode;
  827. begin
  828. tarraydef(resulttype.def).setelementtype(tt);
  829. tarraydef(resulttype.def).IsConstructor:=true;
  830. tarraydef(resulttype.def).IsVariant:=false;
  831. if assigned(left) then
  832. begin
  833. hp:=self;
  834. while assigned(hp) do
  835. begin
  836. inserttypeconv(hp.left,tt);
  837. hp:=tarrayconstructornode(hp.right);
  838. end;
  839. end;
  840. end;
  841. procedure tarrayconstructornode.insert_typeconvs;
  842. var
  843. hp : tarrayconstructornode;
  844. dovariant : boolean;
  845. begin
  846. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  847. { only pass left tree, right tree contains next construct if any }
  848. if assigned(left) then
  849. begin
  850. hp:=self;
  851. while assigned(hp) do
  852. begin
  853. resulttypepass(hp.left);
  854. { Insert typeconvs for array of const }
  855. if dovariant then
  856. begin
  857. case hp.left.resulttype.def.deftype of
  858. enumdef :
  859. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  860. arraydef :
  861. begin
  862. if is_chararray(hp.left.resulttype.def) then
  863. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  864. else
  865. if is_widechararray(hp.left.resulttype.def) then
  866. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  867. else
  868. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  869. end;
  870. orddef :
  871. begin
  872. if is_integer(hp.left.resulttype.def) and
  873. not(is_64bitint(hp.left.resulttype.def)) then
  874. hp.left:=ctypeconvnode.create(hp.left,s32inttype)
  875. else if is_void(hp.left.resulttype.def) then
  876. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  877. end;
  878. floatdef :
  879. if not(is_currency(hp.left.resulttype.def)) then
  880. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  881. procvardef :
  882. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  883. stringdef,
  884. variantdef,
  885. pointerdef,
  886. classrefdef:
  887. ;
  888. objectdef :
  889. if is_object(hp.left.resulttype.def) then
  890. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  891. else
  892. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  893. end;
  894. end;
  895. resulttypepass(hp.left);
  896. hp:=tarrayconstructornode(hp.right);
  897. end;
  898. end;
  899. end;
  900. function tarrayconstructornode.pass_1 : tnode;
  901. var
  902. hp : tarrayconstructornode;
  903. do_variant:boolean;
  904. begin
  905. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  906. result:=nil;
  907. { Insert required type convs, this must be
  908. done in pass 1, because the call must be
  909. resulttypepassed already }
  910. if assigned(left) then
  911. begin
  912. insert_typeconvs;
  913. { call firstpass for all nodes }
  914. hp:=self;
  915. while assigned(hp) do
  916. begin
  917. if hp.left<>nil then
  918. begin
  919. {This check is pessimistic; a call will happen depending
  920. on the location in which the elements will be found in
  921. pass 2.}
  922. if not do_variant then
  923. include(current_procinfo.flags,pi_do_call);
  924. firstpass(hp.left);
  925. end;
  926. hp:=tarrayconstructornode(hp.right);
  927. end;
  928. end;
  929. expectloc:=LOC_CREFERENCE;
  930. calcregisters(self,0,0,0);
  931. end;
  932. function tarrayconstructornode.docompare(p: tnode): boolean;
  933. begin
  934. docompare:=inherited docompare(p);
  935. end;
  936. {*****************************************************************************
  937. TTYPENODE
  938. *****************************************************************************}
  939. constructor ttypenode.create(t : ttype);
  940. begin
  941. inherited create(typen);
  942. restype:=t;
  943. allowed:=false;
  944. end;
  945. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  946. begin
  947. inherited ppuload(t,ppufile);
  948. ppufile.gettype(restype);
  949. allowed:=boolean(ppufile.getbyte);
  950. end;
  951. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  952. begin
  953. inherited ppuwrite(ppufile);
  954. ppufile.puttype(restype);
  955. ppufile.putbyte(byte(allowed));
  956. end;
  957. procedure ttypenode.buildderefimpl;
  958. begin
  959. inherited buildderefimpl;
  960. restype.buildderef;
  961. end;
  962. procedure ttypenode.derefimpl;
  963. begin
  964. inherited derefimpl;
  965. restype.resolve;
  966. end;
  967. function ttypenode.det_resulttype:tnode;
  968. begin
  969. result:=nil;
  970. resulttype:=restype;
  971. { check if it's valid }
  972. if restype.def.deftype = errordef then
  973. CGMessage(parser_e_illegal_expression);
  974. end;
  975. function ttypenode.pass_1 : tnode;
  976. begin
  977. result:=nil;
  978. expectloc:=LOC_VOID;
  979. { a typenode can't generate code, so we give here
  980. an error. Else it'll be an abstract error in pass_2.
  981. Only when the allowed flag is set we don't generate
  982. an error }
  983. if not allowed then
  984. Message(parser_e_no_type_not_allowed_here);
  985. end;
  986. function ttypenode.docompare(p: tnode): boolean;
  987. begin
  988. docompare :=
  989. inherited docompare(p);
  990. end;
  991. {*****************************************************************************
  992. TRTTINODE
  993. *****************************************************************************}
  994. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  995. begin
  996. inherited create(rttin);
  997. rttidef:=def;
  998. rttitype:=rt;
  999. end;
  1000. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1001. begin
  1002. inherited ppuload(t,ppufile);
  1003. ppufile.getderef(rttidefderef);
  1004. rttitype:=trttitype(ppufile.getbyte);
  1005. end;
  1006. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1007. begin
  1008. inherited ppuwrite(ppufile);
  1009. ppufile.putderef(rttidefderef);
  1010. ppufile.putbyte(byte(rttitype));
  1011. end;
  1012. procedure trttinode.buildderefimpl;
  1013. begin
  1014. inherited buildderefimpl;
  1015. rttidefderef.build(rttidef);
  1016. end;
  1017. procedure trttinode.derefimpl;
  1018. begin
  1019. inherited derefimpl;
  1020. rttidef:=tstoreddef(rttidefderef.resolve);
  1021. end;
  1022. function trttinode.getcopy : tnode;
  1023. var
  1024. n : trttinode;
  1025. begin
  1026. n:=trttinode(inherited getcopy);
  1027. n.rttidef:=rttidef;
  1028. n.rttitype:=rttitype;
  1029. result:=n;
  1030. end;
  1031. function trttinode.det_resulttype:tnode;
  1032. begin
  1033. { rtti information will be returned as a void pointer }
  1034. result:=nil;
  1035. resulttype:=voidpointertype;
  1036. end;
  1037. function trttinode.pass_1 : tnode;
  1038. begin
  1039. result:=nil;
  1040. expectloc:=LOC_CREFERENCE;
  1041. end;
  1042. function trttinode.docompare(p: tnode): boolean;
  1043. begin
  1044. docompare :=
  1045. inherited docompare(p) and
  1046. (rttidef = trttinode(p).rttidef) and
  1047. (rttitype = trttinode(p).rttitype);
  1048. end;
  1049. begin
  1050. cloadnode:=tloadnode;
  1051. cassignmentnode:=tassignmentnode;
  1052. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1053. carrayconstructornode:=tarrayconstructornode;
  1054. ctypenode:=ttypenode;
  1055. crttinode:=trttinode;
  1056. end.