nld.pas 55 KB

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