pdecl.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does declaration (but not type) parsing for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecl;
  19. {$define UseUnionSymtable}
  20. interface
  21. uses
  22. globtype,tokens,globals,symtable;
  23. procedure parameter_dec(aktprocdef:pabstractprocdef);
  24. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  25. { reads the declaration blocks }
  26. procedure read_declarations(islibrary : boolean);
  27. { reads declarations in the interface part of a unit }
  28. procedure read_interface_declarations;
  29. implementation
  30. uses
  31. cobjects,scanner,
  32. symconst,aasm,tree,pass_1,strings,
  33. files,types,verbose,systems,import,
  34. cpubase
  35. {$ifndef newcg}
  36. ,tccnv
  37. {$endif newcg}
  38. {$ifdef GDB}
  39. ,gdb
  40. {$endif GDB}
  41. { parser specific stuff }
  42. ,pbase,ptconst,pexpr,ptype,psub,pexports
  43. { processor specific stuff }
  44. { codegen }
  45. {$ifdef newcg}
  46. ,cgbase
  47. {$else}
  48. ,hcodegen
  49. {$endif}
  50. ,hcgdata
  51. ;
  52. procedure parameter_dec(aktprocdef:pabstractprocdef);
  53. {
  54. handle_procvar needs the same changes
  55. }
  56. var
  57. is_procvar : boolean;
  58. sc : Pstringcontainer;
  59. s : string;
  60. storetokenpos : tfileposinfo;
  61. tt : ttype;
  62. hvs,
  63. vs : Pvarsym;
  64. hs1,hs2 : string;
  65. varspez : Tvarspez;
  66. inserthigh : boolean;
  67. begin
  68. { parsing a proc or procvar ? }
  69. is_procvar:=(aktprocdef^.deftype=procvardef);
  70. consume(_LKLAMMER);
  71. inc(testcurobject);
  72. repeat
  73. if try_to_consume(_VAR) then
  74. varspez:=vs_var
  75. else
  76. if try_to_consume(_CONST) then
  77. varspez:=vs_const
  78. else
  79. varspez:=vs_value;
  80. inserthigh:=false;
  81. tt.reset;
  82. if idtoken=_SELF then
  83. begin
  84. { only allowed in procvars and class methods }
  85. if is_procvar or
  86. (assigned(procinfo^._class) and procinfo^._class^.is_class) then
  87. begin
  88. if not is_procvar then
  89. begin
  90. {$ifndef UseNiceNames}
  91. hs2:=hs2+'$'+'self';
  92. {$else UseNiceNames}
  93. hs2:=hs2+tostr(length('self'))+'self';
  94. {$endif UseNiceNames}
  95. vs:=new(Pvarsym,initdef('@',procinfo^._class));
  96. vs^.varspez:=vs_var;
  97. { insert the sym in the parasymtable }
  98. pprocdef(aktprocdef)^.parast^.insert(vs);
  99. {$ifdef INCLUDEOK}
  100. include(aktprocdef^.procoptions,po_containsself);
  101. {$else}
  102. aktprocdef^.procoptions:=aktprocdef^.procoptions+[po_containsself];
  103. {$endif}
  104. inc(procinfo^.selfpointer_offset,vs^.address);
  105. end;
  106. consume(idtoken);
  107. consume(_COLON);
  108. single_type(tt,hs1,false);
  109. aktprocdef^.concatpara(tt,vs_value);
  110. { check the types for procedures only }
  111. if not is_procvar then
  112. CheckTypes(tt.def,procinfo^._class);
  113. end
  114. else
  115. consume(_ID);
  116. end
  117. else
  118. begin
  119. { read identifiers }
  120. sc:=idlist;
  121. {$ifdef fixLeaksOnError}
  122. strContStack.push(sc);
  123. {$endif fixLeaksOnError}
  124. { read type declaration, force reading for value and const paras }
  125. if (token=_COLON) or (varspez=vs_value) then
  126. begin
  127. consume(_COLON);
  128. { check for an open array }
  129. if token=_ARRAY then
  130. begin
  131. consume(_ARRAY);
  132. consume(_OF);
  133. { define range and type of range }
  134. tt.setdef(new(Parraydef,init(0,-1,s32bitdef)));
  135. { array of const ? }
  136. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  137. begin
  138. consume(_CONST);
  139. srsym:=nil;
  140. getsymonlyin(systemunit,'TVARREC');
  141. if not assigned(srsym) then
  142. InternalError(1234124);
  143. Parraydef(tt.def)^.elementtype:=ptypesym(srsym)^.restype;
  144. Parraydef(tt.def)^.IsArrayOfConst:=true;
  145. hs1:='array_of_const';
  146. end
  147. else
  148. begin
  149. { define field type }
  150. single_type(parraydef(tt.def)^.elementtype,hs1,false);
  151. hs1:='array_of_'+hs1;
  152. end;
  153. inserthigh:=true;
  154. end
  155. { open string ? }
  156. else if (varspez=vs_var) and
  157. (
  158. (
  159. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  160. (cs_openstring in aktmoduleswitches) and
  161. not(cs_ansistrings in aktlocalswitches)
  162. ) or
  163. (idtoken=_OPENSTRING)) then
  164. begin
  165. consume(token);
  166. tt.setdef(openshortstringdef);
  167. hs1:='openstring';
  168. inserthigh:=true;
  169. end
  170. { everything else }
  171. else
  172. single_type(tt,hs1,false);
  173. end
  174. else
  175. begin
  176. {$ifndef UseNiceNames}
  177. hs1:='$$$';
  178. {$else UseNiceNames}
  179. hs1:='var';
  180. {$endif UseNiceNames}
  181. tt.setdef(cformaldef);
  182. end;
  183. if not is_procvar then
  184. hs2:=pprocdef(aktprocdef)^.mangledname;
  185. storetokenpos:=tokenpos;
  186. while not sc^.empty do
  187. begin
  188. s:=sc^.get_with_tokeninfo(tokenpos);
  189. aktprocdef^.concatpara(tt,varspez);
  190. { For proc vars we only need the definitions }
  191. if not is_procvar then
  192. begin
  193. {$ifndef UseNiceNames}
  194. hs2:=hs2+'$'+hs1;
  195. {$else UseNiceNames}
  196. hs2:=hs2+tostr(length(hs1))+hs1;
  197. {$endif UseNiceNames}
  198. vs:=new(pvarsym,init(s,tt));
  199. vs^.varspez:=varspez;
  200. { we have to add this to avoid var param to be in registers !!!}
  201. if (varspez in [vs_var,vs_const]) and push_addr_param(tt.def) then
  202. include(vs^.varoptions,vo_regable);
  203. { insert the sym in the parasymtable }
  204. pprocdef(aktprocdef)^.parast^.insert(vs);
  205. { do we need a local copy? Then rename the varsym, do this after the
  206. insert so the dup id checking is done correctly }
  207. if (varspez=vs_value) and
  208. push_addr_param(tt.def) and
  209. not(is_open_array(tt.def) or is_array_of_const(tt.def)) then
  210. pprocdef(aktprocdef)^.parast^.rename(vs^.name,'val'+vs^.name);
  211. { also need to push a high value? }
  212. if inserthigh then
  213. begin
  214. hvs:=new(Pvarsym,initdef('high'+s,s32bitdef));
  215. hvs^.varspez:=vs_const;
  216. pprocdef(aktprocdef)^.parast^.insert(hvs);
  217. end;
  218. end;
  219. end;
  220. {$ifdef fixLeaksOnError}
  221. if PStringContainer(strContStack.pop) <> sc then
  222. writeln('problem with strContStack in pdecl (1)');
  223. {$endif fixLeaksOnError}
  224. dispose(sc,done);
  225. tokenpos:=storetokenpos;
  226. end;
  227. { set the new mangled name }
  228. if not is_procvar then
  229. pprocdef(aktprocdef)^.setmangledname(hs2);
  230. until not try_to_consume(_SEMICOLON);
  231. dec(testcurobject);
  232. consume(_RKLAMMER);
  233. end;
  234. const
  235. variantrecordlevel : longint = 0;
  236. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  237. { reads the filed of a record into a }
  238. { symtablestack, if record=false }
  239. { variants are forbidden, so this procedure }
  240. { can be used to read object fields }
  241. { if absolute is true, ABSOLUTE and file }
  242. { types are allowed }
  243. { => the procedure is also used to read }
  244. { a sequence of variable declaration }
  245. procedure insert_syms(st : psymtable;sc : pstringcontainer;tt : ttype;is_threadvar : boolean);
  246. { inserts the symbols of sc in st with def as definition or sym as ptypesym, sc is disposed }
  247. var
  248. s : string;
  249. filepos : tfileposinfo;
  250. ss : pvarsym;
  251. begin
  252. filepos:=tokenpos;
  253. while not sc^.empty do
  254. begin
  255. s:=sc^.get_with_tokeninfo(tokenpos);
  256. ss:=new(pvarsym,init(s,tt));
  257. if is_threadvar then
  258. {$ifdef INCLUDEOK}
  259. include(ss^.varoptions,vo_is_thread_var);
  260. {$else}
  261. ss^.varoptions:=ss^.varoptions+[vo_is_thread_var];
  262. {$endif}
  263. st^.insert(ss);
  264. { static data fields are inserted in the globalsymtable }
  265. if (st^.symtabletype=objectsymtable) and
  266. (sp_static in current_object_option) then
  267. begin
  268. s:=lower(st^.name^)+'_'+s;
  269. st^.defowner^.owner^.insert(new(pvarsym,init(s,tt)));
  270. end;
  271. end;
  272. {$ifdef fixLeaksOnError}
  273. if strContStack.pop <> sc then
  274. writeln('problem with strContStack in pdecl (2)');
  275. {$endif fixLeaksOnError}
  276. dispose(sc,done);
  277. tokenpos:=filepos;
  278. end;
  279. var
  280. sc : pstringcontainer;
  281. s : stringid;
  282. old_block_type : tblock_type;
  283. declarepos,storetokenpos : tfileposinfo;
  284. symdone : boolean;
  285. { to handle absolute }
  286. abssym : pabsolutesym;
  287. l : longint;
  288. code : integer;
  289. { c var }
  290. newtype : ptypesym;
  291. is_dll,
  292. is_gpc_name,is_cdecl,extern_aktvarsym,export_aktvarsym : boolean;
  293. old_current_object_option : tsymoptions;
  294. dll_name,
  295. C_name : string;
  296. tt,casetype : ttype;
  297. { Delphi initialized vars }
  298. pconstsym : ptypedconstsym;
  299. { maxsize contains the max. size of a variant }
  300. { startvarrec contains the start of the variant part of a record }
  301. maxsize,maxalignment,startvarrecalign,startvarrecsize : longint;
  302. pt : ptree;
  303. {$ifdef UseUnionSymtable}
  304. unionsymtable : psymtable;
  305. offset : longint;
  306. uniondef : precorddef;
  307. unionsym : pvarsym;
  308. uniontype : ttype;
  309. {$endif UseUnionSymtable}
  310. begin
  311. old_current_object_option:=current_object_option;
  312. { all variables are public if not in a object declaration }
  313. if not is_object then
  314. current_object_option:=[sp_public];
  315. old_block_type:=block_type;
  316. block_type:=bt_type;
  317. is_gpc_name:=false;
  318. { Force an expected ID error message }
  319. if not (token in [_ID,_CASE,_END]) then
  320. consume(_ID);
  321. { read vars }
  322. while (token=_ID) and
  323. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  324. begin
  325. C_name:=orgpattern;
  326. sc:=idlist;
  327. {$ifdef fixLeaksOnError}
  328. strContStack.push(sc);
  329. {$endif fixLeaksOnError}
  330. consume(_COLON);
  331. if (m_gpc in aktmodeswitches) and
  332. not(is_record or is_object or is_threadvar) and
  333. (token=_ID) and (orgpattern='__asmname__') then
  334. begin
  335. consume(_ID);
  336. C_name:=pattern;
  337. if token=_CCHAR then
  338. consume(_CCHAR)
  339. else
  340. consume(_CSTRING);
  341. Is_gpc_name:=true;
  342. end;
  343. { this is needed for Delphi mode at least
  344. but should be OK for all modes !! (PM) }
  345. ignore_equal:=true;
  346. read_type(tt,'');
  347. if (variantrecordlevel>0) and tt.def^.needs_inittable then
  348. Message(parser_e_cant_use_inittable_here);
  349. ignore_equal:=false;
  350. symdone:=false;
  351. if is_gpc_name then
  352. begin
  353. storetokenpos:=tokenpos;
  354. s:=sc^.get_with_tokeninfo(tokenpos);
  355. if not sc^.empty then
  356. Message(parser_e_absolute_only_one_var);
  357. {$ifdef fixLeaksOnError}
  358. if strContStack.pop <> sc then
  359. writeln('problem with strContStack in pdecl (3)');
  360. {$endif fixLeaksOnError}
  361. dispose(sc,done);
  362. aktvarsym:=new(pvarsym,init_C(s,target_os.Cprefix+C_name,tt));
  363. {$ifdef INCLUDEOK}
  364. include(aktvarsym^.varoptions,vo_is_external);
  365. {$else}
  366. aktvarsym^.varoptions:=aktvarsym^.varoptions+[vo_is_external];
  367. {$endif}
  368. symtablestack^.insert(aktvarsym);
  369. tokenpos:=storetokenpos;
  370. symdone:=true;
  371. end;
  372. { check for absolute }
  373. if not symdone and
  374. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  375. begin
  376. consume(_ABSOLUTE);
  377. { only allowed for one var }
  378. s:=sc^.get_with_tokeninfo(declarepos);
  379. if not sc^.empty then
  380. Message(parser_e_absolute_only_one_var);
  381. {$ifdef fixLeaksOnError}
  382. if strContStack.pop <> sc then
  383. writeln('problem with strContStack in pdecl (4)');
  384. {$endif fixLeaksOnError}
  385. dispose(sc,done);
  386. { parse the rest }
  387. if token=_ID then
  388. begin
  389. getsym(pattern,true);
  390. consume(_ID);
  391. { support unit.variable }
  392. if srsym^.typ=unitsym then
  393. begin
  394. consume(_POINT);
  395. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  396. consume(_ID);
  397. end;
  398. { we should check the result type of srsym }
  399. if not (srsym^.typ in [varsym,typedconstsym]) then
  400. Message(parser_e_absolute_only_to_var_or_const);
  401. storetokenpos:=tokenpos;
  402. tokenpos:=declarepos;
  403. abssym:=new(pabsolutesym,init(s,tt));
  404. abssym^.abstyp:=tovar;
  405. abssym^.ref:=srsym;
  406. symtablestack^.insert(abssym);
  407. tokenpos:=storetokenpos;
  408. end
  409. else
  410. if (token=_CSTRING) or (token=_CCHAR) then
  411. begin
  412. storetokenpos:=tokenpos;
  413. tokenpos:=declarepos;
  414. abssym:=new(pabsolutesym,init(s,tt));
  415. s:=pattern;
  416. consume(token);
  417. abssym^.abstyp:=toasm;
  418. abssym^.asmname:=stringdup(s);
  419. symtablestack^.insert(abssym);
  420. tokenpos:=storetokenpos;
  421. end
  422. else
  423. { absolute address ?!? }
  424. if token=_INTCONST then
  425. begin
  426. if (target_info.target=target_i386_go32v2) then
  427. begin
  428. storetokenpos:=tokenpos;
  429. tokenpos:=declarepos;
  430. abssym:=new(pabsolutesym,init(s,tt));
  431. abssym^.abstyp:=toaddr;
  432. abssym^.absseg:=false;
  433. s:=pattern;
  434. consume(_INTCONST);
  435. val(s,abssym^.address,code);
  436. if token=_COLON then
  437. begin
  438. consume(token);
  439. s:=pattern;
  440. consume(_INTCONST);
  441. val(s,l,code);
  442. abssym^.address:=abssym^.address shl 4+l;
  443. abssym^.absseg:=true;
  444. end;
  445. symtablestack^.insert(abssym);
  446. tokenpos:=storetokenpos;
  447. end
  448. else
  449. Message(parser_e_absolute_only_to_var_or_const);
  450. end
  451. else
  452. Message(parser_e_absolute_only_to_var_or_const);
  453. symdone:=true;
  454. end;
  455. { Handling of Delphi typed const = initialized vars ! }
  456. { When should this be rejected ?
  457. - in parasymtable
  458. - in record or object
  459. - ... (PM) }
  460. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  461. not (symtablestack^.symtabletype in [parasymtable]) and
  462. not is_record and not is_object then
  463. begin
  464. storetokenpos:=tokenpos;
  465. s:=sc^.get_with_tokeninfo(tokenpos);
  466. if not sc^.empty then
  467. Message(parser_e_initialized_only_one_var);
  468. pconstsym:=new(ptypedconstsym,inittype(s,tt,false));
  469. symtablestack^.insert(pconstsym);
  470. tokenpos:=storetokenpos;
  471. consume(_EQUAL);
  472. readtypedconst(tt.def,pconstsym,false);
  473. symdone:=true;
  474. end;
  475. { for a record there doesn't need to be a ; before the END or ) }
  476. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  477. consume(_SEMICOLON);
  478. { procvar handling }
  479. if (tt.def^.deftype=procvardef) and (tt.def^.typesym=nil) then
  480. begin
  481. newtype:=new(ptypesym,init('unnamed',tt));
  482. parse_var_proc_directives(psym(newtype));
  483. newtype^.restype.def:=nil;
  484. tt.def^.typesym:=nil;
  485. dispose(newtype,done);
  486. end;
  487. { Check for variable directives }
  488. if not symdone and (token=_ID) then
  489. begin
  490. { Check for C Variable declarations }
  491. if (m_cvar_support in aktmodeswitches) and
  492. not(is_record or is_object or is_threadvar) and
  493. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  494. begin
  495. { only allowed for one var }
  496. s:=sc^.get_with_tokeninfo(declarepos);
  497. if not sc^.empty then
  498. Message(parser_e_absolute_only_one_var);
  499. {$ifdef fixLeaksOnError}
  500. if strContStack.pop <> sc then
  501. writeln('problem with strContStack in pdecl (5)');
  502. {$endif fixLeaksOnError}
  503. dispose(sc,done);
  504. { defaults }
  505. is_dll:=false;
  506. is_cdecl:=false;
  507. extern_aktvarsym:=false;
  508. export_aktvarsym:=false;
  509. { cdecl }
  510. if idtoken=_CVAR then
  511. begin
  512. consume(_CVAR);
  513. consume(_SEMICOLON);
  514. is_cdecl:=true;
  515. C_name:=target_os.Cprefix+C_name;
  516. end;
  517. { external }
  518. if idtoken=_EXTERNAL then
  519. begin
  520. consume(_EXTERNAL);
  521. extern_aktvarsym:=true;
  522. end;
  523. { export }
  524. if idtoken in [_EXPORT,_PUBLIC] then
  525. begin
  526. consume(_ID);
  527. if extern_aktvarsym or
  528. (symtablestack^.symtabletype in [parasymtable,localsymtable]) then
  529. Message(parser_e_not_external_and_export)
  530. else
  531. export_aktvarsym:=true;
  532. end;
  533. { external and export need a name after when no cdecl is used }
  534. if not is_cdecl then
  535. begin
  536. { dll name ? }
  537. if (extern_aktvarsym) and (idtoken<>_NAME) then
  538. begin
  539. is_dll:=true;
  540. dll_name:=get_stringconst;
  541. end;
  542. consume(_NAME);
  543. C_name:=get_stringconst;
  544. end;
  545. { consume the ; when export or external is used }
  546. if extern_aktvarsym or export_aktvarsym then
  547. consume(_SEMICOLON);
  548. { insert in the symtable }
  549. storetokenpos:=tokenpos;
  550. tokenpos:=declarepos;
  551. if is_dll then
  552. aktvarsym:=new(pvarsym,init_dll(s,tt))
  553. else
  554. aktvarsym:=new(pvarsym,init_C(s,C_name,tt));
  555. { set some vars options }
  556. if export_aktvarsym then
  557. begin
  558. inc(aktvarsym^.refs);
  559. {$ifdef INCLUDEOK}
  560. include(aktvarsym^.varoptions,vo_is_exported);
  561. {$else}
  562. aktvarsym^.varoptions:=aktvarsym^.varoptions+[vo_is_exported];
  563. {$endif}
  564. end;
  565. if extern_aktvarsym then
  566. {$ifdef INCLUDEOK}
  567. include(aktvarsym^.varoptions,vo_is_external);
  568. {$else}
  569. aktvarsym^.varoptions:=aktvarsym^.varoptions+[vo_is_external];
  570. {$endif}
  571. { insert in the stack/datasegment }
  572. symtablestack^.insert(aktvarsym);
  573. tokenpos:=storetokenpos;
  574. { now we can insert it in the import lib if its a dll, or
  575. add it to the externals }
  576. if extern_aktvarsym then
  577. begin
  578. if is_dll then
  579. begin
  580. if not(current_module^.uses_imports) then
  581. begin
  582. current_module^.uses_imports:=true;
  583. importlib^.preparelib(current_module^.modulename^);
  584. end;
  585. importlib^.importvariable(aktvarsym^.mangledname,dll_name,C_name)
  586. end
  587. end;
  588. symdone:=true;
  589. end
  590. else
  591. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  592. begin
  593. {$ifdef INCLUDEOK}
  594. include(current_object_option,sp_static);
  595. {$else}
  596. current_object_option:=current_object_option+[sp_static];
  597. {$endif}
  598. insert_syms(symtablestack,sc,tt,false);
  599. {$ifdef INCLUDEOK}
  600. exclude(current_object_option,sp_static);
  601. {$else}
  602. current_object_option:=current_object_option-[sp_static];
  603. {$endif}
  604. consume(_STATIC);
  605. consume(_SEMICOLON);
  606. symdone:=true;
  607. end;
  608. end;
  609. { insert it in the symtable, if not done yet }
  610. if not symdone then
  611. begin
  612. { save object option, because we can turn of the sp_published }
  613. if (sp_published in current_object_option) and
  614. (not((tt.def^.deftype=objectdef) and (pobjectdef(tt.def)^.is_class))) then
  615. begin
  616. Message(parser_e_cant_publish_that);
  617. exclude(current_object_option,sp_published);
  618. end
  619. else
  620. if (sp_published in current_object_option) and
  621. not(oo_can_have_published in pobjectdef(tt.def)^.objectoptions) then
  622. begin
  623. Message(parser_e_only_publishable_classes_can__be_published);
  624. exclude(current_object_option,sp_published);
  625. end;
  626. insert_syms(symtablestack,sc,tt,is_threadvar);
  627. current_object_option:=old_current_object_option;
  628. end;
  629. end;
  630. { Check for Case }
  631. if is_record and (token=_CASE) then
  632. begin
  633. maxsize:=0;
  634. maxalignment:=0;
  635. consume(_CASE);
  636. s:=pattern;
  637. getsym(s,false);
  638. { may be only a type: }
  639. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  640. read_type(casetype,'')
  641. else
  642. begin
  643. consume(_ID);
  644. consume(_COLON);
  645. read_type(casetype,'');
  646. symtablestack^.insert(new(pvarsym,init(s,casetype)));
  647. end;
  648. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  649. Message(type_e_ordinal_expr_expected);
  650. consume(_OF);
  651. {$ifdef UseUnionSymtable}
  652. UnionSymtable:=new(psymtable,init(recordsymtable));
  653. UnionSymtable^.next:=symtablestack;
  654. registerdef:=false;
  655. UnionDef:=new(precorddef,init(unionsymtable));
  656. registerdef:=true;
  657. symtablestack:=UnionSymtable;
  658. {$endif UseUnionSymtable}
  659. startvarrecsize:=symtablestack^.datasize;
  660. startvarrecalign:=symtablestack^.dataalignment;
  661. repeat
  662. repeat
  663. pt:=comp_expr(true);
  664. do_firstpass(pt);
  665. if not(pt^.treetype=ordconstn) then
  666. Message(cg_e_illegal_expression);
  667. disposetree(pt);
  668. if token=_COMMA then
  669. consume(_COMMA)
  670. else
  671. break;
  672. until false;
  673. consume(_COLON);
  674. { read the vars }
  675. consume(_LKLAMMER);
  676. inc(variantrecordlevel);
  677. if token<>_RKLAMMER then
  678. read_var_decs(true,false,false);
  679. dec(variantrecordlevel);
  680. consume(_RKLAMMER);
  681. { calculates maximal variant size }
  682. maxsize:=max(maxsize,symtablestack^.datasize);
  683. maxalignment:=max(maxalignment,symtablestack^.dataalignment);
  684. { the items of the next variant are overlayed }
  685. symtablestack^.datasize:=startvarrecsize;
  686. symtablestack^.dataalignment:=startvarrecalign;
  687. if (token<>_END) and (token<>_RKLAMMER) then
  688. consume(_SEMICOLON)
  689. else
  690. break;
  691. until (token=_END) or (token=_RKLAMMER);
  692. { at last set the record size to that of the biggest variant }
  693. symtablestack^.datasize:=maxsize;
  694. symtablestack^.dataalignment:=maxalignment;
  695. {$ifdef UseUnionSymtable}
  696. uniontype.def:=uniondef;
  697. uniontype.sym:=nil;
  698. UnionSym:=new(pvarsym,init('case',uniontype));
  699. symtablestack:=symtablestack^.next;
  700. { we do NOT call symtablestack^.insert
  701. on purpose PM }
  702. offset:=align_from_size(symtablestack^.datasize,maxalignment);
  703. symtablestack^.datasize:=offset+unionsymtable^.datasize;
  704. if maxalignment>symtablestack^.dataalignment then
  705. symtablestack^.dataalignment:=maxalignment;
  706. UnionSymtable^.Insert_in(symtablestack,offset);
  707. UnionSym^.owner:=nil;
  708. dispose(unionsym,done);
  709. dispose(uniondef,done);
  710. {$endif UseUnionSymtable}
  711. end;
  712. block_type:=old_block_type;
  713. current_object_option:=old_current_object_option;
  714. end;
  715. procedure const_dec;
  716. var
  717. name : stringid;
  718. p : ptree;
  719. tt : ttype;
  720. sym : psym;
  721. storetokenpos,filepos : tfileposinfo;
  722. old_block_type : tblock_type;
  723. ps : pconstset;
  724. pd : pbestreal;
  725. sp : pchar;
  726. skipequal : boolean;
  727. begin
  728. consume(_CONST);
  729. old_block_type:=block_type;
  730. block_type:=bt_const;
  731. repeat
  732. name:=pattern;
  733. filepos:=tokenpos;
  734. consume(_ID);
  735. case token of
  736. _EQUAL:
  737. begin
  738. consume(_EQUAL);
  739. p:=comp_expr(true);
  740. do_firstpass(p);
  741. storetokenpos:=tokenpos;
  742. tokenpos:=filepos;
  743. case p^.treetype of
  744. ordconstn:
  745. begin
  746. if is_constintnode(p) then
  747. symtablestack^.insert(new(pconstsym,init_def(name,constint,p^.value,nil)))
  748. else if is_constcharnode(p) then
  749. symtablestack^.insert(new(pconstsym,init_def(name,constchar,p^.value,nil)))
  750. else if is_constboolnode(p) then
  751. symtablestack^.insert(new(pconstsym,init_def(name,constbool,p^.value,nil)))
  752. else if p^.resulttype^.deftype=enumdef then
  753. symtablestack^.insert(new(pconstsym,init_def(name,constord,p^.value,p^.resulttype)))
  754. else if p^.resulttype^.deftype=pointerdef then
  755. symtablestack^.insert(new(pconstsym,init_def(name,constord,p^.value,p^.resulttype)))
  756. else internalerror(111);
  757. end;
  758. stringconstn:
  759. begin
  760. getmem(sp,p^.length+1);
  761. move(p^.value_str^,sp^,p^.length+1);
  762. symtablestack^.insert(new(pconstsym,init_string(name,conststring,sp,p^.length)));
  763. end;
  764. realconstn :
  765. begin
  766. new(pd);
  767. pd^:=p^.value_real;
  768. symtablestack^.insert(new(pconstsym,init(name,constreal,longint(pd))));
  769. end;
  770. setconstn :
  771. begin
  772. new(ps);
  773. ps^:=p^.value_set^;
  774. symtablestack^.insert(new(pconstsym,init_def(name,constset,longint(ps),p^.resulttype)));
  775. end;
  776. pointerconstn :
  777. begin
  778. symtablestack^.insert(new(pconstsym,init_def(name,constpointer,p^.value,p^.resulttype)))
  779. end;
  780. niln :
  781. begin
  782. symtablestack^.insert(new(pconstsym,init_def(name,constnil,0,p^.resulttype)));
  783. end;
  784. else
  785. Message(cg_e_illegal_expression);
  786. end;
  787. tokenpos:=storetokenpos;
  788. consume(_SEMICOLON);
  789. disposetree(p);
  790. end;
  791. _COLON:
  792. begin
  793. { set the blocktype first so a consume also supports a
  794. caret, to support const s : ^string = nil }
  795. block_type:=bt_type;
  796. consume(_COLON);
  797. ignore_equal:=true;
  798. read_type(tt,'');
  799. ignore_equal:=false;
  800. block_type:=bt_const;
  801. skipequal:=false;
  802. { create symbol }
  803. storetokenpos:=tokenpos;
  804. tokenpos:=filepos;
  805. {$ifdef DELPHI_CONST_IN_RODATA}
  806. if m_delphi in aktmodeswitches then
  807. begin
  808. if assigned(readtypesym) then
  809. sym:=new(ptypedconstsym,initsym(name,readtypesym,true))
  810. else
  811. sym:=new(ptypedconstsym,init(name,def,true))
  812. end
  813. else
  814. {$endif DELPHI_CONST_IN_RODATA}
  815. begin
  816. sym:=new(ptypedconstsym,inittype(name,tt,false))
  817. end;
  818. tokenpos:=storetokenpos;
  819. symtablestack^.insert(sym);
  820. { procvar can have proc directives }
  821. if (tt.def^.deftype=procvardef) then
  822. begin
  823. { support p : procedure;stdcall=nil; }
  824. if (token=_SEMICOLON) then
  825. begin
  826. consume(_SEMICOLON);
  827. if is_proc_directive(token) then
  828. parse_var_proc_directives(sym)
  829. else
  830. begin
  831. Message(parser_e_proc_directive_expected);
  832. skipequal:=true;
  833. end;
  834. end
  835. else
  836. { support p : procedure stdcall=nil; }
  837. begin
  838. if is_proc_directive(token) then
  839. parse_var_proc_directives(sym);
  840. end;
  841. end;
  842. if not skipequal then
  843. begin
  844. { get init value }
  845. consume(_EQUAL);
  846. {$ifdef DELPHI_CONST_IN_RODATA}
  847. if m_delphi in aktmodeswitches then
  848. readtypedconst(tt.def,ptypedconstsym(sym),true)
  849. else
  850. {$endif DELPHI_CONST_IN_RODATA}
  851. readtypedconst(tt.def,ptypedconstsym(sym),false);
  852. consume(_SEMICOLON);
  853. end;
  854. end;
  855. else
  856. { generate an error }
  857. consume(_EQUAL);
  858. end;
  859. until token<>_ID;
  860. block_type:=old_block_type;
  861. end;
  862. procedure label_dec;
  863. var
  864. hl : pasmlabel;
  865. begin
  866. consume(_LABEL);
  867. if not(cs_support_goto in aktmoduleswitches) then
  868. Message(sym_e_goto_and_label_not_supported);
  869. repeat
  870. if not(token in [_ID,_INTCONST]) then
  871. consume(_ID)
  872. else
  873. begin
  874. if (cs_create_smart in aktmoduleswitches) then
  875. begin
  876. getdatalabel(hl);
  877. { we still want a warning if unused }
  878. hl^.refs:=0;
  879. end
  880. else
  881. getlabel(hl);
  882. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  883. consume(token);
  884. end;
  885. if token<>_SEMICOLON then consume(_COMMA);
  886. until not(token in [_ID,_INTCONST]);
  887. consume(_SEMICOLON);
  888. end;
  889. { search in symtablestack used, but not defined type }
  890. procedure resolve_type_forward(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  891. var
  892. hpd,pd : pdef;
  893. stpos : tfileposinfo;
  894. again : boolean;
  895. begin
  896. { Check only typesyms or record/object fields }
  897. case psym(p)^.typ of
  898. typesym :
  899. pd:=ptypesym(p)^.restype.def;
  900. varsym :
  901. if (psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  902. pd:=pvarsym(p)^.vartype.def
  903. else
  904. exit;
  905. else
  906. exit;
  907. end;
  908. repeat
  909. again:=false;
  910. case pd^.deftype of
  911. arraydef :
  912. begin
  913. { elementtype could also be defined using a forwarddef }
  914. pd:=parraydef(pd)^.elementtype.def;
  915. again:=true;
  916. end;
  917. pointerdef,
  918. classrefdef :
  919. begin
  920. { classrefdef inherits from pointerdef }
  921. hpd:=ppointerdef(pd)^.pointertype.def;
  922. { still a forward def ? }
  923. if hpd^.deftype=forwarddef then
  924. begin
  925. { try to resolve the forward }
  926. { get the correct position for it }
  927. stpos:=tokenpos;
  928. tokenpos:=pforwarddef(hpd)^.forwardpos;
  929. resolving_forward:=true;
  930. make_ref:=false;
  931. getsym(pforwarddef(hpd)^.tosymname,false);
  932. make_ref:=true;
  933. resolving_forward:=false;
  934. tokenpos:=stpos;
  935. { we don't need the forwarddef anymore, dispose it }
  936. dispose(hpd,done);
  937. { was a type sym found ? }
  938. if assigned(srsym) and
  939. (srsym^.typ=typesym) then
  940. begin
  941. ppointerdef(pd)^.pointertype.setsym(srsym);
  942. { avoid wrong unused warnings web bug 801 PM }
  943. inc(srsym^.refs);
  944. {$ifdef GDB}
  945. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  946. (psym(p)^.owner^.symtabletype in [globalsymtable,staticsymtable]) then
  947. begin
  948. ptypesym(p)^.isusedinstab := true;
  949. psym(p)^.concatstabto(debuglist);
  950. end;
  951. {$endif GDB}
  952. { we need a class type for classrefdef }
  953. if (pd^.deftype=classrefdef) and
  954. not((ptypesym(srsym)^.restype.def^.deftype=objectdef) and
  955. pobjectdef(ptypesym(srsym)^.restype.def)^.is_class) then
  956. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename);
  957. end
  958. else
  959. begin
  960. MessagePos1(psym(p)^.fileinfo,sym_e_forward_type_not_resolved,p^.name);
  961. { try to recover }
  962. ppointerdef(pd)^.pointertype.def:=generrordef;
  963. end;
  964. end;
  965. end;
  966. recorddef :
  967. precorddef(pd)^.symtable^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  968. objectdef :
  969. { Don't check objectdefs in objects/records, because these can't
  970. exist (anonymous objects aren't allowed) }
  971. if not(psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  972. pobjectdef(pd)^.symtable^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  973. end;
  974. until not again;
  975. end;
  976. { reads a type declaration to the symbol table }
  977. procedure type_dec;
  978. var
  979. typename : stringid;
  980. newtype : ptypesym;
  981. sym : psym;
  982. tt : ttype;
  983. defpos,storetokenpos : tfileposinfo;
  984. old_block_type : tblock_type;
  985. begin
  986. old_block_type:=block_type;
  987. block_type:=bt_type;
  988. consume(_TYPE);
  989. typecanbeforward:=true;
  990. repeat
  991. typename:=pattern;
  992. defpos:=tokenpos;
  993. consume(_ID);
  994. consume(_EQUAL);
  995. { support 'ttype=type word' syntax }
  996. if token=_TYPE then
  997. Consume(_TYPE);
  998. { is the type already defined? }
  999. getsym(typename,false);
  1000. sym:=srsym;
  1001. newtype:=nil;
  1002. { found a symbol with this name? }
  1003. if assigned(sym) then
  1004. begin
  1005. if (sym^.typ=typesym) then
  1006. begin
  1007. if (token=_CLASS) and
  1008. (assigned(ptypesym(sym)^.restype.def)) and
  1009. (ptypesym(sym)^.restype.def^.deftype=objectdef) and
  1010. pobjectdef(ptypesym(sym)^.restype.def)^.is_class and
  1011. (oo_is_forward in pobjectdef(ptypesym(sym)^.restype.def)^.objectoptions) then
  1012. begin
  1013. { we can ignore the result }
  1014. { the definition is modified }
  1015. object_dec(typename,pobjectdef(ptypesym(sym)^.restype.def));
  1016. newtype:=ptypesym(sym);
  1017. end;
  1018. end;
  1019. end;
  1020. { no old type reused ? Then insert this new type }
  1021. if not assigned(newtype) then
  1022. begin
  1023. read_type(tt,typename);
  1024. storetokenpos:=tokenpos;
  1025. tokenpos:=defpos;
  1026. newtype:=new(ptypesym,init(typename,tt));
  1027. symtablestack^.insert(newtype);
  1028. tokenpos:=storetokenpos;
  1029. end;
  1030. if assigned(newtype^.restype.def) and
  1031. (newtype^.restype.def^.deftype=procvardef) then
  1032. begin
  1033. if not is_proc_directive(token) then
  1034. consume(_SEMICOLON);
  1035. parse_var_proc_directives(psym(newtype));
  1036. end
  1037. else
  1038. consume(_SEMICOLON);
  1039. until token<>_ID;
  1040. typecanbeforward:=false;
  1041. symtablestack^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  1042. block_type:=old_block_type;
  1043. end;
  1044. procedure var_dec;
  1045. { parses variable declarations and inserts them in }
  1046. { the top symbol table of symtablestack }
  1047. begin
  1048. consume(_VAR);
  1049. read_var_decs(false,false,false);
  1050. end;
  1051. procedure threadvar_dec;
  1052. { parses thread variable declarations and inserts them in }
  1053. { the top symbol table of symtablestack }
  1054. begin
  1055. consume(_THREADVAR);
  1056. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  1057. message(parser_e_threadvars_only_sg);
  1058. read_var_decs(false,false,true);
  1059. end;
  1060. procedure resourcestring_dec;
  1061. var
  1062. name : stringid;
  1063. p : ptree;
  1064. storetokenpos,filepos : tfileposinfo;
  1065. old_block_type : tblock_type;
  1066. sp : pchar;
  1067. begin
  1068. consume(_RESOURCESTRING);
  1069. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  1070. message(parser_e_resourcestring_only_sg);
  1071. old_block_type:=block_type;
  1072. block_type:=bt_const;
  1073. repeat
  1074. name:=pattern;
  1075. filepos:=tokenpos;
  1076. consume(_ID);
  1077. case token of
  1078. _EQUAL:
  1079. begin
  1080. consume(_EQUAL);
  1081. p:=comp_expr(true);
  1082. do_firstpass(p);
  1083. storetokenpos:=tokenpos;
  1084. tokenpos:=filepos;
  1085. case p^.treetype of
  1086. ordconstn:
  1087. begin
  1088. if is_constcharnode(p) then
  1089. begin
  1090. getmem(sp,2);
  1091. sp[0]:=chr(p^.value);
  1092. sp[1]:=#0;
  1093. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,1)));
  1094. end
  1095. else
  1096. Message(cg_e_illegal_expression);
  1097. end;
  1098. stringconstn:
  1099. begin
  1100. getmem(sp,p^.length+1);
  1101. move(p^.value_str^,sp^,p^.length+1);
  1102. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,p^.length)));
  1103. end;
  1104. else
  1105. Message(cg_e_illegal_expression);
  1106. end;
  1107. tokenpos:=storetokenpos;
  1108. consume(_SEMICOLON);
  1109. disposetree(p);
  1110. end;
  1111. else consume(_EQUAL);
  1112. end;
  1113. until token<>_ID;
  1114. block_type:=old_block_type;
  1115. end;
  1116. procedure Not_supported_for_inline(t : ttoken);
  1117. begin
  1118. if assigned(aktprocsym) and
  1119. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1120. Begin
  1121. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  1122. Message(parser_w_inlining_disabled);
  1123. {$ifdef INCLUDEOK}
  1124. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  1125. {$else}
  1126. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions-[pocall_inline];
  1127. {$endif}
  1128. End;
  1129. end;
  1130. procedure read_declarations(islibrary : boolean);
  1131. begin
  1132. repeat
  1133. case token of
  1134. _LABEL:
  1135. begin
  1136. Not_supported_for_inline(token);
  1137. label_dec;
  1138. end;
  1139. _CONST:
  1140. begin
  1141. Not_supported_for_inline(token);
  1142. const_dec;
  1143. end;
  1144. _TYPE:
  1145. begin
  1146. Not_supported_for_inline(token);
  1147. type_dec;
  1148. end;
  1149. _VAR:
  1150. var_dec;
  1151. _THREADVAR:
  1152. threadvar_dec;
  1153. _CONSTRUCTOR,_DESTRUCTOR,
  1154. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1155. begin
  1156. Not_supported_for_inline(token);
  1157. read_proc;
  1158. end;
  1159. _RESOURCESTRING:
  1160. resourcestring_dec;
  1161. _EXPORTS:
  1162. begin
  1163. Not_supported_for_inline(token);
  1164. { here we should be at lexlevel 1, no ? PM }
  1165. if (lexlevel<>main_program_level) or
  1166. (current_module^.is_unit) then
  1167. begin
  1168. Message(parser_e_syntax_error);
  1169. consume_all_until(_SEMICOLON);
  1170. end
  1171. else if islibrary or (target_info.target=target_i386_WIN32) then
  1172. read_exports;
  1173. end
  1174. else break;
  1175. end;
  1176. until false;
  1177. end;
  1178. procedure read_interface_declarations;
  1179. begin
  1180. {Since the body is now parsed at lexlevel 1, and the declarations
  1181. must be parsed at the same lexlevel we increase the lexlevel.}
  1182. inc(lexlevel);
  1183. repeat
  1184. case token of
  1185. _CONST : const_dec;
  1186. _TYPE : type_dec;
  1187. _VAR : var_dec;
  1188. _THREADVAR : threadvar_dec;
  1189. _RESOURCESTRING:
  1190. resourcestring_dec;
  1191. _FUNCTION,
  1192. _PROCEDURE,
  1193. _OPERATOR : read_proc;
  1194. else
  1195. break;
  1196. end;
  1197. until false;
  1198. dec(lexlevel);
  1199. end;
  1200. end.
  1201. {
  1202. $Log$
  1203. Revision 1.2 2000-07-13 11:32:44 michael
  1204. + removed logs
  1205. }