pdecvar.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Parses variable declarations. Used for var statement and record
  5. definitions
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit pdecvar;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symsym,symdef;
  24. function read_property_dec(aclass:tobjectdef):tpropertysym;
  25. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  26. implementation
  27. uses
  28. { common }
  29. cutils,cclasses,
  30. { global }
  31. globtype,globals,tokens,verbose,
  32. systems,
  33. { symtable }
  34. symconst,symbase,symtype,symtable,defutil,defcmp,
  35. fmodule,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,
  39. { codegen }
  40. ncgutil,
  41. { parser }
  42. scanner,
  43. pbase,pexpr,ptype,ptconst,pdecsub,
  44. { link }
  45. import
  46. {$ifdef Delphi}
  47. ,dmisc
  48. ,sysutils
  49. {$endif}
  50. ;
  51. function read_property_dec(aclass:tobjectdef):tpropertysym;
  52. { convert a node tree to symlist and return the last
  53. symbol }
  54. function parse_symlist(pl:tsymlist;var def:tdef):boolean;
  55. var
  56. idx : longint;
  57. sym : tsym;
  58. srsymtable : tsymtable;
  59. st : tsymtable;
  60. p : tnode;
  61. begin
  62. result:=true;
  63. def:=nil;
  64. if token=_ID then
  65. begin
  66. if assigned(aclass) then
  67. sym:=search_class_member(aclass,pattern)
  68. else
  69. searchsym(pattern,sym,srsymtable);
  70. if assigned(sym) then
  71. begin
  72. case sym.typ of
  73. varsym :
  74. begin
  75. pl.addsym(sl_load,sym);
  76. def:=tvarsym(sym).vartype.def;
  77. end;
  78. procsym :
  79. begin
  80. pl.addsym(sl_call,sym);
  81. end;
  82. end;
  83. end
  84. else
  85. begin
  86. Message1(parser_e_illegal_field_or_method,pattern);
  87. result:=false;
  88. end;
  89. consume(_ID);
  90. repeat
  91. case token of
  92. _ID,
  93. _SEMICOLON :
  94. begin
  95. break;
  96. end;
  97. _POINT :
  98. begin
  99. consume(_POINT);
  100. if assigned(def) then
  101. begin
  102. st:=def.getsymtable(gs_record);
  103. if assigned(st) then
  104. begin
  105. sym:=searchsymonlyin(st,pattern);
  106. if assigned(sym) then
  107. begin
  108. pl.addsym(sl_subscript,sym);
  109. case sym.typ of
  110. varsym :
  111. def:=tvarsym(sym).vartype.def;
  112. else
  113. begin
  114. Message1(sym_e_illegal_field,pattern);
  115. result:=false;
  116. end;
  117. end;
  118. end
  119. else
  120. begin
  121. Message1(sym_e_illegal_field,pattern);
  122. result:=false;
  123. end;
  124. end
  125. else
  126. begin
  127. Message(parser_e_invalid_qualifier);
  128. result:=false;
  129. end;
  130. end
  131. else
  132. begin
  133. Message(parser_e_invalid_qualifier);
  134. result:=false;
  135. end;
  136. consume(_ID);
  137. end;
  138. _LECKKLAMMER :
  139. begin
  140. consume(_LECKKLAMMER);
  141. repeat
  142. if def.deftype=arraydef then
  143. begin
  144. idx:=0;
  145. p:=comp_expr(true);
  146. if (not codegenerror) then
  147. begin
  148. if (p.nodetype=ordconstn) then
  149. begin
  150. if compare_defs(p.resulttype.def,tarraydef(def).rangetype.def,nothingn)>=te_equal then
  151. idx:=tordconstnode(p).value
  152. else
  153. IncompatibleTypes(p.resulttype.def,tarraydef(def).rangetype.def);
  154. end
  155. else
  156. Message(parser_e_illegal_expression)
  157. end;
  158. p.free;
  159. pl.addconst(sl_vec,idx);
  160. def:=tarraydef(def).elementtype.def;
  161. end
  162. else
  163. begin
  164. Message(parser_e_invalid_qualifier);
  165. result:=false;
  166. end;
  167. until not try_to_consume(_COMMA);
  168. consume(_RECKKLAMMER);
  169. end;
  170. else
  171. begin
  172. Message(parser_e_ill_property_access_sym);
  173. result:=false;
  174. break;
  175. end;
  176. end;
  177. until false;
  178. end
  179. else
  180. begin
  181. Message(parser_e_ill_property_access_sym);
  182. result:=false;
  183. end;
  184. end;
  185. var
  186. sym : tsym;
  187. p : tpropertysym;
  188. overriden : tsym;
  189. hs : string;
  190. varspez : tvarspez;
  191. s : string;
  192. tt : ttype;
  193. arraytype : ttype;
  194. def : tdef;
  195. pt : tnode;
  196. propname : stringid;
  197. sc : tsinglelist;
  198. oldregisterdef : boolean;
  199. readvs,
  200. hvs : tvarsym;
  201. readprocdef,
  202. writeprocdef : tprocvardef;
  203. oldsymtablestack : tsymtable;
  204. begin
  205. { Generate temp procvardefs to search for matching read/write
  206. procedures. the readprocdef will store all definitions }
  207. oldregisterdef:=registerdef;
  208. registerdef:=false;
  209. readprocdef:=tprocvardef.create(normal_function_level);
  210. writeprocdef:=tprocvardef.create(normal_function_level);
  211. registerdef:=oldregisterdef;
  212. { make it method pointers }
  213. if assigned(aclass) then
  214. begin
  215. include(readprocdef.procoptions,po_methodpointer);
  216. include(writeprocdef.procoptions,po_methodpointer);
  217. end;
  218. if token<>_ID then
  219. begin
  220. consume(_ID);
  221. consume(_SEMICOLON);
  222. exit;
  223. end;
  224. { Generate propertysym and insert in symtablestack }
  225. p:=tpropertysym.create(orgpattern);
  226. symtablestack.insert(p);
  227. propname:=pattern;
  228. consume(_ID);
  229. { Set the symtablestack to the parast of readprop so
  230. temp defs will be destroyed after declaration }
  231. readprocdef.parast.next:=symtablestack;
  232. symtablestack:=readprocdef.parast;
  233. { property parameters ? }
  234. if token=_LECKKLAMMER then
  235. begin
  236. if (sp_published in current_object_option) then
  237. Message(parser_e_cant_publish_that_property);
  238. { create a list of the parameters }
  239. sc:=tsinglelist.create;
  240. consume(_LECKKLAMMER);
  241. inc(testcurobject);
  242. repeat
  243. if token=_VAR then
  244. begin
  245. consume(_VAR);
  246. varspez:=vs_var;
  247. end
  248. else if token=_CONST then
  249. begin
  250. consume(_CONST);
  251. varspez:=vs_const;
  252. end
  253. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  254. begin
  255. consume(_OUT);
  256. varspez:=vs_out;
  257. end
  258. else
  259. varspez:=vs_value;
  260. sc.reset;
  261. repeat
  262. readvs:=tvarsym.create(orgpattern,varspez,generrortype);
  263. readprocdef.parast.insert(readvs);
  264. sc.insert(readvs);
  265. consume(_ID);
  266. until not try_to_consume(_COMMA);
  267. if try_to_consume(_COLON) then
  268. begin
  269. { for records, don't search the recordsymtable for
  270. the symbols of the types }
  271. oldsymtablestack:=symtablestack;
  272. symtablestack:=symtablestack.next;
  273. if try_to_consume(_ARRAY) then
  274. begin
  275. consume(_OF);
  276. { define range and type of range }
  277. tt.setdef(tarraydef.create(0,-1,s32inttype));
  278. { define field type }
  279. single_type(arraytype,s,false);
  280. tarraydef(tt.def).setelementtype(arraytype);
  281. end
  282. else
  283. single_type(tt,s,false);
  284. symtablestack:=oldsymtablestack;
  285. end
  286. else
  287. tt:=cformaltype;
  288. readvs:=tvarsym(sc.first);
  289. while assigned(readvs) do
  290. begin
  291. readprocdef.concatpara(nil,tt,readvs,nil,false);
  292. { also update the writeprocdef }
  293. hvs:=tvarsym.create(readvs.realname,vs_value,generrortype);
  294. writeprocdef.parast.insert(hvs);
  295. writeprocdef.concatpara(nil,tt,hvs,nil,false);
  296. readvs:=tvarsym(readvs.listnext);
  297. end;
  298. until not try_to_consume(_SEMICOLON);
  299. sc.free;
  300. dec(testcurobject);
  301. consume(_RECKKLAMMER);
  302. { the parser need to know if a property has parameters, the
  303. index parameter doesn't count (PFV) }
  304. if readprocdef.minparacount>0 then
  305. include(p.propoptions,ppo_hasparameters);
  306. end;
  307. { overriden property ? }
  308. { force property interface
  309. there is a property parameter
  310. a global property }
  311. if (token=_COLON) or (readprocdef.minparacount>0) or (aclass=nil) then
  312. begin
  313. consume(_COLON);
  314. { insert types in global symtable }
  315. oldsymtablestack:=symtablestack;
  316. while not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) do
  317. symtablestack:=symtablestack.next;
  318. single_type(p.proptype,hs,false);
  319. symtablestack:=oldsymtablestack;
  320. if (idtoken=_INDEX) then
  321. begin
  322. consume(_INDEX);
  323. pt:=comp_expr(true);
  324. if is_constnode(pt) and
  325. is_ordinal(pt.resulttype.def)
  326. {$ifndef cpu64bit}
  327. and (not is_64bitint(pt.resulttype.def))
  328. {$endif}
  329. then
  330. p.index:=tordconstnode(pt).value
  331. else
  332. begin
  333. Message(parser_e_invalid_property_index_value);
  334. p.index:=0;
  335. end;
  336. inserttypeconv(pt,sinttype);
  337. p.indextype.setdef(pt.resulttype.def);
  338. include(p.propoptions,ppo_indexed);
  339. { concat a longint to the para templates }
  340. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  341. readprocdef.parast.insert(hvs);
  342. readprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  343. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  344. writeprocdef.parast.insert(hvs);
  345. writeprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  346. pt.free;
  347. end;
  348. end
  349. else
  350. begin
  351. { do an property override }
  352. overriden:=search_class_member(aclass.childof,propname);
  353. if assigned(overriden) and (overriden.typ=propertysym) then
  354. begin
  355. p.dooverride(tpropertysym(overriden));
  356. end
  357. else
  358. begin
  359. p.proptype:=generrortype;
  360. message(parser_e_no_property_found_to_override);
  361. end;
  362. end;
  363. if (sp_published in current_object_option) and
  364. not(p.proptype.def.is_publishable) then
  365. Message(parser_e_cant_publish_that_property);
  366. if try_to_consume(_READ) then
  367. begin
  368. p.readaccess.clear;
  369. if parse_symlist(p.readaccess,def) then
  370. begin
  371. sym:=p.readaccess.firstsym^.sym;
  372. case sym.typ of
  373. procsym :
  374. begin
  375. { read is function returning the type of the property }
  376. readprocdef.rettype:=p.proptype;
  377. { Insert hidden parameters }
  378. handle_calling_convention(readprocdef);
  379. calc_parast(readprocdef);
  380. { search procdefs matching readprocdef }
  381. { we ignore hidden stuff here because the property access symbol might have
  382. non default calling conventions which might change the hidden stuff;
  383. see tw3216.pp (FK) }
  384. p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,[cpo_allowdefaults,cpo_ignorehidden,cpo_allowconvert]);
  385. if not assigned(p.readaccess.procdef) then
  386. Message(parser_e_ill_property_access_sym);
  387. end;
  388. varsym :
  389. begin
  390. if not assigned(def) then
  391. internalerror(200310071);
  392. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  393. begin
  394. { property parameters are allowed if this is
  395. an indexed property, because the index is then
  396. the parameter.
  397. Note: In the help of Kylix it is written
  398. that it isn't allowed, but the compiler accepts it (PFV) }
  399. if (ppo_hasparameters in p.propoptions) then
  400. Message(parser_e_ill_property_access_sym);
  401. end
  402. else
  403. IncompatibleTypes(def,p.proptype.def);
  404. end;
  405. else
  406. Message(parser_e_ill_property_access_sym);
  407. end;
  408. end;
  409. end;
  410. if try_to_consume(_WRITE) then
  411. begin
  412. p.writeaccess.clear;
  413. if parse_symlist(p.writeaccess,def) then
  414. begin
  415. sym:=p.writeaccess.firstsym^.sym;
  416. case sym.typ of
  417. procsym :
  418. begin
  419. { write is a procedure with an extra value parameter
  420. of the of the property }
  421. writeprocdef.rettype:=voidtype;
  422. hvs:=tvarsym.create('$value',vs_value,p.proptype);
  423. writeprocdef.parast.insert(hvs);
  424. writeprocdef.concatpara(nil,p.proptype,hvs,nil,false);
  425. { Insert hidden parameters }
  426. handle_calling_convention(writeprocdef);
  427. calc_parast(writeprocdef);
  428. { search procdefs matching writeprocdef }
  429. p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,[cpo_allowdefaults,cpo_allowconvert]);
  430. if not assigned(p.writeaccess.procdef) then
  431. Message(parser_e_ill_property_access_sym);
  432. end;
  433. varsym :
  434. begin
  435. if not assigned(def) then
  436. internalerror(200310072);
  437. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  438. begin
  439. { property parameters are allowed if this is
  440. an indexed property, because the index is then
  441. the parameter.
  442. Note: In the help of Kylix it is written
  443. that it isn't allowed, but the compiler accepts it (PFV) }
  444. if (ppo_hasparameters in p.propoptions) then
  445. Message(parser_e_ill_property_access_sym);
  446. end
  447. else
  448. IncompatibleTypes(def,p.proptype.def);
  449. end;
  450. else
  451. Message(parser_e_ill_property_access_sym);
  452. end;
  453. end;
  454. end;
  455. if assigned(aclass) then
  456. begin
  457. include(p.propoptions,ppo_stored);
  458. if try_to_consume(_STORED) then
  459. begin
  460. p.storedaccess.clear;
  461. case token of
  462. _ID:
  463. begin
  464. { in the case that idtoken=_DEFAULT }
  465. { we have to do nothing except }
  466. { setting ppo_stored, it's the same }
  467. { as stored true }
  468. if idtoken<>_DEFAULT then
  469. begin
  470. if parse_symlist(p.storedaccess,def) then
  471. begin
  472. sym:=p.storedaccess.firstsym^.sym;
  473. case sym.typ of
  474. procsym :
  475. begin
  476. p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
  477. if not assigned(p.storedaccess.procdef) then
  478. message(parser_e_ill_property_storage_sym);
  479. end;
  480. varsym :
  481. begin
  482. if not assigned(def) then
  483. internalerror(200310073);
  484. if (ppo_hasparameters in p.propoptions) or
  485. not(is_boolean(def)) then
  486. Message(parser_e_stored_property_must_be_boolean);
  487. end;
  488. else
  489. Message(parser_e_ill_property_access_sym);
  490. end;
  491. end;
  492. end;
  493. end;
  494. _FALSE:
  495. begin
  496. consume(_FALSE);
  497. exclude(p.propoptions,ppo_stored);
  498. end;
  499. _TRUE:
  500. consume(_TRUE);
  501. end;
  502. end;
  503. end;
  504. if try_to_consume(_DEFAULT) then
  505. begin
  506. if not(is_ordinal(p.proptype.def) or
  507. {$ifndef cpu64bit}
  508. is_64bitint(p.proptype.def) or
  509. {$endif cpu64bit}
  510. is_class(p.proptype.def) or
  511. is_single(p.proptype.def) or
  512. (p.proptype.def.deftype in [classrefdef,pointerdef]) or
  513. ((p.proptype.def.deftype=setdef) and
  514. (tsetdef(p.proptype.def).settype=smallset))) or
  515. ((p.proptype.def.deftype=arraydef) and
  516. (ppo_indexed in p.propoptions)) or
  517. (ppo_hasparameters in p.propoptions) then
  518. begin
  519. Message(parser_e_property_cant_have_a_default_value);
  520. { Error recovery }
  521. pt:=comp_expr(true);
  522. pt.free;
  523. end
  524. else
  525. begin
  526. { Get the result of the default, the firstpass is
  527. needed to support values like -1 }
  528. pt:=comp_expr(true);
  529. if (p.proptype.def.deftype=setdef) and
  530. (pt.nodetype=arrayconstructorn) then
  531. begin
  532. arrayconstructor_to_set(pt);
  533. do_resulttypepass(pt);
  534. end;
  535. inserttypeconv(pt,p.proptype);
  536. if not(is_constnode(pt)) then
  537. Message(parser_e_property_default_value_must_const);
  538. { Set default value }
  539. case pt.nodetype of
  540. setconstn :
  541. p.default:=plongint(tsetconstnode(pt).value_set)^;
  542. ordconstn :
  543. p.default:=tordconstnode(pt).value;
  544. niln :
  545. p.default:=0;
  546. realconstn:
  547. p.default:=longint(single(trealconstnode(pt).value_real));
  548. end;
  549. pt.free;
  550. end;
  551. end
  552. else if try_to_consume(_NODEFAULT) then
  553. begin
  554. p.default:=0;
  555. end;
  556. { remove temporary procvardefs }
  557. symtablestack:=symtablestack.next;
  558. readprocdef.free;
  559. writeprocdef.free;
  560. result:=p;
  561. end;
  562. const
  563. variantrecordlevel : longint = 0;
  564. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  565. { reads the filed of a record into a }
  566. { symtablestack, if record=false }
  567. { variants are forbidden, so this procedure }
  568. { can be used to read object fields }
  569. { if absolute is true, ABSOLUTE and file }
  570. { types are allowed }
  571. { => the procedure is also used to read }
  572. { a sequence of variable declaration }
  573. procedure insert_syms(sc : tsinglelist;tt : ttype;is_threadvar : boolean; addsymopts : tsymoptions);
  574. { inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
  575. var
  576. vs,vs2 : tvarsym;
  577. begin
  578. vs:=tvarsym(sc.first);
  579. while assigned(vs) do
  580. begin
  581. vs.vartype:=tt;
  582. { insert any additional hint directives }
  583. vs.symoptions := vs.symoptions + addsymopts;
  584. if (sp_static in current_object_option) then
  585. include(vs.symoptions,sp_static);
  586. if is_threadvar then
  587. include(vs.varoptions,vo_is_thread_var);
  588. { static data fields are inserted in the globalsymtable }
  589. if (symtablestack.symtabletype=objectsymtable) and
  590. (sp_static in current_object_option) then
  591. begin
  592. vs2:=tvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,vs_value,tt);
  593. symtablestack.defowner.owner.insert(vs2);
  594. insertbssdata(vs2);
  595. end
  596. else
  597. begin
  598. { external data is not possible here }
  599. case symtablestack.symtabletype of
  600. globalsymtable,
  601. staticsymtable :
  602. insertbssdata(vs);
  603. recordsymtable,
  604. objectsymtable :
  605. tabstractrecordsymtable(symtablestack).insertfield(vs,false);
  606. end;
  607. end;
  608. vs:=tvarsym(vs.listnext);
  609. end;
  610. end;
  611. procedure read_default_value(sc : tsinglelist;tt : ttype;is_threadvar : boolean);
  612. var
  613. vs : tvarsym;
  614. tcsym : ttypedconstsym;
  615. begin
  616. vs:=tvarsym(sc.first);
  617. if assigned(vs.listnext) then
  618. Message(parser_e_initialized_only_one_var);
  619. if is_threadvar then
  620. Message(parser_e_initialized_not_for_threadvar);
  621. if symtablestack.symtabletype=localsymtable then
  622. begin
  623. consume(_EQUAL);
  624. tcsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
  625. vs.defaultconstsym:=tcsym;
  626. symtablestack.insert(tcsym);
  627. insertconstdata(tcsym);
  628. readtypedconst(tt,tcsym,false);
  629. end
  630. else
  631. begin
  632. tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  633. tcsym.fileinfo:=vs.fileinfo;
  634. symtablestack.replace(vs,tcsym);
  635. vs.free;
  636. insertconstdata(tcsym);
  637. consume(_EQUAL);
  638. readtypedconst(tt,tcsym,true);
  639. end;
  640. end;
  641. var
  642. sc : tsinglelist;
  643. old_block_type : tblock_type;
  644. symdone : boolean;
  645. { to handle absolute }
  646. abssym : tabsolutesym;
  647. { c var }
  648. newtype : ttypesym;
  649. is_dll,
  650. hasdefaultvalue,
  651. is_gpc_name,is_cdecl,
  652. extern_var,export_var : boolean;
  653. old_current_object_option : tsymoptions;
  654. hs,sorg,C_name,dll_name : string;
  655. tt,casetype : ttype;
  656. { maxsize contains the max. size of a variant }
  657. { startvarrec contains the start of the variant part of a record }
  658. maxsize, startvarrecsize : longint;
  659. usedalign,
  660. maxalignment,startvarrecalign : byte;
  661. hp,pt : tnode;
  662. vs,vs2 : tvarsym;
  663. srsym : tsym;
  664. oldsymtablestack,
  665. srsymtable : tsymtable;
  666. unionsymtable : trecordsymtable;
  667. offset : longint;
  668. uniondef : trecorddef;
  669. unionsym : tvarsym;
  670. uniontype : ttype;
  671. dummysymoptions : tsymoptions;
  672. semicolonatend: boolean;
  673. begin
  674. old_current_object_option:=current_object_option;
  675. { all variables are public if not in a object declaration }
  676. if not is_object then
  677. current_object_option:=[sp_public];
  678. old_block_type:=block_type;
  679. block_type:=bt_type;
  680. is_gpc_name:=false;
  681. { Force an expected ID error message }
  682. if not (token in [_ID,_CASE,_END]) then
  683. consume(_ID);
  684. { read vars }
  685. sc:=tsinglelist.create;
  686. while (token=_ID) and
  687. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  688. begin
  689. sorg:=orgpattern;
  690. sc.reset;
  691. repeat
  692. vs:=tvarsym.create(orgpattern,vs_value,generrortype);
  693. symtablestack.insert(vs);
  694. if assigned(vs.owner) then
  695. sc.insert(vs)
  696. else
  697. vs.free;
  698. consume(_ID);
  699. until not try_to_consume(_COMMA);
  700. consume(_COLON);
  701. if (m_gpc in aktmodeswitches) and
  702. not(is_record or is_object or is_threadvar) and
  703. (token=_ID) and (orgpattern='__asmname__') then
  704. begin
  705. consume(_ID);
  706. C_name:=get_stringconst;
  707. Is_gpc_name:=true;
  708. end;
  709. { this is needed for Delphi mode at least
  710. but should be OK for all modes !! (PM) }
  711. ignore_equal:=true;
  712. if is_record or is_object then
  713. begin
  714. { for records, don't search the recordsymtable for
  715. the symbols of the types }
  716. oldsymtablestack:=symtablestack;
  717. symtablestack:=symtablestack.next;
  718. read_type(tt,'',false);
  719. symtablestack:=oldsymtablestack;
  720. end
  721. else
  722. read_type(tt,'',false);
  723. { Process procvar directives }
  724. if (tt.def.deftype=procvardef) and
  725. (tt.def.typesym=nil) and
  726. is_proc_directive(token,true) then
  727. begin
  728. newtype:=ttypesym.create('unnamed',tt);
  729. parse_var_proc_directives(tsym(newtype));
  730. newtype.restype.def:=nil;
  731. tt.def.typesym:=nil;
  732. newtype.free;
  733. end;
  734. { types that use init/final are not allowed in variant parts, but
  735. classes are allowed }
  736. if (variantrecordlevel>0) and
  737. (tt.def.needs_inittable and not is_class(tt.def)) then
  738. Message(parser_e_cant_use_inittable_here);
  739. ignore_equal:=false;
  740. hasdefaultvalue:=false;
  741. symdone:=false;
  742. if is_gpc_name then
  743. begin
  744. vs:=tvarsym(sc.first);
  745. if assigned(vs.listnext) then
  746. Message(parser_e_absolute_only_one_var);
  747. vs.vartype:=tt;
  748. include(vs.varoptions,vo_is_C_var);
  749. vs.set_mangledname(target_info.Cprefix+sorg);
  750. include(vs.varoptions,vo_is_external);
  751. symdone:=true;
  752. end;
  753. { check for absolute }
  754. if not symdone and
  755. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  756. begin
  757. consume(_ABSOLUTE);
  758. abssym:=nil;
  759. { only allowed for one var }
  760. vs:=tvarsym(sc.first);
  761. if assigned(vs.listnext) then
  762. Message(parser_e_absolute_only_one_var);
  763. { parse the rest }
  764. pt:=expr;
  765. { check allowed absolute types }
  766. if (pt.nodetype=stringconstn) or
  767. (is_constcharnode(pt)) then
  768. begin
  769. abssym:=tabsolutesym.create(vs.realname,tt);
  770. abssym.fileinfo:=vs.fileinfo;
  771. if pt.nodetype=stringconstn then
  772. hs:=strpas(tstringconstnode(pt).value_str)
  773. else
  774. hs:=chr(tordconstnode(pt).value);
  775. consume(token);
  776. abssym.abstyp:=toasm;
  777. abssym.asmname:=stringdup(hs);
  778. { replace the varsym }
  779. symtablestack.replace(vs,abssym);
  780. vs.free;
  781. end
  782. { address }
  783. else if is_constintnode(pt) and
  784. ((target_info.system in [system_i386_go32v2,system_i386_watcom,
  785. system_i386_wdosx,system_i386_win32]) or
  786. (m_objfpc in aktmodeswitches) or
  787. (m_delphi in aktmodeswitches)) then
  788. begin
  789. abssym:=tabsolutesym.create(vs.realname,tt);
  790. abssym.fileinfo:=vs.fileinfo;
  791. abssym.abstyp:=toaddr;
  792. abssym.fieldoffset:=tordconstnode(pt).value;
  793. {$ifdef i386}
  794. abssym.absseg:=false;
  795. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  796. try_to_consume(_COLON) then
  797. begin
  798. pt.free;
  799. pt:=expr;
  800. if is_constintnode(pt) then
  801. begin
  802. abssym.fieldoffset:=abssym.fieldoffset shl 4+tordconstnode(pt).value;
  803. abssym.absseg:=true;
  804. end
  805. else
  806. Message(type_e_ordinal_expr_expected);
  807. end;
  808. {$endif i386}
  809. symtablestack.replace(vs,abssym);
  810. vs.free;
  811. end
  812. { variable }
  813. else
  814. begin
  815. { remove subscriptn before checking for loadn }
  816. hp:=pt;
  817. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  818. hp:=tunarynode(hp).left;
  819. if (hp.nodetype=loadn) then
  820. begin
  821. { we should check the result type of loadn }
  822. if not (tloadnode(hp).symtableentry.typ in [varsym,typedconstsym]) then
  823. Message(parser_e_absolute_only_to_var_or_const);
  824. abssym:=tabsolutesym.create(vs.realname,tt);
  825. abssym.fileinfo:=vs.fileinfo;
  826. abssym.abstyp:=tovar;
  827. abssym.ref:=node_to_symlist(pt);
  828. symtablestack.replace(vs,abssym);
  829. vs.free;
  830. end
  831. else
  832. Message(parser_e_absolute_only_to_var_or_const);
  833. end;
  834. if assigned(abssym) then
  835. begin
  836. { try to consume the hint directives with absolute symbols }
  837. dummysymoptions:=[];
  838. try_consume_hintdirective(dummysymoptions);
  839. abssym.symoptions := abssym.symoptions + dummysymoptions;
  840. end;
  841. pt.free;
  842. symdone:=true;
  843. end;
  844. { Process procvar directives before = and ; }
  845. if (tt.def.deftype=procvardef) and
  846. (tt.def.typesym=nil) and
  847. is_proc_directive(token,true) then
  848. begin
  849. newtype:=ttypesym.create('unnamed',tt);
  850. parse_var_proc_directives(tsym(newtype));
  851. newtype.restype.def:=nil;
  852. tt.def.typesym:=nil;
  853. newtype.free;
  854. end;
  855. { try to parse the hint directives }
  856. dummysymoptions:=[];
  857. try_consume_hintdirective(dummysymoptions);
  858. { Records and objects can't have default values }
  859. if is_record or is_object then
  860. begin
  861. { for a record there doesn't need to be a ; before the END or ) }
  862. if not(token in [_END,_RKLAMMER]) then
  863. consume(_SEMICOLON);
  864. end
  865. else
  866. begin
  867. { Handling of Delphi typed const = initialized vars }
  868. if (token=_EQUAL) and
  869. not(m_tp7 in aktmodeswitches) and
  870. (symtablestack.symtabletype<>parasymtable) then
  871. begin
  872. if (tt.def.deftype=procvardef) and
  873. (tt.def.typesym=nil) then
  874. begin
  875. { Add calling convention for procvar }
  876. handle_calling_convention(tprocvardef(tt.def));
  877. calc_parast(tprocvardef(tt.def));
  878. end;
  879. read_default_value(sc,tt,is_threadvar);
  880. { for locals we've created typedconstsym with a different name }
  881. if symtablestack.symtabletype<>localsymtable then
  882. symdone:=true;
  883. hasdefaultvalue:=true;
  884. end;
  885. consume(_SEMICOLON);
  886. end;
  887. { Support calling convention for procvars after semicolon }
  888. if not(hasdefaultvalue) and
  889. (tt.def.deftype=procvardef) and
  890. (tt.def.typesym=nil) then
  891. begin
  892. { Parse procvar directives after ; }
  893. if is_proc_directive(token,true) then
  894. begin
  895. newtype:=ttypesym.create('unnamed',tt);
  896. parse_var_proc_directives(tsym(newtype));
  897. newtype.restype.def:=nil;
  898. tt.def.typesym:=nil;
  899. newtype.free;
  900. end;
  901. { Add calling convention for procvar }
  902. handle_calling_convention(tprocvardef(tt.def));
  903. calc_parast(tprocvardef(tt.def));
  904. { Handling of Delphi typed const = initialized vars }
  905. if (token=_EQUAL) and
  906. not(is_record or is_object) and
  907. not(m_tp7 in aktmodeswitches) and
  908. (symtablestack.symtabletype<>parasymtable) then
  909. begin
  910. read_default_value(sc,tt,is_threadvar);
  911. consume(_SEMICOLON);
  912. symdone:=true;
  913. hasdefaultvalue:=true;
  914. end;
  915. end;
  916. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  917. if not symdone and not(is_record or is_object or is_threadvar) then
  918. begin
  919. if (
  920. (token=_ID) and
  921. (m_cvar_support in aktmodeswitches) and
  922. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
  923. ) or
  924. (
  925. (m_mac in aktmodeswitches) and
  926. ((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
  927. ) then
  928. begin
  929. { only allowed for one var }
  930. vs:=tvarsym(sc.first);
  931. if assigned(vs.listnext) then
  932. Message(parser_e_absolute_only_one_var);
  933. { set type of the var }
  934. vs.vartype:=tt;
  935. vs.symoptions := vs.symoptions + dummysymoptions;
  936. { defaults }
  937. is_dll:=false;
  938. is_cdecl:=false;
  939. extern_var:=false;
  940. export_var:=false;
  941. C_name:=sorg;
  942. semicolonatend:= false;
  943. { cdecl }
  944. if idtoken=_CVAR then
  945. begin
  946. consume(_CVAR);
  947. consume(_SEMICOLON);
  948. is_cdecl:=true;
  949. C_name:=target_info.Cprefix+sorg;
  950. end;
  951. { external }
  952. if idtoken=_EXTERNAL then
  953. begin
  954. consume(_EXTERNAL);
  955. extern_var:=true;
  956. semicolonatend:= true;
  957. end;
  958. { macpas specific handling due to some switches}
  959. if (m_mac in aktmodeswitches) then
  960. begin
  961. if (cs_external_var in aktlocalswitches) then
  962. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  963. is_cdecl:=true;
  964. C_name:=target_info.Cprefix+sorg;
  965. extern_var:=true;
  966. end
  967. else if (cs_externally_visible in aktlocalswitches) then
  968. begin {The effect of this is the same as if cvar has been given as directives.}
  969. is_cdecl:=true;
  970. C_name:=target_info.Cprefix+sorg;
  971. end;
  972. end;
  973. { export }
  974. if idtoken in [_EXPORT,_PUBLIC] then
  975. begin
  976. consume(_ID);
  977. if extern_var or
  978. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  979. Message(parser_e_not_external_and_export)
  980. else
  981. begin
  982. export_var:=true;
  983. semicolonatend:= true;
  984. end;
  985. end;
  986. { external and export need a name after when no cdecl is used }
  987. if not is_cdecl then
  988. begin
  989. { dll name ? }
  990. if (extern_var) and (idtoken<>_NAME) then
  991. begin
  992. is_dll:=true;
  993. dll_name:=get_stringconst;
  994. end;
  995. consume(_NAME);
  996. C_name:=get_stringconst;
  997. end;
  998. { consume the ; when export or external is used }
  999. if semicolonatend then
  1000. consume(_SEMICOLON);
  1001. { set some vars options }
  1002. if is_dll then
  1003. include(vs.varoptions,vo_is_dll_var)
  1004. else
  1005. include(vs.varoptions,vo_is_C_var);
  1006. if (is_dll) and
  1007. (target_info.system = system_powerpc_darwin) then
  1008. C_Name := target_info.Cprefix+C_Name;
  1009. vs.set_mangledname(C_Name);
  1010. if export_var then
  1011. begin
  1012. inc(vs.refs);
  1013. include(vs.varoptions,vo_is_exported);
  1014. end;
  1015. if extern_var then
  1016. include(vs.varoptions,vo_is_external);
  1017. { insert in the datasegment when it is not external }
  1018. if (not extern_var) then
  1019. insertbssdata(vs);
  1020. { now we can insert it in the import lib if its a dll, or
  1021. add it to the externals }
  1022. if extern_var then
  1023. begin
  1024. if is_dll then
  1025. begin
  1026. if not(current_module.uses_imports) then
  1027. begin
  1028. current_module.uses_imports:=true;
  1029. importlib.preparelib(current_module.modulename^);
  1030. end;
  1031. importlib.importvariable(vs,C_name,dll_name);
  1032. end
  1033. else
  1034. if target_info.DllScanSupported then
  1035. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  1036. end;
  1037. symdone:=true;
  1038. end;
  1039. end;
  1040. { Check for STATIC directive }
  1041. if not symdone and (is_object) and
  1042. (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  1043. begin
  1044. include(current_object_option,sp_static);
  1045. insert_syms(sc,tt,false,dummysymoptions);
  1046. exclude(current_object_option,sp_static);
  1047. consume(_STATIC);
  1048. consume(_SEMICOLON);
  1049. symdone:=true;
  1050. end;
  1051. { insert it in the symtable, if not done yet }
  1052. if not symdone then
  1053. begin
  1054. { save object option, because we can turn of the sp_published }
  1055. if (sp_published in current_object_option) and
  1056. not(is_class(tt.def)) then
  1057. begin
  1058. Message(parser_e_cant_publish_that);
  1059. exclude(current_object_option,sp_published);
  1060. { recover by changing access type to public }
  1061. vs2:=tvarsym(sc.first);
  1062. while assigned (vs2) do
  1063. begin
  1064. exclude(vs2.symoptions,sp_published);
  1065. include(vs2.symoptions,sp_public);
  1066. vs2:=tvarsym(vs2.listnext);
  1067. end;
  1068. end
  1069. else
  1070. if (sp_published in current_object_option) and
  1071. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  1072. begin
  1073. Message(parser_e_only_publishable_classes_can__be_published);
  1074. exclude(current_object_option,sp_published);
  1075. end;
  1076. insert_syms(sc,tt,is_threadvar,dummysymoptions);
  1077. current_object_option:=old_current_object_option;
  1078. end;
  1079. end;
  1080. { Check for Case }
  1081. if is_record and (token=_CASE) then
  1082. begin
  1083. maxsize:=0;
  1084. maxalignment:=0;
  1085. consume(_CASE);
  1086. sorg:=orgpattern;
  1087. hs:=pattern;
  1088. searchsym(hs,srsym,srsymtable);
  1089. { may be only a type: }
  1090. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  1091. begin
  1092. { for records, don't search the recordsymtable for
  1093. the symbols of the types }
  1094. oldsymtablestack:=symtablestack;
  1095. symtablestack:=symtablestack.next;
  1096. read_type(casetype,'',true);
  1097. symtablestack:=oldsymtablestack;
  1098. end
  1099. else
  1100. begin
  1101. consume(_ID);
  1102. consume(_COLON);
  1103. { for records, don't search the recordsymtable for
  1104. the symbols of the types }
  1105. oldsymtablestack:=symtablestack;
  1106. symtablestack:=symtablestack.next;
  1107. read_type(casetype,'',true);
  1108. symtablestack:=oldsymtablestack;
  1109. vs:=tvarsym.create(sorg,vs_value,casetype);
  1110. tabstractrecordsymtable(symtablestack).insertfield(vs,true);
  1111. end;
  1112. if not(is_ordinal(casetype.def))
  1113. {$ifndef cpu64bit}
  1114. or is_64bitint(casetype.def)
  1115. {$endif cpu64bit}
  1116. then
  1117. Message(type_e_ordinal_expr_expected);
  1118. consume(_OF);
  1119. UnionSymtable:=trecordsymtable.create(aktpackrecords);
  1120. Unionsymtable.next:=symtablestack;
  1121. registerdef:=false;
  1122. UnionDef:=trecorddef.create(unionsymtable);
  1123. uniondef.isunion:=true;
  1124. if assigned(symtablestack.defowner) then
  1125. Uniondef.owner:=symtablestack.defowner.owner;
  1126. registerdef:=true;
  1127. startvarrecsize:=UnionSymtable.datasize;
  1128. startvarrecalign:=UnionSymtable.fieldalignment;
  1129. symtablestack:=UnionSymtable;
  1130. repeat
  1131. repeat
  1132. pt:=comp_expr(true);
  1133. if not(pt.nodetype=ordconstn) then
  1134. Message(parser_e_illegal_expression);
  1135. pt.free;
  1136. if token=_COMMA then
  1137. consume(_COMMA)
  1138. else
  1139. break;
  1140. until false;
  1141. consume(_COLON);
  1142. { read the vars }
  1143. consume(_LKLAMMER);
  1144. inc(variantrecordlevel);
  1145. if token<>_RKLAMMER then
  1146. read_var_decs(true,false,false);
  1147. dec(variantrecordlevel);
  1148. consume(_RKLAMMER);
  1149. { calculates maximal variant size }
  1150. maxsize:=max(maxsize,unionsymtable.datasize);
  1151. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1152. { the items of the next variant are overlayed }
  1153. unionsymtable.datasize:=startvarrecsize;
  1154. unionsymtable.fieldalignment:=startvarrecalign;
  1155. if (token<>_END) and (token<>_RKLAMMER) then
  1156. consume(_SEMICOLON)
  1157. else
  1158. break;
  1159. until (token=_END) or (token=_RKLAMMER);
  1160. { at last set the record size to that of the biggest variant }
  1161. unionsymtable.datasize:=maxsize;
  1162. unionsymtable.fieldalignment:=maxalignment;
  1163. uniontype.def:=uniondef;
  1164. uniontype.sym:=nil;
  1165. UnionSym:=tvarsym.create('$case',vs_value,uniontype);
  1166. symtablestack:=symtablestack.next;
  1167. { Align the offset where the union symtable is added }
  1168. if (trecordsymtable(symtablestack).usefieldalignment=-1) then
  1169. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  1170. else
  1171. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
  1172. offset:=align(trecordsymtable(symtablestack).datasize,usedalign);
  1173. trecordsymtable(symtablestack).datasize:=offset+unionsymtable.datasize;
  1174. if unionsymtable.recordalignment>trecordsymtable(symtablestack).fieldalignment then
  1175. trecordsymtable(symtablestack).fieldalignment:=unionsymtable.recordalignment;
  1176. trecordsymtable(symtablestack).insertunionst(Unionsymtable,offset);
  1177. Unionsym.owner:=nil;
  1178. unionsym.free;
  1179. uniondef.owner:=nil;
  1180. uniondef.free;
  1181. end;
  1182. block_type:=old_block_type;
  1183. current_object_option:=old_current_object_option;
  1184. { free the list }
  1185. sc.free;
  1186. end;
  1187. end.
  1188. {
  1189. $Log$
  1190. Revision 1.78 2004-08-15 13:30:18 florian
  1191. * fixed alignment of variant records
  1192. * more alignment problems fixed
  1193. Revision 1.77 2004/08/07 19:14:50 florian
  1194. * fixed problem with explicit specified calling conventions for property symbols
  1195. Revision 1.76 2004/07/14 23:19:22 olle
  1196. + added external facilities for macpas
  1197. Revision 1.75 2004/06/20 08:55:30 florian
  1198. * logs truncated
  1199. Revision 1.74 2004/06/16 20:07:09 florian
  1200. * dwarf branch merged
  1201. Revision 1.73 2004/04/11 12:38:16 peter
  1202. * fix calling convention problem when parsing default value before
  1203. the semicolon
  1204. Revision 1.72.2.5 2004/05/02 20:54:04 peter
  1205. * allow convert when choosing property function with index
  1206. Revision 1.72.2.4 2004/05/01 20:51:20 peter
  1207. * fix val code parameter
  1208. Revision 1.72.2.3 2004/04/28 19:55:52 peter
  1209. * new warning for ordinal-pointer when size is different
  1210. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1211. }