nld.pas 55 KB

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