ptype.pas 60 KB

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