nld.pas 51 KB

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