ptconst.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Reads typed constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptconst;
  19. {$i defines.inc}
  20. interface
  21. uses symtype,symsym;
  22. { this procedure reads typed constants }
  23. { sym is only needed for ansi strings }
  24. { the assembler label is in the middle (PM) }
  25. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;no_change_allowed : boolean);
  26. implementation
  27. uses
  28. {$ifdef Delphi}
  29. sysutils,
  30. {$else}
  31. strings,
  32. {$endif Delphi}
  33. globtype,systems,tokens,cpuinfo,
  34. cutils,globals,scanner,
  35. symconst,symbase,symdef,aasm,types,verbose,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  39. { parser specific stuff }
  40. pbase,pexpr,
  41. { codegen }
  42. hcodegen
  43. {$ifdef newcg}
  44. ,cgbase
  45. {$endif}
  46. ;
  47. {$ifdef fpc}
  48. {$maxfpuregisters 0}
  49. {$endif fpc}
  50. { this procedure reads typed constants }
  51. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;no_change_allowed : boolean);
  52. var
  53. len,base : longint;
  54. p,hp : tnode;
  55. i,l,offset,
  56. strlength : longint;
  57. curconstsegment : TAAsmoutput;
  58. ll : tasmlabel;
  59. s : string;
  60. ca : pchar;
  61. tmpguid : tguid;
  62. aktpos : longint;
  63. obj : tobjectdef;
  64. recsym,
  65. srsym : tsym;
  66. symt : tsymtable;
  67. value : bestreal;
  68. strval : pchar;
  69. error : boolean;
  70. procedure check_range(def:torddef);
  71. begin
  72. if ((tordconstnode(p).value>def.high) or
  73. (tordconstnode(p).value<def.low)) then
  74. begin
  75. if (cs_check_range in aktlocalswitches) then
  76. Message(parser_e_range_check_error)
  77. else
  78. Message(parser_w_range_check_error);
  79. end;
  80. end;
  81. {$R-} {Range check creates problem with init_8bit(-1) !!}
  82. begin
  83. if no_change_allowed then
  84. curconstsegment:=consts
  85. else
  86. curconstsegment:=datasegment;
  87. case t.def.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. case torddef(t.def).typ of
  92. bool8bit :
  93. begin
  94. if is_constboolnode(p) then
  95. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  96. else
  97. Message(cg_e_illegal_expression);
  98. end;
  99. bool16bit :
  100. begin
  101. if is_constboolnode(p) then
  102. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  103. else
  104. Message(cg_e_illegal_expression);
  105. end;
  106. bool32bit :
  107. begin
  108. if is_constboolnode(p) then
  109. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  110. else
  111. Message(cg_e_illegal_expression);
  112. end;
  113. uchar :
  114. begin
  115. if is_constcharnode(p) then
  116. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  117. else
  118. Message(cg_e_illegal_expression);
  119. end;
  120. uwidechar :
  121. begin
  122. if is_constcharnode(p) then
  123. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  124. else
  125. Message(cg_e_illegal_expression);
  126. end;
  127. s8bit,
  128. u8bit :
  129. begin
  130. if is_constintnode(p) then
  131. begin
  132. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  133. check_range(torddef(t.def));
  134. end
  135. else
  136. Message(cg_e_illegal_expression);
  137. end;
  138. u16bit,
  139. s16bit :
  140. begin
  141. if is_constintnode(p) then
  142. begin
  143. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  144. check_range(torddef(t.def));
  145. end
  146. else
  147. Message(cg_e_illegal_expression);
  148. end;
  149. s32bit,
  150. u32bit :
  151. begin
  152. if is_constintnode(p) then
  153. begin
  154. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  155. if torddef(t.def).typ<>u32bit then
  156. check_range(torddef(t.def));
  157. end
  158. else
  159. Message(cg_e_illegal_expression);
  160. end;
  161. s64bit,
  162. u64bit:
  163. begin
  164. if is_constintnode(p) then
  165. begin
  166. {!!!!! hmmm, we can write yet only consts til 2^32-1 :( (FK) }
  167. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  168. curconstSegment.concat(Tai_const.Create_32bit(0));
  169. end
  170. else
  171. Message(cg_e_illegal_expression);
  172. end;
  173. else
  174. internalerror(3799);
  175. end;
  176. p.free;
  177. end;
  178. floatdef:
  179. begin
  180. p:=comp_expr(true);
  181. if is_constrealnode(p) then
  182. value:=trealconstnode(p).value_real
  183. else if is_constintnode(p) then
  184. value:=tordconstnode(p).value
  185. else
  186. Message(cg_e_illegal_expression);
  187. case tfloatdef(t.def).typ of
  188. s32real :
  189. curconstSegment.concat(Tai_real_32bit.Create(value));
  190. s64real :
  191. curconstSegment.concat(Tai_real_64bit.Create(value));
  192. s80real :
  193. curconstSegment.concat(Tai_real_80bit.Create(value));
  194. s64comp :
  195. curconstSegment.concat(Tai_comp_64bit.Create(value));
  196. else
  197. internalerror(18);
  198. end;
  199. p.free;
  200. end;
  201. classrefdef:
  202. begin
  203. p:=comp_expr(true);
  204. case p.nodetype of
  205. loadvmtn:
  206. begin
  207. if not(tobjectdef(tclassrefdef(p.resulttype.def).pointertype.def).is_related(
  208. tobjectdef(tclassrefdef(t.def).pointertype.def))) then
  209. Message(cg_e_illegal_expression);
  210. curconstSegment.concat(Tai_const_symbol.Create(newasmsymbol(tobjectdef(
  211. tclassrefdef(p.resulttype.def).pointertype.def).vmt_mangledname)));
  212. end;
  213. niln:
  214. curconstSegment.concat(Tai_const.Create_32bit(0));
  215. else Message(cg_e_illegal_expression);
  216. end;
  217. p.free;
  218. end;
  219. pointerdef:
  220. begin
  221. p:=comp_expr(true);
  222. if (p.nodetype=typeconvn) and
  223. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  224. is_equal(t.def,p.resulttype.def) then
  225. begin
  226. hp:=ttypeconvnode(p).left;
  227. ttypeconvnode(p).left:=nil;
  228. p.free;
  229. p:=hp;
  230. end;
  231. { allows horrible ofs(typeof(TButton)^) code !! }
  232. if (p.nodetype=addrn) and
  233. (taddrnode(p).left.nodetype=derefn) then
  234. begin
  235. hp:=tderefnode(taddrnode(p).left).left;
  236. tderefnode(taddrnode(p).left).left:=nil;
  237. p.free;
  238. p:=hp;
  239. end;
  240. { const pointer ? }
  241. if (p.nodetype = pointerconstn) then
  242. curconstsegment.concat(Tai_const.Create_32bit(
  243. tpointerconstnode(p).value))
  244. { nil pointer ? }
  245. else if p.nodetype=niln then
  246. curconstSegment.concat(Tai_const.Create_32bit(0))
  247. { maybe pchar ? }
  248. else
  249. if is_char(tpointerdef(t.def).pointertype.def) and
  250. (p.nodetype<>addrn) then
  251. begin
  252. getdatalabel(ll);
  253. curconstSegment.concat(Tai_const_symbol.Create(ll));
  254. Consts.concat(Tai_label.Create(ll));
  255. if p.nodetype=stringconstn then
  256. begin
  257. len:=tstringconstnode(p).len;
  258. { For tp7 the maximum lentgh can be 255 }
  259. if (m_tp in aktmodeswitches) and
  260. (len>255) then
  261. len:=255;
  262. getmem(ca,len+2);
  263. move(tstringconstnode(p).value_str^,ca^,len+1);
  264. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  265. end
  266. else
  267. if is_constcharnode(p) then
  268. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  269. else
  270. Message(cg_e_illegal_expression);
  271. end
  272. else
  273. if p.nodetype=addrn then
  274. begin
  275. hp:=taddrnode(p).left;
  276. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  277. hp:=tbinarynode(hp).left;
  278. if (is_equal(tpointerdef(p.resulttype.def).pointertype.def,tpointerdef(t.def).pointertype.def) or
  279. (is_void(tpointerdef(p.resulttype.def).pointertype.def)) or
  280. (is_void(tpointerdef(t.def).pointertype.def))) and
  281. (hp.nodetype=loadn) then
  282. begin
  283. do_resulttypepass(taddrnode(p).left);
  284. hp:=taddrnode(p).left;
  285. offset:=0;
  286. while assigned(hp) and (hp.nodetype<>loadn) do
  287. begin
  288. case hp.nodetype of
  289. vecn :
  290. begin
  291. case tvecnode(hp).left.resulttype.def.deftype of
  292. stringdef :
  293. begin
  294. { this seems OK for shortstring and ansistrings PM }
  295. { it is wrong for widestrings !! }
  296. len:=1;
  297. base:=0;
  298. end;
  299. arraydef :
  300. begin
  301. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  302. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  303. end
  304. else
  305. Message(cg_e_illegal_expression);
  306. end;
  307. if is_constintnode(tvecnode(hp).right) then
  308. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  309. else
  310. Message(cg_e_illegal_expression);
  311. end;
  312. subscriptn :
  313. inc(offset,tsubscriptnode(hp).vs.address)
  314. else
  315. Message(cg_e_illegal_expression);
  316. end;
  317. hp:=tbinarynode(hp).left;
  318. end;
  319. if tloadnode(hp).symtableentry.typ=constsym then
  320. Message(type_e_variable_id_expected);
  321. curconstSegment.concat(Tai_const_symbol.Createname_offset(tloadnode(hp).symtableentry.mangledname,offset));
  322. end
  323. else
  324. Message(cg_e_illegal_expression);
  325. end
  326. else
  327. { allow typeof(Object type)}
  328. if (p.nodetype=inlinen) and
  329. (tinlinenode(p).inlinenumber=in_typeof_x) then
  330. begin
  331. if (tinlinenode(p).left.nodetype=typen) then
  332. begin
  333. curconstSegment.concat(Tai_const_symbol.createname(
  334. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname));
  335. end
  336. else
  337. Message(cg_e_illegal_expression);
  338. end
  339. else
  340. Message(cg_e_illegal_expression);
  341. p.free;
  342. end;
  343. setdef:
  344. begin
  345. p:=comp_expr(true);
  346. if p.nodetype=setconstn then
  347. begin
  348. { we only allow const sets }
  349. if assigned(tsetconstnode(p).left) then
  350. Message(cg_e_illegal_expression)
  351. else
  352. begin
  353. { this writing is endian independant }
  354. for l:=0 to t.def.size-1 do
  355. curconstSegment.concat(Tai_const.Create_8bit(tsetconstnode(p).value_set^[l]));
  356. end;
  357. end
  358. else
  359. Message(cg_e_illegal_expression);
  360. p.free;
  361. end;
  362. enumdef:
  363. begin
  364. p:=comp_expr(true);
  365. if p.nodetype=ordconstn then
  366. begin
  367. if is_equal(p.resulttype.def,t.def) or
  368. is_subequal(p.resulttype.def,t.def) then
  369. begin
  370. case p.resulttype.def.size of
  371. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  372. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  373. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  374. end;
  375. end
  376. else
  377. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  378. end
  379. else
  380. Message(cg_e_illegal_expression);
  381. p.free;
  382. end;
  383. stringdef:
  384. begin
  385. p:=comp_expr(true);
  386. { load strval and strlength of the constant tree }
  387. if p.nodetype=stringconstn then
  388. begin
  389. strlength:=tstringconstnode(p).len;
  390. strval:=tstringconstnode(p).value_str;
  391. end
  392. else if is_constcharnode(p) then
  393. begin
  394. strval:=pchar(@tordconstnode(p).value);
  395. strlength:=1
  396. end
  397. else if is_constresourcestringnode(p) then
  398. begin
  399. strval:=pchar(tpointerord(tconstsym(tloadnode(p).symtableentry).value));
  400. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  401. end
  402. else
  403. begin
  404. Message(cg_e_illegal_expression);
  405. strlength:=-1;
  406. end;
  407. if strlength>=0 then
  408. begin
  409. case tstringdef(t.def).string_typ of
  410. st_shortstring:
  411. begin
  412. if strlength>=t.def.size then
  413. begin
  414. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  415. strlength:=t.def.size-1;
  416. end;
  417. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  418. { this can also handle longer strings }
  419. getmem(ca,strlength+1);
  420. move(strval^,ca^,strlength);
  421. ca[strlength]:=#0;
  422. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  423. { fillup with spaces if size is shorter }
  424. if t.def.size>strlength then
  425. begin
  426. getmem(ca,t.def.size-strlength);
  427. { def.size contains also the leading length, so we }
  428. { we have to subtract one }
  429. fillchar(ca[0],t.def.size-strlength-1,' ');
  430. ca[t.def.size-strlength-1]:=#0;
  431. { this can also handle longer strings }
  432. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  433. end;
  434. end;
  435. {$ifdef UseLongString}
  436. st_longstring:
  437. begin
  438. { first write the maximum size }
  439. curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  440. { fill byte }
  441. curconstSegment.concat(Tai_const.Create_8bit(0));
  442. getmem(ca,strlength+1);
  443. move(strval^,ca^,strlength);
  444. ca[strlength]:=#0;
  445. generate_pascii(consts,ca,strlength);
  446. curconstSegment.concat(Tai_const.Create_8bit(0));
  447. end;
  448. {$endif UseLongString}
  449. st_ansistring:
  450. begin
  451. { an empty ansi string is nil! }
  452. if (strlength=0) then
  453. curconstSegment.concat(Tai_const.Create_32bit(0))
  454. else
  455. begin
  456. getdatalabel(ll);
  457. curconstSegment.concat(Tai_const_symbol.Create(ll));
  458. { first write the maximum size }
  459. Consts.concat(Tai_const.Create_32bit(strlength));
  460. { second write the real length }
  461. Consts.concat(Tai_const.Create_32bit(strlength));
  462. { redondent with maxlength but who knows ... (PM) }
  463. { third write use count (set to -1 for safety ) }
  464. Consts.concat(Tai_const.Create_32bit(-1));
  465. Consts.concat(Tai_label.Create(ll));
  466. getmem(ca,strlength+2);
  467. move(strval^,ca^,strlength);
  468. { The terminating #0 to be stored in the .data section (JM) }
  469. ca[strlength]:=#0;
  470. { End of the PChar. The memory has to be allocated because in }
  471. { tai_string.done, there is a freemem(len+1) (JM) }
  472. ca[strlength+1]:=#0;
  473. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  474. end;
  475. end;
  476. end;
  477. end;
  478. p.free;
  479. end;
  480. arraydef:
  481. begin
  482. if token=_LKLAMMER then
  483. begin
  484. consume(_LKLAMMER);
  485. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  486. begin
  487. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  488. consume(_COMMA);
  489. end;
  490. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  491. consume(_RKLAMMER);
  492. end
  493. else
  494. { if array of char then we allow also a string }
  495. if is_char(tarraydef(t.def).elementtype.def) then
  496. begin
  497. p:=comp_expr(true);
  498. if p.nodetype=stringconstn then
  499. begin
  500. len:=tstringconstnode(p).len;
  501. { For tp7 the maximum lentgh can be 255 }
  502. if (m_tp in aktmodeswitches) and
  503. (len>255) then
  504. len:=255;
  505. ca:=tstringconstnode(p).value_str;
  506. end
  507. else
  508. if is_constcharnode(p) then
  509. begin
  510. ca:=pchar(@tordconstnode(p).value);
  511. len:=1;
  512. end
  513. else
  514. begin
  515. Message(cg_e_illegal_expression);
  516. len:=0;
  517. end;
  518. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  519. Message(parser_e_string_larger_array);
  520. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  521. begin
  522. if i+1-tarraydef(t.def).lowrange<=len then
  523. begin
  524. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  525. inc(ca);
  526. end
  527. else
  528. {Fill the remaining positions with #0.}
  529. curconstSegment.concat(Tai_const.Create_8bit(0));
  530. end;
  531. p.free;
  532. end
  533. else
  534. begin
  535. { we want the ( }
  536. consume(_LKLAMMER);
  537. end;
  538. end;
  539. procvardef:
  540. begin
  541. { Procvars and pointers are no longer compatible. }
  542. { under tp: =nil or =var under fpc: =nil or =@var }
  543. if token=_NIL then
  544. begin
  545. curconstSegment.concat(Tai_const.Create_32bit(0));
  546. consume(_NIL);
  547. exit;
  548. end
  549. else
  550. if not(m_tp_procvar in aktmodeswitches) then
  551. if token=_KLAMMERAFFE then
  552. consume(_KLAMMERAFFE);
  553. getprocvar:=true;
  554. getprocvardef:=tprocvardef(t.def);
  555. p:=comp_expr(true);
  556. getprocvar:=false;
  557. if codegenerror then
  558. begin
  559. p.free;
  560. exit;
  561. end;
  562. { convert calln to loadn }
  563. if p.nodetype=calln then
  564. begin
  565. hp:=cloadnode.create(tprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc);
  566. if (tcallnode(p).symtableprocentry.owner.symtabletype=objectsymtable) and
  567. is_class(tdef(tcallnode(p).symtableprocentry.owner.defowner)) then
  568. tloadnode(hp).set_mp(tcallnode(p).methodpointer.getcopy);
  569. p.free;
  570. do_resulttypepass(hp);
  571. p:=hp;
  572. if codegenerror then
  573. begin
  574. p.free;
  575. exit;
  576. end;
  577. end
  578. else if (p.nodetype=addrn) and assigned(taddrnode(p).left) and
  579. (taddrnode(p).left.nodetype=calln) then
  580. begin
  581. hp:=cloadnode.create(tprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  582. tcallnode(taddrnode(p).left).symtableproc);
  583. if (tcallnode(taddrnode(p).left).symtableprocentry.owner.symtabletype=objectsymtable) and
  584. is_class(tdef(tcallnode(taddrnode(p).left).symtableprocentry.owner.defowner)) then
  585. tloadnode(hp).set_mp(tcallnode(taddrnode(p).left).methodpointer.getcopy);
  586. p.free;
  587. do_resulttypepass(hp);
  588. p:=hp;
  589. if codegenerror then
  590. begin
  591. p.free;
  592. exit;
  593. end;
  594. end;
  595. { let type conversion check everything needed }
  596. inserttypeconv(p,t);
  597. if codegenerror then
  598. begin
  599. p.free;
  600. exit;
  601. end;
  602. { remove typeconvn, that will normally insert a lea
  603. instruction which is not necessary for us }
  604. if p.nodetype=typeconvn then
  605. begin
  606. hp:=ttypeconvnode(p).left;
  607. ttypeconvnode(p).left:=nil;
  608. p.free;
  609. p:=hp;
  610. end;
  611. { remove addrn which we also don't need here }
  612. if p.nodetype=addrn then
  613. begin
  614. hp:=taddrnode(p).left;
  615. taddrnode(p).left:=nil;
  616. p.free;
  617. p:=hp;
  618. end;
  619. { we now need to have a loadn with a procsym }
  620. if (p.nodetype=loadn) and
  621. (tloadnode(p).symtableentry.typ=procsym) then
  622. begin
  623. curconstSegment.concat(Tai_const_symbol.createname(
  624. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  625. end
  626. else
  627. Message(cg_e_illegal_expression);
  628. p.free;
  629. end;
  630. { reads a typed constant record }
  631. recorddef:
  632. begin
  633. { KAZ }
  634. if (trecorddef(t.def)=rec_tguid) and
  635. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  636. begin
  637. p:=comp_expr(true);
  638. inserttypeconv(p,cshortstringtype);
  639. if p.nodetype=stringconstn then
  640. begin
  641. s:=strpas(tstringconstnode(p).value_str);
  642. p.free;
  643. if string2guid(s,tmpguid) then
  644. begin
  645. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  646. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  647. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  648. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  649. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  650. end
  651. else
  652. Message(parser_e_improper_guid_syntax);
  653. end
  654. else
  655. begin
  656. p.free;
  657. Message(cg_e_illegal_expression);
  658. exit;
  659. end;
  660. end
  661. else
  662. begin
  663. consume(_LKLAMMER);
  664. aktpos:=0;
  665. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  666. while token<>_RKLAMMER do
  667. begin
  668. s:=pattern;
  669. consume(_ID);
  670. consume(_COLON);
  671. error := false;
  672. recsym := tsym(trecorddef(t.def).symtable.search(s));
  673. if not assigned(recsym) then
  674. begin
  675. Message1(sym_e_illegal_field,s);
  676. error := true;
  677. end;
  678. if not assigned(srsym) or
  679. (s <> srsym.name) then
  680. { possible variant record (JM) }
  681. begin
  682. { All parts of a variant start at the same offset }
  683. { Also allow jumping from one variant part to another, }
  684. { as long as the offsets match }
  685. if (assigned(srsym) and
  686. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  687. { srsym is not assigned after parsing w2 in the }
  688. { typed const in the next example: }
  689. { type tr = record case byte of }
  690. { 1: (l1,l2: dword); }
  691. { 2: (w1,w2: word); }
  692. { end; }
  693. { const r: tr = (w1:1;w2:1;l2:5); }
  694. (tvarsym(recsym).address = aktpos) then
  695. srsym := recsym
  696. { going backwards isn't allowed in any mode }
  697. else if (tvarsym(recsym).address<aktpos) then
  698. begin
  699. Message(parser_e_invalid_record_const);
  700. error := true;
  701. end
  702. { Delphi allows you to skip fields }
  703. else if (m_delphi in aktmodeswitches) then
  704. begin
  705. Message1(parser_w_skipped_fields_before,s);
  706. srsym := recsym;
  707. end
  708. { FPC and TP don't }
  709. else
  710. begin
  711. Message1(parser_e_skipped_fields_before,s);
  712. error := true;
  713. end;
  714. end;
  715. if error then
  716. consume_all_until(_SEMICOLON)
  717. else
  718. begin
  719. { if needed fill (alignment) }
  720. if tvarsym(srsym).address>aktpos then
  721. for i:=1 to tvarsym(srsym).address-aktpos do
  722. curconstSegment.concat(Tai_const.Create_8bit(0));
  723. { new position }
  724. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  725. { read the data }
  726. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  727. { keep previous field for checking whether whole }
  728. { record was initialized (JM) }
  729. recsym := srsym;
  730. { goto next field }
  731. srsym := tsym(srsym.indexnext);
  732. if token=_SEMICOLON then
  733. consume(_SEMICOLON)
  734. else break;
  735. end;
  736. end;
  737. { are there any fields left? }
  738. if assigned(srsym) and
  739. { don't complain if there only come other variant parts }
  740. { after the last initialized field }
  741. (tvarsym(srsym).address > tvarsym(recsym).address) then
  742. Message1(parser_h_skipped_fields_after,s);
  743. for i:=1 to t.def.size-aktpos do
  744. curconstSegment.concat(Tai_const.Create_8bit(0));
  745. consume(_RKLAMMER);
  746. end;
  747. end;
  748. { reads a typed object }
  749. objectdef:
  750. begin
  751. if is_class_or_interface(t.def) then
  752. begin
  753. p:=comp_expr(true);
  754. if p.nodetype<>niln then
  755. begin
  756. Message(parser_e_type_const_not_possible);
  757. consume_all_until(_RKLAMMER);
  758. end
  759. else
  760. begin
  761. curconstSegment.concat(Tai_const.Create_32bit(0));
  762. end;
  763. p.free;
  764. end
  765. { for objects we allow it only if it doesn't contain a vmt }
  766. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  767. not(m_tp in aktmodeswitches) then
  768. Message(parser_e_type_const_not_possible)
  769. else
  770. begin
  771. consume(_LKLAMMER);
  772. aktpos:=0;
  773. while token<>_RKLAMMER do
  774. begin
  775. s:=pattern;
  776. consume(_ID);
  777. consume(_COLON);
  778. srsym:=nil;
  779. obj:=tobjectdef(t.def);
  780. symt:=obj.symtable;
  781. while (srsym=nil) and assigned(symt) do
  782. begin
  783. srsym:=tsym(symt.search(s));
  784. if assigned(obj) then
  785. obj:=obj.childof;
  786. if assigned(obj) then
  787. symt:=obj.symtable
  788. else
  789. symt:=nil;
  790. end;
  791. if srsym=nil then
  792. begin
  793. Message1(sym_e_id_not_found,s);
  794. consume_all_until(_SEMICOLON);
  795. end
  796. else
  797. begin
  798. { check position }
  799. if tvarsym(srsym).address<aktpos then
  800. Message(parser_e_invalid_record_const);
  801. { check in VMT needs to be added for TP mode }
  802. if (m_tp in aktmodeswitches) and
  803. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  804. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  805. begin
  806. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  807. curconstsegment.concat(tai_const.create_8bit(0));
  808. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  809. { this is more general }
  810. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  811. end;
  812. { if needed fill }
  813. if tvarsym(srsym).address>aktpos then
  814. for i:=1 to tvarsym(srsym).address-aktpos do
  815. curconstSegment.concat(Tai_const.Create_8bit(0));
  816. { new position }
  817. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  818. { read the data }
  819. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  820. if token=_SEMICOLON then
  821. consume(_SEMICOLON)
  822. else break;
  823. end;
  824. end;
  825. if (m_tp in aktmodeswitches) and
  826. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  827. (tobjectdef(t.def).vmt_offset>=aktpos) then
  828. begin
  829. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  830. curconstsegment.concat(tai_const.create_8bit(0));
  831. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  832. { this is more general }
  833. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  834. end;
  835. for i:=1 to t.def.size-aktpos do
  836. curconstSegment.concat(Tai_const.Create_8bit(0));
  837. consume(_RKLAMMER);
  838. end;
  839. end;
  840. errordef:
  841. begin
  842. { try to consume something useful }
  843. if token=_LKLAMMER then
  844. consume_all_until(_RKLAMMER)
  845. else
  846. consume_all_until(_SEMICOLON);
  847. end;
  848. else Message(parser_e_type_const_not_possible);
  849. end;
  850. end;
  851. {$ifdef fpc}
  852. {$maxfpuregisters default}
  853. {$endif fpc}
  854. end.
  855. {
  856. $Log$
  857. Revision 1.24 2001-06-18 20:36:25 peter
  858. * -Ur switch (merged)
  859. * masm fixes (merged)
  860. * quoted filenames for go32v2 and win32
  861. Revision 1.23 2001/05/06 17:15:00 jonas
  862. + detect incomplete typed constant records
  863. Revision 1.22 2001/04/18 22:01:57 peter
  864. * registration of targets and assemblers
  865. Revision 1.21 2001/04/13 01:22:13 peter
  866. * symtable change to classes
  867. * range check generation and errors fixed, make cycle DEBUG=1 works
  868. * memory leaks fixed
  869. Revision 1.20 2001/04/04 22:43:53 peter
  870. * remove unnecessary calls to firstpass
  871. Revision 1.19 2001/04/02 21:20:34 peter
  872. * resulttype rewrite
  873. Revision 1.18 2001/03/11 22:58:50 peter
  874. * getsym redesign, removed the globals srsym,srsymtable
  875. Revision 1.17 2001/02/04 11:12:16 jonas
  876. * fixed web bug 1377 & const pointer arithmtic
  877. Revision 1.16 2001/02/03 00:26:35 peter
  878. * merged fix for bug 1365
  879. Revision 1.15 2000/12/25 00:07:28 peter
  880. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  881. tlinkedlist objects)
  882. Revision 1.14 2000/12/10 20:24:18 peter
  883. * allow subtypes for enums
  884. Revision 1.13 2000/11/29 00:30:38 florian
  885. * unused units removed from uses clause
  886. * some changes for widestrings
  887. Revision 1.12 2000/11/06 15:54:15 florian
  888. * fixed two bugs to get make cycle work, but it's not enough
  889. Revision 1.11 2000/11/04 14:25:21 florian
  890. + merged Attila's changes for interfaces, not tested yet
  891. Revision 1.10 2000/10/31 22:02:51 peter
  892. * symtable splitted, no real code changes
  893. Revision 1.9 2000/10/14 10:14:52 peter
  894. * moehrendorf oct 2000 rewrite
  895. Revision 1.8 2000/09/30 13:23:04 peter
  896. * const array of char and pchar length fixed (merged)
  897. Revision 1.7 2000/09/24 15:06:25 peter
  898. * use defines.inc
  899. Revision 1.6 2000/08/27 16:11:52 peter
  900. * moved some util functions from globals,cobjects to cutils
  901. * splitted files into finput,fmodule
  902. Revision 1.5 2000/08/24 19:13:18 peter
  903. * allow nil for class typed consts (merged)
  904. Revision 1.4 2000/08/16 13:06:06 florian
  905. + support of 64 bit integer constants
  906. Revision 1.3 2000/08/05 13:25:06 peter
  907. * packenum 1 fixes (merged)
  908. Revision 1.2 2000/07/13 11:32:47 michael
  909. + removed logs
  910. }