pdecl.pas 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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. if smartlink then
  903. datasegment^.concat(new(pai_cut,init));
  904. {$ifdef GDB}
  905. { generate the VMT }
  906. if cs_debuginfo in aktswitches then
  907. begin
  908. do_count_dbx:=true;
  909. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  910. debuglist^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  911. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  912. end;
  913. {$endif * GDB *}
  914. datasegment^.concat(new(pai_symbol,init_global(aktclass^.vmt_mangledname)));
  915. { determine the size with publicsyms^.datasize, because }
  916. { size gives back 4 for CLASSes }
  917. datasegment^.concat(new(pai_const,init_32bit(aktclass^.publicsyms^.datasize)));
  918. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.publicsyms^.datasize)));
  919. { write pointer to parent VMT, this isn't implemented in TP }
  920. { but this is not used in FPC ? (PM) }
  921. { it's not used yet, but the delphi-operators as and is need it (FK) }
  922. if assigned(aktclass^.childof) then
  923. begin
  924. datasegment^.concat(new(pai_const,init_symbol(strpnew(aktclass^.childof^.vmt_mangledname))));
  925. if aktclass^.childof^.owner^.symtabletype=unitsymtable then
  926. concat_external(aktclass^.childof^.vmt_mangledname,EXT_NEAR);
  927. end
  928. else
  929. datasegment^.concat(new(pai_const,init_32bit(0)));
  930. { this generates the entries }
  931. genvmt(aktclass);
  932. { restore old state }
  933. symtablestack:=symtablestack^.next;
  934. procinfo._class:=nil;
  935. {Restore the aktprocsym.}
  936. aktprocsym:=oldprocsym;
  937. object_dec:=aktclass;
  938. end;
  939. { reads a record declaration }
  940. function record_dec : pdef;
  941. var
  942. symtable : psymtable;
  943. begin
  944. symtable:=new(psymtable,init(recordsymtable));
  945. symtable^.next:=symtablestack;
  946. symtablestack:=symtable;
  947. consume(_RECORD);
  948. read_var_decs(true,false);
  949. { may be scale record size to a size of n*4 ? }
  950. if ((symtablestack^.datasize mod aktpackrecords)<>0) then
  951. inc(symtablestack^.datasize,aktpackrecords-(symtablestack^.datasize mod aktpackrecords));
  952. consume(_END);
  953. symtablestack:=symtable^.next;
  954. record_dec:=new(precdef,init(symtable));
  955. end;
  956. { reads a type definition and returns a pointer to it }
  957. function read_type(const name : stringid) : pdef;
  958. function handle_procvar:Pprocvardef;
  959. var
  960. sc : pstringcontainer;
  961. s : string;
  962. p : pdef;
  963. varspez : tvarspez;
  964. procvardef : pprocvardef;
  965. begin
  966. procvardef:=new(pprocvardef,init);
  967. if token=LKLAMMER then
  968. begin
  969. consume(LKLAMMER);
  970. inc(testcurobject);
  971. repeat
  972. if token=_VAR then
  973. begin
  974. consume(_VAR);
  975. varspez:=vs_var;
  976. end
  977. else if token=_CONST then
  978. begin
  979. consume(_CONST);
  980. varspez:=vs_const;
  981. end
  982. else varspez:=vs_value;
  983. sc:=idlist;
  984. if token=COLON then
  985. begin
  986. consume(COLON);
  987. if token=_ARRAY then
  988. begin
  989. {
  990. if (varspez<>vs_const) and
  991. (varspez<>vs_var) then
  992. begin
  993. varspez:=vs_const;
  994. Message(parser_e_illegal_open_parameter);
  995. end;
  996. }
  997. consume(_ARRAY);
  998. consume(_OF);
  999. { define range and type of range }
  1000. p:=new(parraydef,init(0,-1,s32bitdef));
  1001. { define field type }
  1002. parraydef(p)^.definition:=single_type(s);
  1003. end
  1004. else
  1005. p:=single_type(s);
  1006. end
  1007. else
  1008. p:=new(pformaldef,init);
  1009. s:=sc^.get;
  1010. while s<>'' do
  1011. begin
  1012. procvardef^.concatdef(p,varspez);
  1013. s:=sc^.get;
  1014. end;
  1015. dispose(sc,done);
  1016. if token=SEMICOLON then consume(SEMICOLON)
  1017. else break;
  1018. until false;
  1019. dec(testcurobject);
  1020. consume(RKLAMMER);
  1021. if token=_OF then
  1022. begin
  1023. consume(_OF);
  1024. consume(_OBJECT);
  1025. procvardef^.options:=procvardef^.options or pomethodpointer;
  1026. end;
  1027. end;
  1028. handle_procvar:=procvardef;
  1029. end;
  1030. var
  1031. hp1,p : pdef;
  1032. aufdef : penumdef;
  1033. aufsym : penumsym;
  1034. ap : parraydef;
  1035. s : stringid;
  1036. l,v,oldaktpackrecords : longint;
  1037. hs : string;
  1038. procedure expr_type;
  1039. var
  1040. pt1,pt2 : ptree;
  1041. begin
  1042. { use of current parsed object ? }
  1043. if (token=ID) and (testcurobject=2) and (curobjectname=pattern) then
  1044. begin
  1045. consume(ID);
  1046. p:=aktobjectdef;
  1047. exit;
  1048. end;
  1049. { we can't accept a equal in type }
  1050. pt1:=comp_expr(not(ignore_equal));
  1051. if (pt1^.treetype=typen) and (token<>POINTPOINT) then
  1052. begin
  1053. { a simple type renaming }
  1054. p:=pt1^.resulttype;
  1055. end
  1056. else
  1057. begin
  1058. { range type }
  1059. consume(POINTPOINT);
  1060. { range type declaration }
  1061. do_firstpass(pt1);
  1062. pt2:=comp_expr(not(ignore_equal));
  1063. do_firstpass(pt2);
  1064. { valid expression ? }
  1065. if (pt1^.treetype<>ordconstn) or
  1066. (pt2^.treetype<>ordconstn) then
  1067. Begin
  1068. Message(sym_e_error_in_type_def);
  1069. { Here we create a node type with a range of 0 }
  1070. { To make sure that no crashes will occur later }
  1071. { on in the compiler. }
  1072. p:=new(porddef,init(uauto,0,0));
  1073. end
  1074. else
  1075. p:=new(porddef,init(uauto,pt1^.value,pt2^.value));
  1076. disposetree(pt2);
  1077. end;
  1078. disposetree(pt1);
  1079. end;
  1080. var
  1081. pt : ptree;
  1082. procedure array_dec;
  1083. begin
  1084. consume(_ARRAY);
  1085. consume(LECKKLAMMER);
  1086. p:=nil;
  1087. repeat
  1088. { read the expression and check it }
  1089. pt:=expr;
  1090. if pt^.treetype=typen then
  1091. begin
  1092. if pt^.resulttype^.deftype=enumdef then
  1093. begin
  1094. if p=nil then
  1095. begin
  1096. ap:=new(parraydef,
  1097. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1098. p:=ap;
  1099. end
  1100. else
  1101. begin
  1102. ap^.definition:=new(parraydef,
  1103. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1104. ap:=parraydef(ap^.definition);
  1105. end;
  1106. end
  1107. else if pt^.resulttype^.deftype=orddef then
  1108. begin
  1109. case porddef(pt^.resulttype)^.typ of
  1110. s8bit,u8bit,s16bit,u16bit,s32bit :
  1111. begin
  1112. if p=nil then
  1113. begin
  1114. ap:=new(parraydef,init(porddef(pt^.resulttype)^.von,
  1115. porddef(pt^.resulttype)^.bis,pt^.resulttype));
  1116. p:=ap;
  1117. end
  1118. else
  1119. begin
  1120. ap^.definition:=new(parraydef,init(porddef(pt^.resulttype)^.von,
  1121. porddef(pt^.resulttype)^.bis,pt^.resulttype));
  1122. ap:=parraydef(ap^.definition);
  1123. end;
  1124. end;
  1125. bool8bit:
  1126. begin
  1127. if p=nil then
  1128. begin
  1129. ap:=new(parraydef,init(0,1,pt^.resulttype));
  1130. p:=ap;
  1131. end
  1132. else
  1133. begin
  1134. ap^.definition:=new(parraydef,init(0,1,pt^.resulttype));
  1135. ap:=parraydef(ap^.definition);
  1136. end;
  1137. end;
  1138. uchar:
  1139. begin
  1140. if p=nil then
  1141. begin
  1142. ap:=new(parraydef,init(0,255,pt^.resulttype));
  1143. p:=ap;
  1144. end
  1145. else
  1146. begin
  1147. ap^.definition:=new(parraydef,init(0,255,pt^.resulttype));
  1148. ap:=parraydef(ap^.definition);
  1149. end;
  1150. end;
  1151. else Message(sym_e_error_in_type_def);
  1152. end;
  1153. end
  1154. else Message(sym_e_error_in_type_def);
  1155. end
  1156. else
  1157. begin
  1158. do_firstpass(pt);
  1159. if (pt^.treetype<>rangen) or
  1160. (pt^.left^.treetype<>ordconstn) then
  1161. Message(sym_e_error_in_type_def);
  1162. { force the registration of the ranges }
  1163. {$ifndef GDB}
  1164. if pt^.right^.resulttype=pdef(s32bitdef) then
  1165. pt^.right^.resulttype:=new(porddef,init(
  1166. s32bit,$80000000,$7fffffff));
  1167. {$endif GDB}
  1168. if p=nil then
  1169. begin
  1170. ap:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1171. p:=ap;
  1172. end
  1173. else
  1174. begin
  1175. ap^.definition:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1176. ap:=parraydef(ap^.definition);
  1177. end;
  1178. end;
  1179. disposetree(pt);
  1180. if token=COMMA then consume(COMMA)
  1181. else break;
  1182. until false;
  1183. consume(RECKKLAMMER);
  1184. consume(_OF);
  1185. hp1:=read_type('');
  1186. { if no error, set element type }
  1187. if assigned(ap) then
  1188. ap^.definition:=hp1;
  1189. end;
  1190. begin
  1191. case token of
  1192. _STRING,_FILE:
  1193. p:=single_type(hs);
  1194. LKLAMMER:
  1195. begin
  1196. consume(LKLAMMER);
  1197. l:=-1;
  1198. aufsym := Nil;
  1199. aufdef:=new(penumdef,init);
  1200. repeat
  1201. s:=pattern;
  1202. consume(ID);
  1203. if token=ASSIGNMENT then
  1204. begin
  1205. consume(ASSIGNMENT);
  1206. v:=get_intconst;
  1207. { please leave that a note, allows type save }
  1208. { declarations in the win32 units ! }
  1209. if v<=l then
  1210. Message(parser_n_duplicate_enum);
  1211. l:=v;
  1212. end
  1213. else
  1214. inc(l);
  1215. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1216. if token=COMMA then
  1217. consume(COMMA)
  1218. else
  1219. break;
  1220. until false;
  1221. aufdef^.max:=l;
  1222. p:=aufdef;
  1223. consume(RKLAMMER);
  1224. end;
  1225. _ARRAY:
  1226. array_dec;
  1227. _SET:
  1228. begin
  1229. consume(_SET);
  1230. consume(_OF);
  1231. hp1:=read_type('');
  1232. case hp1^.deftype of
  1233. enumdef : p:=new(psetdef,init(hp1,penumdef(hp1)^.max));
  1234. orddef : begin
  1235. case porddef(hp1)^.typ of
  1236. uchar : p:=new(psetdef,init(hp1,255));
  1237. u8bit,s8bit,u16bit,s16bit,s32bit :
  1238. begin
  1239. if (porddef(hp1)^.von>=0) then
  1240. p:=new(psetdef,init(hp1,porddef(hp1)^.bis))
  1241. else Message(sym_e_ill_type_decl_set);
  1242. end;
  1243. else Message(sym_e_ill_type_decl_set);
  1244. end;
  1245. end;
  1246. else Message(sym_e_ill_type_decl_set);
  1247. end;
  1248. end;
  1249. CARET:
  1250. begin
  1251. consume(CARET);
  1252. { forwards allowed only inside TYPE statements }
  1253. if typecanbeforward then
  1254. forwardsallowed:=true;
  1255. hp1:=single_type(hs);
  1256. p:=new(ppointerdef,init(hp1));
  1257. {$ifndef GDB}
  1258. if lasttypesym<>nil then
  1259. save_forward(ppointerdef(p),lasttypesym);
  1260. {$else * GDB *}
  1261. {I add big troubles here
  1262. with var p : ^byte in graph.putimage
  1263. because a save_forward was called and
  1264. no resolve forward
  1265. => so the definition was rewritten after
  1266. having been disposed !!
  1267. Strange problems appeared !!!!}
  1268. {Anyhow forwards should only be allowed
  1269. inside a type statement ??
  1270. don't you think so }
  1271. if (lasttypesym<>nil)
  1272. and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  1273. lasttypesym^.forwardpointer:=ppointerdef(p);
  1274. {$endif * GDB *}
  1275. forwardsallowed:=false;
  1276. end;
  1277. _RECORD:
  1278. p:=record_dec;
  1279. _PACKED:
  1280. begin
  1281. consume(_PACKED);
  1282. if token=_ARRAY then
  1283. array_dec
  1284. else
  1285. begin
  1286. oldaktpackrecords:=aktpackrecords;
  1287. aktpackrecords:=1;
  1288. if token in [_CLASS,_OBJECT] then
  1289. p:=object_dec(name,nil)
  1290. else
  1291. p:=record_dec;
  1292. aktpackrecords:=oldaktpackrecords;
  1293. end;
  1294. end;
  1295. _CLASS,
  1296. _OBJECT:
  1297. p:=object_dec(name,nil);
  1298. _PROCEDURE:
  1299. begin
  1300. consume(_PROCEDURE);
  1301. p:=handle_procvar;
  1302. end;
  1303. _FUNCTION:
  1304. begin
  1305. consume(_FUNCTION);
  1306. p:=handle_procvar;
  1307. consume(COLON);
  1308. pprocvardef(p)^.retdef:=single_type(hs);
  1309. end;
  1310. else
  1311. expr_type;
  1312. end;
  1313. read_type:=p;
  1314. end;
  1315. { search in symtablestack used, but not defined type }
  1316. procedure testforward_types(p : psym);{$ifndef FPC}far;{$endif}
  1317. begin
  1318. if (p^.typ=typesym) and ((p^.properties and sp_forwarddef)<>0) then
  1319. Message(sym_e_type_id_not_defined);
  1320. end;
  1321. { reads a type declaration to the symbol table }
  1322. procedure type_dec;
  1323. var
  1324. typename : stringid;
  1325. {$ifdef dummy}
  1326. olddef,newdef : pdef;
  1327. s : string;
  1328. {$endif dummy}
  1329. begin
  1330. block_type:=bt_type;
  1331. consume(_TYPE);
  1332. typecanbeforward:=true;
  1333. repeat
  1334. typename:=pattern;
  1335. consume(ID);
  1336. consume(EQUAL);
  1337. { here you loose the strictness of pascal
  1338. for which a redefinition like
  1339. childtype = parenttype;
  1340. child2type = parenttype;
  1341. does not make the two child types equal !!
  1342. here all vars from childtype and child2type
  1343. get the definition of parenttype !! }
  1344. {$ifdef testequaltype}
  1345. if (token = ID) or (token=_FILE) or (token=_STRING) then
  1346. begin
  1347. olddef := single_type(s);
  1348. { make a clone of olddef }
  1349. { is that ok ??? }
  1350. getmem(newdef,SizeOf(olddef));
  1351. move(olddef^,newdef^,SizeOf(olddef));
  1352. symtablestack^.insert(new(ptypesym,init(typename,newdef)));
  1353. end
  1354. else
  1355. {$endif testequaltype}
  1356. begin
  1357. getsym(typename,false);
  1358. { check if it is the definition of a forward defined class }
  1359. if assigned(srsym) and (token=_CLASS) and
  1360. (srsym^.typ=typesym) and
  1361. (ptypesym(srsym)^.definition^.deftype=objectdef) and
  1362. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oo_isforward)<>0) and
  1363. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oois_class)<>0) then
  1364. begin
  1365. { we can ignore the result }
  1366. { the definition is modified }
  1367. object_dec(typename,pobjectdef(ptypesym(srsym)^.definition));
  1368. end
  1369. else
  1370. symtablestack^.insert(new(ptypesym,init(typename,read_type(typename))));
  1371. end;
  1372. consume(SEMICOLON);
  1373. until token<>ID;
  1374. typecanbeforward:=false;
  1375. {$ifdef tp}
  1376. symtablestack^.foreach(testforward_types);
  1377. {$else}
  1378. symtablestack^.foreach(@testforward_types);
  1379. {$endif}
  1380. resolve_forwards;
  1381. block_type:=bt_general;
  1382. end;
  1383. { parses varaible declarations and inserts them in }
  1384. { the top symbol table of symtablestack }
  1385. procedure var_dec;
  1386. {var
  1387. p : pdef;
  1388. sc : pstringcontainer; }
  1389. begin
  1390. consume(_VAR);
  1391. read_var_decs(false,true);
  1392. end;
  1393. { reads the filed of a record into a }
  1394. { symtablestack, if record=false }
  1395. { variants are forbidden, so this procedure }
  1396. { can be used to read object fields }
  1397. { if absolute is true, ABSOLUTE and file }
  1398. { types are allowed }
  1399. { => the procedure is also used to read }
  1400. { a sequence of variable declaration }
  1401. procedure read_var_decs(is_record : boolean;do_absolute : boolean);
  1402. var
  1403. sc : pstringcontainer;
  1404. s : stringid;
  1405. l : longint;
  1406. code : word;
  1407. hs : string;
  1408. p,casedef : pdef;
  1409. { maxsize contains the max. size of a variant }
  1410. { startvarrec contains the start of the variant part of a record }
  1411. maxsize,startvarrec : longint;
  1412. pt : ptree;
  1413. old_block_type : tblock_type;
  1414. { to handle absolute }
  1415. abssym : pabsolutesym;
  1416. begin
  1417. hs:='';
  1418. old_block_type:=block_type;
  1419. block_type:=bt_type;
  1420. while (token=ID) and
  1421. (pattern<>'PUBLIC') and
  1422. (pattern<>'PRIVATE') and
  1423. (pattern<>'PUBLISHED') and
  1424. (pattern<>'PROTECTED') do
  1425. begin
  1426. sc:=idlist;
  1427. consume(COLON);
  1428. p:=read_type('');
  1429. if do_absolute and (token=ID) and (pattern='ABSOLUTE') then
  1430. begin
  1431. s:=sc^.get;
  1432. if sc^.get<>'' then
  1433. Message(parser_e_absolute_only_one_var);
  1434. dispose(sc,done);
  1435. consume(ID);
  1436. if token=ID then
  1437. begin
  1438. getsym(pattern,true);
  1439. consume(ID);
  1440. { we should check the result type of srsym }
  1441. if not (srsym^.typ in [varsym,typedconstsym]) then
  1442. Message(parser_e_absolute_only_to_var_or_const);
  1443. abssym:=new(pabsolutesym,init(s,p));
  1444. abssym^.typ:=absolutesym;
  1445. abssym^.abstyp:=tovar;
  1446. abssym^.ref:=srsym;
  1447. symtablestack^.insert(abssym);
  1448. end
  1449. else
  1450. if token=CSTRING then
  1451. begin
  1452. abssym:=new(pabsolutesym,init(s,p));
  1453. s:=pattern;
  1454. consume(CSTRING);
  1455. abssym^.typ:=absolutesym;
  1456. abssym^.abstyp:=toasm;
  1457. abssym^.asmname:=stringdup(s);
  1458. symtablestack^.insert(abssym);
  1459. end
  1460. else
  1461. { absolute address ?!? }
  1462. if token=INTCONST then
  1463. begin
  1464. if (target_info.target=target_GO32V2) then
  1465. begin
  1466. abssym:=new(pabsolutesym,init(s,p));
  1467. abssym^.typ:=absolutesym;
  1468. abssym^.abstyp:=toaddr;
  1469. abssym^.absseg:=false;
  1470. s:=pattern;
  1471. consume(INTCONST);
  1472. val(s,abssym^.address,code);
  1473. if token=COLON then
  1474. begin
  1475. consume(token);
  1476. s:=pattern;
  1477. consume(INTCONST);
  1478. val(s,l,code);
  1479. abssym^.address:=abssym^.address shl 4+l;
  1480. abssym^.absseg:=true;
  1481. end;
  1482. symtablestack^.insert(abssym);
  1483. end
  1484. else
  1485. Message(parser_e_absolute_only_to_var_or_const);
  1486. end
  1487. else
  1488. Message(parser_e_absolute_only_to_var_or_const);
  1489. end
  1490. else
  1491. begin
  1492. if token=SEMICOLON then
  1493. begin
  1494. if (symtablestack^.symtabletype=objectsymtable) then
  1495. begin
  1496. consume(SEMICOLON);
  1497. if (token=ID) and (pattern='STATIC') and
  1498. (cs_static_keyword in aktswitches) then
  1499. begin
  1500. current_object_option:=current_object_option or sp_static;
  1501. insert_syms(symtablestack,sc,p);
  1502. current_object_option:=current_object_option - sp_static;
  1503. consume(ID);
  1504. consume(SEMICOLON);
  1505. end
  1506. else
  1507. { this will still be a the wrong line !! }
  1508. insert_syms(symtablestack,sc,p);
  1509. end
  1510. else
  1511. begin
  1512. { at the right line }
  1513. insert_syms(symtablestack,sc,p);
  1514. consume(SEMICOLON);
  1515. end
  1516. end
  1517. else
  1518. begin
  1519. insert_syms(symtablestack,sc,p);
  1520. if not(is_record) then
  1521. consume(SEMICOLON);
  1522. end;
  1523. end;
  1524. while token=SEMICOLON do
  1525. consume(SEMICOLON);
  1526. end;
  1527. if (token=_CASE) and is_record then
  1528. begin
  1529. maxsize:=0;
  1530. consume(_CASE);
  1531. s:=pattern;
  1532. getsym(s,false);
  1533. { may be only a type: }
  1534. if assigned(srsym) and ((srsym^.typ=typesym) or
  1535. { and with unit qualifier: }
  1536. (srsym^.typ=unitsym)) then
  1537. begin
  1538. casedef:=read_type('');
  1539. end
  1540. else
  1541. begin
  1542. consume(ID);
  1543. consume(COLON);
  1544. casedef:=read_type('');
  1545. symtablestack^.insert(new(pvarsym,init(s,casedef)));
  1546. end;
  1547. if not is_ordinal(casedef) then
  1548. Message(parser_e_ordinal_expected);
  1549. consume(_OF);
  1550. startvarrec:=symtablestack^.datasize;
  1551. repeat
  1552. repeat
  1553. pt:=expr;
  1554. do_firstpass(pt);
  1555. if not(pt^.treetype=ordconstn) then
  1556. Message(cg_e_illegal_expression);
  1557. disposetree(pt);
  1558. if token=COMMA then consume(COMMA)
  1559. else break;
  1560. until false;
  1561. consume(COLON);
  1562. consume(LKLAMMER);
  1563. if token<>RKLAMMER then
  1564. read_var_decs(true,false);
  1565. { calculates maximal variant size }
  1566. maxsize:=max(maxsize,symtablestack^.datasize);
  1567. { the items of the next variant are overlayed }
  1568. symtablestack^.datasize:=startvarrec;
  1569. consume(RKLAMMER);
  1570. if token<>SEMICOLON then
  1571. break
  1572. else
  1573. consume(SEMICOLON);
  1574. while token=SEMICOLON do
  1575. consume(SEMICOLON);
  1576. until (token=_END) or (token=RKLAMMER);
  1577. { at last set the record size to that of the biggest variant }
  1578. symtablestack^.datasize:=maxsize;
  1579. end;
  1580. block_type:=old_block_type;
  1581. end;
  1582. procedure read_declarations(islibrary : boolean);
  1583. begin
  1584. repeat
  1585. case token of
  1586. _LABEL:
  1587. label_dec;
  1588. _CONST:
  1589. const_dec;
  1590. _TYPE:
  1591. type_dec;
  1592. _VAR:
  1593. var_dec;
  1594. _CONSTRUCTOR,_DESTRUCTOR,
  1595. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1596. unter_dec;
  1597. _EXPORTS:
  1598. if islibrary then
  1599. read_exports
  1600. else
  1601. break;
  1602. else break;
  1603. end;
  1604. until false;
  1605. end;
  1606. procedure read_interface_declarations;
  1607. begin
  1608. {Since the body is now parsed at lexlevel 1, and the declarations
  1609. must be parsed at the same lexlevel we increase the lexlevel.}
  1610. inc(lexlevel);
  1611. repeat
  1612. case token of
  1613. _CONST : const_dec;
  1614. _TYPE : type_dec;
  1615. _VAR : var_dec;
  1616. { should we allow operator in interface ? }
  1617. { of course otherwise you cannot }
  1618. { declare an operator usable by other }
  1619. { units or progs PM }
  1620. _FUNCTION,_PROCEDURE,_OPERATOR : unter_dec;
  1621. else
  1622. break;
  1623. end;
  1624. until false;
  1625. dec(lexlevel);
  1626. end;
  1627. end.
  1628. {
  1629. $Log$
  1630. Revision 1.10 1998-04-27 23:10:28 peter
  1631. + new scanner
  1632. * $makelib -> if smartlink
  1633. * small filename fixes pmodule.setfilename
  1634. * moved import from files.pas -> import.pas
  1635. Revision 1.9 1998/04/10 21:36:56 florian
  1636. + some stuff to support method pointers (procedure of object) added
  1637. (declaration, parameter handling)
  1638. Revision 1.8 1998/04/10 15:39:48 florian
  1639. * more fixes to get classes.pas compiled
  1640. Revision 1.7 1998/04/09 23:02:15 florian
  1641. * small problems solved to get remake3 work
  1642. Revision 1.6 1998/04/09 22:16:35 florian
  1643. * problem with previous REGALLOC solved
  1644. * improved property support
  1645. Revision 1.5 1998/04/08 14:59:20 florian
  1646. * problem with new expr_type solved
  1647. Revision 1.4 1998/04/08 10:26:09 florian
  1648. * correct error handling of virtual constructors
  1649. * problem with new type declaration handling fixed
  1650. Revision 1.3 1998/04/07 22:45:05 florian
  1651. * bug0092, bug0115 and bug0121 fixed
  1652. + packed object/class/array
  1653. Revision 1.2 1998/04/05 13:58:35 peter
  1654. * fixed the -Ss bug
  1655. + warning for Virtual constructors
  1656. * helppages updated with -TGO32V1
  1657. Revision 1.1.1.1 1998/03/25 11:18:14 root
  1658. * Restored version
  1659. Revision 1.31 1998/03/24 21:48:33 florian
  1660. * just a couple of fixes applied:
  1661. - problem with fixed16 solved
  1662. - internalerror 10005 problem fixed
  1663. - patch for assembler reading
  1664. - small optimizer fix
  1665. - mem is now supported
  1666. Revision 1.30 1998/03/21 23:59:39 florian
  1667. * indexed properties fixed
  1668. * ppu i/o of properties fixed
  1669. * field can be also used for write access
  1670. * overriding of properties
  1671. Revision 1.29 1998/03/18 22:50:11 florian
  1672. + fstp/fld optimization
  1673. * routines which contains asm aren't longer optimzed
  1674. * wrong ifdef TEST_FUNCRET corrected
  1675. * wrong data generation for array[0..n] of char = '01234'; fixed
  1676. * bug0097 is fixed partial
  1677. * bug0116 fixed (-Og doesn't use enter of the stack frame is greater than
  1678. 65535)
  1679. Revision 1.28 1998/03/10 16:27:41 pierre
  1680. * better line info in stabs debug
  1681. * symtabletype and lexlevel separated into two fields of tsymtable
  1682. + ifdef MAKELIB for direct library output, not complete
  1683. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  1684. working
  1685. + ifdef TESTFUNCRET for setting func result in underfunction, not
  1686. working
  1687. Revision 1.27 1998/03/10 01:17:23 peter
  1688. * all files have the same header
  1689. * messages are fully implemented, EXTDEBUG uses Comment()
  1690. + AG... files for the Assembler generation
  1691. Revision 1.26 1998/03/06 00:52:41 peter
  1692. * replaced all old messages from errore.msg, only ExtDebug and some
  1693. Comment() calls are left
  1694. * fixed options.pas
  1695. Revision 1.25 1998/03/05 22:43:49 florian
  1696. * some win32 support stuff added
  1697. Revision 1.24 1998/03/04 17:33:49 michael
  1698. + Changed ifdef FPK to ifdef FPC
  1699. Revision 1.23 1998/03/04 01:35:06 peter
  1700. * messages for unit-handling and assembler/linker
  1701. * the compiler compiles without -dGDB, but doesn't work yet
  1702. + -vh for Hint
  1703. Revision 1.22 1998/03/02 01:49:00 peter
  1704. * renamed target_DOS to target_GO32V1
  1705. + new verbose system, merged old errors and verbose units into one new
  1706. verbose.pas, so errors.pas is obsolete
  1707. Revision 1.21 1998/02/28 14:43:47 florian
  1708. * final implemenation of win32 imports
  1709. * extended tai_align to allow 8 and 16 byte aligns
  1710. Revision 1.20 1998/02/19 00:11:07 peter
  1711. * fixed -g to work again
  1712. * fixed some typos with the scriptobject
  1713. Revision 1.19 1998/02/13 10:35:23 daniel
  1714. * Made Motorola version compilable.
  1715. * Fixed optimizer
  1716. Revision 1.18 1998/02/12 17:19:19 florian
  1717. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  1718. also that aktswitches isn't a pointer)
  1719. Revision 1.17 1998/02/12 11:50:25 daniel
  1720. Yes! Finally! After three retries, my patch!
  1721. Changes:
  1722. Complete rewrite of psub.pas.
  1723. Added support for DLL's.
  1724. Compiler requires less memory.
  1725. Platform units for each platform.
  1726. Revision 1.16 1998/02/11 21:56:36 florian
  1727. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1728. Revision 1.15 1998/02/06 10:34:25 florian
  1729. * bug0082 and bug0084 fixed
  1730. Revision 1.14 1998/02/02 11:56:49 pierre
  1731. * better line info for var statement
  1732. Revision 1.13 1998/01/30 21:25:31 carl
  1733. * bugfix #86 + checking of all other macros for crashes, fixed typeof
  1734. partly among others.
  1735. Revision 1.12 1998/01/23 17:12:19 pierre
  1736. * added some improvements for as and ld :
  1737. - doserror and dosexitcode treated separately
  1738. - PATH searched if doserror=2
  1739. + start of long and ansi string (far from complete)
  1740. in conditionnal UseLongString and UseAnsiString
  1741. * options.pas cleaned (some variables shifted to globals)gl
  1742. Revision 1.11 1998/01/21 21:25:46 florian
  1743. * small problem with variante records fixed:
  1744. case a : (x,y,z) of
  1745. ...
  1746. is now allowed
  1747. Revision 1.10 1998/01/13 23:11:13 florian
  1748. + class methods
  1749. Revision 1.9 1998/01/12 13:03:31 florian
  1750. + parsing of class methods implemented
  1751. Revision 1.8 1998/01/11 10:54:23 florian
  1752. + generic library support
  1753. Revision 1.7 1998/01/09 23:08:32 florian
  1754. + C++/Delphi styled //-comments
  1755. * some bugs in Delphi object model fixed
  1756. + override directive
  1757. Revision 1.6 1998/01/09 18:01:16 florian
  1758. * VIRTUAL isn't anymore a common keyword
  1759. + DYNAMIC is equal to VIRTUAL
  1760. Revision 1.5 1998/01/09 16:08:23 florian
  1761. * abstract methods call now abstracterrorproc if they are called
  1762. a class with an abstract method can be create with a class reference else
  1763. the compiler forbides this
  1764. Revision 1.4 1998/01/09 13:39:55 florian
  1765. * public, protected and private aren't anymore key words
  1766. + published is equal to public
  1767. Revision 1.3 1998/01/09 13:18:12 florian
  1768. + "forward" class declarations (type tclass = class; )
  1769. Revision 1.2 1998/01/09 09:09:58 michael
  1770. + Initial implementation, second try
  1771. }