nld.pas 42 KB

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