nld.pas 55 KB

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