ptconst.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. else
  569. if not(m_tp_procvar in aktmodeswitches) then
  570. if token=_KLAMMERAFFE then
  571. consume(_KLAMMERAFFE);
  572. getprocvar:=true;
  573. getprocvardef:=tprocvardef(t.def);
  574. p:=comp_expr(true);
  575. getprocvar:=false;
  576. if codegenerror then
  577. begin
  578. p.free;
  579. exit;
  580. end;
  581. { convert calln to loadn }
  582. if p.nodetype=calln then
  583. begin
  584. hp:=cloadnode.create(tprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc);
  585. if (tcallnode(p).symtableprocentry.owner.symtabletype=objectsymtable) and
  586. is_class(tdef(tcallnode(p).symtableprocentry.owner.defowner)) then
  587. tloadnode(hp).set_mp(tcallnode(p).methodpointer.getcopy);
  588. p.free;
  589. do_resulttypepass(hp);
  590. p:=hp;
  591. if codegenerror then
  592. begin
  593. p.free;
  594. exit;
  595. end;
  596. end
  597. else if (p.nodetype=addrn) and assigned(taddrnode(p).left) and
  598. (taddrnode(p).left.nodetype=calln) then
  599. begin
  600. hp:=cloadnode.create(tprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  601. tcallnode(taddrnode(p).left).symtableproc);
  602. if (tcallnode(taddrnode(p).left).symtableprocentry.owner.symtabletype=objectsymtable) and
  603. is_class(tdef(tcallnode(taddrnode(p).left).symtableprocentry.owner.defowner)) then
  604. tloadnode(hp).set_mp(tcallnode(taddrnode(p).left).methodpointer.getcopy);
  605. p.free;
  606. do_resulttypepass(hp);
  607. p:=hp;
  608. if codegenerror then
  609. begin
  610. p.free;
  611. exit;
  612. end;
  613. end;
  614. { let type conversion check everything needed }
  615. inserttypeconv(p,t);
  616. if codegenerror then
  617. begin
  618. p.free;
  619. exit;
  620. end;
  621. { remove typeconvn, that will normally insert a lea
  622. instruction which is not necessary for us }
  623. if p.nodetype=typeconvn then
  624. begin
  625. hp:=ttypeconvnode(p).left;
  626. ttypeconvnode(p).left:=nil;
  627. p.free;
  628. p:=hp;
  629. end;
  630. { remove addrn which we also don't need here }
  631. if p.nodetype=addrn then
  632. begin
  633. hp:=taddrnode(p).left;
  634. taddrnode(p).left:=nil;
  635. p.free;
  636. p:=hp;
  637. end;
  638. { we now need to have a loadn with a procsym }
  639. if (p.nodetype=loadn) and
  640. (tloadnode(p).symtableentry.typ=procsym) then
  641. begin
  642. curconstSegment.concat(Tai_const_symbol.createname(
  643. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  644. end
  645. else
  646. Message(cg_e_illegal_expression);
  647. p.free;
  648. end;
  649. { reads a typed constant record }
  650. recorddef:
  651. begin
  652. { KAZ }
  653. if (trecorddef(t.def)=rec_tguid) and
  654. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  655. begin
  656. p:=comp_expr(true);
  657. inserttypeconv(p,cshortstringtype);
  658. if p.nodetype=stringconstn then
  659. begin
  660. s:=strpas(tstringconstnode(p).value_str);
  661. p.free;
  662. if string2guid(s,tmpguid) then
  663. begin
  664. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  665. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  666. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  667. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  668. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  669. end
  670. else
  671. Message(parser_e_improper_guid_syntax);
  672. end
  673. else
  674. begin
  675. p.free;
  676. Message(cg_e_illegal_expression);
  677. exit;
  678. end;
  679. end
  680. else
  681. begin
  682. consume(_LKLAMMER);
  683. aktpos:=0;
  684. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  685. while token<>_RKLAMMER do
  686. begin
  687. s:=pattern;
  688. consume(_ID);
  689. consume(_COLON);
  690. error := false;
  691. recsym := tsym(trecorddef(t.def).symtable.search(s));
  692. if not assigned(recsym) then
  693. begin
  694. Message1(sym_e_illegal_field,s);
  695. error := true;
  696. end;
  697. if not assigned(srsym) or
  698. (s <> srsym.name) then
  699. { possible variant record (JM) }
  700. begin
  701. { All parts of a variant start at the same offset }
  702. { Also allow jumping from one variant part to another, }
  703. { as long as the offsets match }
  704. if (assigned(srsym) and
  705. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  706. { srsym is not assigned after parsing w2 in the }
  707. { typed const in the next example: }
  708. { type tr = record case byte of }
  709. { 1: (l1,l2: dword); }
  710. { 2: (w1,w2: word); }
  711. { end; }
  712. { const r: tr = (w1:1;w2:1;l2:5); }
  713. (tvarsym(recsym).address = aktpos) then
  714. srsym := recsym
  715. { going backwards isn't allowed in any mode }
  716. else if (tvarsym(recsym).address<aktpos) then
  717. begin
  718. Message(parser_e_invalid_record_const);
  719. error := true;
  720. end
  721. { Delphi allows you to skip fields }
  722. else if (m_delphi in aktmodeswitches) then
  723. begin
  724. Message1(parser_w_skipped_fields_before,s);
  725. srsym := recsym;
  726. end
  727. { FPC and TP don't }
  728. else
  729. begin
  730. Message1(parser_e_skipped_fields_before,s);
  731. error := true;
  732. end;
  733. end;
  734. if error then
  735. consume_all_until(_SEMICOLON)
  736. else
  737. begin
  738. { if needed fill (alignment) }
  739. if tvarsym(srsym).address>aktpos then
  740. for i:=1 to tvarsym(srsym).address-aktpos do
  741. curconstSegment.concat(Tai_const.Create_8bit(0));
  742. { new position }
  743. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  744. { read the data }
  745. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  746. { keep previous field for checking whether whole }
  747. { record was initialized (JM) }
  748. recsym := srsym;
  749. { goto next field }
  750. srsym := tsym(srsym.indexnext);
  751. if token=_SEMICOLON then
  752. consume(_SEMICOLON)
  753. else break;
  754. end;
  755. end;
  756. { are there any fields left? }
  757. if assigned(srsym) and
  758. { don't complain if there only come other variant parts }
  759. { after the last initialized field }
  760. (tvarsym(srsym).address > tvarsym(recsym).address) then
  761. Message1(parser_h_skipped_fields_after,s);
  762. for i:=1 to t.def.size-aktpos do
  763. curconstSegment.concat(Tai_const.Create_8bit(0));
  764. consume(_RKLAMMER);
  765. end;
  766. end;
  767. { reads a typed object }
  768. objectdef:
  769. begin
  770. if is_class_or_interface(t.def) then
  771. begin
  772. p:=comp_expr(true);
  773. if p.nodetype<>niln then
  774. begin
  775. Message(parser_e_type_const_not_possible);
  776. consume_all_until(_RKLAMMER);
  777. end
  778. else
  779. begin
  780. curconstSegment.concat(Tai_const.Create_32bit(0));
  781. end;
  782. p.free;
  783. end
  784. { for objects we allow it only if it doesn't contain a vmt }
  785. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  786. not(m_tp in aktmodeswitches) then
  787. Message(parser_e_type_const_not_possible)
  788. else
  789. begin
  790. consume(_LKLAMMER);
  791. aktpos:=0;
  792. while token<>_RKLAMMER do
  793. begin
  794. s:=pattern;
  795. consume(_ID);
  796. consume(_COLON);
  797. srsym:=nil;
  798. obj:=tobjectdef(t.def);
  799. symt:=obj.symtable;
  800. while (srsym=nil) and assigned(symt) do
  801. begin
  802. srsym:=tsym(symt.search(s));
  803. if assigned(obj) then
  804. obj:=obj.childof;
  805. if assigned(obj) then
  806. symt:=obj.symtable
  807. else
  808. symt:=nil;
  809. end;
  810. if srsym=nil then
  811. begin
  812. Message1(sym_e_id_not_found,s);
  813. consume_all_until(_SEMICOLON);
  814. end
  815. else
  816. begin
  817. { check position }
  818. if tvarsym(srsym).address<aktpos then
  819. Message(parser_e_invalid_record_const);
  820. { check in VMT needs to be added for TP mode }
  821. if (m_tp in aktmodeswitches) and
  822. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  823. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  824. begin
  825. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  826. curconstsegment.concat(tai_const.create_8bit(0));
  827. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  828. { this is more general }
  829. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  830. end;
  831. { if needed fill }
  832. if tvarsym(srsym).address>aktpos then
  833. for i:=1 to tvarsym(srsym).address-aktpos do
  834. curconstSegment.concat(Tai_const.Create_8bit(0));
  835. { new position }
  836. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  837. { read the data }
  838. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  839. if token=_SEMICOLON then
  840. consume(_SEMICOLON)
  841. else break;
  842. end;
  843. end;
  844. if (m_tp in aktmodeswitches) and
  845. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  846. (tobjectdef(t.def).vmt_offset>=aktpos) then
  847. begin
  848. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  849. curconstsegment.concat(tai_const.create_8bit(0));
  850. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  851. { this is more general }
  852. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  853. end;
  854. for i:=1 to t.def.size-aktpos do
  855. curconstSegment.concat(Tai_const.Create_8bit(0));
  856. consume(_RKLAMMER);
  857. end;
  858. end;
  859. errordef:
  860. begin
  861. { try to consume something useful }
  862. if token=_LKLAMMER then
  863. consume_all_until(_RKLAMMER)
  864. else
  865. consume_all_until(_SEMICOLON);
  866. end;
  867. else Message(parser_e_type_const_not_possible);
  868. end;
  869. end;
  870. {$ifdef fpc}
  871. {$maxfpuregisters default}
  872. {$endif fpc}
  873. end.
  874. {
  875. $Log$
  876. Revision 1.25 2001-06-27 21:37:36 peter
  877. * v10 merges
  878. Revision 1.24 2001/06/18 20:36:25 peter
  879. * -Ur switch (merged)
  880. * masm fixes (merged)
  881. * quoted filenames for go32v2 and win32
  882. Revision 1.23 2001/05/06 17:15:00 jonas
  883. + detect incomplete typed constant records
  884. Revision 1.22 2001/04/18 22:01:57 peter
  885. * registration of targets and assemblers
  886. Revision 1.21 2001/04/13 01:22:13 peter
  887. * symtable change to classes
  888. * range check generation and errors fixed, make cycle DEBUG=1 works
  889. * memory leaks fixed
  890. Revision 1.20 2001/04/04 22:43:53 peter
  891. * remove unnecessary calls to firstpass
  892. Revision 1.19 2001/04/02 21:20:34 peter
  893. * resulttype rewrite
  894. Revision 1.18 2001/03/11 22:58:50 peter
  895. * getsym redesign, removed the globals srsym,srsymtable
  896. Revision 1.17 2001/02/04 11:12:16 jonas
  897. * fixed web bug 1377 & const pointer arithmtic
  898. Revision 1.16 2001/02/03 00:26:35 peter
  899. * merged fix for bug 1365
  900. Revision 1.15 2000/12/25 00:07:28 peter
  901. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  902. tlinkedlist objects)
  903. Revision 1.14 2000/12/10 20:24:18 peter
  904. * allow subtypes for enums
  905. Revision 1.13 2000/11/29 00:30:38 florian
  906. * unused units removed from uses clause
  907. * some changes for widestrings
  908. Revision 1.12 2000/11/06 15:54:15 florian
  909. * fixed two bugs to get make cycle work, but it's not enough
  910. Revision 1.11 2000/11/04 14:25:21 florian
  911. + merged Attila's changes for interfaces, not tested yet
  912. Revision 1.10 2000/10/31 22:02:51 peter
  913. * symtable splitted, no real code changes
  914. Revision 1.9 2000/10/14 10:14:52 peter
  915. * moehrendorf oct 2000 rewrite
  916. Revision 1.8 2000/09/30 13:23:04 peter
  917. * const array of char and pchar length fixed (merged)
  918. Revision 1.7 2000/09/24 15:06:25 peter
  919. * use defines.inc
  920. Revision 1.6 2000/08/27 16:11:52 peter
  921. * moved some util functions from globals,cobjects to cutils
  922. * splitted files into finput,fmodule
  923. Revision 1.5 2000/08/24 19:13:18 peter
  924. * allow nil for class typed consts (merged)
  925. Revision 1.4 2000/08/16 13:06:06 florian
  926. + support of 64 bit integer constants
  927. Revision 1.3 2000/08/05 13:25:06 peter
  928. * packenum 1 fixes (merged)
  929. Revision 1.2 2000/07/13 11:32:47 michael
  930. + removed logs
  931. }