ptype.pas 61 KB

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