pdecvar.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,[cpo_allowdefaults,cpo_allowconvert]);
  382. if not assigned(p.readaccess.procdef) then
  383. Message(parser_e_ill_property_access_sym);
  384. end;
  385. varsym :
  386. begin
  387. if not assigned(def) then
  388. internalerror(200310071);
  389. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  390. begin
  391. { property parameters are allowed if this is
  392. an indexed property, because the index is then
  393. the parameter.
  394. Note: In the help of Kylix it is written
  395. that it isn't allowed, but the compiler accepts it (PFV) }
  396. if (ppo_hasparameters in p.propoptions) then
  397. Message(parser_e_ill_property_access_sym);
  398. end
  399. else
  400. IncompatibleTypes(def,p.proptype.def);
  401. end;
  402. else
  403. Message(parser_e_ill_property_access_sym);
  404. end;
  405. end;
  406. end;
  407. if try_to_consume(_WRITE) then
  408. begin
  409. p.writeaccess.clear;
  410. if parse_symlist(p.writeaccess,def) then
  411. begin
  412. sym:=p.writeaccess.firstsym^.sym;
  413. case sym.typ of
  414. procsym :
  415. begin
  416. { write is a procedure with an extra value parameter
  417. of the of the property }
  418. writeprocdef.rettype:=voidtype;
  419. hvs:=tvarsym.create('$value',vs_value,p.proptype);
  420. writeprocdef.parast.insert(hvs);
  421. writeprocdef.concatpara(nil,p.proptype,hvs,nil,false);
  422. { Insert hidden parameters }
  423. handle_calling_convention(writeprocdef);
  424. calc_parast(writeprocdef);
  425. { search procdefs matching writeprocdef }
  426. p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,[cpo_allowdefaults,cpo_allowconvert]);
  427. if not assigned(p.writeaccess.procdef) then
  428. Message(parser_e_ill_property_access_sym);
  429. end;
  430. varsym :
  431. begin
  432. if not assigned(def) then
  433. internalerror(200310072);
  434. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  435. begin
  436. { property parameters are allowed if this is
  437. an indexed property, because the index is then
  438. the parameter.
  439. Note: In the help of Kylix it is written
  440. that it isn't allowed, but the compiler accepts it (PFV) }
  441. if (ppo_hasparameters in p.propoptions) then
  442. Message(parser_e_ill_property_access_sym);
  443. end
  444. else
  445. IncompatibleTypes(def,p.proptype.def);
  446. end;
  447. else
  448. Message(parser_e_ill_property_access_sym);
  449. end;
  450. end;
  451. end;
  452. if assigned(aclass) then
  453. begin
  454. include(p.propoptions,ppo_stored);
  455. if try_to_consume(_STORED) then
  456. begin
  457. p.storedaccess.clear;
  458. case token of
  459. _ID:
  460. begin
  461. { in the case that idtoken=_DEFAULT }
  462. { we have to do nothing except }
  463. { setting ppo_stored, it's the same }
  464. { as stored true }
  465. if idtoken<>_DEFAULT then
  466. begin
  467. if parse_symlist(p.storedaccess,def) then
  468. begin
  469. sym:=p.storedaccess.firstsym^.sym;
  470. case sym.typ of
  471. procsym :
  472. begin
  473. p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
  474. if not assigned(p.storedaccess.procdef) then
  475. message(parser_e_ill_property_storage_sym);
  476. end;
  477. varsym :
  478. begin
  479. if not assigned(def) then
  480. internalerror(200310073);
  481. if (ppo_hasparameters in p.propoptions) or
  482. not(is_boolean(def)) then
  483. Message(parser_e_stored_property_must_be_boolean);
  484. end;
  485. else
  486. Message(parser_e_ill_property_access_sym);
  487. end;
  488. end;
  489. end;
  490. end;
  491. _FALSE:
  492. begin
  493. consume(_FALSE);
  494. exclude(p.propoptions,ppo_stored);
  495. end;
  496. _TRUE:
  497. consume(_TRUE);
  498. end;
  499. end;
  500. end;
  501. if try_to_consume(_DEFAULT) then
  502. begin
  503. if not(is_ordinal(p.proptype.def) or
  504. {$ifndef cpu64bit}
  505. is_64bitint(p.proptype.def) or
  506. {$endif cpu64bit}
  507. is_class(p.proptype.def) or
  508. is_single(p.proptype.def) or
  509. (p.proptype.def.deftype in [classrefdef,pointerdef]) or
  510. ((p.proptype.def.deftype=setdef) and
  511. (tsetdef(p.proptype.def).settype=smallset))) or
  512. ((p.proptype.def.deftype=arraydef) and
  513. (ppo_indexed in p.propoptions)) or
  514. (ppo_hasparameters in p.propoptions) then
  515. begin
  516. Message(parser_e_property_cant_have_a_default_value);
  517. { Error recovery }
  518. pt:=comp_expr(true);
  519. pt.free;
  520. end
  521. else
  522. begin
  523. { Get the result of the default, the firstpass is
  524. needed to support values like -1 }
  525. pt:=comp_expr(true);
  526. if (p.proptype.def.deftype=setdef) and
  527. (pt.nodetype=arrayconstructorn) then
  528. begin
  529. arrayconstructor_to_set(pt);
  530. do_resulttypepass(pt);
  531. end;
  532. inserttypeconv(pt,p.proptype);
  533. if not(is_constnode(pt)) then
  534. Message(parser_e_property_default_value_must_const);
  535. { Set default value }
  536. case pt.nodetype of
  537. setconstn :
  538. p.default:=plongint(tsetconstnode(pt).value_set)^;
  539. ordconstn :
  540. p.default:=tordconstnode(pt).value;
  541. niln :
  542. p.default:=0;
  543. realconstn:
  544. p.default:=longint(single(trealconstnode(pt).value_real));
  545. end;
  546. pt.free;
  547. end;
  548. end
  549. else if try_to_consume(_NODEFAULT) then
  550. begin
  551. p.default:=0;
  552. end;
  553. { remove temporary procvardefs }
  554. symtablestack:=symtablestack.next;
  555. readprocdef.free;
  556. writeprocdef.free;
  557. result:=p;
  558. end;
  559. const
  560. variantrecordlevel : longint = 0;
  561. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  562. { reads the filed of a record into a }
  563. { symtablestack, if record=false }
  564. { variants are forbidden, so this procedure }
  565. { can be used to read object fields }
  566. { if absolute is true, ABSOLUTE and file }
  567. { types are allowed }
  568. { => the procedure is also used to read }
  569. { a sequence of variable declaration }
  570. procedure insert_syms(sc : tsinglelist;tt : ttype;is_threadvar : boolean; addsymopts : tsymoptions);
  571. { inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
  572. var
  573. vs,vs2 : tvarsym;
  574. begin
  575. vs:=tvarsym(sc.first);
  576. while assigned(vs) do
  577. begin
  578. vs.vartype:=tt;
  579. { insert any additional hint directives }
  580. vs.symoptions := vs.symoptions + addsymopts;
  581. if (sp_static in current_object_option) then
  582. include(vs.symoptions,sp_static);
  583. if is_threadvar then
  584. include(vs.varoptions,vo_is_thread_var);
  585. { static data fields are inserted in the globalsymtable }
  586. if (symtablestack.symtabletype=objectsymtable) and
  587. (sp_static in current_object_option) then
  588. begin
  589. vs2:=tvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,vs_value,tt);
  590. symtablestack.defowner.owner.insert(vs2);
  591. insertbssdata(vs2);
  592. end
  593. else
  594. begin
  595. { external data is not possible here }
  596. case symtablestack.symtabletype of
  597. globalsymtable,
  598. staticsymtable :
  599. insertbssdata(vs);
  600. recordsymtable,
  601. objectsymtable :
  602. tabstractrecordsymtable(symtablestack).insertfield(vs,false);
  603. end;
  604. end;
  605. vs:=tvarsym(vs.listnext);
  606. end;
  607. end;
  608. procedure read_default_value(sc : tsinglelist;tt : ttype;is_threadvar : boolean);
  609. var
  610. vs : tvarsym;
  611. tcsym : ttypedconstsym;
  612. begin
  613. vs:=tvarsym(sc.first);
  614. if assigned(vs.listnext) then
  615. Message(parser_e_initialized_only_one_var);
  616. if is_threadvar then
  617. Message(parser_e_initialized_not_for_threadvar);
  618. if symtablestack.symtabletype=localsymtable then
  619. begin
  620. consume(_EQUAL);
  621. tcsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
  622. vs.defaultconstsym:=tcsym;
  623. symtablestack.insert(tcsym);
  624. insertconstdata(tcsym);
  625. readtypedconst(tt,tcsym,false);
  626. end
  627. else
  628. begin
  629. tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  630. tcsym.fileinfo:=vs.fileinfo;
  631. symtablestack.replace(vs,tcsym);
  632. vs.free;
  633. insertconstdata(tcsym);
  634. consume(_EQUAL);
  635. readtypedconst(tt,tcsym,true);
  636. end;
  637. end;
  638. var
  639. sc : tsinglelist;
  640. old_block_type : tblock_type;
  641. symdone : boolean;
  642. { to handle absolute }
  643. abssym : tabsolutesym;
  644. { c var }
  645. newtype : ttypesym;
  646. is_dll,
  647. hasdefaultvalue,
  648. is_gpc_name,is_cdecl,
  649. extern_var,export_var : boolean;
  650. old_current_object_option : tsymoptions;
  651. hs,sorg,C_name,dll_name : string;
  652. tt,casetype : ttype;
  653. { maxsize contains the max. size of a variant }
  654. { startvarrec contains the start of the variant part of a record }
  655. maxsize, startvarrecsize : longint;
  656. usedalign,
  657. maxalignment,startvarrecalign : byte;
  658. hp,pt : tnode;
  659. vs,vs2 : tvarsym;
  660. srsym : tsym;
  661. oldsymtablestack,
  662. srsymtable : tsymtable;
  663. unionsymtable : trecordsymtable;
  664. offset : longint;
  665. uniondef : trecorddef;
  666. unionsym : tvarsym;
  667. uniontype : ttype;
  668. dummysymoptions : tsymoptions;
  669. semicolonatend: boolean;
  670. begin
  671. old_current_object_option:=current_object_option;
  672. { all variables are public if not in a object declaration }
  673. if not is_object then
  674. current_object_option:=[sp_public];
  675. old_block_type:=block_type;
  676. block_type:=bt_type;
  677. is_gpc_name:=false;
  678. { Force an expected ID error message }
  679. if not (token in [_ID,_CASE,_END]) then
  680. consume(_ID);
  681. { read vars }
  682. sc:=tsinglelist.create;
  683. while (token=_ID) and
  684. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  685. begin
  686. sorg:=orgpattern;
  687. sc.reset;
  688. repeat
  689. vs:=tvarsym.create(orgpattern,vs_value,generrortype);
  690. symtablestack.insert(vs);
  691. if assigned(vs.owner) then
  692. sc.insert(vs)
  693. else
  694. vs.free;
  695. consume(_ID);
  696. until not try_to_consume(_COMMA);
  697. consume(_COLON);
  698. if (m_gpc in aktmodeswitches) and
  699. not(is_record or is_object or is_threadvar) and
  700. (token=_ID) and (orgpattern='__asmname__') then
  701. begin
  702. consume(_ID);
  703. C_name:=get_stringconst;
  704. Is_gpc_name:=true;
  705. end;
  706. { this is needed for Delphi mode at least
  707. but should be OK for all modes !! (PM) }
  708. ignore_equal:=true;
  709. if is_record or is_object then
  710. begin
  711. { for records, don't search the recordsymtable for
  712. the symbols of the types }
  713. oldsymtablestack:=symtablestack;
  714. symtablestack:=symtablestack.next;
  715. read_type(tt,'',false);
  716. symtablestack:=oldsymtablestack;
  717. end
  718. else
  719. read_type(tt,'',false);
  720. { Process procvar directives }
  721. if (tt.def.deftype=procvardef) and
  722. (tt.def.typesym=nil) and
  723. is_proc_directive(token,true) then
  724. begin
  725. newtype:=ttypesym.create('unnamed',tt);
  726. parse_var_proc_directives(tsym(newtype));
  727. newtype.restype.def:=nil;
  728. tt.def.typesym:=nil;
  729. newtype.free;
  730. end;
  731. { types that use init/final are not allowed in variant parts, but
  732. classes are allowed }
  733. if (variantrecordlevel>0) and
  734. (tt.def.needs_inittable and not is_class(tt.def)) then
  735. Message(parser_e_cant_use_inittable_here);
  736. ignore_equal:=false;
  737. hasdefaultvalue:=false;
  738. symdone:=false;
  739. if is_gpc_name then
  740. begin
  741. vs:=tvarsym(sc.first);
  742. if assigned(vs.listnext) then
  743. Message(parser_e_absolute_only_one_var);
  744. vs.vartype:=tt;
  745. include(vs.varoptions,vo_is_C_var);
  746. vs.set_mangledname(target_info.Cprefix+sorg);
  747. include(vs.varoptions,vo_is_external);
  748. symdone:=true;
  749. end;
  750. { check for absolute }
  751. if not symdone and
  752. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  753. begin
  754. consume(_ABSOLUTE);
  755. abssym:=nil;
  756. { only allowed for one var }
  757. vs:=tvarsym(sc.first);
  758. if assigned(vs.listnext) then
  759. Message(parser_e_absolute_only_one_var);
  760. { parse the rest }
  761. pt:=expr;
  762. { check allowed absolute types }
  763. if (pt.nodetype=stringconstn) or
  764. (is_constcharnode(pt)) then
  765. begin
  766. abssym:=tabsolutesym.create(vs.realname,tt);
  767. abssym.fileinfo:=vs.fileinfo;
  768. if pt.nodetype=stringconstn then
  769. hs:=strpas(tstringconstnode(pt).value_str)
  770. else
  771. hs:=chr(tordconstnode(pt).value);
  772. consume(token);
  773. abssym.abstyp:=toasm;
  774. abssym.asmname:=stringdup(hs);
  775. { replace the varsym }
  776. symtablestack.replace(vs,abssym);
  777. vs.free;
  778. end
  779. { address }
  780. else if is_constintnode(pt) and
  781. ((target_info.system in [system_i386_go32v2,system_i386_watcom,
  782. system_i386_wdosx,system_i386_win32]) or
  783. (m_objfpc in aktmodeswitches) or
  784. (m_delphi in aktmodeswitches)) then
  785. begin
  786. abssym:=tabsolutesym.create(vs.realname,tt);
  787. abssym.fileinfo:=vs.fileinfo;
  788. abssym.abstyp:=toaddr;
  789. abssym.fieldoffset:=tordconstnode(pt).value;
  790. {$ifdef i386}
  791. abssym.absseg:=false;
  792. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  793. try_to_consume(_COLON) then
  794. begin
  795. pt.free;
  796. pt:=expr;
  797. if is_constintnode(pt) then
  798. begin
  799. abssym.fieldoffset:=abssym.fieldoffset shl 4+tordconstnode(pt).value;
  800. abssym.absseg:=true;
  801. end
  802. else
  803. Message(type_e_ordinal_expr_expected);
  804. end;
  805. {$endif i386}
  806. symtablestack.replace(vs,abssym);
  807. vs.free;
  808. end
  809. { variable }
  810. else
  811. begin
  812. { remove subscriptn before checking for loadn }
  813. hp:=pt;
  814. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  815. hp:=tunarynode(hp).left;
  816. if (hp.nodetype=loadn) then
  817. begin
  818. { we should check the result type of loadn }
  819. if not (tloadnode(hp).symtableentry.typ in [varsym,typedconstsym]) then
  820. Message(parser_e_absolute_only_to_var_or_const);
  821. abssym:=tabsolutesym.create(vs.realname,tt);
  822. abssym.fileinfo:=vs.fileinfo;
  823. abssym.abstyp:=tovar;
  824. abssym.ref:=node_to_symlist(pt);
  825. symtablestack.replace(vs,abssym);
  826. vs.free;
  827. end
  828. else
  829. Message(parser_e_absolute_only_to_var_or_const);
  830. end;
  831. if assigned(abssym) then
  832. begin
  833. { try to consume the hint directives with absolute symbols }
  834. dummysymoptions:=[];
  835. try_consume_hintdirective(dummysymoptions);
  836. abssym.symoptions := abssym.symoptions + dummysymoptions;
  837. end;
  838. pt.free;
  839. symdone:=true;
  840. end;
  841. { Process procvar directives before = and ; }
  842. if (tt.def.deftype=procvardef) and
  843. (tt.def.typesym=nil) and
  844. is_proc_directive(token,true) then
  845. begin
  846. newtype:=ttypesym.create('unnamed',tt);
  847. parse_var_proc_directives(tsym(newtype));
  848. newtype.restype.def:=nil;
  849. tt.def.typesym:=nil;
  850. newtype.free;
  851. end;
  852. { try to parse the hint directives }
  853. dummysymoptions:=[];
  854. try_consume_hintdirective(dummysymoptions);
  855. { Records and objects can't have default values }
  856. if is_record or is_object then
  857. begin
  858. { for a record there doesn't need to be a ; before the END or ) }
  859. if not(token in [_END,_RKLAMMER]) then
  860. consume(_SEMICOLON);
  861. end
  862. else
  863. begin
  864. { Handling of Delphi typed const = initialized vars }
  865. if (token=_EQUAL) and
  866. not(m_tp7 in aktmodeswitches) and
  867. (symtablestack.symtabletype<>parasymtable) then
  868. begin
  869. if (tt.def.deftype=procvardef) and
  870. (tt.def.typesym=nil) then
  871. begin
  872. { Add calling convention for procvar }
  873. handle_calling_convention(tprocvardef(tt.def));
  874. calc_parast(tprocvardef(tt.def));
  875. end;
  876. read_default_value(sc,tt,is_threadvar);
  877. { for locals we've created typedconstsym with a different name }
  878. if symtablestack.symtabletype<>localsymtable then
  879. symdone:=true;
  880. hasdefaultvalue:=true;
  881. end;
  882. consume(_SEMICOLON);
  883. end;
  884. { Support calling convention for procvars after semicolon }
  885. if not(hasdefaultvalue) and
  886. (tt.def.deftype=procvardef) and
  887. (tt.def.typesym=nil) then
  888. begin
  889. { Parse procvar directives after ; }
  890. if is_proc_directive(token,true) then
  891. begin
  892. newtype:=ttypesym.create('unnamed',tt);
  893. parse_var_proc_directives(tsym(newtype));
  894. newtype.restype.def:=nil;
  895. tt.def.typesym:=nil;
  896. newtype.free;
  897. end;
  898. { Add calling convention for procvar }
  899. handle_calling_convention(tprocvardef(tt.def));
  900. calc_parast(tprocvardef(tt.def));
  901. { Handling of Delphi typed const = initialized vars }
  902. if (token=_EQUAL) and
  903. not(is_record or is_object) and
  904. not(m_tp7 in aktmodeswitches) and
  905. (symtablestack.symtabletype<>parasymtable) then
  906. begin
  907. read_default_value(sc,tt,is_threadvar);
  908. consume(_SEMICOLON);
  909. symdone:=true;
  910. hasdefaultvalue:=true;
  911. end;
  912. end;
  913. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  914. if not symdone and not(is_record or is_object or is_threadvar) then
  915. begin
  916. if (
  917. (token=_ID) and
  918. (m_cvar_support in aktmodeswitches) and
  919. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
  920. ) or
  921. (
  922. (m_mac in aktmodeswitches) and
  923. ((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
  924. ) then
  925. begin
  926. { only allowed for one var }
  927. vs:=tvarsym(sc.first);
  928. if assigned(vs.listnext) then
  929. Message(parser_e_absolute_only_one_var);
  930. { set type of the var }
  931. vs.vartype:=tt;
  932. vs.symoptions := vs.symoptions + dummysymoptions;
  933. { defaults }
  934. is_dll:=false;
  935. is_cdecl:=false;
  936. extern_var:=false;
  937. export_var:=false;
  938. C_name:=sorg;
  939. semicolonatend:= false;
  940. { cdecl }
  941. if idtoken=_CVAR then
  942. begin
  943. consume(_CVAR);
  944. consume(_SEMICOLON);
  945. is_cdecl:=true;
  946. C_name:=target_info.Cprefix+sorg;
  947. end;
  948. { external }
  949. if idtoken=_EXTERNAL then
  950. begin
  951. consume(_EXTERNAL);
  952. extern_var:=true;
  953. semicolonatend:= true;
  954. end;
  955. { macpas specific handling due to some switches}
  956. if (m_mac in aktmodeswitches) then
  957. begin
  958. if (cs_external_var in aktlocalswitches) then
  959. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  960. is_cdecl:=true;
  961. C_name:=target_info.Cprefix+sorg;
  962. extern_var:=true;
  963. end
  964. else if (cs_externally_visible in aktlocalswitches) then
  965. begin {The effect of this is the same as if cvar has been given as directives.}
  966. is_cdecl:=true;
  967. C_name:=target_info.Cprefix+sorg;
  968. end;
  969. end;
  970. { export }
  971. if idtoken in [_EXPORT,_PUBLIC] then
  972. begin
  973. consume(_ID);
  974. if extern_var or
  975. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  976. Message(parser_e_not_external_and_export)
  977. else
  978. begin
  979. export_var:=true;
  980. semicolonatend:= true;
  981. end;
  982. end;
  983. { external and export need a name after when no cdecl is used }
  984. if not is_cdecl then
  985. begin
  986. { dll name ? }
  987. if (extern_var) and (idtoken<>_NAME) then
  988. begin
  989. is_dll:=true;
  990. dll_name:=get_stringconst;
  991. end;
  992. consume(_NAME);
  993. C_name:=get_stringconst;
  994. end;
  995. { consume the ; when export or external is used }
  996. if semicolonatend then
  997. consume(_SEMICOLON);
  998. { set some vars options }
  999. if is_dll then
  1000. include(vs.varoptions,vo_is_dll_var)
  1001. else
  1002. include(vs.varoptions,vo_is_C_var);
  1003. if (is_dll) and
  1004. (target_info.system = system_powerpc_darwin) then
  1005. C_Name := target_info.Cprefix+C_Name;
  1006. vs.set_mangledname(C_Name);
  1007. if export_var then
  1008. begin
  1009. inc(vs.refs);
  1010. include(vs.varoptions,vo_is_exported);
  1011. end;
  1012. if extern_var then
  1013. include(vs.varoptions,vo_is_external);
  1014. { insert in the datasegment when it is not external }
  1015. if (not extern_var) then
  1016. insertbssdata(vs);
  1017. { now we can insert it in the import lib if its a dll, or
  1018. add it to the externals }
  1019. if extern_var then
  1020. begin
  1021. if is_dll then
  1022. begin
  1023. if not(current_module.uses_imports) then
  1024. begin
  1025. current_module.uses_imports:=true;
  1026. importlib.preparelib(current_module.modulename^);
  1027. end;
  1028. importlib.importvariable(vs,C_name,dll_name);
  1029. end
  1030. else
  1031. if target_info.DllScanSupported then
  1032. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  1033. end;
  1034. symdone:=true;
  1035. end;
  1036. end;
  1037. { Check for STATIC directive }
  1038. if not symdone and (is_object) and
  1039. (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  1040. begin
  1041. include(current_object_option,sp_static);
  1042. insert_syms(sc,tt,false,dummysymoptions);
  1043. exclude(current_object_option,sp_static);
  1044. consume(_STATIC);
  1045. consume(_SEMICOLON);
  1046. symdone:=true;
  1047. end;
  1048. { insert it in the symtable, if not done yet }
  1049. if not symdone then
  1050. begin
  1051. { save object option, because we can turn of the sp_published }
  1052. if (sp_published in current_object_option) and
  1053. not(is_class(tt.def)) then
  1054. begin
  1055. Message(parser_e_cant_publish_that);
  1056. exclude(current_object_option,sp_published);
  1057. { recover by changing access type to public }
  1058. vs2:=tvarsym(sc.first);
  1059. while assigned (vs2) do
  1060. begin
  1061. exclude(vs2.symoptions,sp_published);
  1062. include(vs2.symoptions,sp_public);
  1063. vs2:=tvarsym(vs2.listnext);
  1064. end;
  1065. end
  1066. else
  1067. if (sp_published in current_object_option) and
  1068. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  1069. begin
  1070. Message(parser_e_only_publishable_classes_can__be_published);
  1071. exclude(current_object_option,sp_published);
  1072. end;
  1073. insert_syms(sc,tt,is_threadvar,dummysymoptions);
  1074. current_object_option:=old_current_object_option;
  1075. end;
  1076. end;
  1077. { Check for Case }
  1078. if is_record and (token=_CASE) then
  1079. begin
  1080. maxsize:=0;
  1081. maxalignment:=0;
  1082. consume(_CASE);
  1083. sorg:=orgpattern;
  1084. hs:=pattern;
  1085. searchsym(hs,srsym,srsymtable);
  1086. { may be only a type: }
  1087. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  1088. begin
  1089. { for records, don't search the recordsymtable for
  1090. the symbols of the types }
  1091. oldsymtablestack:=symtablestack;
  1092. symtablestack:=symtablestack.next;
  1093. read_type(casetype,'',true);
  1094. symtablestack:=oldsymtablestack;
  1095. end
  1096. else
  1097. begin
  1098. consume(_ID);
  1099. consume(_COLON);
  1100. { for records, don't search the recordsymtable for
  1101. the symbols of the types }
  1102. oldsymtablestack:=symtablestack;
  1103. symtablestack:=symtablestack.next;
  1104. read_type(casetype,'',true);
  1105. symtablestack:=oldsymtablestack;
  1106. vs:=tvarsym.create(sorg,vs_value,casetype);
  1107. tabstractrecordsymtable(symtablestack).insertfield(vs,true);
  1108. end;
  1109. if not(is_ordinal(casetype.def))
  1110. {$ifndef cpu64bit}
  1111. or is_64bitint(casetype.def)
  1112. {$endif cpu64bit}
  1113. then
  1114. Message(type_e_ordinal_expr_expected);
  1115. consume(_OF);
  1116. UnionSymtable:=trecordsymtable.create(aktpackrecords);
  1117. Unionsymtable.next:=symtablestack;
  1118. registerdef:=false;
  1119. UnionDef:=trecorddef.create(unionsymtable);
  1120. uniondef.isunion:=true;
  1121. if assigned(symtablestack.defowner) then
  1122. Uniondef.owner:=symtablestack.defowner.owner;
  1123. registerdef:=true;
  1124. startvarrecsize:=UnionSymtable.datasize;
  1125. startvarrecalign:=UnionSymtable.fieldalignment;
  1126. symtablestack:=UnionSymtable;
  1127. repeat
  1128. repeat
  1129. pt:=comp_expr(true);
  1130. if not(pt.nodetype=ordconstn) then
  1131. Message(parser_e_illegal_expression);
  1132. pt.free;
  1133. if token=_COMMA then
  1134. consume(_COMMA)
  1135. else
  1136. break;
  1137. until false;
  1138. consume(_COLON);
  1139. { read the vars }
  1140. consume(_LKLAMMER);
  1141. inc(variantrecordlevel);
  1142. if token<>_RKLAMMER then
  1143. read_var_decs(true,false,false);
  1144. dec(variantrecordlevel);
  1145. consume(_RKLAMMER);
  1146. { calculates maximal variant size }
  1147. maxsize:=max(maxsize,unionsymtable.datasize);
  1148. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1149. { the items of the next variant are overlayed }
  1150. unionsymtable.datasize:=startvarrecsize;
  1151. unionsymtable.fieldalignment:=startvarrecalign;
  1152. if (token<>_END) and (token<>_RKLAMMER) then
  1153. consume(_SEMICOLON)
  1154. else
  1155. break;
  1156. until (token=_END) or (token=_RKLAMMER);
  1157. { at last set the record size to that of the biggest variant }
  1158. unionsymtable.datasize:=maxsize;
  1159. unionsymtable.fieldalignment:=maxalignment;
  1160. uniontype.def:=uniondef;
  1161. uniontype.sym:=nil;
  1162. UnionSym:=tvarsym.create('$case',vs_value,uniontype);
  1163. symtablestack:=symtablestack.next;
  1164. { Align the offset where the union symtable is added }
  1165. if (trecordsymtable(symtablestack).usefieldalignment=-1) then
  1166. usedalign:=used_align(maxalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  1167. else
  1168. usedalign:=used_align(maxalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
  1169. offset:=align(trecordsymtable(symtablestack).datasize,usedalign);
  1170. trecordsymtable(symtablestack).datasize:=offset+unionsymtable.datasize;
  1171. if maxalignment>trecordsymtable(symtablestack).fieldalignment then
  1172. trecordsymtable(symtablestack).fieldalignment:=maxalignment;
  1173. trecordsymtable(symtablestack).insertunionst(Unionsymtable,offset);
  1174. Unionsym.owner:=nil;
  1175. unionsym.free;
  1176. uniondef.owner:=nil;
  1177. uniondef.free;
  1178. end;
  1179. block_type:=old_block_type;
  1180. current_object_option:=old_current_object_option;
  1181. { free the list }
  1182. sc.free;
  1183. end;
  1184. end.
  1185. {
  1186. $Log$
  1187. Revision 1.76 2004-07-14 23:19:22 olle
  1188. + added external facilities for macpas
  1189. Revision 1.75 2004/06/20 08:55:30 florian
  1190. * logs truncated
  1191. Revision 1.74 2004/06/16 20:07:09 florian
  1192. * dwarf branch merged
  1193. Revision 1.73 2004/04/11 12:38:16 peter
  1194. * fix calling convention problem when parsing default value before
  1195. the semicolon
  1196. Revision 1.72.2.5 2004/05/02 20:54:04 peter
  1197. * allow convert when choosing property function with index
  1198. Revision 1.72.2.4 2004/05/01 20:51:20 peter
  1199. * fix val code parameter
  1200. Revision 1.72.2.3 2004/04/28 19:55:52 peter
  1201. * new warning for ordinal-pointer when size is different
  1202. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1203. }