nld.pas 51 KB

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