pdecl.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Does declaration 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. globals,symtable;
  22. var
  23. { pointer to the last read type symbol, (for "forward" }
  24. { types) }
  25. lasttypesym : ptypesym;
  26. { hack, which allows to use the current parsed }
  27. { object type as function argument type }
  28. testcurobject : byte;
  29. curobjectname : stringid;
  30. { reads a string type with optional length }
  31. { and returns a pointer to the string }
  32. { definition }
  33. function stringtype : pdef;
  34. { reads a string, file type or a type id and returns a name and }
  35. { pdef }
  36. function single_type(var s : string) : pdef;
  37. { reads the declaration blocks }
  38. procedure read_declarations(islibrary : boolean);
  39. { reads declarations in the interface part of a unit }
  40. procedure read_interface_declarations;
  41. implementation
  42. uses
  43. cobjects,scanner,aasm,tree,pass_1,
  44. types,hcodegen,verbose,systems
  45. {$ifdef GDB}
  46. ,gdb
  47. {$endif GDB}
  48. { parser specific stuff }
  49. ,pbase,ptconst,pexpr,psub,pexports
  50. { processor specific stuff }
  51. {$ifdef i386}
  52. ,i386
  53. {$endif}
  54. {$ifdef m68k}
  55. ,m68k
  56. {$endif}
  57. ;
  58. function read_type(const name : stringid) : pdef;forward;
  59. procedure read_var_decs(is_record : boolean;do_absolute : boolean);forward;
  60. procedure const_dec;
  61. var
  62. name : stringid;
  63. p : ptree;
  64. def : pdef;
  65. ps : pconstset;
  66. pd : pdouble;
  67. begin
  68. consume(_CONST);
  69. repeat
  70. name:=pattern;
  71. consume(ID);
  72. case token of
  73. EQUAL:
  74. begin
  75. consume(EQUAL);
  76. p:=expr;
  77. do_firstpass(p);
  78. case p^.treetype of
  79. ordconstn:
  80. begin
  81. if is_constintnode(p) then
  82. symtablestack^.insert(new(pconstsym,init(name,constint,p^.value,nil)))
  83. else if is_constcharnode(p) then
  84. symtablestack^.insert(new(pconstsym,init(name,constchar,p^.value,nil)))
  85. else if is_constboolnode(p) then
  86. symtablestack^.insert(new(pconstsym,init(name,constbool,p^.value,nil)))
  87. else if p^.resulttype^.deftype=enumdef then
  88. symtablestack^.insert(new(pconstsym,init(name,constord,p^.value,p^.resulttype)))
  89. else internalerror(111);
  90. end;
  91. stringconstn:
  92. {values is disposed with p so I need a copy !}
  93. symtablestack^.insert(new(pconstsym,init(name,conststring,longint(stringdup(p^.values^)),nil)));
  94. realconstn : begin
  95. new(pd);
  96. pd^:=p^.valued;
  97. symtablestack^.insert(new(pconstsym,init(name,constreal,longint(pd),nil)));
  98. end;
  99. setconstrn : begin
  100. new(ps);
  101. ps^:=p^.constset^;
  102. symtablestack^.insert(new(pconstsym,init(name,
  103. constseta,longint(ps),p^.resulttype)));
  104. end;
  105. else Message(cg_e_illegal_expression);
  106. end;
  107. consume(SEMICOLON);
  108. end;
  109. COLON:
  110. begin
  111. { this was missed, so const s : ^string = nil gives an
  112. error (FK)
  113. }
  114. block_type:=bt_type;
  115. consume(COLON);
  116. ignore_equal:=true;
  117. def:=read_type('');
  118. block_type:=bt_general;
  119. ignore_equal:=false;
  120. symtablestack^.insert(new(ptypedconstsym,init(name,def)));
  121. consume(EQUAL);
  122. readtypedconst(def);
  123. consume(SEMICOLON);
  124. end;
  125. else consume(EQUAL);
  126. end;
  127. until token<>ID;
  128. end;
  129. procedure label_dec;
  130. var
  131. hl : plabel;
  132. begin
  133. consume(_LABEL);
  134. if not(cs_support_goto in aktswitches) then
  135. Message(sym_e_goto_and_label_not_supported);
  136. repeat
  137. if not(token in [ID,INTCONST]) then
  138. consume(ID)
  139. else
  140. begin
  141. getlabel(hl);
  142. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  143. consume(token);
  144. end;
  145. if token<>SEMICOLON then consume(COMMA);
  146. until not(token in [ID,INTCONST]);
  147. consume(SEMICOLON);
  148. end;
  149. { reads a string type with optional length }
  150. { and returns a pointer to the string }
  151. { definition }
  152. function stringtype : pdef;
  153. var
  154. p : ptree;
  155. d : pdef;
  156. begin
  157. consume(_STRING);
  158. if token=LECKKLAMMER then
  159. begin
  160. consume(LECKKLAMMER);
  161. p:=expr;
  162. do_firstpass(p);
  163. if not is_constintnode(p) then
  164. Message(cg_e_illegal_expression);
  165. {$ifndef UseLongString}
  166. if (p^.value<1) or (p^.value>255) then
  167. begin
  168. Message(parser_e_string_too_long);
  169. p^.value:=255;
  170. end;
  171. consume(RECKKLAMMER);
  172. if p^.value<>255 then
  173. d:=new(pstringdef,init(p^.value))
  174. {$ifndef GDB}
  175. else d:=new(pstringdef,init(255));
  176. {$else * GDB *}
  177. else d:=globaldef('SYSTEM.STRING');
  178. {$endif * GDB *}
  179. {$else UseLongString}
  180. if p^.value>255 then
  181. d:=new(pstringdef,longinit(p^.value)
  182. else if p^.value<>255 then
  183. d:=new(pstringdef,init(p^.value))
  184. {$ifndef GDB}
  185. else d:=new(pstringdef,init(255));
  186. {$else * GDB *}
  187. else d:=globaldef('SYSTEM.STRING');
  188. {$endif * GDB *}
  189. {$endif UseLongString}
  190. disposetree(p);
  191. end
  192. {$ifndef GDB}
  193. else d:=new(pstringdef,init(255));
  194. {$else * GDB *}
  195. else d:=globaldef('SYSTEM.STRING');
  196. {$endif * GDB *}
  197. stringtype:=d;
  198. end;
  199. { reads a type definition and returns a pointer }
  200. { to a appropriating pdef, s gets the name of }
  201. { the type to allow name mangling }
  202. function id_type(var s : string) : pdef;
  203. begin
  204. s:=pattern;
  205. consume(ID);
  206. if (testcurobject=2) and (curobjectname=pattern) then
  207. begin
  208. id_type:=aktobjectdef;
  209. exit;
  210. end;
  211. getsym(s,true);
  212. if assigned(srsym) then
  213. begin
  214. if srsym^.typ=unitsym then
  215. begin
  216. consume(POINT);
  217. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  218. s:=pattern;
  219. consume(ID);
  220. end;
  221. if srsym^.typ<>typesym then
  222. begin
  223. Message(sym_e_type_id_expected);
  224. lasttypesym:=ptypesym(srsym);
  225. id_type:=generrordef;
  226. exit;
  227. end;
  228. end;
  229. lasttypesym:=ptypesym(srsym);
  230. id_type:=ptypesym(srsym)^.definition;
  231. end;
  232. { reads a string, file type or a type id and returns a name and }
  233. { pdef }
  234. function single_type(var s : string) : pdef;
  235. var
  236. hs : string;
  237. begin
  238. case token of
  239. _STRING:
  240. begin
  241. single_type:=stringtype;
  242. s:='STRING';
  243. lasttypesym:=nil;
  244. end;
  245. _FILE:
  246. begin
  247. consume(_FILE);
  248. if token=_OF then
  249. begin
  250. consume(_OF);
  251. single_type:=new(pfiledef,init(ft_typed,single_type(hs)));
  252. s:='FILE$OF$'+hs;
  253. end
  254. else
  255. begin
  256. { single_type:=new(pfiledef,init(ft_untyped,nil));}
  257. single_type:=cfiledef;
  258. s:='FILE';
  259. end;
  260. lasttypesym:=nil;
  261. end;
  262. else single_type:=id_type(s);
  263. end;
  264. end;
  265. { this function parses an object or class declaration }
  266. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  267. var
  268. actmembertype : symprop;
  269. there_is_a_destructor : boolean;
  270. is_a_class : boolean;
  271. childof : pobjectdef;
  272. aktclass : pobjectdef;
  273. procedure constructor_head;
  274. begin
  275. consume(_CONSTRUCTOR);
  276. { must be at same level as in implementation }
  277. _proc_head(poconstructor);
  278. if (cs_checkconsname in aktswitches) and (aktprocsym^.name<>'INIT') then
  279. Message(parser_e_constructorname_must_be_init);
  280. consume(SEMICOLON);
  281. begin
  282. if (aktclass^.options and oois_class)<>0 then
  283. begin
  284. { CLASS constructors return the created instance }
  285. aktprocsym^.definition^.retdef:=aktclass;
  286. end
  287. else
  288. begin
  289. { OBJECT constructors return a boolean }
  290. {$IfDef GDB}
  291. {GDB doesn't like unnamed types !}
  292. aktprocsym^.definition^.retdef:=
  293. globaldef('boolean');
  294. {$Else * GDB *}
  295. aktprocsym^.definition^.retdef:=
  296. new(porddef,init(bool8bit,0,1));
  297. {$Endif * GDB *}
  298. end;
  299. end;
  300. end;
  301. procedure property_dec;
  302. var
  303. sym : psym;
  304. propertyparas : pdefcoll;
  305. { returns the matching procedure to access a property }
  306. function get_procdef : pprocdef;
  307. var
  308. p : pprocdef;
  309. begin
  310. p:=pprocsym(sym)^.definition;
  311. get_procdef:=nil;
  312. while assigned(p) do
  313. begin
  314. if equal_paras(p^.para1,propertyparas,true) then
  315. break;
  316. p:=p^.nextoverloaded;
  317. end;
  318. get_procdef:=p;
  319. end;
  320. var
  321. hp2,datacoll : pdefcoll;
  322. p,p2 : ppropertysym;
  323. overriden : psym;
  324. hs : string;
  325. code : word;
  326. varspez : tvarspez;
  327. sc : pstringcontainer;
  328. hp : pdef;
  329. s : string;
  330. pp : pprocdef;
  331. begin
  332. { check for a class }
  333. if (aktclass^.options and oois_class=0) then
  334. Message(parser_e_syntax_error);
  335. consume(_PROPERTY);
  336. if token=ID then
  337. begin
  338. p:=new(ppropertysym,init(pattern));
  339. consume(ID);
  340. propertyparas:=nil;
  341. datacoll:=nil;
  342. { property parameters ? }
  343. if token=LECKKLAMMER then
  344. begin
  345. { create a list of the parameters in propertyparas }
  346. consume(LECKKLAMMER);
  347. inc(testcurobject);
  348. repeat
  349. if token=_VAR then
  350. begin
  351. consume(_VAR);
  352. varspez:=vs_var;
  353. end
  354. else if token=_CONST then
  355. begin
  356. consume(_CONST);
  357. varspez:=vs_const;
  358. end
  359. else varspez:=vs_value;
  360. sc:=idlist;
  361. if token=COLON then
  362. begin
  363. consume(COLON);
  364. if token=_ARRAY then
  365. begin
  366. {
  367. if (varspez<>vs_const) and
  368. (varspez<>vs_var) then
  369. begin
  370. varspez:=vs_const;
  371. Message(parser_e_illegal_open_parameter);
  372. end;
  373. }
  374. consume(_ARRAY);
  375. consume(_OF);
  376. { define range and type of range }
  377. hp:=new(parraydef,init(0,-1,s32bitdef));
  378. { define field type }
  379. parraydef(hp)^.definition:=single_type(s);
  380. end
  381. else
  382. hp:=single_type(s);
  383. end
  384. else
  385. hp:=new(pformaldef,init);
  386. s:=sc^.get;
  387. while s<>'' do
  388. begin
  389. new(hp2);
  390. hp2^.paratyp:=varspez;
  391. hp2^.data:=hp;
  392. hp2^.next:=propertyparas;
  393. propertyparas:=hp2;
  394. s:=sc^.get;
  395. end;
  396. dispose(sc,done);
  397. if token=SEMICOLON then consume(SEMICOLON)
  398. else break;
  399. until false;
  400. dec(testcurobject);
  401. consume(RECKKLAMMER);
  402. end;
  403. { overriden property ? }
  404. { force property interface, if there is a property parameter }
  405. if (token=COLON) or assigned(propertyparas) then
  406. begin
  407. consume(COLON);
  408. p^.proptype:=single_type(hs);
  409. if (token=ID) and (pattern='INDEX') then
  410. begin
  411. consume(ID);
  412. p^.options:=p^.options or ppo_indexed;
  413. if token=INTCONST then
  414. val(pattern,p^.index,code);
  415. consume(INTCONST);
  416. { concat a longint to the para template }
  417. new(hp2);
  418. hp2^.paratyp:=vs_value;
  419. hp2^.data:=s32bitdef;
  420. hp2^.next:=propertyparas;
  421. propertyparas:=hp2;
  422. end;
  423. end
  424. else
  425. begin
  426. { do an property override }
  427. overriden:=search_class_member(aktclass,pattern);
  428. if assigned(overriden) and (overriden^.typ=propertysym) then
  429. begin
  430. { take the whole info: }
  431. p^.options:=ppropertysym(overriden)^.options;
  432. p^.index:=ppropertysym(overriden)^.index;
  433. p^.proptype:=ppropertysym(overriden)^.proptype;
  434. p^.writeaccesssym:=ppropertysym(overriden)^.writeaccesssym;
  435. p^.readaccesssym:=ppropertysym(overriden)^.readaccesssym;
  436. p^.writeaccessdef:=ppropertysym(overriden)^.writeaccessdef;
  437. p^.readaccessdef:=ppropertysym(overriden)^.readaccessdef;
  438. end
  439. else
  440. begin
  441. p^.proptype:=generrordef;
  442. message(parser_e_no_property_found_to_override);
  443. end;
  444. end;
  445. { create data defcoll to allow correct parameter checks }
  446. new(datacoll);
  447. datacoll^.paratyp:=vs_value;
  448. datacoll^.data:=p^.proptype;
  449. datacoll^.next:=nil;
  450. if (token=ID) and (pattern='READ') then
  451. begin
  452. consume(ID);
  453. sym:=search_class_member(aktclass,pattern);
  454. if not(assigned(sym)) then
  455. Message1(sym_e_unknown_id,pattern)
  456. else
  457. begin
  458. { varsym aren't allowed for an indexed property
  459. or an property with parameters }
  460. if ((sym^.typ=varsym) and
  461. { not necessary, an index forces propertyparas
  462. to be assigned
  463. }
  464. { (((p^.options and ppo_indexed)<>0) or }
  465. assigned(propertyparas)) or
  466. not(sym^.typ in [varsym,procsym]) then
  467. Message(parser_e_ill_property_access_sym);
  468. { search the matching definition }
  469. if sym^.typ=procsym then
  470. begin
  471. pp:=get_procdef;
  472. if not(assigned(pp)) or
  473. not(is_equal(pp^.retdef,p^.proptype)) then
  474. Message(parser_e_ill_property_access_sym);
  475. p^.readaccessdef:=pp;
  476. end
  477. else if sym^.typ=varsym then
  478. begin
  479. if not(is_equal(pvarsym(sym)^.definition,
  480. p^.proptype)) then
  481. Message(parser_e_ill_property_access_sym);
  482. end;
  483. p^.readaccesssym:=sym;
  484. end;
  485. consume(ID);
  486. end;
  487. if (token=ID) and (pattern='WRITE') then
  488. begin
  489. consume(ID);
  490. sym:=search_class_member(aktclass,pattern);
  491. if not(assigned(sym)) then
  492. Message1(sym_e_unknown_id,pattern)
  493. else
  494. begin
  495. if ((sym^.typ=varsym) and
  496. assigned(propertyparas)) or
  497. not(sym^.typ in [varsym,procsym]) then
  498. Message(parser_e_ill_property_access_sym);
  499. { search the matching definition }
  500. if sym^.typ=procsym then
  501. begin
  502. { insert data entry to check access method }
  503. datacoll^.next:=propertyparas;
  504. propertyparas:=datacoll;
  505. pp:=get_procdef;
  506. { ... and remove it }
  507. propertyparas:=propertyparas^.next;
  508. if not(assigned(pp)) then
  509. Message(parser_e_ill_property_access_sym);
  510. p^.writeaccessdef:=pp;
  511. end
  512. else if sym^.typ=varsym then
  513. begin
  514. if not(is_equal(pvarsym(sym)^.definition,
  515. p^.proptype)) then
  516. Message(parser_e_ill_property_access_sym);
  517. end;
  518. p^.writeaccesssym:=sym;
  519. end;
  520. consume(ID);
  521. end;
  522. if (token=ID) and (pattern='STORED') then
  523. begin
  524. consume(ID);
  525. { !!!!!!!! }
  526. end;
  527. if (token=ID) and (pattern='DEFAULT') then
  528. begin
  529. consume(ID);
  530. { !!!!!!! storage }
  531. consume(SEMICOLON);
  532. end
  533. else if (token=ID) and (pattern='NODEFAULT') then
  534. begin
  535. consume(ID);
  536. { !!!!!!!! }
  537. end;
  538. symtablestack^.insert(p);
  539. { default property ? }
  540. consume(SEMICOLON);
  541. if (token=ID) and (pattern='DEFAULT') then
  542. begin
  543. consume(ID);
  544. p2:=search_default_property(aktclass);
  545. if assigned(p2) then
  546. message1(parser_e_only_one_default_property,
  547. pobjectdef(p2^.owner^.defowner)^.name^)
  548. else
  549. begin
  550. p^.options:=p^.options or ppo_defaultproperty;
  551. if not(assigned(propertyparas)) then
  552. message(parser_e_property_need_paras);
  553. end;
  554. consume(SEMICOLON);
  555. end;
  556. { clean up }
  557. if assigned(datacoll) then
  558. dispose(datacoll);
  559. end
  560. else
  561. begin
  562. consume(ID);
  563. consume(SEMICOLON);
  564. end;
  565. end;
  566. procedure destructor_head;
  567. begin
  568. consume(_DESTRUCTOR);
  569. _proc_head(podestructor);
  570. if (cs_checkconsname in aktswitches) and (aktprocsym^.name<>'DONE') then
  571. Message(parser_e_destructorname_must_be_done);
  572. consume(SEMICOLON);
  573. if assigned(aktprocsym^.definition^.para1) then
  574. Message(parser_e_no_paras_for_destructor);
  575. { no return value }
  576. aktprocsym^.definition^.retdef:=voiddef;
  577. end;
  578. procedure object_komponenten;
  579. var
  580. oldparse_only : boolean;
  581. begin
  582. repeat
  583. case token of
  584. ID:
  585. begin
  586. if (pattern='PUBLIC') or
  587. (pattern='PUBLISHED') or
  588. (pattern='PROTECTED') or
  589. (pattern='PRIVATE') then
  590. exit;
  591. read_var_decs(false,false);
  592. end;
  593. _PROPERTY:
  594. property_dec;
  595. _PROCEDURE,_FUNCTION,_CLASS:
  596. begin
  597. oldparse_only:=parse_only;
  598. parse_only:=true;
  599. proc_head;
  600. parse_only:=oldparse_only;
  601. if (token=ID) and
  602. ((pattern='VIRTUAL') or (pattern='DYNAMIC')) then
  603. begin
  604. if actmembertype=sp_private then
  605. Message(parser_w_priv_meth_not_virtual);
  606. consume(ID);
  607. consume(SEMICOLON);
  608. aktprocsym^.definition^.options:=
  609. aktprocsym^.definition^.options or povirtualmethod;
  610. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  611. end
  612. else if (token=ID) and (pattern='OVERRIDE') then
  613. begin
  614. consume(ID);
  615. consume(SEMICOLON);
  616. aktprocsym^.definition^.options:=
  617. aktprocsym^.definition^.options or pooverridingmethod or povirtualmethod;
  618. end;
  619. { Delphi II extension }
  620. if (token=ID) and (pattern='ABSTRACT') then
  621. begin
  622. consume(ID);
  623. consume(SEMICOLON);
  624. if (aktprocsym^.definition^.options and povirtualmethod)<>0 then
  625. begin
  626. aktprocsym^.definition^.options:=
  627. aktprocsym^.definition^.options or
  628. poabstractmethod;
  629. end
  630. else
  631. Message(parser_e_only_virtual_methods_abstract);
  632. { the method is defined }
  633. aktprocsym^.definition^.forwarddef:=false;
  634. end;
  635. if (token=ID) and (pattern='STATIC') and
  636. (cs_static_keyword in aktswitches) then
  637. begin
  638. consume(ID);
  639. consume(SEMICOLON);
  640. aktprocsym^.properties:=
  641. aktprocsym^.properties or
  642. sp_static;
  643. aktprocsym^.definition^.options:=
  644. aktprocsym^.definition^.options or
  645. postaticmethod;
  646. end;
  647. end;
  648. _CONSTRUCTOR:
  649. begin
  650. if actmembertype<>sp_public then
  651. Message(parser_e_constructor_cannot_be_private);
  652. oldparse_only:=parse_only;
  653. parse_only:=true;
  654. constructor_head;
  655. parse_only:=oldparse_only;
  656. if (token=ID) and
  657. ((pattern='VIRTUAL') or (pattern='DYNAMIC')) then
  658. begin
  659. consume(ID);
  660. consume(SEMICOLON);
  661. if not(aktclass^.isclass) then
  662. Message(parser_e_constructor_cannot_be_not_virtual)
  663. else
  664. begin
  665. aktprocsym^.definition^.options:=
  666. aktprocsym^.definition^.options or povirtualmethod;
  667. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  668. end
  669. end
  670. else if (token=ID) and (pattern='OVERRIDE') then
  671. begin
  672. consume(ID);
  673. consume(SEMICOLON);
  674. if (aktclass^.options and oois_class=0) then
  675. Message(parser_e_constructor_cannot_be_not_virtual)
  676. else
  677. begin
  678. aktprocsym^.definition^.options:=
  679. aktprocsym^.definition^.options or pooverridingmethod or povirtualmethod;
  680. end;
  681. end;
  682. end;
  683. _DESTRUCTOR:
  684. begin
  685. if there_is_a_destructor then
  686. Message(parser_n_only_one_destructor);
  687. there_is_a_destructor:=true;
  688. if actmembertype<>sp_public then
  689. Message(parser_e_destructor_cannot_be_private);
  690. oldparse_only:=parse_only;
  691. parse_only:=true;
  692. destructor_head;
  693. parse_only:=oldparse_only;
  694. if (token=ID) and
  695. ((pattern='VIRTUAL') or (pattern='DYNAMIC')) then
  696. begin
  697. consume(ID);
  698. consume(SEMICOLON);
  699. aktprocsym^.definition^.options:=
  700. aktprocsym^.definition^.options or povirtualmethod;
  701. end
  702. else if (token=ID) and (pattern='OVERRIDE') then
  703. begin
  704. consume(ID);
  705. consume(SEMICOLON);
  706. aktprocsym^.definition^.options:=
  707. aktprocsym^.definition^.options or pooverridingmethod or povirtualmethod;
  708. end;
  709. end;
  710. _END : exit;
  711. else Message(parser_e_syntax_error);
  712. end;
  713. until false;
  714. end;
  715. var
  716. hs : string;
  717. pcrd : pclassrefdef;
  718. hp1 : pdef;
  719. oldprocsym:Pprocsym;
  720. begin
  721. {Nowadays aktprocsym may already have a value, so we need to save
  722. it.}
  723. oldprocsym:=aktprocsym;
  724. { forward is resolved }
  725. if assigned(fd) then
  726. fd^.options:=fd^.options and not(oo_isforward);
  727. there_is_a_destructor:=false;
  728. actmembertype:=sp_public;
  729. { objects and class types can't be declared local }
  730. if (symtablestack^.symtabletype<>globalsymtable) and
  731. (symtablestack^.symtabletype<>staticsymtable) then
  732. Message(parser_e_no_local_objects);
  733. { distinguish classes and objects }
  734. if token=_OBJECT then
  735. begin
  736. is_a_class:=false;
  737. consume(_OBJECT)
  738. end
  739. else
  740. begin
  741. is_a_class:=true;
  742. consume(_CLASS);
  743. if not(assigned(fd)) and (token=_OF) then
  744. begin
  745. { a hack, but it's easy to handle }
  746. { class reference type }
  747. consume(_OF);
  748. if typecanbeforward then
  749. forwardsallowed:=true;
  750. hp1:=single_type(hs);
  751. { accept hp1, if is a forward def ...}
  752. if ((lasttypesym<>nil)
  753. and ((lasttypesym^.properties and sp_forwarddef)<>0)) or
  754. { or a class
  755. (if the foward defined type is a class is checked, when
  756. the forward is resolved)
  757. }
  758. ((hp1^.deftype=objectdef) and (
  759. (pobjectdef(hp1)^.options and oois_class)<>0)) then
  760. begin
  761. pcrd:=new(pclassrefdef,init(hp1));
  762. object_dec:=pcrd;
  763. {I add big troubles here
  764. with var p : ^byte in graph.putimage
  765. because a save_forward was called and
  766. no resolve forward
  767. => so the definition was rewritten after
  768. having been disposed !!
  769. Strange problems appeared !!!!}
  770. {Anyhow forwards should only be allowed
  771. inside a type statement ??
  772. don't you think so }
  773. if (lasttypesym<>nil)
  774. and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  775. lasttypesym^.forwardpointer:=ppointerdef(pcrd);
  776. forwardsallowed:=false;
  777. end
  778. else
  779. begin
  780. Message(parser_e_class_type_expected);
  781. object_dec:=new(perrordef,init);
  782. end;
  783. exit;
  784. end
  785. { forward class }
  786. else if not(assigned(fd)) and (token=SEMICOLON) then
  787. begin
  788. { also anonym objects aren't allow (o : object a : longint; end;) }
  789. if n='' then
  790. Message(parser_e_no_anonym_objects);
  791. if n='TOBJECT' then
  792. begin
  793. aktclass:=new(pobjectdef,init(n,nil));
  794. class_tobject:=aktclass;
  795. end
  796. else
  797. aktclass:=new(pobjectdef,init(n,class_tobject));
  798. aktclass^.options:=aktclass^.options or oois_class or oo_isforward;
  799. object_dec:=aktclass;
  800. exit;
  801. end;
  802. end;
  803. { also anonym objects aren't allow (o : object a : longint; end;) }
  804. if n='' then
  805. Message(parser_e_no_anonym_objects);
  806. { read the parent class }
  807. if token=LKLAMMER then
  808. begin
  809. consume(LKLAMMER);
  810. { does not allow objects.tobject !! }
  811. {if token<>ID then
  812. consume(ID);
  813. getsym(pattern,true);}
  814. childof:=pobjectdef(id_type(pattern));
  815. if (childof^.deftype<>objectdef) then
  816. begin
  817. Message(parser_e_class_type_expected);
  818. childof:=nil;
  819. end;
  820. { a mix of class and object isn't allowed }
  821. if (((childof^.options and oois_class)<>0) and not is_a_class) or
  822. (((childof^.options and oois_class)=0) and is_a_class) then
  823. Message(parser_e_mix_of_classes_and_objects);
  824. consume(RKLAMMER);
  825. if assigned(fd) then
  826. begin
  827. fd^.childof:=childof;
  828. aktclass:=fd;
  829. end
  830. else
  831. aktclass:=new(pobjectdef,init(n,childof));
  832. end
  833. { if no parent class, then a class get tobject as parent }
  834. else if is_a_class then
  835. begin
  836. { is the current class tobject? }
  837. { so you could define your own tobject }
  838. if n='TOBJECT' then
  839. begin
  840. if assigned(fd) then
  841. aktclass:=fd
  842. else
  843. aktclass:=new(pobjectdef,init(n,nil));
  844. class_tobject:=aktclass;
  845. end
  846. else
  847. begin
  848. childof:=class_tobject;
  849. if assigned(fd) then
  850. begin
  851. aktclass:=fd;
  852. aktclass^.childof:=childof;
  853. end
  854. else
  855. aktclass:=new(pobjectdef,init(n,childof));
  856. end;
  857. end
  858. else aktclass:=new(pobjectdef,init(n,nil));
  859. { set the class attribute }
  860. if is_a_class then
  861. aktclass^.options:=aktclass^.options or oois_class;
  862. aktobjectdef:=aktclass;
  863. { default access is public }
  864. actmembertype:=sp_public;
  865. aktclass^.publicsyms^.next:=symtablestack;
  866. symtablestack:=aktclass^.publicsyms;
  867. procinfo._class:=aktclass;
  868. testcurobject:=1;
  869. curobjectname:=n;
  870. while token<>_END do
  871. begin
  872. if (token=ID) and (pattern='PRIVATE') then
  873. begin
  874. consume(ID);
  875. actmembertype:=sp_private;
  876. current_object_option:=sp_private;
  877. end;
  878. if (token=ID) and (pattern='PROTECTED') then
  879. begin
  880. consume(ID);
  881. current_object_option:=sp_protected;
  882. actmembertype:=sp_protected;
  883. end;
  884. if (token=ID) and (pattern='PUBLIC') then
  885. begin
  886. consume(ID);
  887. current_object_option:=sp_public;
  888. actmembertype:=sp_public;
  889. end;
  890. if (token=ID) and (pattern='PUBLISHED') then
  891. begin
  892. consume(ID);
  893. current_object_option:=sp_public;
  894. actmembertype:=sp_public;
  895. end;
  896. object_komponenten;
  897. end;
  898. current_object_option:=sp_public;
  899. consume(_END);
  900. testcurobject:=0;
  901. curobjectname:='';
  902. {$ifdef MAKELIB}
  903. datasegment^.concat(new(pai_cut,init));
  904. {$endif MAKELIB}
  905. {$ifdef GDB}
  906. { generate the VMT }
  907. if cs_debuginfo in aktswitches then
  908. begin
  909. do_count_dbx:=true;
  910. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  911. debuglist^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  912. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  913. end;
  914. {$endif * GDB *}
  915. datasegment^.concat(new(pai_symbol,init_global(aktclass^.vmt_mangledname)));
  916. { determine the size with publicsyms^.datasize, because }
  917. { size gives back 4 for CLASSes }
  918. datasegment^.concat(new(pai_const,init_32bit(aktclass^.publicsyms^.datasize)));
  919. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.publicsyms^.datasize)));
  920. { write pointer to parent VMT, this isn't implemented in TP }
  921. { but this is not used in FPC ? (PM) }
  922. { it's not used yet, but the delphi-operators as and is need it (FK) }
  923. if assigned(aktclass^.childof) then
  924. begin
  925. datasegment^.concat(new(pai_const,init_symbol(strpnew(aktclass^.childof^.vmt_mangledname))));
  926. if aktclass^.childof^.owner^.symtabletype=unitsymtable then
  927. concat_external(aktclass^.childof^.vmt_mangledname,EXT_NEAR);
  928. end
  929. else
  930. datasegment^.concat(new(pai_const,init_32bit(0)));
  931. { this generates the entries }
  932. genvmt(aktclass);
  933. { restore old state }
  934. symtablestack:=symtablestack^.next;
  935. procinfo._class:=nil;
  936. {Restore the aktprocsym.}
  937. aktprocsym:=oldprocsym;
  938. object_dec:=aktclass;
  939. end;
  940. { reads a record declaration }
  941. function record_dec : pdef;
  942. var
  943. symtable : psymtable;
  944. begin
  945. symtable:=new(psymtable,init(recordsymtable));
  946. symtable^.next:=symtablestack;
  947. symtablestack:=symtable;
  948. consume(_RECORD);
  949. read_var_decs(true,false);
  950. { may be scale record size to a size of n*4 ? }
  951. if ((symtablestack^.datasize mod aktpackrecords)<>0) then
  952. inc(symtablestack^.datasize,aktpackrecords-(symtablestack^.datasize mod aktpackrecords));
  953. consume(_END);
  954. symtablestack:=symtable^.next;
  955. record_dec:=new(precdef,init(symtable));
  956. end;
  957. { reads a type definition and returns a pointer to it }
  958. function read_type(const name : stringid) : pdef;
  959. function handle_procvar:Pprocvardef;
  960. var
  961. sc : pstringcontainer;
  962. s : string;
  963. p : pdef;
  964. varspez : tvarspez;
  965. procvardef : pprocvardef;
  966. begin
  967. procvardef:=new(pprocvardef,init);
  968. if token=LKLAMMER then
  969. begin
  970. consume(LKLAMMER);
  971. inc(testcurobject);
  972. repeat
  973. if token=_VAR then
  974. begin
  975. consume(_VAR);
  976. varspez:=vs_var;
  977. end
  978. else if token=_CONST then
  979. begin
  980. consume(_CONST);
  981. varspez:=vs_const;
  982. end
  983. else varspez:=vs_value;
  984. sc:=idlist;
  985. if token=COLON then
  986. begin
  987. consume(COLON);
  988. if token=_ARRAY then
  989. begin
  990. {
  991. if (varspez<>vs_const) and
  992. (varspez<>vs_var) then
  993. begin
  994. varspez:=vs_const;
  995. Message(parser_e_illegal_open_parameter);
  996. end;
  997. }
  998. consume(_ARRAY);
  999. consume(_OF);
  1000. { define range and type of range }
  1001. p:=new(parraydef,init(0,-1,s32bitdef));
  1002. { define field type }
  1003. parraydef(p)^.definition:=single_type(s);
  1004. end
  1005. else
  1006. p:=single_type(s);
  1007. end
  1008. else
  1009. p:=new(pformaldef,init);
  1010. s:=sc^.get;
  1011. while s<>'' do
  1012. begin
  1013. procvardef^.concatdef(p,varspez);
  1014. s:=sc^.get;
  1015. end;
  1016. dispose(sc,done);
  1017. if token=SEMICOLON then consume(SEMICOLON)
  1018. else break;
  1019. until false;
  1020. dec(testcurobject);
  1021. consume(RKLAMMER);
  1022. if token=_OF then
  1023. begin
  1024. consume(_OF);
  1025. consume(_OBJECT);
  1026. procvardef^.options:=procvardef^.options or pomethodpointer;
  1027. end;
  1028. end;
  1029. handle_procvar:=procvardef;
  1030. end;
  1031. var
  1032. hp1,p : pdef;
  1033. aufdef : penumdef;
  1034. aufsym : penumsym;
  1035. ap : parraydef;
  1036. s : stringid;
  1037. l,v,oldaktpackrecords : longint;
  1038. hs : string;
  1039. procedure expr_type;
  1040. var
  1041. pt1,pt2 : ptree;
  1042. begin
  1043. { use of current parsed object ? }
  1044. if (token=ID) and (testcurobject=2) and (curobjectname=pattern) then
  1045. begin
  1046. consume(ID);
  1047. p:=aktobjectdef;
  1048. exit;
  1049. end;
  1050. { we can't accept a equal in type }
  1051. pt1:=comp_expr(not(ignore_equal));
  1052. if (pt1^.treetype=typen) and (token<>POINTPOINT) then
  1053. begin
  1054. { a simple type renaming }
  1055. p:=pt1^.resulttype;
  1056. end
  1057. else
  1058. begin
  1059. { range type }
  1060. consume(POINTPOINT);
  1061. { range type declaration }
  1062. do_firstpass(pt1);
  1063. pt2:=comp_expr(not(ignore_equal));
  1064. do_firstpass(pt2);
  1065. { valid expression ? }
  1066. if (pt1^.treetype<>ordconstn) or
  1067. (pt2^.treetype<>ordconstn) then
  1068. Begin
  1069. Message(sym_e_error_in_type_def);
  1070. { Here we create a node type with a range of 0 }
  1071. { To make sure that no crashes will occur later }
  1072. { on in the compiler. }
  1073. p:=new(porddef,init(uauto,0,0));
  1074. end
  1075. else
  1076. p:=new(porddef,init(uauto,pt1^.value,pt2^.value));
  1077. disposetree(pt2);
  1078. end;
  1079. disposetree(pt1);
  1080. end;
  1081. var
  1082. pt : ptree;
  1083. procedure array_dec;
  1084. begin
  1085. consume(_ARRAY);
  1086. consume(LECKKLAMMER);
  1087. p:=nil;
  1088. repeat
  1089. { read the expression and check it }
  1090. pt:=expr;
  1091. if pt^.treetype=typen then
  1092. begin
  1093. if pt^.resulttype^.deftype=enumdef then
  1094. begin
  1095. if p=nil then
  1096. begin
  1097. ap:=new(parraydef,
  1098. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1099. p:=ap;
  1100. end
  1101. else
  1102. begin
  1103. ap^.definition:=new(parraydef,
  1104. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1105. ap:=parraydef(ap^.definition);
  1106. end;
  1107. end
  1108. else if pt^.resulttype^.deftype=orddef then
  1109. begin
  1110. case porddef(pt^.resulttype)^.typ of
  1111. s8bit,u8bit,s16bit,u16bit,s32bit :
  1112. begin
  1113. if p=nil then
  1114. begin
  1115. ap:=new(parraydef,init(porddef(pt^.resulttype)^.von,
  1116. porddef(pt^.resulttype)^.bis,pt^.resulttype));
  1117. p:=ap;
  1118. end
  1119. else
  1120. begin
  1121. ap^.definition:=new(parraydef,init(porddef(pt^.resulttype)^.von,
  1122. porddef(pt^.resulttype)^.bis,pt^.resulttype));
  1123. ap:=parraydef(ap^.definition);
  1124. end;
  1125. end;
  1126. bool8bit:
  1127. begin
  1128. if p=nil then
  1129. begin
  1130. ap:=new(parraydef,init(0,1,pt^.resulttype));
  1131. p:=ap;
  1132. end
  1133. else
  1134. begin
  1135. ap^.definition:=new(parraydef,init(0,1,pt^.resulttype));
  1136. ap:=parraydef(ap^.definition);
  1137. end;
  1138. end;
  1139. uchar:
  1140. begin
  1141. if p=nil then
  1142. begin
  1143. ap:=new(parraydef,init(0,255,pt^.resulttype));
  1144. p:=ap;
  1145. end
  1146. else
  1147. begin
  1148. ap^.definition:=new(parraydef,init(0,255,pt^.resulttype));
  1149. ap:=parraydef(ap^.definition);
  1150. end;
  1151. end;
  1152. else Message(sym_e_error_in_type_def);
  1153. end;
  1154. end
  1155. else Message(sym_e_error_in_type_def);
  1156. end
  1157. else
  1158. begin
  1159. do_firstpass(pt);
  1160. if (pt^.treetype<>rangen) or
  1161. (pt^.left^.treetype<>ordconstn) then
  1162. Message(sym_e_error_in_type_def);
  1163. { force the registration of the ranges }
  1164. {$ifndef GDB}
  1165. if pt^.right^.resulttype=pdef(s32bitdef) then
  1166. pt^.right^.resulttype:=new(porddef,init(
  1167. s32bit,$80000000,$7fffffff));
  1168. {$endif GDB}
  1169. if p=nil then
  1170. begin
  1171. ap:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1172. p:=ap;
  1173. end
  1174. else
  1175. begin
  1176. ap^.definition:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1177. ap:=parraydef(ap^.definition);
  1178. end;
  1179. end;
  1180. disposetree(pt);
  1181. if token=COMMA then consume(COMMA)
  1182. else break;
  1183. until false;
  1184. consume(RECKKLAMMER);
  1185. consume(_OF);
  1186. hp1:=read_type('');
  1187. { if no error, set element type }
  1188. if assigned(ap) then
  1189. ap^.definition:=hp1;
  1190. end;
  1191. begin
  1192. case token of
  1193. _STRING,_FILE:
  1194. p:=single_type(hs);
  1195. LKLAMMER:
  1196. begin
  1197. consume(LKLAMMER);
  1198. l:=-1;
  1199. aufsym := Nil;
  1200. aufdef:=new(penumdef,init);
  1201. repeat
  1202. s:=pattern;
  1203. consume(ID);
  1204. if token=ASSIGNMENT then
  1205. begin
  1206. consume(ASSIGNMENT);
  1207. v:=get_intconst;
  1208. { please leave that a note, allows type save }
  1209. { declarations in the win32 units ! }
  1210. if v<=l then
  1211. Message(parser_n_duplicate_enum);
  1212. l:=v;
  1213. end
  1214. else
  1215. inc(l);
  1216. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1217. if token=COMMA then
  1218. consume(COMMA)
  1219. else
  1220. break;
  1221. until false;
  1222. aufdef^.max:=l;
  1223. p:=aufdef;
  1224. consume(RKLAMMER);
  1225. end;
  1226. _ARRAY:
  1227. array_dec;
  1228. _SET:
  1229. begin
  1230. consume(_SET);
  1231. consume(_OF);
  1232. hp1:=read_type('');
  1233. case hp1^.deftype of
  1234. enumdef : p:=new(psetdef,init(hp1,penumdef(hp1)^.max));
  1235. orddef : begin
  1236. case porddef(hp1)^.typ of
  1237. uchar : p:=new(psetdef,init(hp1,255));
  1238. u8bit,s8bit,u16bit,s16bit,s32bit :
  1239. begin
  1240. if (porddef(hp1)^.von>=0) then
  1241. p:=new(psetdef,init(hp1,porddef(hp1)^.bis))
  1242. else Message(sym_e_ill_type_decl_set);
  1243. end;
  1244. else Message(sym_e_ill_type_decl_set);
  1245. end;
  1246. end;
  1247. else Message(sym_e_ill_type_decl_set);
  1248. end;
  1249. end;
  1250. CARET:
  1251. begin
  1252. consume(CARET);
  1253. { forwards allowed only inside TYPE statements }
  1254. if typecanbeforward then
  1255. forwardsallowed:=true;
  1256. hp1:=single_type(hs);
  1257. p:=new(ppointerdef,init(hp1));
  1258. {$ifndef GDB}
  1259. if lasttypesym<>nil then
  1260. save_forward(ppointerdef(p),lasttypesym);
  1261. {$else * GDB *}
  1262. {I add big troubles here
  1263. with var p : ^byte in graph.putimage
  1264. because a save_forward was called and
  1265. no resolve forward
  1266. => so the definition was rewritten after
  1267. having been disposed !!
  1268. Strange problems appeared !!!!}
  1269. {Anyhow forwards should only be allowed
  1270. inside a type statement ??
  1271. don't you think so }
  1272. if (lasttypesym<>nil)
  1273. and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  1274. lasttypesym^.forwardpointer:=ppointerdef(p);
  1275. {$endif * GDB *}
  1276. forwardsallowed:=false;
  1277. end;
  1278. _RECORD:
  1279. p:=record_dec;
  1280. _PACKED:
  1281. begin
  1282. consume(_PACKED);
  1283. if token=_ARRAY then
  1284. array_dec
  1285. else
  1286. begin
  1287. oldaktpackrecords:=aktpackrecords;
  1288. aktpackrecords:=1;
  1289. if token in [_CLASS,_OBJECT] then
  1290. p:=object_dec(name,nil)
  1291. else
  1292. p:=record_dec;
  1293. aktpackrecords:=oldaktpackrecords;
  1294. end;
  1295. end;
  1296. _CLASS,
  1297. _OBJECT:
  1298. p:=object_dec(name,nil);
  1299. _PROCEDURE:
  1300. begin
  1301. consume(_PROCEDURE);
  1302. p:=handle_procvar;
  1303. end;
  1304. _FUNCTION:
  1305. begin
  1306. consume(_FUNCTION);
  1307. p:=handle_procvar;
  1308. consume(COLON);
  1309. pprocvardef(p)^.retdef:=single_type(hs);
  1310. end;
  1311. else
  1312. expr_type;
  1313. end;
  1314. read_type:=p;
  1315. end;
  1316. { search in symtablestack used, but not defined type }
  1317. procedure testforward_types(p : psym);{$ifndef FPC}far;{$endif}
  1318. begin
  1319. if (p^.typ=typesym) and ((p^.properties and sp_forwarddef)<>0) then
  1320. Message(sym_e_type_id_not_defined);
  1321. end;
  1322. { reads a type declaration to the symbol table }
  1323. procedure type_dec;
  1324. var
  1325. typename : stringid;
  1326. {$ifdef dummy}
  1327. olddef,newdef : pdef;
  1328. s : string;
  1329. {$endif dummy}
  1330. begin
  1331. block_type:=bt_type;
  1332. consume(_TYPE);
  1333. typecanbeforward:=true;
  1334. repeat
  1335. typename:=pattern;
  1336. consume(ID);
  1337. consume(EQUAL);
  1338. { here you loose the strictness of pascal
  1339. for which a redefinition like
  1340. childtype = parenttype;
  1341. child2type = parenttype;
  1342. does not make the two child types equal !!
  1343. here all vars from childtype and child2type
  1344. get the definition of parenttype !! }
  1345. {$ifdef testequaltype}
  1346. if (token = ID) or (token=_FILE) or (token=_STRING) then
  1347. begin
  1348. olddef := single_type(s);
  1349. { make a clone of olddef }
  1350. { is that ok ??? }
  1351. getmem(newdef,SizeOf(olddef));
  1352. move(olddef^,newdef^,SizeOf(olddef));
  1353. symtablestack^.insert(new(ptypesym,init(typename,newdef)));
  1354. end
  1355. else
  1356. {$endif testequaltype}
  1357. begin
  1358. getsym(typename,false);
  1359. { check if it is the definition of a forward defined class }
  1360. if assigned(srsym) and (token=_CLASS) and
  1361. (srsym^.typ=typesym) and
  1362. (ptypesym(srsym)^.definition^.deftype=objectdef) and
  1363. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oo_isforward)<>0) and
  1364. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oois_class)<>0) then
  1365. begin
  1366. { we can ignore the result }
  1367. { the definition is modified }
  1368. object_dec(typename,pobjectdef(ptypesym(srsym)^.definition));
  1369. end
  1370. else
  1371. symtablestack^.insert(new(ptypesym,init(typename,read_type(typename))));
  1372. end;
  1373. consume(SEMICOLON);
  1374. until token<>ID;
  1375. typecanbeforward:=false;
  1376. {$ifdef tp}
  1377. symtablestack^.foreach(testforward_types);
  1378. {$else}
  1379. symtablestack^.foreach(@testforward_types);
  1380. {$endif}
  1381. resolve_forwards;
  1382. block_type:=bt_general;
  1383. end;
  1384. { parses varaible declarations and inserts them in }
  1385. { the top symbol table of symtablestack }
  1386. procedure var_dec;
  1387. {var
  1388. p : pdef;
  1389. sc : pstringcontainer; }
  1390. begin
  1391. consume(_VAR);
  1392. read_var_decs(false,true);
  1393. end;
  1394. { reads the filed of a record into a }
  1395. { symtablestack, if record=false }
  1396. { variants are forbidden, so this procedure }
  1397. { can be used to read object fields }
  1398. { if absolute is true, ABSOLUTE and file }
  1399. { types are allowed }
  1400. { => the procedure is also used to read }
  1401. { a sequence of variable declaration }
  1402. procedure read_var_decs(is_record : boolean;do_absolute : boolean);
  1403. var
  1404. sc : pstringcontainer;
  1405. s : stringid;
  1406. l : longint;
  1407. code : word;
  1408. hs : string;
  1409. p,casedef : pdef;
  1410. { maxsize contains the max. size of a variant }
  1411. { startvarrec contains the start of the variant part of a record }
  1412. maxsize,startvarrec : longint;
  1413. pt : ptree;
  1414. old_block_type : tblock_type;
  1415. { to handle absolute }
  1416. abssym : pabsolutesym;
  1417. begin
  1418. hs:='';
  1419. old_block_type:=block_type;
  1420. block_type:=bt_type;
  1421. while (token=ID) and
  1422. (pattern<>'PUBLIC') and
  1423. (pattern<>'PRIVATE') and
  1424. (pattern<>'PUBLISHED') and
  1425. (pattern<>'PROTECTED') do
  1426. begin
  1427. sc:=idlist;
  1428. consume(COLON);
  1429. p:=read_type('');
  1430. if do_absolute and (token=ID) and (pattern='ABSOLUTE') then
  1431. begin
  1432. s:=sc^.get;
  1433. if sc^.get<>'' then
  1434. Message(parser_e_absolute_only_one_var);
  1435. dispose(sc,done);
  1436. consume(ID);
  1437. if token=ID then
  1438. begin
  1439. getsym(pattern,true);
  1440. consume(ID);
  1441. { we should check the result type of srsym }
  1442. if not (srsym^.typ in [varsym,typedconstsym]) then
  1443. Message(parser_e_absolute_only_to_var_or_const);
  1444. abssym:=new(pabsolutesym,init(s,p));
  1445. abssym^.typ:=absolutesym;
  1446. abssym^.abstyp:=tovar;
  1447. abssym^.ref:=srsym;
  1448. symtablestack^.insert(abssym);
  1449. end
  1450. else
  1451. if token=CSTRING then
  1452. begin
  1453. abssym:=new(pabsolutesym,init(s,p));
  1454. s:=pattern;
  1455. consume(CSTRING);
  1456. abssym^.typ:=absolutesym;
  1457. abssym^.abstyp:=toasm;
  1458. abssym^.asmname:=stringdup(s);
  1459. symtablestack^.insert(abssym);
  1460. end
  1461. else
  1462. { absolute address ?!? }
  1463. if token=INTCONST then
  1464. begin
  1465. if (target_info.target=target_GO32V2) then
  1466. begin
  1467. abssym:=new(pabsolutesym,init(s,p));
  1468. abssym^.typ:=absolutesym;
  1469. abssym^.abstyp:=toaddr;
  1470. abssym^.absseg:=false;
  1471. s:=pattern;
  1472. consume(INTCONST);
  1473. val(s,abssym^.address,code);
  1474. if token=COLON then
  1475. begin
  1476. consume(token);
  1477. s:=pattern;
  1478. consume(INTCONST);
  1479. val(s,l,code);
  1480. abssym^.address:=abssym^.address shl 4+l;
  1481. abssym^.absseg:=true;
  1482. end;
  1483. symtablestack^.insert(abssym);
  1484. end
  1485. else
  1486. Message(parser_e_absolute_only_to_var_or_const);
  1487. end
  1488. else
  1489. Message(parser_e_absolute_only_to_var_or_const);
  1490. end
  1491. else
  1492. begin
  1493. if token=SEMICOLON then
  1494. begin
  1495. if (symtablestack^.symtabletype=objectsymtable) then
  1496. begin
  1497. consume(SEMICOLON);
  1498. if (token=ID) and (pattern='STATIC') and
  1499. (cs_static_keyword in aktswitches) then
  1500. begin
  1501. current_object_option:=current_object_option or sp_static;
  1502. insert_syms(symtablestack,sc,p);
  1503. current_object_option:=current_object_option - sp_static;
  1504. consume(ID);
  1505. consume(SEMICOLON);
  1506. end
  1507. else
  1508. { this will still be a the wrong line !! }
  1509. insert_syms(symtablestack,sc,p);
  1510. end
  1511. else
  1512. begin
  1513. { at the right line }
  1514. insert_syms(symtablestack,sc,p);
  1515. consume(SEMICOLON);
  1516. end
  1517. end
  1518. else
  1519. begin
  1520. insert_syms(symtablestack,sc,p);
  1521. if not(is_record) then
  1522. consume(SEMICOLON);
  1523. end;
  1524. end;
  1525. while token=SEMICOLON do
  1526. consume(SEMICOLON);
  1527. end;
  1528. if (token=_CASE) and is_record then
  1529. begin
  1530. maxsize:=0;
  1531. consume(_CASE);
  1532. s:=pattern;
  1533. getsym(s,false);
  1534. { may be only a type: }
  1535. if assigned(srsym) and ((srsym^.typ=typesym) or
  1536. { and with unit qualifier: }
  1537. (srsym^.typ=unitsym)) then
  1538. begin
  1539. casedef:=read_type('');
  1540. end
  1541. else
  1542. begin
  1543. consume(ID);
  1544. consume(COLON);
  1545. casedef:=read_type('');
  1546. symtablestack^.insert(new(pvarsym,init(s,casedef)));
  1547. end;
  1548. if not is_ordinal(casedef) then
  1549. Message(parser_e_ordinal_expected);
  1550. consume(_OF);
  1551. startvarrec:=symtablestack^.datasize;
  1552. repeat
  1553. repeat
  1554. pt:=expr;
  1555. do_firstpass(pt);
  1556. if not(pt^.treetype=ordconstn) then
  1557. Message(cg_e_illegal_expression);
  1558. disposetree(pt);
  1559. if token=COMMA then consume(COMMA)
  1560. else break;
  1561. until false;
  1562. consume(COLON);
  1563. consume(LKLAMMER);
  1564. if token<>RKLAMMER then
  1565. read_var_decs(true,false);
  1566. { calculates maximal variant size }
  1567. maxsize:=max(maxsize,symtablestack^.datasize);
  1568. { the items of the next variant are overlayed }
  1569. symtablestack^.datasize:=startvarrec;
  1570. consume(RKLAMMER);
  1571. if token<>SEMICOLON then
  1572. break
  1573. else
  1574. consume(SEMICOLON);
  1575. while token=SEMICOLON do
  1576. consume(SEMICOLON);
  1577. until (token=_END) or (token=RKLAMMER);
  1578. { at last set the record size to that of the biggest variant }
  1579. symtablestack^.datasize:=maxsize;
  1580. end;
  1581. block_type:=old_block_type;
  1582. end;
  1583. procedure read_declarations(islibrary : boolean);
  1584. begin
  1585. repeat
  1586. case token of
  1587. _LABEL:
  1588. label_dec;
  1589. _CONST:
  1590. const_dec;
  1591. _TYPE:
  1592. type_dec;
  1593. _VAR:
  1594. var_dec;
  1595. _CONSTRUCTOR,_DESTRUCTOR,
  1596. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1597. unter_dec;
  1598. _EXPORTS:
  1599. if islibrary then
  1600. read_exports
  1601. else
  1602. break;
  1603. else break;
  1604. end;
  1605. until false;
  1606. end;
  1607. procedure read_interface_declarations;
  1608. begin
  1609. {Since the body is now parsed at lexlevel 1, and the declarations
  1610. must be parsed at the same lexlevel we increase the lexlevel.}
  1611. inc(lexlevel);
  1612. repeat
  1613. case token of
  1614. _CONST : const_dec;
  1615. _TYPE : type_dec;
  1616. _VAR : var_dec;
  1617. { should we allow operator in interface ? }
  1618. { of course otherwise you cannot }
  1619. { declare an operator usable by other }
  1620. { units or progs PM }
  1621. _FUNCTION,_PROCEDURE,_OPERATOR : unter_dec;
  1622. else
  1623. break;
  1624. end;
  1625. until false;
  1626. dec(lexlevel);
  1627. end;
  1628. end.
  1629. {
  1630. $Log$
  1631. Revision 1.9 1998-04-10 21:36:56 florian
  1632. + some stuff to support method pointers (procedure of object) added
  1633. (declaration, parameter handling)
  1634. Revision 1.8 1998/04/10 15:39:48 florian
  1635. * more fixes to get classes.pas compiled
  1636. Revision 1.7 1998/04/09 23:02:15 florian
  1637. * small problems solved to get remake3 work
  1638. Revision 1.6 1998/04/09 22:16:35 florian
  1639. * problem with previous REGALLOC solved
  1640. * improved property support
  1641. Revision 1.5 1998/04/08 14:59:20 florian
  1642. * problem with new expr_type solved
  1643. Revision 1.4 1998/04/08 10:26:09 florian
  1644. * correct error handling of virtual constructors
  1645. * problem with new type declaration handling fixed
  1646. Revision 1.3 1998/04/07 22:45:05 florian
  1647. * bug0092, bug0115 and bug0121 fixed
  1648. + packed object/class/array
  1649. Revision 1.2 1998/04/05 13:58:35 peter
  1650. * fixed the -Ss bug
  1651. + warning for Virtual constructors
  1652. * helppages updated with -TGO32V1
  1653. Revision 1.1.1.1 1998/03/25 11:18:14 root
  1654. * Restored version
  1655. Revision 1.31 1998/03/24 21:48:33 florian
  1656. * just a couple of fixes applied:
  1657. - problem with fixed16 solved
  1658. - internalerror 10005 problem fixed
  1659. - patch for assembler reading
  1660. - small optimizer fix
  1661. - mem is now supported
  1662. Revision 1.30 1998/03/21 23:59:39 florian
  1663. * indexed properties fixed
  1664. * ppu i/o of properties fixed
  1665. * field can be also used for write access
  1666. * overriding of properties
  1667. Revision 1.29 1998/03/18 22:50:11 florian
  1668. + fstp/fld optimization
  1669. * routines which contains asm aren't longer optimzed
  1670. * wrong ifdef TEST_FUNCRET corrected
  1671. * wrong data generation for array[0..n] of char = '01234'; fixed
  1672. * bug0097 is fixed partial
  1673. * bug0116 fixed (-Og doesn't use enter of the stack frame is greater than
  1674. 65535)
  1675. Revision 1.28 1998/03/10 16:27:41 pierre
  1676. * better line info in stabs debug
  1677. * symtabletype and lexlevel separated into two fields of tsymtable
  1678. + ifdef MAKELIB for direct library output, not complete
  1679. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  1680. working
  1681. + ifdef TESTFUNCRET for setting func result in underfunction, not
  1682. working
  1683. Revision 1.27 1998/03/10 01:17:23 peter
  1684. * all files have the same header
  1685. * messages are fully implemented, EXTDEBUG uses Comment()
  1686. + AG... files for the Assembler generation
  1687. Revision 1.26 1998/03/06 00:52:41 peter
  1688. * replaced all old messages from errore.msg, only ExtDebug and some
  1689. Comment() calls are left
  1690. * fixed options.pas
  1691. Revision 1.25 1998/03/05 22:43:49 florian
  1692. * some win32 support stuff added
  1693. Revision 1.24 1998/03/04 17:33:49 michael
  1694. + Changed ifdef FPK to ifdef FPC
  1695. Revision 1.23 1998/03/04 01:35:06 peter
  1696. * messages for unit-handling and assembler/linker
  1697. * the compiler compiles without -dGDB, but doesn't work yet
  1698. + -vh for Hint
  1699. Revision 1.22 1998/03/02 01:49:00 peter
  1700. * renamed target_DOS to target_GO32V1
  1701. + new verbose system, merged old errors and verbose units into one new
  1702. verbose.pas, so errors.pas is obsolete
  1703. Revision 1.21 1998/02/28 14:43:47 florian
  1704. * final implemenation of win32 imports
  1705. * extended tai_align to allow 8 and 16 byte aligns
  1706. Revision 1.20 1998/02/19 00:11:07 peter
  1707. * fixed -g to work again
  1708. * fixed some typos with the scriptobject
  1709. Revision 1.19 1998/02/13 10:35:23 daniel
  1710. * Made Motorola version compilable.
  1711. * Fixed optimizer
  1712. Revision 1.18 1998/02/12 17:19:19 florian
  1713. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  1714. also that aktswitches isn't a pointer)
  1715. Revision 1.17 1998/02/12 11:50:25 daniel
  1716. Yes! Finally! After three retries, my patch!
  1717. Changes:
  1718. Complete rewrite of psub.pas.
  1719. Added support for DLL's.
  1720. Compiler requires less memory.
  1721. Platform units for each platform.
  1722. Revision 1.16 1998/02/11 21:56:36 florian
  1723. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1724. Revision 1.15 1998/02/06 10:34:25 florian
  1725. * bug0082 and bug0084 fixed
  1726. Revision 1.14 1998/02/02 11:56:49 pierre
  1727. * better line info for var statement
  1728. Revision 1.13 1998/01/30 21:25:31 carl
  1729. * bugfix #86 + checking of all other macros for crashes, fixed typeof
  1730. partly among others.
  1731. Revision 1.12 1998/01/23 17:12:19 pierre
  1732. * added some improvements for as and ld :
  1733. - doserror and dosexitcode treated separately
  1734. - PATH searched if doserror=2
  1735. + start of long and ansi string (far from complete)
  1736. in conditionnal UseLongString and UseAnsiString
  1737. * options.pas cleaned (some variables shifted to globals)gl
  1738. Revision 1.11 1998/01/21 21:25:46 florian
  1739. * small problem with variante records fixed:
  1740. case a : (x,y,z) of
  1741. ...
  1742. is now allowed
  1743. Revision 1.10 1998/01/13 23:11:13 florian
  1744. + class methods
  1745. Revision 1.9 1998/01/12 13:03:31 florian
  1746. + parsing of class methods implemented
  1747. Revision 1.8 1998/01/11 10:54:23 florian
  1748. + generic library support
  1749. Revision 1.7 1998/01/09 23:08:32 florian
  1750. + C++/Delphi styled //-comments
  1751. * some bugs in Delphi object model fixed
  1752. + override directive
  1753. Revision 1.6 1998/01/09 18:01:16 florian
  1754. * VIRTUAL isn't anymore a common keyword
  1755. + DYNAMIC is equal to VIRTUAL
  1756. Revision 1.5 1998/01/09 16:08:23 florian
  1757. * abstract methods call now abstracterrorproc if they are called
  1758. a class with an abstract method can be create with a class reference else
  1759. the compiler forbides this
  1760. Revision 1.4 1998/01/09 13:39:55 florian
  1761. * public, protected and private aren't anymore key words
  1762. + published is equal to public
  1763. Revision 1.3 1998/01/09 13:18:12 florian
  1764. + "forward" class declarations (type tclass = class; )
  1765. Revision 1.2 1998/01/09 09:09:58 michael
  1766. + Initial implementation, second try
  1767. }