pdecvar.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  21. {$define UseUnionSymtable}
  22. interface
  23. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  24. implementation
  25. uses
  26. { common }
  27. cutils,cobjects,
  28. { global }
  29. globtype,globals,tokens,verbose,
  30. systems,
  31. { symtable }
  32. symconst,symbase,symtype,symdef,symsym,symtable,types,fmodule,
  33. { pass 1 }
  34. node,pass_1,
  35. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  36. { parser }
  37. scanner,
  38. pbase,pexpr,ptype,ptconst,pdecsub,
  39. { link }
  40. import;
  41. const
  42. variantrecordlevel : longint = 0;
  43. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  44. { reads the filed of a record into a }
  45. { symtablestack, if record=false }
  46. { variants are forbidden, so this procedure }
  47. { can be used to read object fields }
  48. { if absolute is true, ABSOLUTE and file }
  49. { types are allowed }
  50. { => the procedure is also used to read }
  51. { a sequence of variable declaration }
  52. procedure insert_syms(st : psymtable;sc : tidstringlist;tt : ttype;is_threadvar : boolean);
  53. { inserts the symbols of sc in st with def as definition or sym as ptypesym, sc is disposed }
  54. var
  55. s : string;
  56. filepos : tfileposinfo;
  57. ss : pvarsym;
  58. begin
  59. filepos:=akttokenpos;
  60. while not sc.empty do
  61. begin
  62. s:=sc.get(akttokenpos);
  63. ss:=new(pvarsym,init(s,tt));
  64. if is_threadvar then
  65. include(ss^.varoptions,vo_is_thread_var);
  66. st^.insert(ss);
  67. { static data fields are inserted in the globalsymtable }
  68. if (st^.symtabletype=objectsymtable) and
  69. (sp_static in current_object_option) then
  70. begin
  71. s:='$'+lower(st^.name^)+'_'+upper(s);
  72. st^.defowner^.owner^.insert(new(pvarsym,init(s,tt)));
  73. end;
  74. end;
  75. {$ifdef fixLeaksOnError}
  76. if strContStack.pop <> sc then
  77. writeln('problem with strContStack in pdecl (2)');
  78. {$endif fixLeaksOnError}
  79. sc.free;
  80. akttokenpos:=filepos;
  81. end;
  82. var
  83. sc : tidstringList;
  84. s : stringid;
  85. old_block_type : tblock_type;
  86. declarepos,storetokenpos : tfileposinfo;
  87. oldsymtablestack : psymtable;
  88. symdone : boolean;
  89. { to handle absolute }
  90. abssym : pabsolutesym;
  91. l : longint;
  92. code : integer;
  93. { c var }
  94. newtype : ptypesym;
  95. is_dll,
  96. is_gpc_name,is_cdecl,extern_aktvarsym,export_aktvarsym : boolean;
  97. old_current_object_option : tsymoptions;
  98. dll_name,
  99. C_name : string;
  100. tt,casetype : ttype;
  101. { Delphi initialized vars }
  102. pconstsym : ptypedconstsym;
  103. { maxsize contains the max. size of a variant }
  104. { startvarrec contains the start of the variant part of a record }
  105. maxsize,maxalignment,startvarrecalign,startvarrecsize : longint;
  106. pt : tnode;
  107. {$ifdef UseUnionSymtable}
  108. unionsymtable : psymtable;
  109. offset : longint;
  110. uniondef : precorddef;
  111. unionsym : pvarsym;
  112. uniontype : ttype;
  113. {$endif UseUnionSymtable}
  114. begin
  115. old_current_object_option:=current_object_option;
  116. { all variables are public if not in a object declaration }
  117. if not is_object then
  118. current_object_option:=[sp_public];
  119. old_block_type:=block_type;
  120. block_type:=bt_type;
  121. is_gpc_name:=false;
  122. { Force an expected ID error message }
  123. if not (token in [_ID,_CASE,_END]) then
  124. consume(_ID);
  125. { read vars }
  126. while (token=_ID) and
  127. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  128. begin
  129. C_name:=orgpattern;
  130. sc:=idlist;
  131. {$ifdef fixLeaksOnError}
  132. strContStack.push(sc);
  133. {$endif fixLeaksOnError}
  134. consume(_COLON);
  135. if (m_gpc in aktmodeswitches) and
  136. not(is_record or is_object or is_threadvar) and
  137. (token=_ID) and (orgpattern='__asmname__') then
  138. begin
  139. consume(_ID);
  140. C_name:=pattern;
  141. if token=_CCHAR then
  142. consume(_CCHAR)
  143. else
  144. consume(_CSTRING);
  145. Is_gpc_name:=true;
  146. end;
  147. { this is needed for Delphi mode at least
  148. but should be OK for all modes !! (PM) }
  149. ignore_equal:=true;
  150. if is_record or is_object then
  151. begin
  152. { for records, don't search the recordsymtable for
  153. the symbols of the types }
  154. oldsymtablestack:=symtablestack;
  155. symtablestack:=symtablestack^.next;
  156. read_type(tt,'');
  157. symtablestack:=oldsymtablestack;
  158. end
  159. else
  160. read_type(tt,'');
  161. if (variantrecordlevel>0) and tt.def^.needs_inittable then
  162. Message(parser_e_cant_use_inittable_here);
  163. ignore_equal:=false;
  164. symdone:=false;
  165. if is_gpc_name then
  166. begin
  167. storetokenpos:=akttokenpos;
  168. s:=sc.get(akttokenpos);
  169. if not sc.empty then
  170. Message(parser_e_absolute_only_one_var);
  171. {$ifdef fixLeaksOnError}
  172. if strContStack.pop <> sc then
  173. writeln('problem with strContStack in pdecl (3)');
  174. {$endif fixLeaksOnError}
  175. sc.free;
  176. aktvarsym:=new(pvarsym,init_C(s,target_os.Cprefix+C_name,tt));
  177. include(aktvarsym^.varoptions,vo_is_external);
  178. symtablestack^.insert(aktvarsym);
  179. akttokenpos:=storetokenpos;
  180. symdone:=true;
  181. end;
  182. { check for absolute }
  183. if not symdone and
  184. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  185. begin
  186. consume(_ABSOLUTE);
  187. { only allowed for one var }
  188. s:=sc.get(declarepos);
  189. if not sc.empty then
  190. Message(parser_e_absolute_only_one_var);
  191. {$ifdef fixLeaksOnError}
  192. if strContStack.pop <> sc then
  193. writeln('problem with strContStack in pdecl (4)');
  194. {$endif fixLeaksOnError}
  195. sc.free;
  196. { parse the rest }
  197. if token=_ID then
  198. begin
  199. getsym(pattern,true);
  200. consume(_ID);
  201. { support unit.variable }
  202. if srsym^.typ=unitsym then
  203. begin
  204. consume(_POINT);
  205. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  206. consume(_ID);
  207. end;
  208. { we should check the result type of srsym }
  209. if not (srsym^.typ in [varsym,typedconstsym,funcretsym]) then
  210. Message(parser_e_absolute_only_to_var_or_const);
  211. storetokenpos:=akttokenpos;
  212. akttokenpos:=declarepos;
  213. abssym:=new(pabsolutesym,init(s,tt));
  214. abssym^.abstyp:=tovar;
  215. abssym^.ref:=pstoredsym(srsym);
  216. symtablestack^.insert(abssym);
  217. akttokenpos:=storetokenpos;
  218. end
  219. else
  220. if (token=_CSTRING) or (token=_CCHAR) then
  221. begin
  222. storetokenpos:=akttokenpos;
  223. akttokenpos:=declarepos;
  224. abssym:=new(pabsolutesym,init(s,tt));
  225. s:=pattern;
  226. consume(token);
  227. abssym^.abstyp:=toasm;
  228. abssym^.asmname:=stringdup(s);
  229. symtablestack^.insert(abssym);
  230. akttokenpos:=storetokenpos;
  231. end
  232. else
  233. { absolute address ?!? }
  234. if token=_INTCONST then
  235. begin
  236. if (target_info.target=target_i386_go32v2) then
  237. begin
  238. storetokenpos:=akttokenpos;
  239. akttokenpos:=declarepos;
  240. abssym:=new(pabsolutesym,init(s,tt));
  241. abssym^.abstyp:=toaddr;
  242. abssym^.absseg:=false;
  243. s:=pattern;
  244. consume(_INTCONST);
  245. val(s,abssym^.address,code);
  246. if token=_COLON then
  247. begin
  248. consume(token);
  249. s:=pattern;
  250. consume(_INTCONST);
  251. val(s,l,code);
  252. abssym^.address:=abssym^.address shl 4+l;
  253. abssym^.absseg:=true;
  254. end;
  255. symtablestack^.insert(abssym);
  256. akttokenpos:=storetokenpos;
  257. end
  258. else
  259. Message(parser_e_absolute_only_to_var_or_const);
  260. end
  261. else
  262. Message(parser_e_absolute_only_to_var_or_const);
  263. symdone:=true;
  264. end;
  265. { Handling of Delphi typed const = initialized vars ! }
  266. { When should this be rejected ?
  267. - in parasymtable
  268. - in record or object
  269. - ... (PM) }
  270. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  271. not (symtablestack^.symtabletype in [parasymtable]) and
  272. not is_record and not is_object then
  273. begin
  274. storetokenpos:=akttokenpos;
  275. s:=sc.get(akttokenpos);
  276. if not sc.empty then
  277. Message(parser_e_initialized_only_one_var);
  278. pconstsym:=new(ptypedconstsym,inittype(s,tt,false));
  279. symtablestack^.insert(pconstsym);
  280. akttokenpos:=storetokenpos;
  281. consume(_EQUAL);
  282. readtypedconst(tt.def,pconstsym,false);
  283. symdone:=true;
  284. end;
  285. { for a record there doesn't need to be a ; before the END or ) }
  286. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  287. consume(_SEMICOLON);
  288. { procvar handling }
  289. if (tt.def^.deftype=procvardef) and (tt.def^.typesym=nil) then
  290. begin
  291. newtype:=new(ptypesym,init('unnamed',tt));
  292. parse_var_proc_directives(psym(newtype));
  293. newtype^.restype.def:=nil;
  294. tt.def^.typesym:=nil;
  295. dispose(newtype,done);
  296. end;
  297. { Check for variable directives }
  298. if not symdone and (token=_ID) then
  299. begin
  300. { Check for C Variable declarations }
  301. if (m_cvar_support in aktmodeswitches) and
  302. not(is_record or is_object or is_threadvar) and
  303. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  304. begin
  305. { only allowed for one var }
  306. s:=sc.get(declarepos);
  307. if not sc.empty then
  308. Message(parser_e_absolute_only_one_var);
  309. {$ifdef fixLeaksOnError}
  310. if strContStack.pop <> sc then
  311. writeln('problem with strContStack in pdecl (5)');
  312. {$endif fixLeaksOnError}
  313. sc.free;
  314. { defaults }
  315. is_dll:=false;
  316. is_cdecl:=false;
  317. extern_aktvarsym:=false;
  318. export_aktvarsym:=false;
  319. { cdecl }
  320. if idtoken=_CVAR then
  321. begin
  322. consume(_CVAR);
  323. consume(_SEMICOLON);
  324. is_cdecl:=true;
  325. C_name:=target_os.Cprefix+C_name;
  326. end;
  327. { external }
  328. if idtoken=_EXTERNAL then
  329. begin
  330. consume(_EXTERNAL);
  331. extern_aktvarsym:=true;
  332. end;
  333. { export }
  334. if idtoken in [_EXPORT,_PUBLIC] then
  335. begin
  336. consume(_ID);
  337. if extern_aktvarsym or
  338. (symtablestack^.symtabletype in [parasymtable,localsymtable]) then
  339. Message(parser_e_not_external_and_export)
  340. else
  341. export_aktvarsym:=true;
  342. end;
  343. { external and export need a name after when no cdecl is used }
  344. if not is_cdecl then
  345. begin
  346. { dll name ? }
  347. if (extern_aktvarsym) and (idtoken<>_NAME) then
  348. begin
  349. is_dll:=true;
  350. dll_name:=get_stringconst;
  351. end;
  352. consume(_NAME);
  353. C_name:=get_stringconst;
  354. end;
  355. { consume the ; when export or external is used }
  356. if extern_aktvarsym or export_aktvarsym then
  357. consume(_SEMICOLON);
  358. { insert in the symtable }
  359. storetokenpos:=akttokenpos;
  360. akttokenpos:=declarepos;
  361. if is_dll then
  362. aktvarsym:=new(pvarsym,init_dll(s,tt))
  363. else
  364. aktvarsym:=new(pvarsym,init_C(s,C_name,tt));
  365. { set some vars options }
  366. if export_aktvarsym then
  367. begin
  368. inc(aktvarsym^.refs);
  369. include(aktvarsym^.varoptions,vo_is_exported);
  370. end;
  371. if extern_aktvarsym then
  372. include(aktvarsym^.varoptions,vo_is_external);
  373. { insert in the stack/datasegment }
  374. symtablestack^.insert(aktvarsym);
  375. akttokenpos:=storetokenpos;
  376. { now we can insert it in the import lib if its a dll, or
  377. add it to the externals }
  378. if extern_aktvarsym then
  379. begin
  380. if is_dll then
  381. begin
  382. if not(current_module.uses_imports) then
  383. begin
  384. current_module.uses_imports:=true;
  385. importlib.preparelib(current_module.modulename^);
  386. end;
  387. importlib.importvariable(aktvarsym^.mangledname,dll_name,C_name)
  388. end
  389. end;
  390. symdone:=true;
  391. end
  392. else
  393. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  394. begin
  395. include(current_object_option,sp_static);
  396. insert_syms(symtablestack,sc,tt,false);
  397. exclude(current_object_option,sp_static);
  398. consume(_STATIC);
  399. consume(_SEMICOLON);
  400. symdone:=true;
  401. end;
  402. end;
  403. { insert it in the symtable, if not done yet }
  404. if not symdone then
  405. begin
  406. { save object option, because we can turn of the sp_published }
  407. if (sp_published in current_object_option) and
  408. not(is_class(tt.def)) then
  409. begin
  410. Message(parser_e_cant_publish_that);
  411. exclude(current_object_option,sp_published);
  412. end
  413. else
  414. if (sp_published in current_object_option) and
  415. not(oo_can_have_published in pobjectdef(tt.def)^.objectoptions) then
  416. begin
  417. Message(parser_e_only_publishable_classes_can__be_published);
  418. exclude(current_object_option,sp_published);
  419. end;
  420. insert_syms(symtablestack,sc,tt,is_threadvar);
  421. current_object_option:=old_current_object_option;
  422. end;
  423. end;
  424. { Check for Case }
  425. if is_record and (token=_CASE) then
  426. begin
  427. maxsize:=0;
  428. maxalignment:=0;
  429. consume(_CASE);
  430. s:=pattern;
  431. getsym(s,false);
  432. { may be only a type: }
  433. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  434. begin
  435. { for records, don't search the recordsymtable for
  436. the symbols of the types }
  437. oldsymtablestack:=symtablestack;
  438. symtablestack:=symtablestack^.next;
  439. read_type(casetype,'');
  440. symtablestack:=oldsymtablestack;
  441. end
  442. else
  443. begin
  444. consume(_ID);
  445. consume(_COLON);
  446. { for records, don't search the recordsymtable for
  447. the symbols of the types }
  448. oldsymtablestack:=symtablestack;
  449. symtablestack:=symtablestack^.next;
  450. read_type(casetype,'');
  451. symtablestack:=oldsymtablestack;
  452. symtablestack^.insert(new(pvarsym,init(s,casetype)));
  453. end;
  454. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  455. Message(type_e_ordinal_expr_expected);
  456. consume(_OF);
  457. {$ifdef UseUnionSymtable}
  458. UnionSymtable:=new(pstoredsymtable,init(recordsymtable));
  459. UnionSymtable^.next:=symtablestack;
  460. registerdef:=false;
  461. UnionDef:=new(precorddef,init(unionsymtable));
  462. registerdef:=true;
  463. symtablestack:=UnionSymtable;
  464. {$endif UseUnionSymtable}
  465. startvarrecsize:=symtablestack^.datasize;
  466. startvarrecalign:=symtablestack^.dataalignment;
  467. repeat
  468. repeat
  469. pt:=comp_expr(true);
  470. do_firstpass(pt);
  471. if not(pt.nodetype=ordconstn) then
  472. Message(cg_e_illegal_expression);
  473. pt.free;
  474. if token=_COMMA then
  475. consume(_COMMA)
  476. else
  477. break;
  478. until false;
  479. consume(_COLON);
  480. { read the vars }
  481. consume(_LKLAMMER);
  482. inc(variantrecordlevel);
  483. if token<>_RKLAMMER then
  484. read_var_decs(true,false,false);
  485. dec(variantrecordlevel);
  486. consume(_RKLAMMER);
  487. { calculates maximal variant size }
  488. maxsize:=max(maxsize,symtablestack^.datasize);
  489. maxalignment:=max(maxalignment,symtablestack^.dataalignment);
  490. { the items of the next variant are overlayed }
  491. symtablestack^.datasize:=startvarrecsize;
  492. symtablestack^.dataalignment:=startvarrecalign;
  493. if (token<>_END) and (token<>_RKLAMMER) then
  494. consume(_SEMICOLON)
  495. else
  496. break;
  497. until (token=_END) or (token=_RKLAMMER);
  498. { at last set the record size to that of the biggest variant }
  499. symtablestack^.datasize:=maxsize;
  500. symtablestack^.dataalignment:=maxalignment;
  501. {$ifdef UseUnionSymtable}
  502. uniontype.def:=uniondef;
  503. uniontype.sym:=nil;
  504. UnionSym:=new(pvarsym,init('case',uniontype));
  505. symtablestack:=symtablestack^.next;
  506. { we do NOT call symtablestack^.insert
  507. on purpose PM }
  508. offset:=align_from_size(symtablestack^.datasize,maxalignment);
  509. symtablestack^.datasize:=offset+unionsymtable^.datasize;
  510. if maxalignment>symtablestack^.dataalignment then
  511. symtablestack^.dataalignment:=maxalignment;
  512. pstoredsymtable(UnionSymtable)^.Insert_in(symtablestack,offset);
  513. UnionSym^.owner:=nil;
  514. dispose(unionsym,done);
  515. dispose(uniondef,done);
  516. {$endif UseUnionSymtable}
  517. end;
  518. block_type:=old_block_type;
  519. current_object_option:=old_current_object_option;
  520. end;
  521. end.
  522. {
  523. $Log$
  524. Revision 1.8 2001-02-20 18:35:35 peter
  525. * same fix for objects and classes
  526. Revision 1.7 2001/02/20 11:19:45 marco
  527. * Fix passing tvarrec to array of const
  528. Revision 1.6 2000/12/25 00:07:27 peter
  529. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  530. tlinkedlist objects)
  531. Revision 1.5 2000/12/17 14:00:18 peter
  532. * fixed static variables
  533. Revision 1.4 2000/11/29 00:30:36 florian
  534. * unused units removed from uses clause
  535. * some changes for widestrings
  536. Revision 1.3 2000/11/04 14:25:20 florian
  537. + merged Attila's changes for interfaces, not tested yet
  538. Revision 1.2 2000/10/31 22:02:49 peter
  539. * symtable splitted, no real code changes
  540. Revision 1.1 2000/10/14 10:14:51 peter
  541. * moehrendorf oct 2000 rewrite
  542. }