ptype.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does parsing types 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 ptype;
  19. interface
  20. uses
  21. globtype,symtable;
  22. const
  23. { forward types should only be possible inside a TYPE statement }
  24. typecanbeforward : boolean = false;
  25. var
  26. { hack, which allows to use the current parsed }
  27. { object type as function argument type }
  28. testcurobject : byte;
  29. curobjectname : stringid;
  30. { parses a string declaration }
  31. function string_dec : pdef;
  32. { parses a object declaration }
  33. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  34. { reads a string, file type or a type id and returns a name and }
  35. { pdef }
  36. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  37. procedure read_type(var tt:ttype;const name : stringid);
  38. implementation
  39. uses
  40. cobjects,globals,verbose,systems,tokens,
  41. aasm,symconst,types,
  42. {$ifdef GDB}
  43. gdb,
  44. {$endif}
  45. tree,hcodegen,hcgdata,
  46. scanner,pbase,pexpr,pdecl,psub,
  47. {$ifdef newcg}
  48. cgbase,
  49. {$else}
  50. tccnv,
  51. {$endif}
  52. pass_1;
  53. function string_dec : pdef;
  54. { reads a string type with optional length }
  55. { and returns a pointer to the string }
  56. { definition }
  57. var
  58. p : ptree;
  59. d : pdef;
  60. begin
  61. consume(_STRING);
  62. if token=_LECKKLAMMER then
  63. begin
  64. consume(_LECKKLAMMER);
  65. p:=comp_expr(true);
  66. do_firstpass(p);
  67. if not is_constintnode(p) then
  68. Message(cg_e_illegal_expression);
  69. if (p^.value<=0) then
  70. begin
  71. Message(parser_e_invalid_string_size);
  72. p^.value:=255;
  73. end;
  74. consume(_RECKKLAMMER);
  75. if p^.value>255 then
  76. d:=new(pstringdef,longinit(p^.value))
  77. else
  78. if p^.value<>255 then
  79. d:=new(pstringdef,shortinit(p^.value))
  80. else
  81. d:=cshortstringdef;
  82. disposetree(p);
  83. end
  84. else
  85. begin
  86. if cs_ansistrings in aktlocalswitches then
  87. d:=cansistringdef
  88. else
  89. d:=cshortstringdef;
  90. end;
  91. string_dec:=d;
  92. end;
  93. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  94. { reads a type definition }
  95. { to a appropriating pdef, s gets the name of }
  96. { the type to allow name mangling }
  97. var
  98. is_unit_specific : boolean;
  99. pos : tfileposinfo;
  100. begin
  101. s:=pattern;
  102. pos:=tokenpos;
  103. { classes can be used also in classes }
  104. if (curobjectname=pattern) and aktobjectdef^.is_class then
  105. begin
  106. tt.setdef(aktobjectdef);
  107. consume(_ID);
  108. exit;
  109. end;
  110. { objects can be parameters }
  111. if (testcurobject=2) and (curobjectname=pattern) then
  112. begin
  113. tt.setdef(aktobjectdef);
  114. consume(_ID);
  115. exit;
  116. end;
  117. { try to load the symbol to see if it's a unitsym }
  118. is_unit_specific:=false;
  119. getsym(s,false);
  120. consume(_ID);
  121. if assigned(srsym) and
  122. (srsym^.typ=unitsym) then
  123. begin
  124. consume(_POINT);
  125. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  126. pos:=tokenpos;
  127. s:=pattern;
  128. consume(_ID);
  129. is_unit_specific:=true;
  130. end;
  131. { are we parsing a possible forward def ? }
  132. if isforwarddef and
  133. not(is_unit_specific) then
  134. begin
  135. tt.setdef(new(pforwarddef,init(s,pos)));
  136. exit;
  137. end;
  138. { unknown sym ? }
  139. if not assigned(srsym) then
  140. begin
  141. Message1(sym_e_id_not_found,s);
  142. tt.setdef(generrordef);
  143. exit;
  144. end;
  145. if (srsym^.typ<>typesym) then
  146. begin
  147. Message(type_e_type_id_expected);
  148. tt.setdef(generrordef);
  149. exit;
  150. end;
  151. { Only use the definitions for system/current unit, becuase
  152. they can be refered from the parameters and symbols are not
  153. loaded at that time. A symbol reference to an other unit
  154. is still possible, because it's already loaded (PFV)
  155. can't use in [] here, becuase unitid can be > 255 }
  156. if (ptypesym(srsym)^.owner^.unitid=0) or
  157. (ptypesym(srsym)^.owner^.unitid=1) then
  158. tt.setdef(ptypesym(srsym)^.restype.def)
  159. else
  160. tt.setsym(srsym);
  161. end;
  162. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  163. { reads a string, file type or a type id and returns a name and }
  164. { pdef }
  165. var
  166. hs : string;
  167. t2 : ttype;
  168. begin
  169. case token of
  170. _STRING:
  171. begin
  172. tt.setdef(string_dec);
  173. s:='STRING';
  174. end;
  175. _FILE:
  176. begin
  177. consume(_FILE);
  178. if token=_OF then
  179. begin
  180. consume(_OF);
  181. single_type(t2,hs,false);
  182. tt.setdef(new(pfiledef,inittyped(t2)));
  183. s:='FILE$OF$'+hs;
  184. end
  185. else
  186. begin
  187. tt.setdef(cfiledef);
  188. s:='FILE';
  189. end;
  190. end;
  191. else
  192. begin
  193. id_type(tt,s,isforwarddef);
  194. end;
  195. end;
  196. end;
  197. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  198. { this function parses an object or class declaration }
  199. var
  200. actmembertype : tsymoptions;
  201. there_is_a_destructor : boolean;
  202. is_a_class : boolean;
  203. childof : pobjectdef;
  204. aktclass : pobjectdef;
  205. procedure constructor_head;
  206. begin
  207. consume(_CONSTRUCTOR);
  208. { must be at same level as in implementation }
  209. inc(lexlevel);
  210. parse_proc_head(potype_constructor);
  211. dec(lexlevel);
  212. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'INIT') then
  213. Message(parser_e_constructorname_must_be_init);
  214. {$ifdef INCLUDEOK}
  215. include(aktclass^.objectoptions,oo_has_constructor);
  216. {$else}
  217. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_constructor];
  218. {$endif}
  219. consume(_SEMICOLON);
  220. begin
  221. if (aktclass^.is_class) then
  222. begin
  223. { CLASS constructors return the created instance }
  224. aktprocsym^.definition^.rettype.def:=aktclass;
  225. end
  226. else
  227. begin
  228. { OBJECT constructors return a boolean }
  229. aktprocsym^.definition^.rettype.setdef(booldef);
  230. end;
  231. end;
  232. end;
  233. procedure property_dec;
  234. var
  235. sym : psym;
  236. propertyparas : plinkedlist;
  237. { returns the matching procedure to access a property }
  238. function get_procdef : pprocdef;
  239. var
  240. p : pprocdef;
  241. begin
  242. p:=pprocsym(sym)^.definition;
  243. get_procdef:=nil;
  244. while assigned(p) do
  245. begin
  246. if equal_paras(p^.para,propertyparas,true) then
  247. break;
  248. p:=p^.nextoverloaded;
  249. end;
  250. get_procdef:=p;
  251. end;
  252. var
  253. hp2,datacoll : pparaitem;
  254. p,p2 : ppropertysym;
  255. overriden : psym;
  256. hs : string;
  257. varspez : tvarspez;
  258. sc : pstringcontainer;
  259. s : string;
  260. tt : ttype;
  261. declarepos : tfileposinfo;
  262. pp : pprocdef;
  263. pt : ptree;
  264. propname : stringid;
  265. begin
  266. { check for a class }
  267. if not(aktclass^.is_class) then
  268. Message(parser_e_syntax_error);
  269. consume(_PROPERTY);
  270. new(propertyparas,init);
  271. datacoll:=nil;
  272. if token=_ID then
  273. begin
  274. p:=new(ppropertysym,init(pattern));
  275. propname:=pattern;
  276. consume(_ID);
  277. { property parameters ? }
  278. if token=_LECKKLAMMER then
  279. begin
  280. if (sp_published in current_object_option) then
  281. Message(parser_e_cant_publish_that_property);
  282. { create a list of the parameters in propertyparas }
  283. consume(_LECKKLAMMER);
  284. inc(testcurobject);
  285. repeat
  286. if token=_VAR then
  287. begin
  288. consume(_VAR);
  289. varspez:=vs_var;
  290. end
  291. else if token=_CONST then
  292. begin
  293. consume(_CONST);
  294. varspez:=vs_const;
  295. end
  296. else varspez:=vs_value;
  297. sc:=idlist;
  298. {$ifdef fixLeaksOnError}
  299. strContStack.push(sc);
  300. {$endif fixLeaksOnError}
  301. if token=_COLON then
  302. begin
  303. consume(_COLON);
  304. if token=_ARRAY then
  305. begin
  306. {
  307. if (varspez<>vs_const) and
  308. (varspez<>vs_var) then
  309. begin
  310. varspez:=vs_const;
  311. Message(parser_e_illegal_open_parameter);
  312. end;
  313. }
  314. consume(_ARRAY);
  315. consume(_OF);
  316. { define range and type of range }
  317. tt.setdef(new(parraydef,init(0,-1,s32bitdef)));
  318. { define field type }
  319. single_type(parraydef(tt.def)^.elementtype,s,false);
  320. end
  321. else
  322. single_type(tt,s,false);
  323. end
  324. else
  325. tt.setdef(cformaldef);
  326. repeat
  327. s:=sc^.get_with_tokeninfo(declarepos);
  328. if s='' then
  329. break;
  330. new(hp2,init);
  331. hp2^.paratyp:=varspez;
  332. hp2^.paratype:=tt;
  333. propertyparas^.insert(hp2);
  334. until false;
  335. {$ifdef fixLeaksOnError}
  336. if strContStack.pop <> sc then
  337. writeln('problem with strContStack in ptype');
  338. {$endif fixLeaksOnError}
  339. dispose(sc,done);
  340. until not try_to_consume(_SEMICOLON);
  341. dec(testcurobject);
  342. consume(_RECKKLAMMER);
  343. end;
  344. { overriden property ? }
  345. { force property interface, if there is a property parameter }
  346. if (token=_COLON) or not(propertyparas^.empty) then
  347. begin
  348. consume(_COLON);
  349. single_type(p^.proptype,hs,false);
  350. if (idtoken=_INDEX) then
  351. begin
  352. consume(_INDEX);
  353. pt:=comp_expr(true);
  354. do_firstpass(pt);
  355. if not(is_ordinal(pt^.resulttype)) or
  356. is_64bitint(pt^.resulttype) then
  357. Message(parser_e_invalid_property_index_value);
  358. p^.index:=pt^.value;
  359. p^.indextype.setdef(pt^.resulttype);
  360. include(p^.propoptions,ppo_indexed);
  361. { concat a longint to the para template }
  362. new(hp2,init);
  363. hp2^.paratyp:=vs_value;
  364. hp2^.paratype:=p^.indextype;
  365. propertyparas^.insert(hp2);
  366. disposetree(pt);
  367. end;
  368. { the parser need to know if a property has parameters }
  369. if not(propertyparas^.empty) then
  370. p^.propoptions:=p^.propoptions+[ppo_hasparameters];
  371. end
  372. else
  373. begin
  374. { do an property override }
  375. overriden:=search_class_member(aktclass,propname);
  376. if assigned(overriden) and (overriden^.typ=propertysym) then
  377. begin
  378. p^.dooverride(ppropertysym(overriden));
  379. end
  380. else
  381. begin
  382. p^.proptype.setdef(generrordef);
  383. message(parser_e_no_property_found_to_override);
  384. end;
  385. end;
  386. if (sp_published in current_object_option) and
  387. not(p^.proptype.def^.is_publishable) then
  388. Message(parser_e_cant_publish_that_property);
  389. { create data defcoll to allow correct parameter checks }
  390. new(datacoll,init);
  391. datacoll^.paratyp:=vs_value;
  392. datacoll^.paratype:=p^.proptype;
  393. if (idtoken=_READ) then
  394. begin
  395. p^.readaccess^.clear;
  396. consume(_READ);
  397. sym:=search_class_member(aktclass,pattern);
  398. if not(assigned(sym)) then
  399. begin
  400. Message1(sym_e_unknown_id,pattern);
  401. consume(_ID);
  402. end
  403. else
  404. begin
  405. consume(_ID);
  406. while (token=_POINT) and
  407. ((sym^.typ=varsym) and
  408. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  409. begin
  410. p^.readaccess^.addsym(sym);
  411. consume(_POINT);
  412. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  413. if not assigned(srsym) then
  414. Message1(sym_e_illegal_field,pattern);
  415. sym:=srsym;
  416. consume(_ID);
  417. end;
  418. end;
  419. if assigned(sym) then
  420. begin
  421. { search the matching definition }
  422. case sym^.typ of
  423. procsym :
  424. begin
  425. pp:=get_procdef;
  426. if not(assigned(pp)) or
  427. not(is_equal(pp^.rettype.def,p^.proptype.def)) then
  428. Message(parser_e_ill_property_access_sym);
  429. p^.readaccess^.setdef(pp);
  430. end;
  431. varsym :
  432. begin
  433. if not(propertyparas^.empty) or
  434. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  435. Message(parser_e_ill_property_access_sym);
  436. end;
  437. else
  438. Message(parser_e_ill_property_access_sym);
  439. end;
  440. p^.readaccess^.addsym(sym);
  441. end;
  442. end;
  443. if (idtoken=_WRITE) then
  444. begin
  445. p^.writeaccess^.clear;
  446. consume(_WRITE);
  447. sym:=search_class_member(aktclass,pattern);
  448. if not(assigned(sym)) then
  449. begin
  450. Message1(sym_e_unknown_id,pattern);
  451. consume(_ID);
  452. end
  453. else
  454. begin
  455. consume(_ID);
  456. while (token=_POINT) and
  457. ((sym^.typ=varsym) and
  458. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  459. begin
  460. p^.writeaccess^.addsym(sym);
  461. consume(_POINT);
  462. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  463. if not assigned(srsym) then
  464. Message1(sym_e_illegal_field,pattern);
  465. sym:=srsym;
  466. consume(_ID);
  467. end;
  468. end;
  469. if assigned(sym) then
  470. begin
  471. { search the matching definition }
  472. case sym^.typ of
  473. procsym :
  474. begin
  475. { insert data entry to check access method }
  476. propertyparas^.insert(datacoll);
  477. pp:=get_procdef;
  478. { ... and remove it }
  479. propertyparas^.remove(datacoll);
  480. if not(assigned(pp)) then
  481. Message(parser_e_ill_property_access_sym);
  482. p^.writeaccess^.setdef(pp);
  483. end;
  484. varsym :
  485. begin
  486. if not(propertyparas^.empty) or
  487. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  488. Message(parser_e_ill_property_access_sym);
  489. end
  490. else
  491. Message(parser_e_ill_property_access_sym);
  492. end;
  493. p^.writeaccess^.addsym(sym);
  494. end;
  495. end;
  496. include(p^.propoptions,ppo_stored);
  497. if (idtoken=_STORED) then
  498. begin
  499. consume(_STORED);
  500. p^.storedaccess^.clear;
  501. case token of
  502. _ID:
  503. { in the case that idtoken=_DEFAULT }
  504. { we have to do nothing except }
  505. { setting ppo_stored, it's the same }
  506. { as stored true }
  507. if idtoken<>_DEFAULT then
  508. begin
  509. sym:=search_class_member(aktclass,pattern);
  510. if not(assigned(sym)) then
  511. begin
  512. Message1(sym_e_unknown_id,pattern);
  513. consume(_ID);
  514. end
  515. else
  516. begin
  517. consume(_ID);
  518. while (token=_POINT) and
  519. ((sym^.typ=varsym) and
  520. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  521. begin
  522. p^.storedaccess^.addsym(sym);
  523. consume(_POINT);
  524. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  525. if not assigned(srsym) then
  526. Message1(sym_e_illegal_field,pattern);
  527. sym:=srsym;
  528. consume(_ID);
  529. end;
  530. end;
  531. if assigned(sym) then
  532. begin
  533. { only non array properties can be stored }
  534. case sym^.typ of
  535. procsym :
  536. begin
  537. pp:=pprocsym(sym)^.definition;
  538. while assigned(pp) do
  539. begin
  540. { the stored function shouldn't have any parameters }
  541. if pp^.para^.empty then
  542. break;
  543. pp:=pp^.nextoverloaded;
  544. end;
  545. { found we a procedure and does it really return a bool? }
  546. if not(assigned(pp)) or
  547. not(is_equal(pp^.rettype.def,booldef)) then
  548. Message(parser_e_ill_property_storage_sym);
  549. p^.storedaccess^.setdef(pp);
  550. end;
  551. varsym :
  552. begin
  553. if not(propertyparas^.empty) or
  554. not(is_equal(pvarsym(sym)^.vartype.def,booldef)) then
  555. Message(parser_e_stored_property_must_be_boolean);
  556. end;
  557. else
  558. Message(parser_e_ill_property_storage_sym);
  559. end;
  560. p^.storedaccess^.addsym(sym);
  561. end;
  562. end;
  563. _FALSE:
  564. begin
  565. consume(_FALSE);
  566. exclude(p^.propoptions,ppo_stored);
  567. end;
  568. _TRUE:
  569. consume(_TRUE);
  570. end;
  571. end;
  572. if (idtoken=_DEFAULT) then
  573. begin
  574. consume(_DEFAULT);
  575. if not(is_ordinal(p^.proptype.def) or
  576. is_64bitint(p^.proptype.def) or
  577. ((p^.proptype.def^.deftype=setdef) and
  578. (psetdef(p^.proptype.def)^.settype=smallset)) or
  579. not(propertyparas^.empty)
  580. ) then
  581. Message(parser_e_property_cant_have_a_default_value);
  582. { Get the result of the default, the firstpass is
  583. needed to support values like -1 }
  584. pt:=comp_expr(true);
  585. do_firstpass(pt);
  586. if p^.proptype.def^.deftype=setdef then
  587. begin
  588. {$ifndef newcg}
  589. {!!!!!!!!!!}
  590. arrayconstructor_to_set(pt);
  591. {$endif newcg}
  592. do_firstpass(pt);
  593. end;
  594. pt:=gentypeconvnode(pt,p^.proptype.def);
  595. do_firstpass(pt);
  596. if not(is_constnode(pt)) then
  597. Message(parser_e_property_default_value_must_const);
  598. if pt^.treetype=setconstn then
  599. p^.default:=plongint(pt^.value_set)^
  600. else
  601. p^.default:=pt^.value;
  602. disposetree(pt);
  603. end
  604. else if (idtoken=_NODEFAULT) then
  605. begin
  606. consume(_NODEFAULT);
  607. p^.default:=0;
  608. end;
  609. symtablestack^.insert(p);
  610. { default property ? }
  611. consume(_SEMICOLON);
  612. if (idtoken=_DEFAULT) then
  613. begin
  614. consume(_DEFAULT);
  615. p2:=search_default_property(aktclass);
  616. if assigned(p2) then
  617. message1(parser_e_only_one_default_property,
  618. pobjectdef(p2^.owner^.defowner)^.objname^)
  619. else
  620. begin
  621. {$ifdef INCLUDEOK}
  622. include(p^.propoptions,ppo_defaultproperty);
  623. {$else}
  624. p^.propoptions:=p^.propoptions+[ppo_defaultproperty];
  625. {$endif}
  626. if propertyparas^.empty then
  627. message(parser_e_property_need_paras);
  628. end;
  629. consume(_SEMICOLON);
  630. end;
  631. { clean up }
  632. if assigned(datacoll) then
  633. dispose(datacoll,done);
  634. end
  635. else
  636. begin
  637. consume(_ID);
  638. consume(_SEMICOLON);
  639. end;
  640. dispose(propertyparas,done);
  641. end;
  642. procedure destructor_head;
  643. begin
  644. consume(_DESTRUCTOR);
  645. inc(lexlevel);
  646. parse_proc_head(potype_destructor);
  647. dec(lexlevel);
  648. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  649. Message(parser_e_destructorname_must_be_done);
  650. {$ifdef INCLUDEOK}
  651. include(aktclass^.objectoptions,oo_has_destructor);
  652. {$else}
  653. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_destructor];
  654. {$endif}
  655. consume(_SEMICOLON);
  656. if not(aktprocsym^.definition^.para^.empty) then
  657. Message(parser_e_no_paras_for_destructor);
  658. { no return value }
  659. aktprocsym^.definition^.rettype.def:=voiddef;
  660. end;
  661. var
  662. hs : string;
  663. pcrd : pclassrefdef;
  664. tt : ttype;
  665. oldprocinfo : pprocinfo;
  666. oldprocsym : pprocsym;
  667. oldparse_only : boolean;
  668. methodnametable,intmessagetable,
  669. strmessagetable,classnamelabel : pasmlabel;
  670. storetypecanbeforward : boolean;
  671. vmtlist : taasmoutput;
  672. begin
  673. {Nowadays aktprocsym may already have a value, so we need to save
  674. it.}
  675. oldprocsym:=aktprocsym;
  676. { forward is resolved }
  677. if assigned(fd) then
  678. {$ifdef INCLUDEOK}
  679. exclude(fd^.objectoptions,oo_is_forward);
  680. {$else}
  681. fd^.objectoptions:=fd^.objectoptions-[oo_is_forward];
  682. {$endif}
  683. there_is_a_destructor:=false;
  684. actmembertype:=[sp_public];
  685. { objects and class types can't be declared local }
  686. if (symtablestack^.symtabletype<>globalsymtable) and
  687. (symtablestack^.symtabletype<>staticsymtable) then
  688. Message(parser_e_no_local_objects);
  689. storetypecanbeforward:=typecanbeforward;
  690. { for tp mode don't allow forward types }
  691. if m_tp in aktmodeswitches then
  692. typecanbeforward:=false;
  693. { distinguish classes and objects }
  694. if token=_OBJECT then
  695. begin
  696. is_a_class:=false;
  697. consume(_OBJECT)
  698. end
  699. else
  700. begin
  701. is_a_class:=true;
  702. consume(_CLASS);
  703. if not(assigned(fd)) and (token=_OF) then
  704. begin
  705. { a hack, but it's easy to handle }
  706. { class reference type }
  707. consume(_OF);
  708. single_type(tt,hs,typecanbeforward);
  709. { accept hp1, if is a forward def or a class }
  710. if (tt.def^.deftype=forwarddef) or
  711. ((tt.def^.deftype=objectdef) and pobjectdef(tt.def)^.is_class) then
  712. begin
  713. pcrd:=new(pclassrefdef,init(tt.def));
  714. object_dec:=pcrd;
  715. end
  716. else
  717. begin
  718. object_dec:=generrordef;
  719. Message1(type_e_class_type_expected,generrordef^.typename);
  720. end;
  721. typecanbeforward:=storetypecanbeforward;
  722. exit;
  723. end
  724. { forward class }
  725. else if not(assigned(fd)) and (token=_SEMICOLON) then
  726. begin
  727. { also anonym objects aren't allow (o : object a : longint; end;) }
  728. if n='' then
  729. begin
  730. Message(parser_f_no_anonym_objects)
  731. end;
  732. if n='TOBJECT' then
  733. begin
  734. aktclass:=new(pobjectdef,init(n,nil));
  735. class_tobject:=aktclass;
  736. end
  737. else
  738. aktclass:=new(pobjectdef,init(n,nil));
  739. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class,oo_is_forward];
  740. { all classes must have a vmt !! at offset zero }
  741. if not(oo_has_vmt in aktclass^.objectoptions) then
  742. aktclass^.insertvmt;
  743. object_dec:=aktclass;
  744. typecanbeforward:=storetypecanbeforward;
  745. exit;
  746. end;
  747. end;
  748. { also anonym objects aren't allow (o : object a : longint; end;) }
  749. if n='' then
  750. Message(parser_f_no_anonym_objects);
  751. { read the parent class }
  752. if token=_LKLAMMER then
  753. begin
  754. consume(_LKLAMMER);
  755. id_type(tt,pattern,false);
  756. childof:=pobjectdef(tt.def);
  757. if (childof^.deftype<>objectdef) then
  758. begin
  759. Message1(type_e_class_type_expected,childof^.typename);
  760. childof:=nil;
  761. aktclass:=new(pobjectdef,init(n,nil));
  762. end
  763. else
  764. begin
  765. { a mix of class and object isn't allowed }
  766. if (childof^.is_class and not is_a_class) or
  767. (not childof^.is_class and is_a_class) then
  768. Message(parser_e_mix_of_classes_and_objects);
  769. { the forward of the child must be resolved to get
  770. correct field addresses }
  771. if assigned(fd) then
  772. begin
  773. if (oo_is_forward in childof^.objectoptions) then
  774. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  775. aktclass:=fd;
  776. { we must inherit several options !!
  777. this was missing !!
  778. all is now done in set_parent
  779. including symtable datasize setting PM }
  780. fd^.set_parent(childof);
  781. end
  782. else
  783. aktclass:=new(pobjectdef,init(n,childof));
  784. end;
  785. consume(_RKLAMMER);
  786. end
  787. { if no parent class, then a class get tobject as parent }
  788. else if is_a_class then
  789. begin
  790. { is the current class tobject? }
  791. { so you could define your own tobject }
  792. if n='TOBJECT' then
  793. begin
  794. if assigned(fd) then
  795. aktclass:=fd
  796. else
  797. aktclass:=new(pobjectdef,init(n,nil));
  798. class_tobject:=aktclass;
  799. end
  800. else
  801. begin
  802. childof:=class_tobject;
  803. if assigned(fd) then
  804. begin
  805. { the forward of the child must be resolved to get
  806. correct field addresses
  807. }
  808. if (oo_is_forward in childof^.objectoptions) then
  809. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  810. aktclass:=fd;
  811. aktclass^.set_parent(childof);
  812. end
  813. else
  814. begin
  815. aktclass:=new(pobjectdef,init(n,childof));
  816. aktclass^.set_parent(childof);
  817. end;
  818. end;
  819. end
  820. else
  821. aktclass:=new(pobjectdef,init(n,nil));
  822. { default access is public }
  823. actmembertype:=[sp_public];
  824. { set the class attribute }
  825. if is_a_class then
  826. begin
  827. {$ifdef INCLUDEOK}
  828. include(aktclass^.objectoptions,oo_is_class);
  829. {$else}
  830. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class];
  831. {$endif}
  832. if (cs_generate_rtti in aktlocalswitches) or
  833. (assigned(aktclass^.childof) and
  834. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  835. begin
  836. include(aktclass^.objectoptions,oo_can_have_published);
  837. { in "publishable" classes the default access type is published }
  838. actmembertype:=[sp_published];
  839. { don't know if this is necessary (FK) }
  840. current_object_option:=[sp_published];
  841. end;
  842. end;
  843. aktobjectdef:=aktclass;
  844. aktclass^.symtable^.next:=symtablestack;
  845. symtablestack:=aktclass^.symtable;
  846. testcurobject:=1;
  847. curobjectname:=n;
  848. { new procinfo }
  849. oldprocinfo:=procinfo;
  850. new(procinfo,init);
  851. procinfo^._class:=aktclass;
  852. { short class declaration ? }
  853. if (not is_a_class) or (token<>_SEMICOLON) then
  854. begin
  855. { Parse componenten }
  856. repeat
  857. if (sp_private in actmembertype) then
  858. {$ifdef INCLUDEOK}
  859. include(aktclass^.objectoptions,oo_has_private);
  860. {$else}
  861. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_private];
  862. {$endif}
  863. if (sp_protected in actmembertype) then
  864. {$ifdef INCLUDEOK}
  865. include(aktclass^.objectoptions,oo_has_protected);
  866. {$else}
  867. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_protected];
  868. {$endif}
  869. case token of
  870. _ID : begin
  871. case idtoken of
  872. _PRIVATE : begin
  873. consume(_PRIVATE);
  874. actmembertype:=[sp_private];
  875. current_object_option:=[sp_private];
  876. end;
  877. _PROTECTED : begin
  878. consume(_PROTECTED);
  879. current_object_option:=[sp_protected];
  880. actmembertype:=[sp_protected];
  881. end;
  882. _PUBLIC : begin
  883. consume(_PUBLIC);
  884. current_object_option:=[sp_public];
  885. actmembertype:=[sp_public];
  886. end;
  887. _PUBLISHED : begin
  888. if not(oo_can_have_published in aktclass^.objectoptions) then
  889. Message(parser_e_cant_have_published);
  890. consume(_PUBLISHED);
  891. current_object_option:=[sp_published];
  892. actmembertype:=[sp_published];
  893. end;
  894. else
  895. read_var_decs(false,true,false);
  896. end;
  897. end;
  898. _PROPERTY : property_dec;
  899. _PROCEDURE,
  900. _FUNCTION,
  901. _CLASS : begin
  902. oldparse_only:=parse_only;
  903. parse_only:=true;
  904. parse_proc_dec;
  905. {$ifndef newcg}
  906. parse_object_proc_directives(aktprocsym);
  907. {$endif newcg}
  908. if (po_msgint in aktprocsym^.definition^.procoptions) then
  909. {$ifdef INCLUDEOK}
  910. include(aktclass^.objectoptions,oo_has_msgint);
  911. {$else}
  912. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgint];
  913. {$endif}
  914. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  915. {$ifdef INCLUDEOK}
  916. include(aktclass^.objectoptions,oo_has_msgstr);
  917. {$else}
  918. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgstr];
  919. {$endif}
  920. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  921. {$ifdef INCLUDEOK}
  922. include(aktclass^.objectoptions,oo_has_virtual);
  923. {$else}
  924. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  925. {$endif}
  926. parse_only:=oldparse_only;
  927. end;
  928. _CONSTRUCTOR : begin
  929. if not(sp_public in actmembertype) then
  930. Message(parser_w_constructor_should_be_public);
  931. oldparse_only:=parse_only;
  932. parse_only:=true;
  933. constructor_head;
  934. {$ifndef newcg}
  935. parse_object_proc_directives(aktprocsym);
  936. {$endif newcg}
  937. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  938. {$ifdef INCLUDEOK}
  939. include(aktclass^.objectoptions,oo_has_virtual);
  940. {$else}
  941. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  942. {$endif}
  943. parse_only:=oldparse_only;
  944. end;
  945. _DESTRUCTOR : begin
  946. if there_is_a_destructor then
  947. Message(parser_n_only_one_destructor);
  948. there_is_a_destructor:=true;
  949. if not(sp_public in actmembertype) then
  950. Message(parser_w_destructor_should_be_public);
  951. oldparse_only:=parse_only;
  952. parse_only:=true;
  953. destructor_head;
  954. {$ifndef newcg}
  955. parse_object_proc_directives(aktprocsym);
  956. {$endif newcg}
  957. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  958. {$ifdef INCLUDEOK}
  959. include(aktclass^.objectoptions,oo_has_virtual);
  960. {$else}
  961. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  962. {$endif}
  963. parse_only:=oldparse_only;
  964. end;
  965. _END : begin
  966. consume(_END);
  967. break;
  968. end;
  969. else
  970. consume(_ID); { Give a ident expected message, like tp7 }
  971. end;
  972. until false;
  973. current_object_option:=[sp_public];
  974. end;
  975. testcurobject:=0;
  976. curobjectname:='';
  977. typecanbeforward:=storetypecanbeforward;
  978. { generate vmt space if needed }
  979. if not(oo_has_vmt in aktclass^.objectoptions) and
  980. ([oo_has_virtual,oo_has_constructor,oo_has_destructor,oo_is_class]*aktclass^.objectoptions<>[]) then
  981. aktclass^.insertvmt;
  982. if (cs_create_smart in aktmoduleswitches) then
  983. datasegment^.concat(new(pai_cut,init));
  984. { Write the start of the VMT, wich is equal for classes and objects }
  985. if (oo_has_vmt in aktclass^.objectoptions) then
  986. begin
  987. { this generates the entries }
  988. vmtlist.init;
  989. genvmt(@vmtlist,aktclass);
  990. { write tables for classes, this must be done before the actual
  991. class is written, because we need the labels defined }
  992. if is_a_class then
  993. begin
  994. methodnametable:=genpublishedmethodstable(aktclass);
  995. { rtti }
  996. if (oo_can_have_published in aktclass^.objectoptions) then
  997. aktclass^.generate_rtti;
  998. { write class name }
  999. getdatalabel(classnamelabel);
  1000. datasegment^.concat(new(pai_label,init(classnamelabel)));
  1001. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  1002. datasegment^.concat(new(pai_string,init(aktclass^.objname^)));
  1003. { generate message and dynamic tables }
  1004. if (oo_has_msgstr in aktclass^.objectoptions) then
  1005. strmessagetable:=genstrmsgtab(aktclass);
  1006. if (oo_has_msgint in aktclass^.objectoptions) then
  1007. intmessagetable:=genintmsgtab(aktclass)
  1008. else
  1009. datasegment^.concat(new(pai_const,init_32bit(0)));
  1010. end;
  1011. { write debug info }
  1012. {$ifdef GDB}
  1013. if (cs_debuginfo in aktmoduleswitches) then
  1014. begin
  1015. do_count_dbx:=true;
  1016. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  1017. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  1018. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  1019. end;
  1020. {$endif GDB}
  1021. datasegment^.concat(new(pai_symbol,initname_global(aktclass^.vmt_mangledname,0)));
  1022. { determine the size with symtable^.datasize, because }
  1023. { size gives back 4 for classes }
  1024. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  1025. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  1026. { write pointer to parent VMT, this isn't implemented in TP }
  1027. { but this is not used in FPC ? (PM) }
  1028. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1029. { it is not written for parents that don't have any vmt !! }
  1030. if assigned(aktclass^.childof) and
  1031. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  1032. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  1033. else
  1034. datasegment^.concat(new(pai_const,init_32bit(0)));
  1035. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1036. if is_a_class then
  1037. begin
  1038. { pointer to class name string }
  1039. datasegment^.concat(new(pai_const_symbol,init(classnamelabel)));
  1040. { pointer to dynamic table }
  1041. if (oo_has_msgint in aktclass^.objectoptions) then
  1042. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  1043. else
  1044. datasegment^.concat(new(pai_const,init_32bit(0)));
  1045. { pointer to method table }
  1046. if assigned(methodnametable) then
  1047. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  1048. else
  1049. datasegment^.concat(new(pai_const,init_32bit(0)));
  1050. { pointer to field table }
  1051. datasegment^.concat(new(pai_const,init_32bit(0)));
  1052. { pointer to type info of published section }
  1053. if (oo_can_have_published in aktclass^.objectoptions) then
  1054. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  1055. else
  1056. datasegment^.concat(new(pai_const,init_32bit(0)));
  1057. { inittable for con-/destruction }
  1058. if aktclass^.needs_inittable then
  1059. datasegment^.concat(new(pai_const_symbol,init(aktclass^.get_inittable_label)))
  1060. else
  1061. datasegment^.concat(new(pai_const,init_32bit(0)));
  1062. { auto table }
  1063. datasegment^.concat(new(pai_const,init_32bit(0)));
  1064. { interface table }
  1065. datasegment^.concat(new(pai_const,init_32bit(0)));
  1066. { table for string messages }
  1067. if (oo_has_msgstr in aktclass^.objectoptions) then
  1068. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  1069. else
  1070. datasegment^.concat(new(pai_const,init_32bit(0)));
  1071. end;
  1072. datasegment^.concatlist(@vmtlist);
  1073. vmtlist.done;
  1074. { write the size of the VMT }
  1075. datasegment^.concat(new(pai_symbol_end,initname(aktclass^.vmt_mangledname)));
  1076. end;
  1077. { restore old state }
  1078. symtablestack:=symtablestack^.next;
  1079. aktobjectdef:=nil;
  1080. {Restore procinfo}
  1081. dispose(procinfo,done);
  1082. procinfo:=oldprocinfo;
  1083. {Restore the aktprocsym.}
  1084. aktprocsym:=oldprocsym;
  1085. object_dec:=aktclass;
  1086. end;
  1087. { reads a record declaration }
  1088. function record_dec : pdef;
  1089. var
  1090. symtable : psymtable;
  1091. storetypecanbeforward : boolean;
  1092. begin
  1093. { create recdef }
  1094. symtable:=new(psymtable,init(recordsymtable));
  1095. record_dec:=new(precorddef,init(symtable));
  1096. { update symtable stack }
  1097. symtable^.next:=symtablestack;
  1098. symtablestack:=symtable;
  1099. { parse record }
  1100. consume(_RECORD);
  1101. storetypecanbeforward:=typecanbeforward;
  1102. { for tp mode don't allow forward types }
  1103. if m_tp in aktmodeswitches then
  1104. typecanbeforward:=false;
  1105. read_var_decs(true,false,false);
  1106. consume(_END);
  1107. typecanbeforward:=storetypecanbeforward;
  1108. { may be scale record size to a size of n*4 ? }
  1109. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  1110. { restore symtable stack }
  1111. symtablestack:=symtable^.next;
  1112. end;
  1113. { reads a type definition and returns a pointer to it }
  1114. procedure read_type(var tt : ttype;const name : stringid);
  1115. var
  1116. pt : ptree;
  1117. tt2 : ttype;
  1118. aufdef : penumdef;
  1119. {aufsym : penumsym;}
  1120. ap : parraydef;
  1121. s : stringid;
  1122. l,v : longint;
  1123. oldaktpackrecords : tpackrecords;
  1124. hs : string;
  1125. defpos,storepos : tfileposinfo;
  1126. procedure expr_type;
  1127. var
  1128. pt1,pt2 : ptree;
  1129. begin
  1130. { use of current parsed object ? }
  1131. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  1132. begin
  1133. consume(_ID);
  1134. tt.setdef(aktobjectdef);
  1135. exit;
  1136. end;
  1137. { classes can be used also in classes }
  1138. if (curobjectname=pattern) and aktobjectdef^.is_class then
  1139. begin
  1140. tt.setdef(aktobjectdef);
  1141. consume(_ID);
  1142. exit;
  1143. end;
  1144. { we can't accept a equal in type }
  1145. pt1:=comp_expr(not(ignore_equal));
  1146. do_firstpass(pt1);
  1147. if (token=_POINTPOINT) then
  1148. begin
  1149. consume(_POINTPOINT);
  1150. { get high value of range }
  1151. pt2:=comp_expr(not(ignore_equal));
  1152. do_firstpass(pt2);
  1153. { both must be evaluated to constants now }
  1154. if (pt1^.treetype<>ordconstn) or (pt2^.treetype<>ordconstn) then
  1155. Message(sym_e_error_in_type_def)
  1156. else
  1157. begin
  1158. { check types }
  1159. if CheckTypes(pt1^.resulttype,pt2^.resulttype) then
  1160. begin
  1161. { Check bounds }
  1162. if pt2^.value<pt1^.value then
  1163. Message(cg_e_upper_lower_than_lower)
  1164. else
  1165. begin
  1166. { All checks passed, create the new def }
  1167. case pt1^.resulttype^.deftype of
  1168. enumdef : tt.setdef(new(penumdef,init_subrange(penumdef(pt1^.resulttype),pt1^.value,pt2^.value)));
  1169. orddef : begin
  1170. if is_char(pt1^.resulttype) then
  1171. tt.setdef(new(porddef,init(uchar,pt1^.value,pt2^.value)))
  1172. else
  1173. if is_boolean(pt1^.resulttype) then
  1174. tt.setdef(new(porddef,init(bool8bit,pt1^.value,pt2^.value)))
  1175. else
  1176. tt.setdef(new(porddef,init(uauto,pt1^.value,pt2^.value)));
  1177. end;
  1178. end;
  1179. end;
  1180. end;
  1181. end;
  1182. disposetree(pt2);
  1183. end
  1184. else
  1185. begin
  1186. { a simple type renaming }
  1187. if (pt1^.treetype=typen) then
  1188. begin
  1189. if assigned(pt1^.typenodesym) then
  1190. tt.setsym(pt1^.typenodesym)
  1191. else
  1192. tt.setdef(pt1^.resulttype);
  1193. end
  1194. else
  1195. Message(sym_e_error_in_type_def);
  1196. end;
  1197. disposetree(pt1);
  1198. end;
  1199. procedure array_dec;
  1200. var
  1201. lowval,
  1202. highval : longint;
  1203. arraytype : pdef;
  1204. begin
  1205. consume(_ARRAY);
  1206. consume(_LECKKLAMMER);
  1207. { defaults }
  1208. arraytype:=generrordef;
  1209. lowval:=$80000000;
  1210. highval:=$7fffffff;
  1211. tt.reset;
  1212. repeat
  1213. { read the expression and check it }
  1214. pt:=expr;
  1215. if pt^.treetype=typen then
  1216. begin
  1217. case pt^.resulttype^.deftype of
  1218. enumdef :
  1219. begin
  1220. lowval:=penumdef(pt^.resulttype)^.min;
  1221. highval:=penumdef(pt^.resulttype)^.max;
  1222. arraytype:=pt^.resulttype;
  1223. end;
  1224. orddef :
  1225. begin
  1226. lowval:=porddef(pt^.resulttype)^.low;
  1227. highval:=porddef(pt^.resulttype)^.high;
  1228. arraytype:=pt^.resulttype;
  1229. end;
  1230. else
  1231. Message(sym_e_error_in_type_def);
  1232. end;
  1233. end
  1234. else
  1235. begin
  1236. do_firstpass(pt);
  1237. if (pt^.treetype=rangen) then
  1238. begin
  1239. if (pt^.left^.treetype=ordconstn) and
  1240. (pt^.right^.treetype=ordconstn) then
  1241. begin
  1242. lowval:=pt^.left^.value;
  1243. highval:=pt^.right^.value;
  1244. if highval<lowval then
  1245. begin
  1246. Message(parser_e_array_lower_less_than_upper_bound);
  1247. highval:=lowval;
  1248. end;
  1249. arraytype:=pt^.right^.resulttype;
  1250. end
  1251. else
  1252. Message(type_e_cant_eval_constant_expr);
  1253. end
  1254. else
  1255. Message(sym_e_error_in_type_def)
  1256. end;
  1257. disposetree(pt);
  1258. { create arraydef }
  1259. if not assigned(tt.def) then
  1260. begin
  1261. ap:=new(parraydef,init(lowval,highval,arraytype));
  1262. tt.setdef(ap);
  1263. end
  1264. else
  1265. begin
  1266. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  1267. ap:=parraydef(ap^.elementtype.def);
  1268. end;
  1269. if token=_COMMA then
  1270. consume(_COMMA)
  1271. else
  1272. break;
  1273. until false;
  1274. consume(_RECKKLAMMER);
  1275. consume(_OF);
  1276. read_type(tt2,'');
  1277. { if no error, set element type }
  1278. if assigned(ap) then
  1279. ap^.elementtype:=tt2;
  1280. end;
  1281. begin
  1282. tt.reset;
  1283. case token of
  1284. _STRING,_FILE:
  1285. begin
  1286. single_type(tt,hs,false);
  1287. end;
  1288. _LKLAMMER:
  1289. begin
  1290. consume(_LKLAMMER);
  1291. { allow negativ value_str }
  1292. l:=-1;
  1293. {aufsym := Nil;}
  1294. aufdef:=new(penumdef,init);
  1295. repeat
  1296. s:=pattern;
  1297. defpos:=tokenpos;
  1298. consume(_ID);
  1299. if token=_ASSIGNMENT then
  1300. begin
  1301. consume(_ASSIGNMENT);
  1302. v:=get_intconst;
  1303. { please leave that a note, allows type save }
  1304. { declarations in the win32 units ! }
  1305. if v<=l then
  1306. Message(parser_n_duplicate_enum);
  1307. l:=v;
  1308. end
  1309. else
  1310. inc(l);
  1311. storepos:=tokenpos;
  1312. tokenpos:=defpos;
  1313. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1314. tokenpos:=storepos;
  1315. if token=_COMMA then
  1316. consume(_COMMA)
  1317. else
  1318. break;
  1319. until false;
  1320. {aufdef^.max:=l;
  1321. if we allow unordered enums
  1322. this can be wrong
  1323. min and max are now set in tenumsym.init PM }
  1324. tt.setdef(aufdef);
  1325. consume(_RKLAMMER);
  1326. end;
  1327. _ARRAY:
  1328. begin
  1329. array_dec;
  1330. end;
  1331. _SET:
  1332. begin
  1333. consume(_SET);
  1334. consume(_OF);
  1335. read_type(tt2,'');
  1336. if assigned(tt2.def) then
  1337. begin
  1338. case tt2.def^.deftype of
  1339. { don't forget that min can be negativ PM }
  1340. enumdef :
  1341. if penumdef(tt2.def)^.min>=0 then
  1342. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  1343. else
  1344. Message(sym_e_ill_type_decl_set);
  1345. orddef :
  1346. begin
  1347. case porddef(tt2.def)^.typ of
  1348. uchar :
  1349. tt.setdef(new(psetdef,init(tt2.def,255)));
  1350. u8bit,u16bit,u32bit,
  1351. s8bit,s16bit,s32bit :
  1352. begin
  1353. if (porddef(tt2.def)^.low>=0) then
  1354. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  1355. else
  1356. Message(sym_e_ill_type_decl_set);
  1357. end;
  1358. else
  1359. Message(sym_e_ill_type_decl_set);
  1360. end;
  1361. end;
  1362. else
  1363. Message(sym_e_ill_type_decl_set);
  1364. end;
  1365. end
  1366. else
  1367. tt.setdef(generrordef);
  1368. end;
  1369. _CARET:
  1370. begin
  1371. consume(_CARET);
  1372. single_type(tt2,hs,typecanbeforward);
  1373. tt.setdef(new(ppointerdef,init(tt2)));
  1374. end;
  1375. _RECORD:
  1376. begin
  1377. tt.setdef(record_dec);
  1378. end;
  1379. _PACKED:
  1380. begin
  1381. consume(_PACKED);
  1382. if token=_ARRAY then
  1383. array_dec
  1384. else
  1385. begin
  1386. oldaktpackrecords:=aktpackrecords;
  1387. aktpackrecords:=packrecord_1;
  1388. if token in [_CLASS,_OBJECT] then
  1389. tt.setdef(object_dec(name,nil))
  1390. else
  1391. tt.setdef(record_dec);
  1392. aktpackrecords:=oldaktpackrecords;
  1393. end;
  1394. end;
  1395. _CLASS,
  1396. _OBJECT:
  1397. begin
  1398. tt.setdef(object_dec(name,nil));
  1399. end;
  1400. _PROCEDURE:
  1401. begin
  1402. consume(_PROCEDURE);
  1403. tt.setdef(new(pprocvardef,init));
  1404. if token=_LKLAMMER then
  1405. parameter_dec(pprocvardef(tt.def));
  1406. if token=_OF then
  1407. begin
  1408. consume(_OF);
  1409. consume(_OBJECT);
  1410. {$ifdef INCLUDEOK}
  1411. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1412. {$else}
  1413. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1414. {$endif}
  1415. end;
  1416. end;
  1417. _FUNCTION:
  1418. begin
  1419. consume(_FUNCTION);
  1420. tt.def:=new(pprocvardef,init);
  1421. if token=_LKLAMMER then
  1422. parameter_dec(pprocvardef(tt.def));
  1423. consume(_COLON);
  1424. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  1425. if token=_OF then
  1426. begin
  1427. consume(_OF);
  1428. consume(_OBJECT);
  1429. {$ifdef INCLUDEOK}
  1430. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1431. {$else}
  1432. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1433. {$endif}
  1434. end;
  1435. end;
  1436. else
  1437. expr_type;
  1438. end;
  1439. if tt.def=nil then
  1440. tt.setdef(generrordef);
  1441. end;
  1442. end.
  1443. {
  1444. $Log$
  1445. Revision 1.15 2000-01-27 16:31:40 florian
  1446. * bug 738 fixed
  1447. Revision 1.14 2000/01/11 17:16:06 jonas
  1448. * removed a lot of memory leaks when an error is encountered (caused by
  1449. procinfo and pstringcontainers). There are still plenty left though :)
  1450. Revision 1.13 2000/01/07 01:14:34 peter
  1451. * updated copyright to 2000
  1452. Revision 1.12 1999/11/30 10:40:52 peter
  1453. + ttype, tsymlist
  1454. Revision 1.11 1999/11/26 00:19:12 peter
  1455. * property overriding dereference fix, but it need a bigger redesign
  1456. which i'll do tomorrow. This quick hack is for the lazarus ppl so
  1457. they can hack on mwcustomedit.
  1458. Revision 1.10 1999/11/17 17:05:03 pierre
  1459. * Notes/hints changes
  1460. Revision 1.9 1999/11/11 00:56:54 pierre
  1461. * Enum element reference location corrected
  1462. Revision 1.8 1999/11/09 23:43:09 pierre
  1463. * better browser info
  1464. Revision 1.7 1999/11/08 14:02:16 florian
  1465. * problem with "index X"-properties solved
  1466. * typed constants of class references are now allowed
  1467. Revision 1.6 1999/11/07 23:16:49 florian
  1468. * finally bug 517 solved ...
  1469. Revision 1.5 1999/10/27 16:04:06 peter
  1470. * fixed property reading
  1471. Revision 1.4 1999/10/27 14:17:08 florian
  1472. * property overriding fixed
  1473. Revision 1.3 1999/10/26 12:30:45 peter
  1474. * const parameter is now checked
  1475. * better and generic check if a node can be used for assigning
  1476. * export fixes
  1477. * procvar equal works now (it never had worked at least from 0.99.8)
  1478. * defcoll changed to linkedlist with pparaitem so it can easily be
  1479. walked both directions
  1480. Revision 1.2 1999/10/22 14:37:30 peter
  1481. * error when properties are passed to var parameters
  1482. Revision 1.1 1999/10/22 10:39:35 peter
  1483. * split type reading from pdecl to ptype unit
  1484. * parameter_dec routine is now used for procedure and procvars
  1485. }