pdecl.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. { support unit.variable }
  393. if srsym^.typ=unitsym then
  394. begin
  395. consume(_POINT);
  396. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  397. consume(_ID);
  398. end;
  399. { we should check the result type of srsym }
  400. if not (srsym^.typ in [varsym,typedconstsym]) then
  401. Message(parser_e_absolute_only_to_var_or_const);
  402. storetokenpos:=tokenpos;
  403. tokenpos:=declarepos;
  404. abssym:=new(pabsolutesym,init(s,p));
  405. abssym^.abstyp:=tovar;
  406. abssym^.ref:=srsym;
  407. symtablestack^.insert(abssym);
  408. tokenpos:=storetokenpos;
  409. end
  410. else
  411. if (token=_CSTRING) or (token=_CCHAR) then
  412. begin
  413. storetokenpos:=tokenpos;
  414. tokenpos:=declarepos;
  415. abssym:=new(pabsolutesym,init(s,p));
  416. s:=pattern;
  417. consume(token);
  418. abssym^.abstyp:=toasm;
  419. abssym^.asmname:=stringdup(s);
  420. symtablestack^.insert(abssym);
  421. tokenpos:=storetokenpos;
  422. end
  423. else
  424. { absolute address ?!? }
  425. if token=_INTCONST then
  426. begin
  427. if (target_info.target=target_i386_go32v2) then
  428. begin
  429. storetokenpos:=tokenpos;
  430. tokenpos:=declarepos;
  431. abssym:=new(pabsolutesym,init(s,p));
  432. abssym^.abstyp:=toaddr;
  433. abssym^.absseg:=false;
  434. s:=pattern;
  435. consume(_INTCONST);
  436. val(s,abssym^.address,code);
  437. if token=_COLON then
  438. begin
  439. consume(token);
  440. s:=pattern;
  441. consume(_INTCONST);
  442. val(s,l,code);
  443. abssym^.address:=abssym^.address shl 4+l;
  444. abssym^.absseg:=true;
  445. end;
  446. symtablestack^.insert(abssym);
  447. tokenpos:=storetokenpos;
  448. end
  449. else
  450. Message(parser_e_absolute_only_to_var_or_const);
  451. end
  452. else
  453. Message(parser_e_absolute_only_to_var_or_const);
  454. symdone:=true;
  455. end;
  456. { Handling of Delphi typed const = initialized vars ! }
  457. { When should this be rejected ?
  458. - in parasymtable
  459. - in record or object
  460. - ... (PM) }
  461. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  462. not (symtablestack^.symtabletype in [parasymtable]) and
  463. not is_record and not is_object then
  464. begin
  465. storetokenpos:=tokenpos;
  466. s:=sc^.get_with_tokeninfo(tokenpos);
  467. if not sc^.empty then
  468. Message(parser_e_initialized_only_one_var);
  469. if assigned(readtypesym) then
  470. pconstsym:=new(ptypedconstsym,initsym(s,readtypesym,false))
  471. else
  472. pconstsym:=new(ptypedconstsym,init(s,p,false));
  473. symtablestack^.insert(pconstsym);
  474. tokenpos:=storetokenpos;
  475. consume(_EQUAL);
  476. readtypedconst(p,pconstsym,false);
  477. symdone:=true;
  478. end;
  479. { for a record there doesn't need to be a ; before the END or ) }
  480. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  481. consume(_SEMICOLON);
  482. { procvar handling }
  483. if (p^.deftype=procvardef) and (p^.sym=nil) then
  484. begin
  485. newtype:=new(ptypesym,init('unnamed',p));
  486. parse_var_proc_directives(psym(newtype));
  487. newtype^.definition:=nil;
  488. p^.sym:=nil;
  489. dispose(newtype,done);
  490. end;
  491. { Check for variable directives }
  492. if not symdone and (token=_ID) then
  493. begin
  494. { Check for C Variable declarations }
  495. if (m_cvar_support in aktmodeswitches) and
  496. not(is_record or is_object or is_threadvar) and
  497. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  498. begin
  499. { only allowed for one var }
  500. s:=sc^.get_with_tokeninfo(declarepos);
  501. if not sc^.empty then
  502. Message(parser_e_absolute_only_one_var);
  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 then
  528. Message(parser_e_not_external_and_export)
  529. else
  530. export_aktvarsym:=true;
  531. end;
  532. { external and export need a name after when no cdecl is used }
  533. if not is_cdecl then
  534. begin
  535. { dll name ? }
  536. if (extern_aktvarsym) and (idtoken<>_NAME) then
  537. begin
  538. is_dll:=true;
  539. dll_name:=get_stringconst;
  540. end;
  541. consume(_NAME);
  542. C_name:=get_stringconst;
  543. end;
  544. { consume the ; when export or external is used }
  545. if extern_aktvarsym or export_aktvarsym then
  546. consume(_SEMICOLON);
  547. { insert in the symtable }
  548. storetokenpos:=tokenpos;
  549. tokenpos:=declarepos;
  550. if is_dll then
  551. begin
  552. if assigned(readtypesym) then
  553. aktvarsym:=new(pvarsym,initsym_dll(s,readtypesym))
  554. else
  555. aktvarsym:=new(pvarsym,init_dll(s,p))
  556. end
  557. else
  558. begin
  559. if assigned(readtypesym) then
  560. aktvarsym:=new(pvarsym,initsym_C(s,C_name,readtypesym))
  561. else
  562. aktvarsym:=new(pvarsym,init_C(s,C_name,p));
  563. end;
  564. { set some vars options }
  565. if export_aktvarsym then
  566. inc(aktvarsym^.refs);
  567. if extern_aktvarsym then
  568. {$ifdef INCLUDEOK}
  569. include(aktvarsym^.varoptions,vo_is_external);
  570. {$else}
  571. aktvarsym^.varoptions:=aktvarsym^.varoptions+[vo_is_external];
  572. {$endif}
  573. { insert in the stack/datasegment }
  574. symtablestack^.insert(aktvarsym);
  575. tokenpos:=storetokenpos;
  576. { now we can insert it in the import lib if its a dll, or
  577. add it to the externals }
  578. if extern_aktvarsym then
  579. begin
  580. if is_dll then
  581. begin
  582. if not(current_module^.uses_imports) then
  583. begin
  584. current_module^.uses_imports:=true;
  585. importlib^.preparelib(current_module^.modulename^);
  586. end;
  587. importlib^.importvariable(aktvarsym^.mangledname,dll_name,C_name)
  588. end
  589. end;
  590. symdone:=true;
  591. end
  592. else
  593. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  594. begin
  595. {$ifdef INCLUDEOK}
  596. include(current_object_option,sp_static);
  597. {$else}
  598. current_object_option:=current_object_option+[sp_static];
  599. {$endif}
  600. if assigned(readtypesym) then
  601. insert_syms(symtablestack,sc,nil,readtypesym,false)
  602. else
  603. insert_syms(symtablestack,sc,p,nil,false);
  604. {$ifdef INCLUDEOK}
  605. exclude(current_object_option,sp_static);
  606. {$else}
  607. current_object_option:=current_object_option-[sp_static];
  608. {$endif}
  609. consume(_STATIC);
  610. consume(_SEMICOLON);
  611. symdone:=true;
  612. end;
  613. end;
  614. { insert it in the symtable, if not done yet }
  615. if not symdone then
  616. begin
  617. if (sp_published in current_object_option) and
  618. (not((p^.deftype=objectdef) and (pobjectdef(p)^.is_class))) then
  619. Message(parser_e_cant_publish_that)
  620. else if (sp_published in current_object_option) and
  621. not(oo_can_have_published in pobjectdef(p)^.objectoptions) then
  622. Message(parser_e_only_publishable_classes_can__be_published);
  623. if assigned(readtypesym) then
  624. insert_syms(symtablestack,sc,nil,readtypesym,is_threadvar)
  625. else
  626. insert_syms(symtablestack,sc,p,nil,is_threadvar);
  627. end;
  628. end;
  629. { Check for Case }
  630. if is_record and (token=_CASE) then
  631. begin
  632. maxsize:=0;
  633. consume(_CASE);
  634. s:=pattern;
  635. getsym(s,false);
  636. { may be only a type: }
  637. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  638. casedef:=read_type('')
  639. else
  640. begin
  641. consume(_ID);
  642. consume(_COLON);
  643. casedef:=read_type('');
  644. symtablestack^.insert(new(pvarsym,init(s,casedef)));
  645. end;
  646. if not(is_ordinal(casedef)) or is_64bitint(casedef) then
  647. Message(type_e_ordinal_expr_expected);
  648. consume(_OF);
  649. startvarrec:=symtablestack^.datasize;
  650. repeat
  651. repeat
  652. pt:=comp_expr(true);
  653. do_firstpass(pt);
  654. if not(pt^.treetype=ordconstn) then
  655. Message(cg_e_illegal_expression);
  656. disposetree(pt);
  657. if token=_COMMA then
  658. consume(_COMMA)
  659. else
  660. break;
  661. until false;
  662. consume(_COLON);
  663. { read the vars }
  664. consume(_LKLAMMER);
  665. inc(variantrecordlevel);
  666. if token<>_RKLAMMER then
  667. read_var_decs(true,false,false);
  668. dec(variantrecordlevel);
  669. consume(_RKLAMMER);
  670. { calculates maximal variant size }
  671. maxsize:=max(maxsize,symtablestack^.datasize);
  672. { the items of the next variant are overlayed }
  673. symtablestack^.datasize:=startvarrec;
  674. if (token<>_END) and (token<>_RKLAMMER) then
  675. consume(_SEMICOLON)
  676. else
  677. break;
  678. until (token=_END) or (token=_RKLAMMER);
  679. { at last set the record size to that of the biggest variant }
  680. symtablestack^.datasize:=maxsize;
  681. end;
  682. block_type:=old_block_type;
  683. end;
  684. procedure const_dec;
  685. var
  686. name : stringid;
  687. p : ptree;
  688. def : pdef;
  689. sym : psym;
  690. storetokenpos,filepos : tfileposinfo;
  691. old_block_type : tblock_type;
  692. ps : pconstset;
  693. pd : pbestreal;
  694. sp : pchar;
  695. skipequal : boolean;
  696. begin
  697. consume(_CONST);
  698. old_block_type:=block_type;
  699. block_type:=bt_const;
  700. repeat
  701. name:=pattern;
  702. filepos:=tokenpos;
  703. consume(_ID);
  704. case token of
  705. _EQUAL:
  706. begin
  707. consume(_EQUAL);
  708. p:=comp_expr(true);
  709. do_firstpass(p);
  710. storetokenpos:=tokenpos;
  711. tokenpos:=filepos;
  712. case p^.treetype of
  713. ordconstn:
  714. begin
  715. if is_constintnode(p) then
  716. symtablestack^.insert(new(pconstsym,init_def(name,constint,p^.value,nil)))
  717. else if is_constcharnode(p) then
  718. symtablestack^.insert(new(pconstsym,init_def(name,constchar,p^.value,nil)))
  719. else if is_constboolnode(p) then
  720. symtablestack^.insert(new(pconstsym,init_def(name,constbool,p^.value,nil)))
  721. else if p^.resulttype^.deftype=enumdef then
  722. symtablestack^.insert(new(pconstsym,init_def(name,constord,p^.value,p^.resulttype)))
  723. else if p^.resulttype^.deftype=pointerdef then
  724. symtablestack^.insert(new(pconstsym,init_def(name,constord,p^.value,p^.resulttype)))
  725. else internalerror(111);
  726. end;
  727. stringconstn:
  728. begin
  729. getmem(sp,p^.length+1);
  730. move(p^.value_str^,sp^,p^.length+1);
  731. symtablestack^.insert(new(pconstsym,init_string(name,conststring,sp,p^.length)));
  732. end;
  733. realconstn :
  734. begin
  735. new(pd);
  736. pd^:=p^.value_real;
  737. symtablestack^.insert(new(pconstsym,init(name,constreal,longint(pd))));
  738. end;
  739. setconstn :
  740. begin
  741. new(ps);
  742. ps^:=p^.value_set^;
  743. symtablestack^.insert(new(pconstsym,init_def(name,constset,longint(ps),p^.resulttype)));
  744. end;
  745. pointerconstn :
  746. begin
  747. symtablestack^.insert(new(pconstsym,init_def(name,constpointer,p^.value,p^.resulttype)))
  748. end;
  749. niln :
  750. begin
  751. symtablestack^.insert(new(pconstsym,init_def(name,constnil,0,p^.resulttype)));
  752. end;
  753. else
  754. Message(cg_e_illegal_expression);
  755. end;
  756. tokenpos:=storetokenpos;
  757. consume(_SEMICOLON);
  758. disposetree(p);
  759. end;
  760. _COLON:
  761. begin
  762. { set the blocktype first so a consume also supports a
  763. caret, to support const s : ^string = nil }
  764. block_type:=bt_type;
  765. consume(_COLON);
  766. ignore_equal:=true;
  767. def:=read_type('');
  768. ignore_equal:=false;
  769. block_type:=bt_const;
  770. skipequal:=false;
  771. { create symbol }
  772. storetokenpos:=tokenpos;
  773. tokenpos:=filepos;
  774. {$ifdef DELPHI_CONST_IN_RODATA}
  775. if m_delphi in aktmodeswitches then
  776. begin
  777. if assigned(readtypesym) then
  778. sym:=new(ptypedconstsym,initsym(name,readtypesym,true))
  779. else
  780. sym:=new(ptypedconstsym,init(name,def,true))
  781. end
  782. else
  783. {$endif DELPHI_CONST_IN_RODATA}
  784. begin
  785. if assigned(readtypesym) then
  786. sym:=new(ptypedconstsym,initsym(name,readtypesym,false))
  787. else
  788. sym:=new(ptypedconstsym,init(name,def,false))
  789. end;
  790. tokenpos:=storetokenpos;
  791. symtablestack^.insert(sym);
  792. { procvar can have proc directives }
  793. if (def^.deftype=procvardef) then
  794. begin
  795. { support p : procedure;stdcall=nil; }
  796. if (token=_SEMICOLON) then
  797. begin
  798. consume(_SEMICOLON);
  799. if is_proc_directive(token) then
  800. parse_var_proc_directives(sym)
  801. else
  802. begin
  803. Message(parser_e_proc_directive_expected);
  804. skipequal:=true;
  805. end;
  806. end
  807. else
  808. { support p : procedure stdcall=nil; }
  809. begin
  810. if is_proc_directive(token) then
  811. parse_var_proc_directives(sym);
  812. end;
  813. end;
  814. if not skipequal then
  815. begin
  816. { get init value }
  817. consume(_EQUAL);
  818. {$ifdef DELPHI_CONST_IN_RODATA}
  819. if m_delphi in aktmodeswitches then
  820. readtypedconst(def,ptypedconstsym(sym),true)
  821. else
  822. {$endif DELPHI_CONST_IN_RODATA}
  823. readtypedconst(def,ptypedconstsym(sym),false);
  824. consume(_SEMICOLON);
  825. end;
  826. end;
  827. else
  828. { generate an error }
  829. consume(_EQUAL);
  830. end;
  831. until token<>_ID;
  832. block_type:=old_block_type;
  833. end;
  834. procedure label_dec;
  835. var
  836. hl : pasmlabel;
  837. begin
  838. consume(_LABEL);
  839. if not(cs_support_goto in aktmoduleswitches) then
  840. Message(sym_e_goto_and_label_not_supported);
  841. repeat
  842. if not(token in [_ID,_INTCONST]) then
  843. consume(_ID)
  844. else
  845. begin
  846. getlabel(hl);
  847. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  848. consume(token);
  849. end;
  850. if token<>_SEMICOLON then consume(_COMMA);
  851. until not(token in [_ID,_INTCONST]);
  852. consume(_SEMICOLON);
  853. end;
  854. { search in symtablestack used, but not defined type }
  855. procedure resolve_type_forward(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  856. var
  857. hpd,pd : pdef;
  858. begin
  859. { Check only typesyms or record/object fields }
  860. case psym(p)^.typ of
  861. typesym :
  862. pd:=ptypesym(p)^.definition;
  863. varsym :
  864. if (psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  865. pd:=pvarsym(p)^.definition
  866. else
  867. exit;
  868. else
  869. exit;
  870. end;
  871. case pd^.deftype of
  872. pointerdef,
  873. classrefdef :
  874. begin
  875. { classrefdef inherits from pointerdef }
  876. hpd:=ppointerdef(pd)^.definition;
  877. { still a forward def ? }
  878. if hpd^.deftype=forwarddef then
  879. begin
  880. { try to resolve the forward }
  881. getsym(pforwarddef(hpd)^.tosymname,false);
  882. { we don't need the forwarddef anymore, dispose it }
  883. dispose(hpd,done);
  884. { was a type sym found ? }
  885. if assigned(srsym) and
  886. (srsym^.typ=typesym) then
  887. begin
  888. ppointerdef(pd)^.definition:=ptypesym(srsym)^.definition;
  889. {$ifdef GDB}
  890. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  891. (psym(p)^.owner^.symtabletype in [globalsymtable,staticsymtable]) then
  892. begin
  893. ptypesym(p)^.isusedinstab := true;
  894. psym(p)^.concatstabto(debuglist);
  895. end;
  896. {$endif GDB}
  897. { we need a class type for classrefdef }
  898. if (pd^.deftype=classrefdef) and
  899. not((ptypesym(srsym)^.definition^.deftype=objectdef) and
  900. pobjectdef(ptypesym(srsym)^.definition)^.is_class) then
  901. Message1(type_e_class_type_expected,ptypesym(srsym)^.definition^.typename);
  902. end
  903. else
  904. begin
  905. MessagePos1(psym(p)^.fileinfo,sym_e_forward_type_not_resolved,p^.name);
  906. { try to recover }
  907. ppointerdef(pd)^.definition:=generrordef;
  908. end;
  909. end;
  910. end;
  911. recorddef :
  912. precorddef(pd)^.symtable^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  913. objectdef :
  914. { Don't check objectdefs in objects/records, because these can't
  915. exist (anonymous objects aren't allowed) }
  916. if not(psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  917. pobjectdef(pd)^.symtable^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  918. end;
  919. end;
  920. { reads a type declaration to the symbol table }
  921. procedure type_dec;
  922. var
  923. typename : stringid;
  924. newtype : ptypesym;
  925. sym : psym;
  926. old_block_type : tblock_type;
  927. begin
  928. old_block_type:=block_type;
  929. block_type:=bt_type;
  930. consume(_TYPE);
  931. typecanbeforward:=true;
  932. repeat
  933. typename:=pattern;
  934. consume(_ID);
  935. consume(_EQUAL);
  936. { support 'ttype=type word' syntax }
  937. if token=_TYPE then
  938. Consume(_TYPE);
  939. { is the type already defined? }
  940. getsym(typename,false);
  941. sym:=srsym;
  942. newtype:=nil;
  943. { found a symbol with this name? }
  944. if assigned(sym) then
  945. begin
  946. if (sym^.typ=typesym) then
  947. begin
  948. if (token=_CLASS) and
  949. (assigned(ptypesym(sym)^.definition)) and
  950. (ptypesym(sym)^.definition^.deftype=objectdef) and
  951. pobjectdef(ptypesym(sym)^.definition)^.is_class and
  952. (oo_is_forward in pobjectdef(ptypesym(sym)^.definition)^.objectoptions) then
  953. begin
  954. { we can ignore the result }
  955. { the definition is modified }
  956. object_dec(typename,pobjectdef(ptypesym(sym)^.definition));
  957. newtype:=ptypesym(sym);
  958. end;
  959. end;
  960. end;
  961. { no old type reused ? Then insert this new type }
  962. if not assigned(newtype) then
  963. begin
  964. newtype:=new(ptypesym,init(typename,read_type(typename)));
  965. newtype:=ptypesym(symtablestack^.insert(newtype));
  966. end;
  967. consume(_SEMICOLON);
  968. if assigned(newtype^.definition) and
  969. (newtype^.definition^.deftype=procvardef) then
  970. parse_var_proc_directives(psym(newtype));
  971. until token<>_ID;
  972. typecanbeforward:=false;
  973. symtablestack^.foreach({$ifndef TP}@{$endif}resolve_type_forward);
  974. block_type:=old_block_type;
  975. end;
  976. procedure var_dec;
  977. { parses varaible declarations and inserts them in }
  978. { the top symbol table of symtablestack }
  979. begin
  980. consume(_VAR);
  981. read_var_decs(false,false,false);
  982. end;
  983. procedure threadvar_dec;
  984. { parses thread variable declarations and inserts them in }
  985. { the top symbol table of symtablestack }
  986. begin
  987. consume(_THREADVAR);
  988. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  989. message(parser_e_threadvars_only_sg);
  990. read_var_decs(false,false,true);
  991. end;
  992. procedure resourcestring_dec;
  993. var
  994. name : stringid;
  995. p : ptree;
  996. storetokenpos,filepos : tfileposinfo;
  997. old_block_type : tblock_type;
  998. sp : pchar;
  999. begin
  1000. consume(_RESOURCESTRING);
  1001. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  1002. message(parser_e_resourcestring_only_sg);
  1003. old_block_type:=block_type;
  1004. block_type:=bt_const;
  1005. repeat
  1006. name:=pattern;
  1007. filepos:=tokenpos;
  1008. consume(_ID);
  1009. case token of
  1010. _EQUAL:
  1011. begin
  1012. consume(_EQUAL);
  1013. p:=comp_expr(true);
  1014. do_firstpass(p);
  1015. storetokenpos:=tokenpos;
  1016. tokenpos:=filepos;
  1017. case p^.treetype of
  1018. ordconstn:
  1019. begin
  1020. if is_constcharnode(p) then
  1021. begin
  1022. getmem(sp,2);
  1023. sp[0]:=chr(p^.value);
  1024. sp[1]:=#0;
  1025. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,1)));
  1026. end
  1027. else
  1028. Message(cg_e_illegal_expression);
  1029. end;
  1030. stringconstn:
  1031. begin
  1032. getmem(sp,p^.length+1);
  1033. move(p^.value_str^,sp^,p^.length+1);
  1034. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,p^.length)));
  1035. end;
  1036. else
  1037. Message(cg_e_illegal_expression);
  1038. end;
  1039. tokenpos:=storetokenpos;
  1040. consume(_SEMICOLON);
  1041. disposetree(p);
  1042. end;
  1043. else consume(_EQUAL);
  1044. end;
  1045. until token<>_ID;
  1046. block_type:=old_block_type;
  1047. end;
  1048. procedure Not_supported_for_inline(t : ttoken);
  1049. begin
  1050. if assigned(aktprocsym) and
  1051. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1052. Begin
  1053. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  1054. Message(parser_w_inlining_disabled);
  1055. {$ifdef INCLUDEOK}
  1056. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  1057. {$else}
  1058. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions-[pocall_inline];
  1059. {$endif}
  1060. End;
  1061. end;
  1062. procedure read_declarations(islibrary : boolean);
  1063. begin
  1064. repeat
  1065. case token of
  1066. _LABEL:
  1067. begin
  1068. Not_supported_for_inline(token);
  1069. label_dec;
  1070. end;
  1071. _CONST:
  1072. begin
  1073. Not_supported_for_inline(token);
  1074. const_dec;
  1075. end;
  1076. _TYPE:
  1077. begin
  1078. Not_supported_for_inline(token);
  1079. type_dec;
  1080. end;
  1081. _VAR:
  1082. var_dec;
  1083. _THREADVAR:
  1084. threadvar_dec;
  1085. _CONSTRUCTOR,_DESTRUCTOR,
  1086. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1087. begin
  1088. Not_supported_for_inline(token);
  1089. read_proc;
  1090. end;
  1091. _RESOURCESTRING:
  1092. resourcestring_dec;
  1093. _EXPORTS:
  1094. begin
  1095. Not_supported_for_inline(token);
  1096. { here we should be at lexlevel 1, no ? PM }
  1097. if (lexlevel<>main_program_level) or
  1098. (not islibrary and not DLLsource) then
  1099. begin
  1100. Message(parser_e_syntax_error);
  1101. consume_all_until(_SEMICOLON);
  1102. end
  1103. else if islibrary then
  1104. read_exports;
  1105. end
  1106. else break;
  1107. end;
  1108. until false;
  1109. end;
  1110. procedure read_interface_declarations;
  1111. begin
  1112. {Since the body is now parsed at lexlevel 1, and the declarations
  1113. must be parsed at the same lexlevel we increase the lexlevel.}
  1114. inc(lexlevel);
  1115. repeat
  1116. case token of
  1117. _CONST : const_dec;
  1118. _TYPE : type_dec;
  1119. _VAR : var_dec;
  1120. _THREADVAR : threadvar_dec;
  1121. _RESOURCESTRING:
  1122. resourcestring_dec;
  1123. _FUNCTION,
  1124. _PROCEDURE,
  1125. _OPERATOR : read_proc;
  1126. else
  1127. break;
  1128. end;
  1129. until false;
  1130. dec(lexlevel);
  1131. end;
  1132. end.
  1133. {
  1134. $Log$
  1135. Revision 1.169 1999-11-09 12:58:29 peter
  1136. * support absolute unit.variable
  1137. Revision 1.168 1999/11/06 14:34:21 peter
  1138. * truncated log to 20 revs
  1139. Revision 1.167 1999/10/26 12:30:44 peter
  1140. * const parameter is now checked
  1141. * better and generic check if a node can be used for assigning
  1142. * export fixes
  1143. * procvar equal works now (it never had worked at least from 0.99.8)
  1144. * defcoll changed to linkedlist with pparaitem so it can easily be
  1145. walked both directions
  1146. Revision 1.166 1999/10/22 10:39:34 peter
  1147. * split type reading from pdecl to ptype unit
  1148. * parameter_dec routine is now used for procedure and procvars
  1149. Revision 1.165 1999/10/21 16:41:41 florian
  1150. * problems with readln fixed: esi wasn't restored correctly when
  1151. reading ordinal fields of objects futher the register allocation
  1152. didn't take care of the extra register when reading ordinal values
  1153. * enumerations can now be used in constant indexes of properties
  1154. Revision 1.164 1999/10/14 14:57:52 florian
  1155. - removed the hcodegen use in the new cg, use cgbase instead
  1156. Revision 1.163 1999/10/06 17:39:14 peter
  1157. * fixed stabs writting for forward types
  1158. Revision 1.162 1999/10/03 19:44:42 peter
  1159. * removed objpasunit reference, tvarrec is now searched in systemunit
  1160. where it already was located
  1161. Revision 1.161 1999/10/01 11:18:02 peter
  1162. * class/record type forward checking fixed
  1163. Revision 1.159 1999/10/01 10:05:42 peter
  1164. + procedure directive support in const declarations, fixes bug 232
  1165. Revision 1.158 1999/10/01 08:02:46 peter
  1166. * forward type declaration rewritten
  1167. Revision 1.157 1999/09/27 23:44:53 peter
  1168. * procinfo is now a pointer
  1169. * support for result setting in sub procedure
  1170. Revision 1.156 1999/09/26 21:30:19 peter
  1171. + constant pointer support which can happend with typecasting like
  1172. const p=pointer(1)
  1173. * better procvar parsing in typed consts
  1174. Revision 1.155 1999/09/20 16:38:59 peter
  1175. * cs_create_smart instead of cs_smartlink
  1176. * -CX is create smartlink
  1177. * -CD is create dynamic, but does nothing atm.
  1178. Revision 1.154 1999/09/15 22:09:24 florian
  1179. + rtti is now automatically generated for published classes, i.e.
  1180. they are handled like an implicit property
  1181. Revision 1.153 1999/09/14 11:09:08 florian
  1182. * per default a property is stored, fixed
  1183. Revision 1.152 1999/09/12 14:50:50 florian
  1184. + implemented creation of methodname/address tables
  1185. Revision 1.151 1999/09/12 08:48:09 florian
  1186. * bugs 593 and 607 fixed
  1187. * some other potential bugs with array constructors fixed
  1188. * for classes compiled in $M+ and it's childs, the default access method
  1189. is now published
  1190. * fixed copyright message (it is now 1993-99)
  1191. Revision 1.150 1999/09/10 20:57:33 florian
  1192. * some more fixes for stored properties
  1193. Revision 1.149 1999/09/10 18:48:07 florian
  1194. * some bug fixes (e.g. must_be_valid and procinfo^.funcret_is_valid)
  1195. * most things for stored properties fixed
  1196. Revision 1.148 1999/09/08 21:06:06 michael
  1197. * Stored specifier for properties is now correctly parsed
  1198. Revision 1.147 1999/09/02 09:23:51 peter
  1199. * fixed double dispose of propsymlist
  1200. }