pdecl.pas 47 KB

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