ptconst.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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,widestr,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,j,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. { untrue - because they are considered }
  355. { arrays of 32-bit values CEC }
  356. { store as longint values in little-endian format }
  357. if target_info.endian = endian_little then
  358. begin
  359. for l:= 0 to p.resulttype.def.size-1 do
  360. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  361. end
  362. else
  363. begin
  364. { store as longint values in big-endian format }
  365. j:=0;
  366. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  367. begin
  368. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  369. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  370. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  371. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  372. Inc(j,4);
  373. end;
  374. end;
  375. end;
  376. end
  377. else
  378. Message(cg_e_illegal_expression);
  379. p.free;
  380. end;
  381. enumdef:
  382. begin
  383. p:=comp_expr(true);
  384. if p.nodetype=ordconstn then
  385. begin
  386. if is_equal(p.resulttype.def,t.def) or
  387. is_subequal(p.resulttype.def,t.def) then
  388. begin
  389. case p.resulttype.def.size of
  390. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  391. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  392. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  393. end;
  394. end
  395. else
  396. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  397. end
  398. else
  399. Message(cg_e_illegal_expression);
  400. p.free;
  401. end;
  402. stringdef:
  403. begin
  404. p:=comp_expr(true);
  405. { load strval and strlength of the constant tree }
  406. if p.nodetype=stringconstn then
  407. begin
  408. { convert to the expected string type so that
  409. for widestrings strval is a pcompilerwidestring }
  410. inserttypeconv(p,t);
  411. strlength:=tstringconstnode(p).len;
  412. strval:=tstringconstnode(p).value_str;
  413. end
  414. else if is_constcharnode(p) then
  415. begin
  416. strval:=pchar(@tordconstnode(p).value);
  417. strlength:=1
  418. end
  419. else if is_constresourcestringnode(p) then
  420. begin
  421. strval:=pchar(tpointerord(tconstsym(tloadnode(p).symtableentry).value));
  422. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  423. end
  424. else
  425. begin
  426. Message(cg_e_illegal_expression);
  427. strlength:=-1;
  428. end;
  429. if strlength>=0 then
  430. begin
  431. case tstringdef(t.def).string_typ of
  432. st_shortstring:
  433. begin
  434. if strlength>=t.def.size then
  435. begin
  436. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  437. strlength:=t.def.size-1;
  438. end;
  439. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  440. { this can also handle longer strings }
  441. getmem(ca,strlength+1);
  442. move(strval^,ca^,strlength);
  443. ca[strlength]:=#0;
  444. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  445. { fillup with spaces if size is shorter }
  446. if t.def.size>strlength then
  447. begin
  448. getmem(ca,t.def.size-strlength);
  449. { def.size contains also the leading length, so we }
  450. { we have to subtract one }
  451. fillchar(ca[0],t.def.size-strlength-1,' ');
  452. ca[t.def.size-strlength-1]:=#0;
  453. { this can also handle longer strings }
  454. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  455. end;
  456. end;
  457. st_ansistring:
  458. begin
  459. { an empty ansi string is nil! }
  460. if (strlength=0) then
  461. curconstSegment.concat(Tai_const.Create_32bit(0))
  462. else
  463. begin
  464. getdatalabel(ll);
  465. curconstSegment.concat(Tai_const_symbol.Create(ll));
  466. { first write the maximum size }
  467. Consts.concat(Tai_const.Create_32bit(strlength));
  468. { second write the real length }
  469. Consts.concat(Tai_const.Create_32bit(strlength));
  470. { redondent with maxlength but who knows ... (PM) }
  471. { third write use count (set to -1 for safety ) }
  472. Consts.concat(Tai_const.Create_32bit(-1));
  473. Consts.concat(Tai_label.Create(ll));
  474. getmem(ca,strlength+2);
  475. move(strval^,ca^,strlength);
  476. { The terminating #0 to be stored in the .data section (JM) }
  477. ca[strlength]:=#0;
  478. { End of the PChar. The memory has to be allocated because in }
  479. { tai_string.done, there is a freemem(len+1) (JM) }
  480. ca[strlength+1]:=#0;
  481. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  482. end;
  483. end;
  484. st_widestring:
  485. begin
  486. { an empty ansi string is nil! }
  487. if (strlength=0) then
  488. curconstSegment.concat(Tai_const.Create_32bit(0))
  489. else
  490. begin
  491. getdatalabel(ll);
  492. curconstSegment.concat(Tai_const_symbol.Create(ll));
  493. Consts.concat(Tai_const.Create_32bit(strlength));
  494. Consts.concat(Tai_const.Create_32bit(strlength));
  495. Consts.concat(Tai_const.Create_32bit(-1));
  496. Consts.concat(Tai_label.Create(ll));
  497. for i:=0 to strlength-1 do
  498. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  499. end;
  500. end;
  501. st_longstring:
  502. begin
  503. internalerror(200107081);
  504. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  505. curconstSegment.concat(Tai_const.Create_8bit(0));
  506. getmem(ca,strlength+1);
  507. move(strval^,ca^,strlength);
  508. ca[strlength]:=#0;
  509. generate_pascii(consts,ca,strlength);
  510. curconstSegment.concat(Tai_const.Create_8bit(0));}
  511. end;
  512. end;
  513. end;
  514. p.free;
  515. end;
  516. arraydef:
  517. begin
  518. if token=_LKLAMMER then
  519. begin
  520. consume(_LKLAMMER);
  521. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  522. begin
  523. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  524. consume(_COMMA);
  525. end;
  526. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  527. consume(_RKLAMMER);
  528. end
  529. else
  530. { if array of char then we allow also a string }
  531. if is_char(tarraydef(t.def).elementtype.def) then
  532. begin
  533. p:=comp_expr(true);
  534. if p.nodetype=stringconstn then
  535. begin
  536. len:=tstringconstnode(p).len;
  537. { For tp7 the maximum lentgh can be 255 }
  538. if (m_tp in aktmodeswitches) and
  539. (len>255) then
  540. len:=255;
  541. ca:=tstringconstnode(p).value_str;
  542. end
  543. else
  544. if is_constcharnode(p) then
  545. begin
  546. ca:=pchar(@tordconstnode(p).value);
  547. len:=1;
  548. end
  549. else
  550. begin
  551. Message(cg_e_illegal_expression);
  552. len:=0;
  553. end;
  554. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  555. Message(parser_e_string_larger_array);
  556. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  557. begin
  558. if i+1-tarraydef(t.def).lowrange<=len then
  559. begin
  560. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  561. inc(ca);
  562. end
  563. else
  564. {Fill the remaining positions with #0.}
  565. curconstSegment.concat(Tai_const.Create_8bit(0));
  566. end;
  567. p.free;
  568. end
  569. else
  570. begin
  571. { we want the ( }
  572. consume(_LKLAMMER);
  573. end;
  574. end;
  575. procvardef:
  576. begin
  577. { Procvars and pointers are no longer compatible. }
  578. { under tp: =nil or =var under fpc: =nil or =@var }
  579. if token=_NIL then
  580. begin
  581. curconstSegment.concat(Tai_const.Create_32bit(0));
  582. consume(_NIL);
  583. exit;
  584. end;
  585. getprocvar:=true;
  586. getprocvardef:=tprocvardef(t.def);
  587. p:=comp_expr(true);
  588. getprocvar:=false;
  589. if codegenerror then
  590. begin
  591. p.free;
  592. exit;
  593. end;
  594. { let type conversion check everything needed }
  595. inserttypeconv(p,t);
  596. if codegenerror then
  597. begin
  598. p.free;
  599. exit;
  600. end;
  601. { remove typeconvn, that will normally insert a lea
  602. instruction which is not necessary for us }
  603. if p.nodetype=typeconvn then
  604. begin
  605. hp:=ttypeconvnode(p).left;
  606. ttypeconvnode(p).left:=nil;
  607. p.free;
  608. p:=hp;
  609. end;
  610. { remove addrn which we also don't need here }
  611. if p.nodetype=addrn then
  612. begin
  613. hp:=taddrnode(p).left;
  614. taddrnode(p).left:=nil;
  615. p.free;
  616. p:=hp;
  617. end;
  618. { we now need to have a loadn with a procsym }
  619. if (p.nodetype=loadn) and
  620. (tloadnode(p).symtableentry.typ=procsym) then
  621. begin
  622. curconstSegment.concat(Tai_const_symbol.createname(
  623. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  624. end
  625. else
  626. Message(cg_e_illegal_expression);
  627. p.free;
  628. end;
  629. { reads a typed constant record }
  630. recorddef:
  631. begin
  632. { KAZ }
  633. if (trecorddef(t.def)=rec_tguid) and
  634. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  635. begin
  636. p:=comp_expr(true);
  637. inserttypeconv(p,cshortstringtype);
  638. if p.nodetype=stringconstn then
  639. begin
  640. s:=strpas(tstringconstnode(p).value_str);
  641. p.free;
  642. if string2guid(s,tmpguid) then
  643. begin
  644. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  645. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  646. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  647. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  648. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  649. end
  650. else
  651. Message(parser_e_improper_guid_syntax);
  652. end
  653. else
  654. begin
  655. p.free;
  656. Message(cg_e_illegal_expression);
  657. exit;
  658. end;
  659. end
  660. else
  661. begin
  662. consume(_LKLAMMER);
  663. aktpos:=0;
  664. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  665. while token<>_RKLAMMER do
  666. begin
  667. s:=pattern;
  668. consume(_ID);
  669. consume(_COLON);
  670. error := false;
  671. recsym := tsym(trecorddef(t.def).symtable.search(s));
  672. if not assigned(recsym) then
  673. begin
  674. Message1(sym_e_illegal_field,s);
  675. error := true;
  676. end;
  677. if not assigned(srsym) or
  678. (s <> srsym.name) then
  679. { possible variant record (JM) }
  680. begin
  681. { All parts of a variant start at the same offset }
  682. { Also allow jumping from one variant part to another, }
  683. { as long as the offsets match }
  684. if (assigned(srsym) and
  685. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  686. { srsym is not assigned after parsing w2 in the }
  687. { typed const in the next example: }
  688. { type tr = record case byte of }
  689. { 1: (l1,l2: dword); }
  690. { 2: (w1,w2: word); }
  691. { end; }
  692. { const r: tr = (w1:1;w2:1;l2:5); }
  693. (tvarsym(recsym).address = aktpos) then
  694. srsym := recsym
  695. { going backwards isn't allowed in any mode }
  696. else if (tvarsym(recsym).address<aktpos) then
  697. begin
  698. Message(parser_e_invalid_record_const);
  699. error := true;
  700. end
  701. { Delphi allows you to skip fields }
  702. else if (m_delphi in aktmodeswitches) then
  703. begin
  704. Message1(parser_w_skipped_fields_before,s);
  705. srsym := recsym;
  706. end
  707. { FPC and TP don't }
  708. else
  709. begin
  710. Message1(parser_e_skipped_fields_before,s);
  711. error := true;
  712. end;
  713. end;
  714. if error then
  715. consume_all_until(_SEMICOLON)
  716. else
  717. begin
  718. { if needed fill (alignment) }
  719. if tvarsym(srsym).address>aktpos then
  720. for i:=1 to tvarsym(srsym).address-aktpos do
  721. curconstSegment.concat(Tai_const.Create_8bit(0));
  722. { new position }
  723. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  724. { read the data }
  725. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  726. { keep previous field for checking whether whole }
  727. { record was initialized (JM) }
  728. recsym := srsym;
  729. { goto next field }
  730. srsym := tsym(srsym.indexnext);
  731. if token=_SEMICOLON then
  732. consume(_SEMICOLON)
  733. else break;
  734. end;
  735. end;
  736. { are there any fields left? }
  737. if assigned(srsym) and
  738. { don't complain if there only come other variant parts }
  739. { after the last initialized field }
  740. (tvarsym(srsym).address > tvarsym(recsym).address) then
  741. Message1(parser_h_skipped_fields_after,s);
  742. for i:=1 to t.def.size-aktpos do
  743. curconstSegment.concat(Tai_const.Create_8bit(0));
  744. consume(_RKLAMMER);
  745. end;
  746. end;
  747. { reads a typed object }
  748. objectdef:
  749. begin
  750. if is_class_or_interface(t.def) then
  751. begin
  752. p:=comp_expr(true);
  753. if p.nodetype<>niln then
  754. begin
  755. Message(parser_e_type_const_not_possible);
  756. consume_all_until(_RKLAMMER);
  757. end
  758. else
  759. begin
  760. curconstSegment.concat(Tai_const.Create_32bit(0));
  761. end;
  762. p.free;
  763. end
  764. { for objects we allow it only if it doesn't contain a vmt }
  765. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  766. not(m_tp in aktmodeswitches) then
  767. Message(parser_e_type_const_not_possible)
  768. else
  769. begin
  770. consume(_LKLAMMER);
  771. aktpos:=0;
  772. while token<>_RKLAMMER do
  773. begin
  774. s:=pattern;
  775. consume(_ID);
  776. consume(_COLON);
  777. srsym:=nil;
  778. obj:=tobjectdef(t.def);
  779. symt:=obj.symtable;
  780. while (srsym=nil) and assigned(symt) do
  781. begin
  782. srsym:=tsym(symt.search(s));
  783. if assigned(obj) then
  784. obj:=obj.childof;
  785. if assigned(obj) then
  786. symt:=obj.symtable
  787. else
  788. symt:=nil;
  789. end;
  790. if srsym=nil then
  791. begin
  792. Message1(sym_e_id_not_found,s);
  793. consume_all_until(_SEMICOLON);
  794. end
  795. else
  796. begin
  797. { check position }
  798. if tvarsym(srsym).address<aktpos then
  799. Message(parser_e_invalid_record_const);
  800. { check in VMT needs to be added for TP mode }
  801. if (m_tp in aktmodeswitches) and
  802. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  803. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  804. begin
  805. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  806. curconstsegment.concat(tai_const.create_8bit(0));
  807. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  808. { this is more general }
  809. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  810. end;
  811. { if needed fill }
  812. if tvarsym(srsym).address>aktpos then
  813. for i:=1 to tvarsym(srsym).address-aktpos do
  814. curconstSegment.concat(Tai_const.Create_8bit(0));
  815. { new position }
  816. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  817. { read the data }
  818. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  819. if token=_SEMICOLON then
  820. consume(_SEMICOLON)
  821. else break;
  822. end;
  823. end;
  824. if (m_tp in aktmodeswitches) and
  825. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  826. (tobjectdef(t.def).vmt_offset>=aktpos) then
  827. begin
  828. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  829. curconstsegment.concat(tai_const.create_8bit(0));
  830. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  831. { this is more general }
  832. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  833. end;
  834. for i:=1 to t.def.size-aktpos do
  835. curconstSegment.concat(Tai_const.Create_8bit(0));
  836. consume(_RKLAMMER);
  837. end;
  838. end;
  839. errordef:
  840. begin
  841. { try to consume something useful }
  842. if token=_LKLAMMER then
  843. consume_all_until(_RKLAMMER)
  844. else
  845. consume_all_until(_SEMICOLON);
  846. end;
  847. else Message(parser_e_type_const_not_possible);
  848. end;
  849. end;
  850. {$ifdef fpc}
  851. {$maxfpuregisters default}
  852. {$endif fpc}
  853. end.
  854. {
  855. $Log$
  856. Revision 1.27 2001-07-08 21:00:15 peter
  857. * various widestring updates, it works now mostly without charset
  858. mapping supported
  859. Revision 1.26 2001/06/29 14:16:57 jonas
  860. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  861. required to assign the address of a procedure to a procvar, sometimes
  862. not. Now it is always required) (merged)
  863. Revision 1.25 2001/06/27 21:37:36 peter
  864. * v10 merges
  865. Revision 1.24 2001/06/18 20:36:25 peter
  866. * -Ur switch (merged)
  867. * masm fixes (merged)
  868. * quoted filenames for go32v2 and win32
  869. Revision 1.23 2001/05/06 17:15:00 jonas
  870. + detect incomplete typed constant records
  871. Revision 1.22 2001/04/18 22:01:57 peter
  872. * registration of targets and assemblers
  873. Revision 1.21 2001/04/13 01:22:13 peter
  874. * symtable change to classes
  875. * range check generation and errors fixed, make cycle DEBUG=1 works
  876. * memory leaks fixed
  877. Revision 1.20 2001/04/04 22:43:53 peter
  878. * remove unnecessary calls to firstpass
  879. Revision 1.19 2001/04/02 21:20:34 peter
  880. * resulttype rewrite
  881. Revision 1.18 2001/03/11 22:58:50 peter
  882. * getsym redesign, removed the globals srsym,srsymtable
  883. Revision 1.17 2001/02/04 11:12:16 jonas
  884. * fixed web bug 1377 & const pointer arithmtic
  885. Revision 1.16 2001/02/03 00:26:35 peter
  886. * merged fix for bug 1365
  887. Revision 1.15 2000/12/25 00:07:28 peter
  888. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  889. tlinkedlist objects)
  890. Revision 1.14 2000/12/10 20:24:18 peter
  891. * allow subtypes for enums
  892. Revision 1.13 2000/11/29 00:30:38 florian
  893. * unused units removed from uses clause
  894. * some changes for widestrings
  895. Revision 1.12 2000/11/06 15:54:15 florian
  896. * fixed two bugs to get make cycle work, but it's not enough
  897. Revision 1.11 2000/11/04 14:25:21 florian
  898. + merged Attila's changes for interfaces, not tested yet
  899. Revision 1.10 2000/10/31 22:02:51 peter
  900. * symtable splitted, no real code changes
  901. Revision 1.9 2000/10/14 10:14:52 peter
  902. * moehrendorf oct 2000 rewrite
  903. Revision 1.8 2000/09/30 13:23:04 peter
  904. * const array of char and pchar length fixed (merged)
  905. Revision 1.7 2000/09/24 15:06:25 peter
  906. * use defines.inc
  907. Revision 1.6 2000/08/27 16:11:52 peter
  908. * moved some util functions from globals,cobjects to cutils
  909. * splitted files into finput,fmodule
  910. Revision 1.5 2000/08/24 19:13:18 peter
  911. * allow nil for class typed consts (merged)
  912. Revision 1.4 2000/08/16 13:06:06 florian
  913. + support of 64 bit integer constants
  914. Revision 1.3 2000/08/05 13:25:06 peter
  915. * packenum 1 fixes (merged)
  916. Revision 1.2 2000/07/13 11:32:47 michael
  917. + removed logs
  918. }