ptconst.pas 40 KB

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