ptype.pas 62 KB

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