pdecvar.pas 22 KB

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