nld.pas 54 KB

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