nld.pas 40 KB

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