nld.pas 58 KB

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