nld.pas 58 KB

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