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