pdecvar.pas 21 KB

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