nld.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. Trttidatatype=(rdt_normal,rdt_ord2str,rdt_str2ord);
  28. tloadnode = class(tunarynode)
  29. protected
  30. fprocdef : tprocdef;
  31. fprocdefderef : tderef;
  32. public
  33. symtableentry : tsym;
  34. symtableentryderef : tderef;
  35. symtable : TSymtable;
  36. constructor create(v : tsym;st : TSymtable);virtual;
  37. constructor create_procvar(v : tsym;d:tprocdef;st : TSymtable);virtual;
  38. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  39. procedure ppuwrite(ppufile:tcompilerppufile);override;
  40. procedure buildderefimpl;override;
  41. procedure derefimpl;override;
  42. procedure set_mp(p:tnode);
  43. function is_addr_param_load:boolean;
  44. function dogetcopy : tnode;override;
  45. function pass_1 : tnode;override;
  46. function pass_typecheck:tnode;override;
  47. procedure mark_write;override;
  48. function docompare(p: tnode): boolean; override;
  49. procedure printnodedata(var t:text);override;
  50. procedure setprocdef(p : tprocdef);
  51. property procdef: tprocdef read fprocdef write setprocdef;
  52. end;
  53. tloadnodeclass = class of tloadnode;
  54. { different assignment types }
  55. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  56. tassignmentnode = class(tbinarynode)
  57. assigntype : tassigntype;
  58. constructor create(l,r : tnode);virtual;
  59. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  60. procedure ppuwrite(ppufile:tcompilerppufile);override;
  61. function dogetcopy : tnode;override;
  62. function pass_1 : tnode;override;
  63. function pass_typecheck:tnode;override;
  64. function simplify(forinline : boolean) : tnode;override;
  65. {$ifdef state_tracking}
  66. function track_state_pass(exec_known:boolean):boolean;override;
  67. {$endif state_tracking}
  68. function docompare(p: tnode): boolean; override;
  69. end;
  70. tassignmentnodeclass = class of tassignmentnode;
  71. tarrayconstructorrangenode = class(tbinarynode)
  72. constructor create(l,r : tnode);virtual;
  73. function pass_1 : tnode;override;
  74. function pass_typecheck:tnode;override;
  75. end;
  76. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  77. tarrayconstructornode = class(tbinarynode)
  78. constructor create(l,r : tnode);virtual;
  79. function dogetcopy : tnode;override;
  80. function pass_1 : tnode;override;
  81. function pass_typecheck:tnode;override;
  82. function docompare(p: tnode): boolean; override;
  83. procedure force_type(def:tdef);
  84. procedure insert_typeconvs;
  85. end;
  86. tarrayconstructornodeclass = class of tarrayconstructornode;
  87. ttypenode = class(tnode)
  88. allowed : boolean;
  89. typedef : tdef;
  90. typedefderef : tderef;
  91. constructor create(def:tdef);virtual;
  92. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  93. procedure ppuwrite(ppufile:tcompilerppufile);override;
  94. procedure buildderefimpl;override;
  95. procedure derefimpl;override;
  96. function pass_1 : tnode;override;
  97. function pass_typecheck:tnode;override;
  98. function dogetcopy : tnode;override;
  99. function docompare(p: tnode): boolean; override;
  100. end;
  101. ttypenodeclass = class of ttypenode;
  102. trttinode = class(tnode)
  103. l1,l2 : longint;
  104. rttitype : trttitype;
  105. rttidef : tstoreddef;
  106. rttidefderef : tderef;
  107. rttidatatype : Trttidatatype;
  108. constructor create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);virtual;
  109. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. procedure buildderefimpl;override;
  112. procedure derefimpl;override;
  113. function dogetcopy : tnode;override;
  114. function pass_1 : tnode;override;
  115. function pass_typecheck:tnode;override;
  116. function docompare(p: tnode): boolean; override;
  117. end;
  118. trttinodeclass = class of trttinode;
  119. var
  120. cloadnode : tloadnodeclass = tloadnode;
  121. cassignmentnode : tassignmentnodeclass = tassignmentnode;
  122. carrayconstructorrangenode : tarrayconstructorrangenodeclass = tarrayconstructorrangenode;
  123. carrayconstructornode : tarrayconstructornodeclass = tarrayconstructornode;
  124. ctypenode : ttypenodeclass = ttypenode;
  125. crttinode : trttinodeclass = trttinode;
  126. { Current assignment node }
  127. aktassignmentnode : tassignmentnode;
  128. implementation
  129. uses
  130. cutils,verbose,globtype,globals,systems,
  131. symnot,
  132. defutil,defcmp,
  133. htypechk,pass_1,procinfo,paramgr,
  134. cpuinfo,
  135. ncon,ninl,ncnv,nmem,ncal,nutils,nbas,
  136. cgobj,cgbase
  137. ;
  138. {*****************************************************************************
  139. TLOADNODE
  140. *****************************************************************************}
  141. constructor tloadnode.create(v : tsym;st : TSymtable);
  142. begin
  143. inherited create(loadn,nil);
  144. if not assigned(v) then
  145. internalerror(200108121);
  146. symtableentry:=v;
  147. symtable:=st;
  148. fprocdef:=nil;
  149. end;
  150. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : TSymtable);
  151. begin
  152. inherited create(loadn,nil);
  153. if not assigned(v) then
  154. internalerror(200108122);
  155. symtableentry:=v;
  156. symtable:=st;
  157. fprocdef:=d;
  158. end;
  159. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  160. begin
  161. inherited ppuload(t,ppufile);
  162. ppufile.getderef(symtableentryderef);
  163. symtable:=nil;
  164. ppufile.getderef(fprocdefderef);
  165. end;
  166. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  167. begin
  168. inherited ppuwrite(ppufile);
  169. ppufile.putderef(symtableentryderef);
  170. ppufile.putderef(fprocdefderef);
  171. end;
  172. procedure tloadnode.buildderefimpl;
  173. begin
  174. inherited buildderefimpl;
  175. symtableentryderef.build(symtableentry);
  176. fprocdefderef.build(fprocdef);
  177. end;
  178. procedure tloadnode.derefimpl;
  179. begin
  180. inherited derefimpl;
  181. symtableentry:=tsym(symtableentryderef.resolve);
  182. symtable:=symtableentry.owner;
  183. fprocdef:=tprocdef(fprocdefderef.resolve);
  184. end;
  185. procedure tloadnode.set_mp(p:tnode);
  186. begin
  187. { typen nodes should not be set }
  188. if p.nodetype=typen then
  189. internalerror(200301042);
  190. left:=p;
  191. end;
  192. function tloadnode.dogetcopy : tnode;
  193. var
  194. n : tloadnode;
  195. begin
  196. n:=tloadnode(inherited dogetcopy);
  197. n.symtable:=symtable;
  198. n.symtableentry:=symtableentry;
  199. n.fprocdef:=fprocdef;
  200. result:=n;
  201. end;
  202. function tloadnode.is_addr_param_load:boolean;
  203. begin
  204. result:=(symtable.symtabletype=parasymtable) and
  205. (symtableentry.typ=paravarsym) and
  206. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  207. not(nf_load_self_pointer in flags) and
  208. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vardef,tprocdef(symtable.defowner).proccalloption);
  209. end;
  210. function tloadnode.pass_typecheck:tnode;
  211. begin
  212. result:=nil;
  213. case symtableentry.typ of
  214. absolutevarsym :
  215. resultdef:=tabsolutevarsym(symtableentry).vardef;
  216. constsym:
  217. begin
  218. if tconstsym(symtableentry).consttyp=constresourcestring then
  219. resultdef:=cansistringtype
  220. else
  221. internalerror(22799);
  222. end;
  223. staticvarsym :
  224. begin
  225. tabstractvarsym(symtableentry).IncRefCountBy(1);
  226. { static variables referenced in procedures or from finalization,
  227. variable needs to be in memory.
  228. It is too hard and the benefit is too small to detect whether a
  229. variable is only used in the finalization to add support for it (PFV) }
  230. if assigned(current_procinfo) and
  231. (symtable.symtabletype=staticsymtable) and
  232. (
  233. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  234. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  235. ) then
  236. make_not_regable(self,[ra_addr_taken]);
  237. resultdef:=tabstractvarsym(symtableentry).vardef;
  238. end;
  239. paravarsym,
  240. localvarsym :
  241. begin
  242. tabstractvarsym(symtableentry).IncRefCountBy(1);
  243. { Nested variable? The we need to load the framepointer of
  244. the parent procedure }
  245. if assigned(current_procinfo) and
  246. (symtable.symtabletype in [localsymtable,parasymtable]) and
  247. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  248. begin
  249. if assigned(left) then
  250. internalerror(200309289);
  251. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  252. { we can't inline the referenced parent procedure }
  253. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  254. { reference in nested procedures, variable needs to be in memory }
  255. { and behaves as if its address escapes its parent block }
  256. make_not_regable(self,[ra_addr_taken]);
  257. end;
  258. { fix self type which is declared as voidpointer in the
  259. definition }
  260. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  261. begin
  262. resultdef:=tprocdef(symtableentry.owner.defowner).struct;
  263. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  264. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  265. resultdef:=tclassrefdef.create(resultdef)
  266. else if (is_object(resultdef) or is_record(resultdef)) and
  267. (nf_load_self_pointer in flags) then
  268. resultdef:=tpointerdef.create(resultdef);
  269. end
  270. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  271. begin
  272. resultdef:=tprocdef(symtableentry.owner.defowner).struct;
  273. resultdef:=tclassrefdef.create(resultdef);
  274. end
  275. else
  276. resultdef:=tabstractvarsym(symtableentry).vardef;
  277. end;
  278. procsym :
  279. begin
  280. { Return the first procdef. In case of overloaded
  281. procdefs the matching procdef will be choosen
  282. when the expected procvardef is known, see get_information
  283. in htypechk.pas (PFV) }
  284. if not assigned(fprocdef) then
  285. fprocdef:=tprocdef(tprocsym(symtableentry).ProcdefList[0])
  286. else if po_kylixlocal in fprocdef.procoptions then
  287. CGMessage(type_e_cant_take_address_of_local_subroutine);
  288. { the result is a fprocdef, addrn and proc_to_procvar
  289. typeconvn need this as resultdef so they know
  290. that the address needs to be returned }
  291. resultdef:=fprocdef;
  292. { process methodpointer/framepointer }
  293. if assigned(left) then
  294. typecheckpass(left);
  295. end;
  296. labelsym:
  297. resultdef:=voidtype;
  298. else
  299. internalerror(200104141);
  300. end;
  301. end;
  302. procedure Tloadnode.mark_write;
  303. begin
  304. include(flags,nf_write);
  305. end;
  306. function tloadnode.pass_1 : tnode;
  307. begin
  308. result:=nil;
  309. expectloc:=LOC_REFERENCE;
  310. if (cs_create_pic in current_settings.moduleswitches) and
  311. not(symtableentry.typ in [paravarsym,localvarsym]) then
  312. include(current_procinfo.flags,pi_needs_got);
  313. case symtableentry.typ of
  314. absolutevarsym :
  315. ;
  316. constsym:
  317. begin
  318. if tconstsym(symtableentry).consttyp=constresourcestring then
  319. expectloc:=LOC_CREFERENCE;
  320. end;
  321. staticvarsym,
  322. localvarsym,
  323. paravarsym :
  324. begin
  325. if assigned(left) then
  326. firstpass(left);
  327. if not is_addr_param_load and
  328. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  329. expectloc:=tvarregable2tcgloc[tabstractvarsym(symtableentry).varregable]
  330. else
  331. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  332. expectloc:=LOC_CREFERENCE;
  333. if (target_info.system=system_powerpc_darwin) and
  334. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  335. include(current_procinfo.flags,pi_needs_got);
  336. { call to get address of threadvar }
  337. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  338. include(current_procinfo.flags,pi_do_call);
  339. if nf_write in flags then
  340. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  341. else
  342. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  343. end;
  344. procsym :
  345. begin
  346. { initialise left for nested procs if necessary }
  347. if (m_nested_procvars in current_settings.modeswitches) then
  348. setprocdef(fprocdef);
  349. { method pointer or nested proc ? }
  350. if assigned(left) then
  351. begin
  352. expectloc:=LOC_CREFERENCE;
  353. firstpass(left);
  354. end;
  355. end;
  356. labelsym :
  357. ;
  358. else
  359. internalerror(200104143);
  360. end;
  361. end;
  362. function tloadnode.docompare(p: tnode): boolean;
  363. begin
  364. docompare :=
  365. inherited docompare(p) and
  366. (symtableentry = tloadnode(p).symtableentry) and
  367. (fprocdef = tloadnode(p).fprocdef) and
  368. (symtable = tloadnode(p).symtable);
  369. end;
  370. procedure tloadnode.printnodedata(var t:text);
  371. begin
  372. inherited printnodedata(t);
  373. write(t,printnodeindention,'symbol = ',symtableentry.name);
  374. if symtableentry.typ=procsym then
  375. write(t,printnodeindention,'procdef = ',fprocdef.mangledname);
  376. writeln(t,'');
  377. end;
  378. procedure tloadnode.setprocdef(p : tprocdef);
  379. begin
  380. fprocdef:=p;
  381. resultdef:=p;
  382. { nested procedure? }
  383. if assigned(p) and
  384. is_nested_pd(p) then
  385. begin
  386. if not(m_nested_procvars in current_settings.modeswitches) then
  387. CGMessage(type_e_cant_take_address_of_local_subroutine)
  388. else
  389. begin
  390. { parent frame pointer pointer as "self" }
  391. left.free;
  392. left:=cloadparentfpnode.create(tprocdef(p.owner.defowner));
  393. end;
  394. end
  395. { we should never go from nested to non-nested }
  396. else if assigned(left) and
  397. (left.nodetype=loadparentfpn) then
  398. internalerror(2010072201);
  399. end;
  400. {*****************************************************************************
  401. TASSIGNMENTNODE
  402. *****************************************************************************}
  403. constructor tassignmentnode.create(l,r : tnode);
  404. begin
  405. inherited create(assignn,l,r);
  406. l.mark_write;
  407. assigntype:=at_normal;
  408. if r.nodetype = typeconvn then
  409. ttypeconvnode(r).warn_pointer_to_signed:=false;
  410. end;
  411. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  412. begin
  413. inherited ppuload(t,ppufile);
  414. assigntype:=tassigntype(ppufile.getbyte);
  415. end;
  416. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  417. begin
  418. inherited ppuwrite(ppufile);
  419. ppufile.putbyte(byte(assigntype));
  420. end;
  421. function tassignmentnode.dogetcopy : tnode;
  422. var
  423. n : tassignmentnode;
  424. begin
  425. n:=tassignmentnode(inherited dogetcopy);
  426. n.assigntype:=assigntype;
  427. result:=n;
  428. end;
  429. function tassignmentnode.simplify(forinline : boolean) : tnode;
  430. begin
  431. result:=nil;
  432. { assignment nodes can perform several floating point }
  433. { type conversions directly, so no typeconversions }
  434. { are inserted in those cases. When inlining, a }
  435. { variable may be replaced by a constant which can be }
  436. { converted at compile time, so check for this case }
  437. if is_real(left.resultdef) and
  438. is_real(right.resultdef) and
  439. is_constrealnode(right) and
  440. not equal_defs(right.resultdef,left.resultdef) then
  441. inserttypeconv(right,left.resultdef);
  442. end;
  443. function tassignmentnode.pass_typecheck:tnode;
  444. var
  445. hp : tnode;
  446. useshelper : boolean;
  447. begin
  448. result:=nil;
  449. resultdef:=voidtype;
  450. { must be made unique }
  451. set_unique(left);
  452. typecheckpass(left);
  453. typecheckpass(right);
  454. set_varstate(right,vs_read,[vsf_must_be_valid]);
  455. set_varstate(left,vs_written,[]);
  456. if codegenerror then
  457. exit;
  458. { tp procvar support, when we don't expect a procvar
  459. then we need to call the procvar }
  460. if (left.resultdef.typ<>procvardef) then
  461. maybe_call_procvar(right,true);
  462. { assignments to formaldefs and open arrays aren't allowed }
  463. if (left.resultdef.typ=formaldef) or
  464. is_open_array(left.resultdef) then
  465. CGMessage(type_e_assignment_not_allowed);
  466. { test if node can be assigned, properties are allowed }
  467. valid_for_assignment(left,true);
  468. { assigning nil to a dynamic array clears the array }
  469. if is_dynamic_array(left.resultdef) and
  470. (right.nodetype=niln) then
  471. begin
  472. { remove property flag to avoid errors, see comments for }
  473. { tf_winlikewidestring assignments below }
  474. exclude(left.flags,nf_isproperty);
  475. hp:=ccallparanode.create(caddrnode.create_internal
  476. (crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  477. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  478. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  479. left:=nil;
  480. exit;
  481. end;
  482. { shortstring helpers can do the conversion directly,
  483. so treat them separatly }
  484. if (is_shortstring(left.resultdef)) then
  485. begin
  486. { insert typeconv, except for chars that are handled in
  487. secondpass and except for ansi/wide string that can
  488. be converted immediatly }
  489. if not(is_char(right.resultdef) or
  490. (right.resultdef.typ=stringdef)) then
  491. inserttypeconv(right,left.resultdef);
  492. if right.resultdef.typ=stringdef then
  493. begin
  494. useshelper:=true;
  495. { convert constant strings to shortstrings. But
  496. skip empty constant strings, that will be handled
  497. in secondpass }
  498. if (right.nodetype=stringconstn) then
  499. begin
  500. { verify if range fits within shortstring }
  501. { just emit a warning, delphi gives an }
  502. { error, only if the type definition of }
  503. { of the string is less < 255 characters }
  504. if not is_open_string(left.resultdef) and
  505. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  506. cgmessage(type_w_string_too_long);
  507. inserttypeconv(right,left.resultdef);
  508. if (right.nodetype=stringconstn) and
  509. (tstringconstnode(right).len=0) then
  510. useshelper:=false;
  511. end;
  512. { rest is done in pass 1 (JM) }
  513. if useshelper then
  514. exit;
  515. end
  516. end
  517. { floating point assignments can also perform the conversion directly }
  518. else if is_real(left.resultdef) and is_real(right.resultdef) and
  519. not is_constrealnode(right)
  520. {$ifdef cpufpemu}
  521. { the emulator can't do this obviously }
  522. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  523. {$endif cpufpemu}
  524. {$ifdef x86}
  525. { the assignment node code can't convert a double in an }
  526. { sse register to an extended value in memory more }
  527. { efficiently than a type conversion node, so don't }
  528. { bother implementing support for that }
  529. and (use_vectorfpu(left.resultdef) or not(use_vectorfpu(right.resultdef)))
  530. {$endif}
  531. {$ifdef arm}
  532. { the assignment node code can't convert a single in
  533. an interger register to a double in an mmregister or
  534. vice versa }
  535. and (use_vectorfpu(left.resultdef) and
  536. use_vectorfpu(right.resultdef) and
  537. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype))
  538. {$endif}
  539. then
  540. begin
  541. check_ranges(fileinfo,right,left.resultdef);
  542. end
  543. else
  544. begin
  545. { check if the assignment may cause a range check error }
  546. check_ranges(fileinfo,right,left.resultdef);
  547. inserttypeconv(right,left.resultdef);
  548. end;
  549. { call helpers for interface }
  550. if is_interfacecom_or_dispinterface(left.resultdef) then
  551. begin
  552. { Normal interface assignments are handled by the generic refcount incr/decr }
  553. if not right.resultdef.is_related(left.resultdef) then
  554. begin
  555. { remove property flag to avoid errors, see comments for }
  556. { tf_winlikewidestring assignments below }
  557. exclude(left.flags,nf_isproperty);
  558. hp:=
  559. ccallparanode.create(
  560. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  561. ccallparanode.create(
  562. ctypeconvnode.create_internal(right,voidpointertype),
  563. ccallparanode.create(
  564. ctypeconvnode.create_internal(left,voidpointertype),
  565. nil)));
  566. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  567. left:=nil;
  568. right:=nil;
  569. exit;
  570. end;
  571. end;
  572. { check if local proc/func is assigned to procvar }
  573. if right.resultdef.typ=procvardef then
  574. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  575. end;
  576. function tassignmentnode.pass_1 : tnode;
  577. var
  578. hp: tnode;
  579. oldassignmentnode : tassignmentnode;
  580. begin
  581. result:=nil;
  582. expectloc:=LOC_VOID;
  583. firstpass(left);
  584. { Optimize the reuse of the destination of the assingment in left.
  585. Allow the use of the left inside the tree generated on the right.
  586. This is especially useful for string routines where the destination
  587. is pushed as a parameter. Using the final destination of left directly
  588. save a temp allocation and copy of data (PFV) }
  589. oldassignmentnode:=aktassignmentnode;
  590. aktassignmentnode:=self;
  591. firstpass(right);
  592. aktassignmentnode:=oldassignmentnode;
  593. if nf_assign_done_in_right in flags then
  594. begin
  595. result:=right;
  596. right:=nil;
  597. exit;
  598. end;
  599. if codegenerror then
  600. exit;
  601. { assignment to refcounted variable -> inc/decref }
  602. if is_managed_type(left.resultdef) then
  603. include(current_procinfo.flags,pi_do_call);
  604. if (is_shortstring(left.resultdef)) then
  605. begin
  606. if right.resultdef.typ=stringdef then
  607. begin
  608. if (right.nodetype<>stringconstn) or
  609. (tstringconstnode(right).len<>0) then
  610. begin
  611. { remove property flag to avoid errors, see comments for }
  612. { tf_winlikewidestring assignments below }
  613. exclude(left.flags, nf_isproperty);
  614. hp:=ccallparanode.create
  615. (right,
  616. ccallparanode.create(left,nil));
  617. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  618. firstpass(result);
  619. left:=nil;
  620. right:=nil;
  621. exit;
  622. end;
  623. end;
  624. end
  625. { call helpers for composite types containing automated types }
  626. else if is_managed_type(left.resultdef) and
  627. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  628. not is_interfacecom_or_dispinterface(left.resultdef) and
  629. not is_dynamic_array(left.resultdef) then
  630. begin
  631. hp:=ccallparanode.create(caddrnode.create_internal(
  632. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  633. ccallparanode.create(ctypeconvnode.create_internal(
  634. caddrnode.create_internal(left),voidpointertype),
  635. ccallparanode.create(ctypeconvnode.create_internal(
  636. caddrnode.create_internal(right),voidpointertype),
  637. nil)));
  638. result:=ccallnode.createintern('fpc_copy_proc',hp);
  639. firstpass(result);
  640. left:=nil;
  641. right:=nil;
  642. exit;
  643. end
  644. { call helpers for variant, they can contain non ref. counted types like
  645. vararrays which must be really copied }
  646. else if left.resultdef.typ=variantdef then
  647. begin
  648. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  649. caddrnode.create_internal(right),voidpointertype),
  650. ccallparanode.create(ctypeconvnode.create_internal(
  651. caddrnode.create_internal(left),voidpointertype),
  652. nil));
  653. result:=ccallnode.createintern('fpc_variant_copy',hp);
  654. firstpass(result);
  655. left:=nil;
  656. right:=nil;
  657. exit;
  658. end
  659. { call helpers for windows widestrings, they aren't ref. counted }
  660. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  661. begin
  662. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  663. { be passed to var or out parameters, because in that case setters/getters are not }
  664. { used. Further, if we would allow it in case there are no getters or setters, you }
  665. { would need source changes in case these are introduced later on, thus defeating }
  666. { part of the transparency advantages of properties. In this particular case, }
  667. { however: }
  668. { a) if there is a setter, this code will not be used since then the assignment }
  669. { will be converted to a procedure call }
  670. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  671. { the refcount of the field to which we are writing }
  672. { c) source code changes are not required if a setter is added/removed, because }
  673. { this transformation is handled at compile time }
  674. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  675. { is a property which refers to a field without a setter call, we will not get }
  676. { an error about trying to pass a property as a var parameter }
  677. exclude(left.flags,nf_isproperty);
  678. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  679. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  680. nil));
  681. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  682. firstpass(result);
  683. left:=nil;
  684. right:=nil;
  685. exit;
  686. end;
  687. end;
  688. function tassignmentnode.docompare(p: tnode): boolean;
  689. begin
  690. docompare :=
  691. inherited docompare(p) and
  692. (assigntype = tassignmentnode(p).assigntype);
  693. end;
  694. {$ifdef state_tracking}
  695. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  696. var se:Tstate_entry;
  697. begin
  698. track_state_pass:=false;
  699. if exec_known then
  700. begin
  701. track_state_pass:=right.track_state_pass(exec_known);
  702. {Force a new resultdef pass.}
  703. right.resultdef:=nil;
  704. do_typecheckpass(right);
  705. typecheckpass(right);
  706. aktstate.store_fact(left.getcopy,right.getcopy);
  707. end
  708. else
  709. aktstate.delete_fact(left);
  710. end;
  711. {$endif}
  712. {*****************************************************************************
  713. TARRAYCONSTRUCTORRANGENODE
  714. *****************************************************************************}
  715. constructor tarrayconstructorrangenode.create(l,r : tnode);
  716. begin
  717. inherited create(arrayconstructorrangen,l,r);
  718. end;
  719. function tarrayconstructorrangenode.pass_typecheck:tnode;
  720. begin
  721. result:=nil;
  722. typecheckpass(left);
  723. typecheckpass(right);
  724. set_varstate(left,vs_read,[vsf_must_be_valid]);
  725. set_varstate(right,vs_read,[vsf_must_be_valid]);
  726. if codegenerror then
  727. exit;
  728. resultdef:=left.resultdef;
  729. end;
  730. function tarrayconstructorrangenode.pass_1 : tnode;
  731. begin
  732. result:=nil;
  733. CGMessage(parser_e_illegal_expression);
  734. end;
  735. {****************************************************************************
  736. TARRAYCONSTRUCTORNODE
  737. *****************************************************************************}
  738. constructor tarrayconstructornode.create(l,r : tnode);
  739. begin
  740. inherited create(arrayconstructorn,l,r);
  741. end;
  742. function tarrayconstructornode.dogetcopy : tnode;
  743. var
  744. n : tarrayconstructornode;
  745. begin
  746. n:=tarrayconstructornode(inherited dogetcopy);
  747. result:=n;
  748. end;
  749. function tarrayconstructornode.pass_typecheck:tnode;
  750. var
  751. hdef : tdef;
  752. hp : tarrayconstructornode;
  753. len : longint;
  754. varia : boolean;
  755. eq : tequaltype;
  756. hnodetype : tnodetype;
  757. begin
  758. result:=nil;
  759. { are we allowing array constructor? Then convert it to a set.
  760. Do this only if we didn't convert the arrayconstructor yet. This
  761. is needed for the cases where the resultdef is forced for a second
  762. run }
  763. if not(allow_array_constructor) then
  764. begin
  765. hp:=tarrayconstructornode(getcopy);
  766. arrayconstructor_to_set(tnode(hp));
  767. result:=hp;
  768. exit;
  769. end;
  770. { only pass left tree, right tree contains next construct if any }
  771. hdef:=nil;
  772. hnodetype:=errorn;
  773. len:=0;
  774. varia:=false;
  775. if assigned(left) then
  776. begin
  777. hp:=self;
  778. while assigned(hp) do
  779. begin
  780. typecheckpass(hp.left);
  781. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  782. if (hdef=nil) then
  783. begin
  784. hdef:=hp.left.resultdef;
  785. hnodetype:=hp.left.nodetype;
  786. end
  787. else
  788. begin
  789. { If we got a niln we don't know the type yet and need to take the
  790. type of the next array element.
  791. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  792. if hnodetype=niln then
  793. begin
  794. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  795. if eq>te_incompatible then
  796. begin
  797. hdef:=hp.left.resultdef;
  798. hnodetype:=hp.left.nodetype;
  799. end;
  800. end
  801. else
  802. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  803. if (not varia) and (eq<te_equal) then
  804. begin
  805. { If both are integers we need to take the type that can hold both
  806. defs }
  807. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  808. begin
  809. if is_in_limit(hdef,hp.left.resultdef) then
  810. hdef:=hp.left.resultdef;
  811. end
  812. else
  813. if (nf_novariaallowed in flags) then
  814. varia:=true;
  815. end;
  816. end;
  817. inc(len);
  818. hp:=tarrayconstructornode(hp.right);
  819. end;
  820. end;
  821. { Set the type of empty or varia arrays to void. Also
  822. do this if the type is array of const/open array
  823. because those can't be used with setelementdef }
  824. if not assigned(hdef) or
  825. varia or
  826. is_array_of_const(hdef) or
  827. is_open_array(hdef) then
  828. hdef:=voidtype;
  829. resultdef:=tarraydef.create(0,len-1,s32inttype);
  830. tarraydef(resultdef).elementdef:=hdef;
  831. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  832. if varia then
  833. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  834. end;
  835. procedure tarrayconstructornode.force_type(def:tdef);
  836. var
  837. hp : tarrayconstructornode;
  838. begin
  839. tarraydef(resultdef).elementdef:=def;
  840. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  841. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  842. if assigned(left) then
  843. begin
  844. hp:=self;
  845. while assigned(hp) do
  846. begin
  847. inserttypeconv(hp.left,def);
  848. hp:=tarrayconstructornode(hp.right);
  849. end;
  850. end;
  851. end;
  852. procedure tarrayconstructornode.insert_typeconvs;
  853. var
  854. hp : tarrayconstructornode;
  855. dovariant : boolean;
  856. begin
  857. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  858. { only pass left tree, right tree contains next construct if any }
  859. if assigned(left) then
  860. begin
  861. hp:=self;
  862. while assigned(hp) do
  863. begin
  864. typecheckpass(hp.left);
  865. { Insert typeconvs for array of const }
  866. if dovariant then
  867. { at this time C varargs are no longer an arrayconstructornode }
  868. insert_varargstypeconv(hp.left,false);
  869. hp:=tarrayconstructornode(hp.right);
  870. end;
  871. end;
  872. end;
  873. function tarrayconstructornode.pass_1 : tnode;
  874. var
  875. hp : tarrayconstructornode;
  876. do_variant:boolean;
  877. begin
  878. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  879. result:=nil;
  880. { Insert required type convs, this must be
  881. done in pass 1, because the call must be
  882. typecheckpassed already }
  883. if assigned(left) then
  884. begin
  885. insert_typeconvs;
  886. { call firstpass for all nodes }
  887. hp:=self;
  888. while assigned(hp) do
  889. begin
  890. if hp.left<>nil then
  891. begin
  892. {This check is pessimistic; a call will happen depending
  893. on the location in which the elements will be found in
  894. pass 2.}
  895. if not do_variant then
  896. include(current_procinfo.flags,pi_do_call);
  897. firstpass(hp.left);
  898. end;
  899. hp:=tarrayconstructornode(hp.right);
  900. end;
  901. end;
  902. expectloc:=LOC_CREFERENCE;
  903. end;
  904. function tarrayconstructornode.docompare(p: tnode): boolean;
  905. begin
  906. docompare:=inherited docompare(p);
  907. end;
  908. {*****************************************************************************
  909. TTYPENODE
  910. *****************************************************************************}
  911. constructor ttypenode.create(def:tdef);
  912. begin
  913. inherited create(typen);
  914. typedef:=def;
  915. allowed:=false;
  916. end;
  917. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  918. begin
  919. inherited ppuload(t,ppufile);
  920. ppufile.getderef(typedefderef);
  921. allowed:=boolean(ppufile.getbyte);
  922. end;
  923. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  924. begin
  925. inherited ppuwrite(ppufile);
  926. ppufile.putderef(typedefderef);
  927. ppufile.putbyte(byte(allowed));
  928. end;
  929. procedure ttypenode.buildderefimpl;
  930. begin
  931. inherited buildderefimpl;
  932. typedefderef.build(typedef);
  933. end;
  934. procedure ttypenode.derefimpl;
  935. begin
  936. inherited derefimpl;
  937. typedef:=tdef(typedefderef.resolve);
  938. end;
  939. function ttypenode.pass_typecheck:tnode;
  940. begin
  941. result:=nil;
  942. resultdef:=typedef;
  943. { check if it's valid }
  944. if typedef.typ = errordef then
  945. CGMessage(parser_e_illegal_expression);
  946. end;
  947. function ttypenode.pass_1 : tnode;
  948. begin
  949. result:=nil;
  950. expectloc:=LOC_VOID;
  951. { a typenode can't generate code, so we give here
  952. an error. Else it'll be an abstract error in pass_generate_code.
  953. Only when the allowed flag is set we don't generate
  954. an error }
  955. if not allowed then
  956. Message(parser_e_no_type_not_allowed_here);
  957. end;
  958. function ttypenode.dogetcopy : tnode;
  959. var
  960. n : ttypenode;
  961. begin
  962. n:=ttypenode(inherited dogetcopy);
  963. n.allowed:=allowed;
  964. n.typedef:=typedef;
  965. result:=n;
  966. end;
  967. function ttypenode.docompare(p: tnode): boolean;
  968. begin
  969. docompare :=
  970. inherited docompare(p);
  971. end;
  972. {*****************************************************************************
  973. TRTTINODE
  974. *****************************************************************************}
  975. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  976. begin
  977. inherited create(rttin);
  978. rttidef:=def;
  979. rttitype:=rt;
  980. rttidatatype:=dt;
  981. end;
  982. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  983. begin
  984. inherited ppuload(t,ppufile);
  985. ppufile.getderef(rttidefderef);
  986. rttitype:=trttitype(ppufile.getbyte);
  987. rttidatatype:=trttidatatype(ppufile.getbyte);
  988. end;
  989. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  990. begin
  991. inherited ppuwrite(ppufile);
  992. ppufile.putderef(rttidefderef);
  993. ppufile.putbyte(byte(rttitype));
  994. ppufile.putbyte(byte(rttidatatype));
  995. end;
  996. procedure trttinode.buildderefimpl;
  997. begin
  998. inherited buildderefimpl;
  999. rttidefderef.build(rttidef);
  1000. end;
  1001. procedure trttinode.derefimpl;
  1002. begin
  1003. inherited derefimpl;
  1004. rttidef:=tstoreddef(rttidefderef.resolve);
  1005. end;
  1006. function trttinode.dogetcopy : tnode;
  1007. var
  1008. n : trttinode;
  1009. begin
  1010. n:=trttinode(inherited dogetcopy);
  1011. n.rttidef:=rttidef;
  1012. n.rttitype:=rttitype;
  1013. n.rttidatatype:=rttidatatype;
  1014. result:=n;
  1015. end;
  1016. function trttinode.pass_typecheck:tnode;
  1017. begin
  1018. { rtti information will be returned as a void pointer }
  1019. result:=nil;
  1020. resultdef:=voidpointertype;
  1021. end;
  1022. function trttinode.pass_1 : tnode;
  1023. begin
  1024. result:=nil;
  1025. expectloc:=LOC_CREFERENCE;
  1026. end;
  1027. function trttinode.docompare(p: tnode): boolean;
  1028. begin
  1029. docompare :=
  1030. inherited docompare(p) and
  1031. (rttidef = trttinode(p).rttidef) and
  1032. (rttitype = trttinode(p).rttitype);
  1033. end;
  1034. end.