nld.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. tloadnodeflags = (
  29. loadnf_is_self,
  30. { tell the load node the address of the symbol into the location, i.e. location^ must
  31. be used to access the symbol
  32. this is for example needed to load self for objects }
  33. loadnf_load_addr,
  34. loadnf_inherited,
  35. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  36. this requires that the parameter is actually passed by value
  37. Be really carefull when using this flag! }
  38. loadnf_isinternal_ignoreconst,
  39. loadnf_only_uninitialized_hint
  40. );
  41. tloadnode = class(tunarynode)
  42. protected
  43. fprocdef : tprocdef;
  44. fprocdefderef : tderef;
  45. function handle_threadvar_access: tnode; virtual;
  46. public
  47. loadnodeflags : set of tloadnodeflags;
  48. symtableentry : tsym;
  49. symtableentryderef : tderef;
  50. symtable : TSymtable;
  51. constructor create(v : tsym;st : TSymtable);virtual;
  52. constructor create_procvar(v : tsym;d:tprocdef;st : TSymtable);virtual;
  53. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  54. procedure ppuwrite(ppufile:tcompilerppufile);override;
  55. procedure buildderefimpl;override;
  56. procedure derefimpl;override;
  57. procedure set_mp(p:tnode);
  58. function is_addr_param_load:boolean;virtual;
  59. function dogetcopy : tnode;override;
  60. function pass_1 : tnode;override;
  61. function pass_typecheck:tnode;override;
  62. procedure mark_write;override;
  63. function docompare(p: tnode): boolean; override;
  64. procedure printnodedata(var t:text);override;
  65. {$ifdef DEBUG_NODE_XML}
  66. procedure XMLPrintNodeData(var T: Text); override;
  67. {$endif DEBUG_NODE_XML}
  68. procedure setprocdef(p : tprocdef);
  69. property procdef: tprocdef read fprocdef write setprocdef;
  70. end;
  71. tloadnodeclass = class of tloadnode;
  72. { different assignment types }
  73. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  74. tassignmentnode = class(tbinarynode)
  75. protected
  76. function direct_shortstring_assignment: boolean; virtual;
  77. public
  78. assigntype : tassigntype;
  79. constructor create(l,r : tnode);virtual;
  80. { no checks for validity of assignment }
  81. constructor create_internal(l,r : tnode);virtual;
  82. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. function dogetcopy : tnode;override;
  85. function pass_1 : tnode;override;
  86. function pass_typecheck:tnode;override;
  87. function simplify(forinline : boolean) : tnode;override;
  88. {$ifdef state_tracking}
  89. function track_state_pass(exec_known:boolean):boolean;override;
  90. {$endif state_tracking}
  91. function docompare(p: tnode): boolean; override;
  92. {$ifdef DEBUG_NODE_XML}
  93. procedure XMLPrintNodeData(var T: Text); override;
  94. {$endif DEBUG_NODE_XML}
  95. end;
  96. tassignmentnodeclass = class of tassignmentnode;
  97. tarrayconstructorrangenode = class(tbinarynode)
  98. constructor create(l,r : tnode);virtual;
  99. function pass_1 : tnode;override;
  100. function pass_typecheck:tnode;override;
  101. end;
  102. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  103. tarrayconstructornode = class(tbinarynode)
  104. allow_array_constructor : boolean;
  105. private
  106. function has_range_node:boolean;
  107. protected
  108. procedure wrapmanagedvarrec(var n: tnode);virtual;abstract;
  109. public
  110. constructor create(l,r : tnode);virtual;
  111. function dogetcopy : tnode;override;
  112. function pass_1 : tnode;override;
  113. function pass_typecheck:tnode;override;
  114. function docompare(p: tnode): boolean; override;
  115. procedure force_type(def:tdef);
  116. procedure insert_typeconvs;
  117. function isempty : boolean;
  118. end;
  119. tarrayconstructornodeclass = class of tarrayconstructornode;
  120. ttypenode = class(tnode)
  121. allowed : boolean;
  122. helperallowed : boolean;
  123. typedef : tdef;
  124. typedefderef : tderef;
  125. typesym : tsym;
  126. typesymderef : tderef;
  127. constructor create(def:tdef);virtual;
  128. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  129. procedure ppuwrite(ppufile:tcompilerppufile);override;
  130. procedure buildderefimpl;override;
  131. procedure derefimpl;override;
  132. function pass_1 : tnode;override;
  133. function pass_typecheck:tnode;override;
  134. function dogetcopy : tnode;override;
  135. function docompare(p: tnode): boolean; override;
  136. end;
  137. ttypenodeclass = class of ttypenode;
  138. trttinode = class(tnode)
  139. l1,l2 : longint;
  140. rttitype : trttitype;
  141. rttidef : tstoreddef;
  142. rttidefderef : tderef;
  143. rttidatatype : Trttidatatype;
  144. constructor create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);virtual;
  145. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. procedure buildderefimpl;override;
  148. procedure derefimpl;override;
  149. function dogetcopy : tnode;override;
  150. function pass_1 : tnode;override;
  151. function pass_typecheck:tnode;override;
  152. function docompare(p: tnode): boolean; override;
  153. end;
  154. trttinodeclass = class of trttinode;
  155. var
  156. cloadnode : tloadnodeclass = tloadnode;
  157. cassignmentnode : tassignmentnodeclass = tassignmentnode;
  158. carrayconstructorrangenode : tarrayconstructorrangenodeclass = tarrayconstructorrangenode;
  159. carrayconstructornode : tarrayconstructornodeclass = tarrayconstructornode;
  160. ctypenode : ttypenodeclass = ttypenode;
  161. crttinode : trttinodeclass = trttinode;
  162. { Current assignment node }
  163. aktassignmentnode : tassignmentnode;
  164. { Create a node tree to load a variable if symbol is assigned, otherwise an error node.
  165. Generates an internalerror if called for an absolutevarsym of the "tovar" kind (those
  166. are only supported for expansion in the parser) }
  167. function gen_load_var(sym: tabstractvarsym): tnode;
  168. implementation
  169. uses
  170. verbose,globtype,globals,systems,constexp,compinnr,
  171. ppu,
  172. symtable,
  173. defutil,defcmp,
  174. cpuinfo,
  175. htypechk,pass_1,procinfo,paramgr,
  176. nbas,ncon,nflw,ninl,ncnv,nmem,ncal,nutils,
  177. cgbase
  178. ;
  179. function gen_load_var(sym: tabstractvarsym): tnode;
  180. begin
  181. result:=nil;
  182. if assigned(sym) then
  183. begin
  184. if (sym.typ<>absolutevarsym) or
  185. (tabsolutevarsym(sym).abstyp<>tovar) then
  186. begin
  187. result:=cloadnode.create(sym,sym.owner);
  188. end
  189. else
  190. internalerror(2020122601);
  191. end
  192. else
  193. begin
  194. result:=cerrornode.create;
  195. CGMessage(parser_e_illegal_expression);
  196. end;
  197. end;
  198. {*****************************************************************************
  199. TLOADNODE
  200. *****************************************************************************}
  201. function tloadnode.handle_threadvar_access: tnode;
  202. begin
  203. { nothing special by default }
  204. result:=nil;
  205. end;
  206. constructor tloadnode.create(v : tsym;st : TSymtable);
  207. begin
  208. inherited create(loadn,nil);
  209. if not assigned(v) then
  210. internalerror(200108121);
  211. symtableentry:=v;
  212. symtable:=st;
  213. fprocdef:=nil;
  214. end;
  215. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : TSymtable);
  216. begin
  217. inherited create(loadn,nil);
  218. if not assigned(v) then
  219. internalerror(200108122);
  220. symtableentry:=v;
  221. symtable:=st;
  222. fprocdef:=d;
  223. end;
  224. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  225. begin
  226. inherited ppuload(t,ppufile);
  227. ppufile.getderef(symtableentryderef);
  228. symtable:=nil;
  229. ppufile.getderef(fprocdefderef);
  230. ppufile.getset(tppuset1(loadnodeflags));
  231. end;
  232. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  233. begin
  234. inherited ppuwrite(ppufile);
  235. ppufile.putderef(symtableentryderef);
  236. ppufile.putderef(fprocdefderef);
  237. ppufile.putset(tppuset1(loadnodeflags));
  238. end;
  239. procedure tloadnode.buildderefimpl;
  240. begin
  241. inherited buildderefimpl;
  242. symtableentryderef.build(symtableentry);
  243. fprocdefderef.build(fprocdef);
  244. end;
  245. procedure tloadnode.derefimpl;
  246. begin
  247. inherited derefimpl;
  248. symtableentry:=tsym(symtableentryderef.resolve);
  249. symtable:=symtableentry.owner;
  250. fprocdef:=tprocdef(fprocdefderef.resolve);
  251. end;
  252. procedure tloadnode.set_mp(p:tnode);
  253. begin
  254. { typen nodes should not be set }
  255. if p.nodetype=typen then
  256. internalerror(200301042);
  257. left:=p;
  258. end;
  259. function tloadnode.dogetcopy : tnode;
  260. var
  261. n : tloadnode;
  262. orglabel,
  263. labelcopy : tlabelnode;
  264. begin
  265. n:=tloadnode(inherited dogetcopy);
  266. n.symtable:=symtable;
  267. n.symtableentry:=symtableentry;
  268. n.fprocdef:=fprocdef;
  269. n.loadnodeflags:=loadnodeflags;
  270. if symtableentry.typ=labelsym then
  271. begin
  272. { see the comments for the tgotonode.labelsym field }
  273. orglabel:=tlabelnode(tlabelsym(symtableentry).code);
  274. labelcopy:=tlabelnode(orglabel.dogetcopy);
  275. if not assigned(labelcopy.labsym) then
  276. begin
  277. if not assigned(orglabel.labsym) then
  278. internalerror(2019091301);
  279. labelcopy.labsym:=clabelsym.create('$copiedlabelfrom$'+orglabel.labsym.RealName);
  280. labelcopy.labsym.code:=labelcopy;
  281. end;
  282. n.symtableentry:=labelcopy.labsym;
  283. end;
  284. result:=n;
  285. end;
  286. function tloadnode.is_addr_param_load:boolean;
  287. begin
  288. result:=(symtable.symtabletype=parasymtable) and
  289. (symtableentry.typ=paravarsym) and
  290. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  291. not(loadnf_load_addr in loadnodeflags) and
  292. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vardef,tprocdef(symtable.defowner).proccalloption);
  293. end;
  294. function tloadnode.pass_typecheck:tnode;
  295. begin
  296. result:=nil;
  297. case symtableentry.typ of
  298. absolutevarsym :
  299. resultdef:=tabsolutevarsym(symtableentry).vardef;
  300. constsym:
  301. begin
  302. if tconstsym(symtableentry).consttyp=constresourcestring then
  303. resultdef:=getansistringdef
  304. else
  305. internalerror(22799);
  306. end;
  307. staticvarsym :
  308. begin
  309. tabstractvarsym(symtableentry).IncRefCountBy(1);
  310. { static variables referenced in procedures or from finalization,
  311. variable needs to be in memory.
  312. It is too hard and the benefit is too small to detect whether a
  313. variable is only used in the finalization to add support for it (PFV) }
  314. if assigned(current_procinfo) and
  315. (symtable.symtabletype=staticsymtable) and
  316. (
  317. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  318. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  319. ) then
  320. make_not_regable(self,[ra_different_scope]);
  321. resultdef:=tabstractvarsym(symtableentry).vardef;
  322. if vo_is_thread_var in tstaticvarsym(symtableentry).varoptions then
  323. result:=handle_threadvar_access;
  324. end;
  325. paravarsym,
  326. localvarsym :
  327. begin
  328. tabstractvarsym(symtableentry).IncRefCountBy(1);
  329. { Nested variable? The we need to load the framepointer of
  330. the parent procedure }
  331. if assigned(current_procinfo) and
  332. (symtable.symtabletype in [localsymtable,parasymtable]) and
  333. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  334. begin
  335. if assigned(left) then
  336. internalerror(200309289);
  337. left:=cloadparentfpnode.create(tprocdef(symtable.defowner),lpf_forload);
  338. current_procinfo.set_needs_parentfp(tprocdef(symtable.defowner).parast.symtablelevel);
  339. { reference in nested procedures, variable needs to be in memory }
  340. { and behaves as if its address escapes its parent block }
  341. make_not_regable(self,[ra_different_scope]);
  342. end;
  343. resultdef:=tabstractvarsym(symtableentry).vardef;
  344. { e.g. self for objects is passed as var-parameter on the caller
  345. side, but on the callee-side we use it as a pointer ->
  346. adjust }
  347. if (loadnf_load_addr in loadnodeflags) then
  348. resultdef:=cpointerdef.getreusable(resultdef);
  349. if (vo_is_self in tabstractvarsym(symtableentry).varoptions) and (resultdef=objc_idtype) and
  350. (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  351. resultdef:=cclassrefdef.create(tprocdef(symtableentry.owner.defowner).struct)
  352. end;
  353. procsym :
  354. begin
  355. { Return the first procdef. In case of overloaded
  356. procdefs the matching procdef will be choosen
  357. when the expected procvardef is known, see get_information
  358. in htypechk.pas (PFV) }
  359. if not assigned(fprocdef) then
  360. fprocdef:=tprocdef(tprocsym(symtableentry).ProcdefList[0])
  361. else if po_kylixlocal in fprocdef.procoptions then
  362. CGMessage(type_e_cant_take_address_of_local_subroutine);
  363. { the result is a fprocdef, addrn and proc_to_procvar
  364. typeconvn need this as resultdef so they know
  365. that the address needs to be returned }
  366. resultdef:=fprocdef;
  367. if is_nested_pd(fprocdef) and is_nested_pd(current_procinfo.procdef) then
  368. current_procinfo.set_needs_parentfp(tprocdef(fprocdef.owner.defowner).parast.symtablelevel);
  369. { process methodpointer/framepointer }
  370. if assigned(left) then
  371. begin
  372. typecheckpass(left);
  373. if (po_classmethod in fprocdef.procoptions) and
  374. is_class(left.resultdef) and
  375. (left.nodetype<>niln) then
  376. begin
  377. left:=cloadvmtaddrnode.create(left);
  378. typecheckpass(left);
  379. end
  380. end;
  381. end;
  382. labelsym:
  383. begin
  384. tlabelsym(symtableentry).used:=true;
  385. resultdef:=voidtype;
  386. end;
  387. else
  388. internalerror(200104141);
  389. end;
  390. end;
  391. procedure Tloadnode.mark_write;
  392. begin
  393. include(flags,nf_write);
  394. end;
  395. function tloadnode.pass_1 : tnode;
  396. begin
  397. result:=nil;
  398. expectloc:=LOC_REFERENCE;
  399. case symtableentry.typ of
  400. absolutevarsym :
  401. ;
  402. constsym:
  403. begin
  404. if tconstsym(symtableentry).consttyp=constresourcestring then
  405. expectloc:=LOC_CREFERENCE;
  406. end;
  407. staticvarsym,
  408. localvarsym,
  409. paravarsym :
  410. begin
  411. if assigned(left) then
  412. firstpass(left);
  413. if not is_addr_param_load and
  414. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  415. expectloc:=tvarregable2tcgloc[tabstractvarsym(symtableentry).varregable]
  416. else
  417. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  418. expectloc:=LOC_CREFERENCE;
  419. { call to get address of threadvar }
  420. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  421. begin
  422. include(current_procinfo.flags,pi_do_call);
  423. include(current_procinfo.flags,pi_uses_threadvar);
  424. end;
  425. end;
  426. procsym :
  427. begin
  428. { initialise left for nested procs if necessary }
  429. if (m_nested_procvars in current_settings.modeswitches) then
  430. setprocdef(fprocdef);
  431. { method pointer or nested proc ? }
  432. if assigned(left) then
  433. begin
  434. expectloc:=LOC_CREGISTER;
  435. firstpass(left);
  436. end;
  437. end;
  438. labelsym :
  439. begin
  440. if not assigned(tlabelsym(symtableentry).asmblocklabel) and
  441. not assigned(tlabelsym(symtableentry).code) then
  442. Message(parser_e_label_outside_proc);
  443. end
  444. else
  445. internalerror(200104143);
  446. end;
  447. end;
  448. function tloadnode.docompare(p: tnode): boolean;
  449. begin
  450. docompare :=
  451. inherited docompare(p) and
  452. (symtableentry = tloadnode(p).symtableentry) and
  453. (fprocdef = tloadnode(p).fprocdef) and
  454. (symtable = tloadnode(p).symtable);
  455. end;
  456. procedure tloadnode.printnodedata(var t:text);
  457. begin
  458. inherited printnodedata(t);
  459. write(t,printnodeindention,'symbol = ',symtableentry.name);
  460. if symtableentry.typ=procsym then
  461. write(t,printnodeindention,'procdef = ',fprocdef.mangledname);
  462. writeln(t,'');
  463. end;
  464. {$ifdef DEBUG_NODE_XML}
  465. procedure TLoadNode.XMLPrintNodeData(var T: Text);
  466. begin
  467. inherited XMLPrintNodeData(T);
  468. WriteLn(T, printnodeindention, '<symbol>', symtableentry.name, '</symbol>');
  469. if symtableentry.typ = procsym then
  470. WriteLn(T, printnodeindention, '<procdef>', fprocdef.mangledname, '</procdef>');
  471. end;
  472. {$endif DEBUG_NODE_XML}
  473. procedure tloadnode.setprocdef(p : tprocdef);
  474. begin
  475. fprocdef:=p;
  476. resultdef:=p;
  477. { nested procedure? }
  478. if assigned(p) and
  479. is_nested_pd(p) then
  480. begin
  481. if not(m_nested_procvars in current_settings.modeswitches) then
  482. CGMessage(type_e_cant_take_address_of_local_subroutine)
  483. else
  484. begin
  485. { parent frame pointer pointer as "self" }
  486. left.free;
  487. left:=cloadparentfpnode.create(tprocdef(p.owner.defowner),lpf_forpara);
  488. end;
  489. end
  490. { we should never go from nested to non-nested }
  491. else if assigned(left) and
  492. (left.nodetype=loadparentfpn) then
  493. internalerror(2010072201);
  494. end;
  495. {*****************************************************************************
  496. TASSIGNMENTNODE
  497. *****************************************************************************}
  498. function tassignmentnode.direct_shortstring_assignment: boolean;
  499. begin
  500. result:=
  501. is_char(right.resultdef) or
  502. (right.resultdef.typ=stringdef);
  503. end;
  504. constructor tassignmentnode.create(l,r : tnode);
  505. begin
  506. inherited create(assignn,l,r);
  507. assigntype:=at_normal;
  508. if r.nodetype = typeconvn then
  509. ttypeconvnode(r).warn_pointer_to_signed:=false;
  510. end;
  511. constructor tassignmentnode.create_internal(l, r: tnode);
  512. begin
  513. create(l,r);
  514. include(flags,nf_internal);
  515. end;
  516. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  517. begin
  518. inherited ppuload(t,ppufile);
  519. assigntype:=tassigntype(ppufile.getbyte);
  520. end;
  521. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  522. begin
  523. inherited ppuwrite(ppufile);
  524. ppufile.putbyte(byte(assigntype));
  525. end;
  526. function tassignmentnode.dogetcopy : tnode;
  527. var
  528. n : tassignmentnode;
  529. begin
  530. n:=tassignmentnode(inherited dogetcopy);
  531. n.assigntype:=assigntype;
  532. result:=n;
  533. end;
  534. function tassignmentnode.simplify(forinline : boolean) : tnode;
  535. begin
  536. result:=nil;
  537. { assignment nodes can perform several floating point }
  538. { type conversions directly, so no typeconversions }
  539. { are inserted in those cases. When inlining, a }
  540. { variable may be replaced by a constant which can be }
  541. { converted at compile time, so check for this case }
  542. if is_real(left.resultdef) and
  543. is_real(right.resultdef) and
  544. is_constrealnode(right) and
  545. not equal_defs(right.resultdef,left.resultdef) then
  546. inserttypeconv(right,left.resultdef);
  547. end;
  548. function tassignmentnode.pass_typecheck:tnode;
  549. var
  550. hp : tnode;
  551. useshelper : boolean;
  552. oldassignmentnode : tassignmentnode;
  553. begin
  554. result:=nil;
  555. resultdef:=voidtype;
  556. { must be made unique }
  557. set_unique(left);
  558. typecheckpass(left);
  559. left.mark_write;
  560. { PI. This is needed to return correct resultdef of add nodes for ansistrings
  561. rawbytestring return needs to be replaced by left.resultdef }
  562. oldassignmentnode:=aktassignmentnode;
  563. aktassignmentnode:=self;
  564. typecheckpass(right);
  565. aktassignmentnode:=oldassignmentnode;
  566. set_varstate(right,vs_read,[vsf_must_be_valid]);
  567. set_varstate(left,vs_written,[]);
  568. if codegenerror then
  569. exit;
  570. { just in case the typecheckpass of right optimized something here }
  571. if nf_assign_done_in_right in flags then
  572. begin
  573. result:=right;
  574. right:=nil;
  575. exit;
  576. end;
  577. { tp procvar support, when we don't expect a procvar
  578. then we need to call the procvar }
  579. if (left.resultdef.typ<>procvardef) then
  580. maybe_call_procvar(right,true);
  581. { assignments to formaldefs and open arrays aren't allowed }
  582. if is_open_array(left.resultdef) then
  583. CGMessage(type_e_assignment_not_allowed)
  584. else if (left.resultdef.typ=formaldef) then
  585. if not(target_info.system in systems_managed_vm) then
  586. CGMessage(type_e_assignment_not_allowed)
  587. else
  588. begin
  589. { on managed platforms, assigning to formaldefs is allowed (but
  590. typecasting them on the left hand side isn't), but primitive
  591. values need to be boxed first }
  592. if (right.resultdef.typ in [orddef,floatdef]) then
  593. begin
  594. right:=cinlinenode.create(in_box_x,false,ccallparanode.create(right,nil));
  595. typecheckpass(right);
  596. end;
  597. end;
  598. { test if node can be assigned, properties are allowed }
  599. if not(nf_internal in flags) then
  600. if not valid_for_assignment(left,true) then
  601. { errors can in situations that cause the compiler to run out of
  602. memory, such as assigning to an implicit pointer-to-array
  603. converted node (that array is 2^31 or 2^63 bytes large) }
  604. exit;
  605. { assigning nil or [] to a dynamic array clears the array }
  606. if is_dynamic_array(left.resultdef) and
  607. (
  608. (right.nodetype=niln) or
  609. (
  610. (right.nodetype=arrayconstructorn) and
  611. (right.resultdef.typ=arraydef) and
  612. (tarraydef(right.resultdef).elementdef=voidtype) and
  613. tarrayconstructornode(right).isempty
  614. )
  615. ) then
  616. begin
  617. { remove property flag to avoid errors, see comments for }
  618. { tf_winlikewidestring assignments below }
  619. exclude(left.flags,nf_isproperty);
  620. { generate a setlength node so it can be intercepted by
  621. target-specific code }
  622. result:=cinlinenode.create(in_setlength_x,false,
  623. ccallparanode.create(genintconstnode(0),
  624. ccallparanode.create(left,nil)));
  625. left:=nil;
  626. exit;
  627. end;
  628. { shortstring helpers can do the conversion directly,
  629. so treat them separatly }
  630. if (is_shortstring(left.resultdef)) then
  631. begin
  632. { insert typeconv, except for chars that are handled in
  633. secondpass and except for ansi/wide string that can
  634. be converted immediatly }
  635. if not direct_shortstring_assignment then
  636. inserttypeconv(right,left.resultdef);
  637. if right.resultdef.typ=stringdef then
  638. begin
  639. useshelper:=true;
  640. { convert constant strings to shortstrings. But
  641. skip empty constant strings, that will be handled
  642. in secondpass }
  643. if (right.nodetype=stringconstn) then
  644. begin
  645. { verify if range fits within shortstring }
  646. { just emit a warning, delphi gives an }
  647. { error, only if the type definition of }
  648. { of the string is less < 255 characters }
  649. if not is_open_string(left.resultdef) and
  650. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  651. cgmessage(type_w_string_too_long);
  652. inserttypeconv(right,left.resultdef);
  653. if (right.nodetype=stringconstn) and
  654. (tstringconstnode(right).len=0) then
  655. useshelper:=false;
  656. end
  657. else if (tstringdef(right.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  658. Message2(type_w_implicit_string_cast_loss,right.resultdef.typename,left.resultdef.typename);
  659. { rest is done in pass 1 (JM) }
  660. if useshelper then
  661. exit;
  662. end
  663. end
  664. { floating point assignments can also perform the conversion directly }
  665. else if is_real(left.resultdef) and is_real(right.resultdef) and
  666. not is_constrealnode(right)
  667. {$ifdef cpufpemu}
  668. { the emulator can't do this obviously }
  669. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  670. {$endif cpufpemu}
  671. {$ifdef x86}
  672. { the assignment node code can't convert a double in an }
  673. { sse register to an extended value in memory more }
  674. { efficiently than a type conversion node, so don't }
  675. { bother implementing support for that }
  676. and (use_vectorfpu(left.resultdef) or not(use_vectorfpu(right.resultdef)))
  677. {$endif}
  678. {$ifdef arm}
  679. { the assignment node code can't convert a single in
  680. an interger register to a double in an mmregister or
  681. vice versa }
  682. and (use_vectorfpu(left.resultdef) and
  683. use_vectorfpu(right.resultdef) and
  684. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype))
  685. {$endif arm}
  686. {$ifdef xtensa}
  687. and not((FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) xor
  688. (FPUXTENSA_DOUBLE in fpu_capabilities[current_settings.fputype]))
  689. {$endif}
  690. then
  691. begin
  692. if not(nf_internal in flags) then
  693. check_ranges(fileinfo,right,left.resultdef);
  694. end
  695. else
  696. begin
  697. { check if the assignment may cause a range check error }
  698. if not(nf_internal in flags) then
  699. check_ranges(fileinfo,right,left.resultdef);
  700. { beginners might be confused about an error message like
  701. Incompatible types: got "untyped" expected "LongInt"
  702. when trying to assign the result of a procedure, so give
  703. a better error message, see also #19122 }
  704. if (left.resultdef.typ<>procvardef) and
  705. (right.nodetype=calln) and is_void(right.resultdef) then
  706. CGMessage(type_e_procedures_return_no_value)
  707. else if nf_internal in flags then
  708. inserttypeconv_internal(right,left.resultdef)
  709. else
  710. inserttypeconv(right,left.resultdef);
  711. end;
  712. { call helpers for interface }
  713. if is_interfacecom_or_dispinterface(left.resultdef) then
  714. begin
  715. { Normal interface assignments are handled by the generic refcount incr/decr }
  716. if not def_is_related(right.resultdef,left.resultdef) then
  717. begin
  718. { remove property flag to avoid errors, see comments for }
  719. { tf_winlikewidestring assignments below }
  720. exclude(left.flags,nf_isproperty);
  721. hp:=
  722. ccallparanode.create(
  723. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  724. ccallparanode.create(
  725. ctypeconvnode.create_internal(right,voidpointertype),
  726. ccallparanode.create(
  727. ctypeconvnode.create_internal(left,voidpointertype),
  728. nil)));
  729. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  730. left:=nil;
  731. right:=nil;
  732. exit;
  733. end;
  734. end;
  735. { check if local proc/func is assigned to procvar }
  736. if right.resultdef.typ=procvardef then
  737. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  738. end;
  739. function tassignmentnode.pass_1 : tnode;
  740. var
  741. hp: tnode;
  742. oldassignmentnode : tassignmentnode;
  743. hdef: tdef;
  744. hs: string;
  745. needrtti: boolean;
  746. begin
  747. result:=nil;
  748. expectloc:=LOC_VOID;
  749. firstpass(left);
  750. { Optimize the reuse of the destination of the assingment in left.
  751. Allow the use of the left inside the tree generated on the right.
  752. This is especially useful for string routines where the destination
  753. is pushed as a parameter. Using the final destination of left directly
  754. save a temp allocation and copy of data (PFV) }
  755. oldassignmentnode:=aktassignmentnode;
  756. aktassignmentnode:=self;
  757. firstpass(right);
  758. aktassignmentnode:=oldassignmentnode;
  759. if nf_assign_done_in_right in flags then
  760. begin
  761. result:=right;
  762. right:=nil;
  763. exit;
  764. end;
  765. if codegenerror then
  766. exit;
  767. { assignment to refcounted variable -> inc/decref }
  768. if is_managed_type(left.resultdef) then
  769. include(current_procinfo.flags,pi_do_call);
  770. needrtti:=false;
  771. if (is_shortstring(left.resultdef)) then
  772. begin
  773. if right.resultdef.typ=stringdef then
  774. begin
  775. if (right.nodetype<>stringconstn) or
  776. (tstringconstnode(right).len<>0) then
  777. begin
  778. { remove property flag to avoid errors, see comments for }
  779. { tf_winlikewidestring assignments below }
  780. exclude(left.flags, nf_isproperty);
  781. hp:=ccallparanode.create
  782. (right,
  783. ccallparanode.create(left,nil));
  784. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  785. firstpass(result);
  786. left:=nil;
  787. right:=nil;
  788. end;
  789. end;
  790. exit;
  791. end
  792. { call helpers for composite types containing automated types }
  793. else if is_managed_type(left.resultdef) and
  794. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  795. not is_interfacecom_or_dispinterface(left.resultdef) and
  796. not is_dynamic_array(left.resultdef) and
  797. not is_const(left) and
  798. not(target_info.system in systems_garbage_collected_managed_types) then
  799. begin
  800. hp:=ccallparanode.create(caddrnode.create_internal(
  801. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  802. ccallparanode.create(ctypeconvnode.create_internal(
  803. caddrnode.create_internal(left),voidpointertype),
  804. ccallparanode.create(ctypeconvnode.create_internal(
  805. caddrnode.create_internal(right),voidpointertype),
  806. nil)));
  807. result:=ccallnode.createintern('fpc_copy_proc',hp);
  808. firstpass(result);
  809. left:=nil;
  810. right:=nil;
  811. exit;
  812. end
  813. { call helpers for variant, they can contain non ref. counted types like
  814. vararrays which must be really copied }
  815. else if (left.resultdef.typ=variantdef) and
  816. not(is_const(left)) and
  817. not(target_info.system in systems_garbage_collected_managed_types) then
  818. begin
  819. { remove property flag to avoid errors, see comments for }
  820. { tf_winlikewidestring assignments below }
  821. exclude(left.flags,nf_isproperty);
  822. hdef:=search_system_type('TVARDATA').typedef;
  823. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  824. right,hdef),
  825. ccallparanode.create(ctypeconvnode.create_internal(
  826. left,hdef),
  827. nil));
  828. result:=ccallnode.createintern('fpc_variant_copy',hp);
  829. firstpass(result);
  830. left:=nil;
  831. right:=nil;
  832. exit;
  833. end
  834. else if not(target_info.system in systems_garbage_collected_managed_types) and
  835. not(is_const(left)) then
  836. begin
  837. { call helpers for pointer-sized managed types }
  838. if is_widestring(left.resultdef) then
  839. hs:='fpc_widestr_assign'
  840. else if is_ansistring(left.resultdef) then
  841. hs:='fpc_ansistr_assign'
  842. else if is_unicodestring(left.resultdef) then
  843. hs:='fpc_unicodestr_assign'
  844. else if is_interfacecom_or_dispinterface(left.resultdef) then
  845. hs:='fpc_intf_assign'
  846. else if is_dynamic_array(left.resultdef) then
  847. begin
  848. hs:='fpc_dynarray_assign';
  849. needrtti:=true;
  850. end
  851. else
  852. exit;
  853. end
  854. else
  855. exit;
  856. { The first argument of these procedures is a var parameter. Properties cannot }
  857. { be passed to var or out parameters, because in that case setters/getters are not }
  858. { used. Further, if we would allow it in case there are no getters or setters, you }
  859. { would need source changes in case these are introduced later on, thus defeating }
  860. { part of the transparency advantages of properties. In this particular case, }
  861. { however: }
  862. { a) if there is a setter, this code will not be used since then the assignment }
  863. { will be converted to a procedure call }
  864. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  865. { the refcount of the field to which we are writing }
  866. { c) source code changes are not required if a setter is added/removed, because }
  867. { this transformation is handled at compile time }
  868. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  869. { is a property which refers to a field without a setter call, we will not get }
  870. { an error about trying to pass a property as a var parameter }
  871. exclude(left.flags,nf_isproperty);
  872. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  873. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  874. nil));
  875. if needrtti then
  876. hp:=ccallparanode.create(
  877. caddrnode.create_internal(
  878. crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  879. hp);
  880. result:=ccallnode.createintern(hs,hp);
  881. firstpass(result);
  882. left:=nil;
  883. right:=nil;
  884. end;
  885. function tassignmentnode.docompare(p: tnode): boolean;
  886. begin
  887. docompare :=
  888. inherited docompare(p) and
  889. (assigntype = tassignmentnode(p).assigntype);
  890. end;
  891. {$ifdef state_tracking}
  892. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  893. var se:Tstate_entry;
  894. begin
  895. track_state_pass:=false;
  896. if exec_known then
  897. begin
  898. track_state_pass:=right.track_state_pass(exec_known);
  899. {Force a new resultdef pass.}
  900. right.resultdef:=nil;
  901. do_typecheckpass(right);
  902. typecheckpass(right);
  903. aktstate.store_fact(left.getcopy,right.getcopy);
  904. end
  905. else
  906. aktstate.delete_fact(left);
  907. end;
  908. {$endif}
  909. {$ifdef DEBUG_NODE_XML}
  910. procedure TAssignmentNode.XMLPrintNodeData(var T: Text);
  911. begin
  912. { For assignments, put the left and right branches on the same level for clarity }
  913. XMLPrintNode(T, Left);
  914. XMLPrintNode(T, Right);
  915. PrintNodeUnindent;
  916. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  917. end;
  918. {$endif DEBUG_NODE_XML}
  919. {*****************************************************************************
  920. TARRAYCONSTRUCTORRANGENODE
  921. *****************************************************************************}
  922. constructor tarrayconstructorrangenode.create(l,r : tnode);
  923. begin
  924. inherited create(arrayconstructorrangen,l,r);
  925. end;
  926. function tarrayconstructorrangenode.pass_typecheck:tnode;
  927. begin
  928. result:=nil;
  929. typecheckpass(left);
  930. typecheckpass(right);
  931. set_varstate(left,vs_read,[vsf_must_be_valid]);
  932. set_varstate(right,vs_read,[vsf_must_be_valid]);
  933. if codegenerror then
  934. exit;
  935. resultdef:=left.resultdef;
  936. end;
  937. function tarrayconstructorrangenode.pass_1 : tnode;
  938. begin
  939. result:=nil;
  940. CGMessage(parser_e_illegal_expression);
  941. end;
  942. {****************************************************************************
  943. TARRAYCONSTRUCTORNODE
  944. *****************************************************************************}
  945. constructor tarrayconstructornode.create(l,r : tnode);
  946. begin
  947. inherited create(arrayconstructorn,l,r);
  948. allow_array_constructor:=false;
  949. end;
  950. function tarrayconstructornode.dogetcopy : tnode;
  951. var
  952. n : tarrayconstructornode;
  953. begin
  954. n:=tarrayconstructornode(inherited dogetcopy);
  955. result:=n;
  956. end;
  957. function tarrayconstructornode.has_range_node:boolean;
  958. var
  959. n : tarrayconstructornode;
  960. begin
  961. result:=false;
  962. n:=self;
  963. while assigned(n) do
  964. begin
  965. if assigned(n.left) and (n.left.nodetype=arrayconstructorrangen) then
  966. begin
  967. result:=true;
  968. break;
  969. end;
  970. n:=tarrayconstructornode(n.right);
  971. end;
  972. end;
  973. function tarrayconstructornode.isempty:boolean;
  974. begin
  975. result:=not(assigned(left)) and not(assigned(right));
  976. end;
  977. function tarrayconstructornode.pass_typecheck:tnode;
  978. var
  979. hdef : tdef;
  980. hp : tarrayconstructornode;
  981. len : longint;
  982. varia : boolean;
  983. eq : tequaltype;
  984. hnodetype : tnodetype;
  985. begin
  986. result:=nil;
  987. { are we allowing array constructor? Then convert it to a set.
  988. Do this only if we didn't convert the arrayconstructor yet. This
  989. is needed for the cases where the resultdef is forced for a second
  990. run }
  991. if not allow_array_constructor or has_range_node then
  992. begin
  993. hp:=tarrayconstructornode(getcopy);
  994. arrayconstructor_to_set(tnode(hp));
  995. result:=hp;
  996. exit;
  997. end;
  998. { only pass left tree, right tree contains next construct if any }
  999. hdef:=nil;
  1000. hnodetype:=errorn;
  1001. len:=0;
  1002. varia:=false;
  1003. if assigned(left) then
  1004. begin
  1005. hp:=self;
  1006. while assigned(hp) do
  1007. begin
  1008. typecheckpass(hp.left);
  1009. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  1010. if (hdef=nil) then
  1011. begin
  1012. hdef:=hp.left.resultdef;
  1013. hnodetype:=hp.left.nodetype;
  1014. end
  1015. else
  1016. begin
  1017. { If we got a niln we don't know the type yet and need to take the
  1018. type of the next array element.
  1019. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  1020. if hnodetype=niln then
  1021. begin
  1022. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  1023. if eq>te_incompatible then
  1024. begin
  1025. hdef:=hp.left.resultdef;
  1026. hnodetype:=hp.left.nodetype;
  1027. end;
  1028. end
  1029. else
  1030. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  1031. if (not varia) and (eq<te_equal) then
  1032. begin
  1033. { If both are integers we need to take the type that can hold both
  1034. defs }
  1035. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  1036. begin
  1037. if is_in_limit(hdef,hp.left.resultdef) then
  1038. hdef:=hp.left.resultdef;
  1039. end
  1040. else
  1041. if (nf_novariaallowed in flags) then
  1042. varia:=true;
  1043. end;
  1044. end;
  1045. inc(len);
  1046. hp:=tarrayconstructornode(hp.right);
  1047. end;
  1048. end;
  1049. { Set the type of empty or varia arrays to void. Also
  1050. do this if the type is array of const/open array
  1051. because those can't be used with setelementdef }
  1052. if not assigned(hdef) or
  1053. varia or
  1054. is_array_of_const(hdef) or
  1055. is_open_array(hdef) then
  1056. hdef:=voidtype;
  1057. resultdef:=carraydef.create(0,len-1,s32inttype);
  1058. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1059. if varia then
  1060. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1061. tarraydef(resultdef).elementdef:=hdef;
  1062. end;
  1063. procedure tarrayconstructornode.force_type(def:tdef);
  1064. var
  1065. hp : tarrayconstructornode;
  1066. begin
  1067. tarraydef(resultdef).elementdef:=def;
  1068. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  1069. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  1070. if assigned(left) then
  1071. begin
  1072. hp:=self;
  1073. while assigned(hp) do
  1074. begin
  1075. inserttypeconv(hp.left,def);
  1076. hp:=tarrayconstructornode(hp.right);
  1077. end;
  1078. end;
  1079. end;
  1080. procedure tarrayconstructornode.insert_typeconvs;
  1081. var
  1082. hp : tarrayconstructornode;
  1083. dovariant : boolean;
  1084. begin
  1085. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1086. { only pass left tree, right tree contains next construct if any }
  1087. if assigned(left) then
  1088. begin
  1089. hp:=self;
  1090. while assigned(hp) do
  1091. begin
  1092. typecheckpass(hp.left);
  1093. { Insert typeconvs for array of const }
  1094. if dovariant then
  1095. { at this time C varargs are no longer an arrayconstructornode }
  1096. insert_varargstypeconv(hp.left,false);
  1097. hp:=tarrayconstructornode(hp.right);
  1098. end;
  1099. end;
  1100. end;
  1101. function tarrayconstructornode.pass_1 : tnode;
  1102. var
  1103. hp : tarrayconstructornode;
  1104. do_variant,
  1105. do_managed_variant:boolean;
  1106. begin
  1107. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  1108. do_managed_variant:=
  1109. do_variant and
  1110. (target_info.system in systems_managed_vm);
  1111. result:=nil;
  1112. { Insert required type convs, this must be
  1113. done in pass 1, because the call must be
  1114. typecheckpassed already }
  1115. if assigned(left) then
  1116. begin
  1117. insert_typeconvs;
  1118. { call firstpass for all nodes }
  1119. hp:=self;
  1120. while assigned(hp) do
  1121. begin
  1122. if hp.left<>nil then
  1123. begin
  1124. {This check is pessimistic; a call will happen depending
  1125. on the location in which the elements will be found in
  1126. pass 2.}
  1127. if not do_variant then
  1128. include(current_procinfo.flags,pi_do_call);
  1129. firstpass(hp.left);
  1130. if do_managed_variant then
  1131. wrapmanagedvarrec(hp.left);
  1132. end;
  1133. hp:=tarrayconstructornode(hp.right);
  1134. end;
  1135. end;
  1136. { set the elementdef to the correct type in case of a variant array }
  1137. if do_variant then
  1138. tarraydef(resultdef).elementdef:=search_system_type('TVARREC').typedef;
  1139. expectloc:=LOC_CREFERENCE;
  1140. inc(current_procinfo.estimatedtempsize,(tarraydef(resultdef).highrange+1)*tarraydef(resultdef).elementdef.size);
  1141. end;
  1142. function tarrayconstructornode.docompare(p: tnode): boolean;
  1143. begin
  1144. docompare:=inherited docompare(p);
  1145. end;
  1146. {*****************************************************************************
  1147. TTYPENODE
  1148. *****************************************************************************}
  1149. constructor ttypenode.create(def:tdef);
  1150. begin
  1151. inherited create(typen);
  1152. typedef:=def;
  1153. typesym:=def.typesym;
  1154. allowed:=false;
  1155. helperallowed:=false;
  1156. end;
  1157. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1158. begin
  1159. inherited ppuload(t,ppufile);
  1160. ppufile.getderef(typedefderef);
  1161. ppufile.getderef(typesymderef);
  1162. allowed:=ppufile.getboolean;
  1163. helperallowed:=ppufile.getboolean;
  1164. end;
  1165. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  1166. begin
  1167. inherited ppuwrite(ppufile);
  1168. ppufile.putderef(typedefderef);
  1169. ppufile.putderef(typesymderef);
  1170. ppufile.putboolean(allowed);
  1171. ppufile.putboolean(helperallowed);
  1172. end;
  1173. procedure ttypenode.buildderefimpl;
  1174. begin
  1175. inherited buildderefimpl;
  1176. typedefderef.build(typedef);
  1177. typesymderef.build(typesym);
  1178. end;
  1179. procedure ttypenode.derefimpl;
  1180. begin
  1181. inherited derefimpl;
  1182. typedef:=tdef(typedefderef.resolve);
  1183. typesym:=tsym(typesymderef.resolve);
  1184. end;
  1185. function ttypenode.pass_typecheck:tnode;
  1186. begin
  1187. result:=nil;
  1188. resultdef:=typedef;
  1189. { check if it's valid }
  1190. if typedef.typ = errordef then
  1191. CGMessage(parser_e_illegal_expression);
  1192. end;
  1193. function ttypenode.pass_1 : tnode;
  1194. begin
  1195. result:=nil;
  1196. expectloc:=LOC_VOID;
  1197. { a typenode can't generate code, so we give here
  1198. an error. Else it'll be an abstract error in pass_generate_code.
  1199. Only when the allowed flag is set we don't generate
  1200. an error }
  1201. if not allowed then
  1202. CGMessage(parser_e_no_type_not_allowed_here);
  1203. if not helperallowed and is_objectpascal_helper(typedef) then
  1204. CGMessage(parser_e_no_category_as_types);
  1205. end;
  1206. function ttypenode.dogetcopy : tnode;
  1207. var
  1208. n : ttypenode;
  1209. begin
  1210. n:=ttypenode(inherited dogetcopy);
  1211. n.allowed:=allowed;
  1212. n.typedef:=typedef;
  1213. n.helperallowed:=helperallowed;
  1214. result:=n;
  1215. end;
  1216. function ttypenode.docompare(p: tnode): boolean;
  1217. begin
  1218. docompare :=
  1219. inherited docompare(p) and
  1220. (typedef=ttypenode(p).typedef) and
  1221. (allowed=ttypenode(p).allowed) and
  1222. (helperallowed=ttypenode(p).helperallowed);
  1223. end;
  1224. {*****************************************************************************
  1225. TRTTINODE
  1226. *****************************************************************************}
  1227. constructor trttinode.create(def:tstoreddef;rt:trttitype;dt:Trttidatatype);
  1228. begin
  1229. inherited create(rttin);
  1230. rttidef:=def;
  1231. rttitype:=rt;
  1232. rttidatatype:=dt;
  1233. end;
  1234. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1235. begin
  1236. inherited ppuload(t,ppufile);
  1237. ppufile.getderef(rttidefderef);
  1238. rttitype:=trttitype(ppufile.getbyte);
  1239. rttidatatype:=trttidatatype(ppufile.getbyte);
  1240. end;
  1241. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1242. begin
  1243. inherited ppuwrite(ppufile);
  1244. ppufile.putderef(rttidefderef);
  1245. ppufile.putbyte(byte(rttitype));
  1246. ppufile.putbyte(byte(rttidatatype));
  1247. end;
  1248. procedure trttinode.buildderefimpl;
  1249. begin
  1250. inherited buildderefimpl;
  1251. rttidefderef.build(rttidef);
  1252. end;
  1253. procedure trttinode.derefimpl;
  1254. begin
  1255. inherited derefimpl;
  1256. rttidef:=tstoreddef(rttidefderef.resolve);
  1257. end;
  1258. function trttinode.dogetcopy : tnode;
  1259. var
  1260. n : trttinode;
  1261. begin
  1262. n:=trttinode(inherited dogetcopy);
  1263. n.rttidef:=rttidef;
  1264. n.rttitype:=rttitype;
  1265. n.rttidatatype:=rttidatatype;
  1266. result:=n;
  1267. end;
  1268. function trttinode.pass_typecheck:tnode;
  1269. begin
  1270. { rtti information will be returned as a void pointer }
  1271. result:=nil;
  1272. resultdef:=voidpointertype;
  1273. end;
  1274. function trttinode.pass_1 : tnode;
  1275. begin
  1276. result:=nil;
  1277. expectloc:=LOC_CREFERENCE;
  1278. end;
  1279. function trttinode.docompare(p: tnode): boolean;
  1280. begin
  1281. docompare :=
  1282. inherited docompare(p) and
  1283. (rttidef = trttinode(p).rttidef) and
  1284. (rttitype = trttinode(p).rttitype) and
  1285. (rttidatatype = trttinode(p).rttidatatype);
  1286. end;
  1287. end.