ptype.pas 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. Message(parser_e_no_paras_for_destructor);
  664. { no return value }
  665. aktprocsym^.definition^.rettype.def:=voiddef;
  666. end;
  667. var
  668. hs : string;
  669. pcrd : pclassrefdef;
  670. tt : ttype;
  671. oldprocinfo : pprocinfo;
  672. oldprocsym : pprocsym;
  673. oldparse_only : boolean;
  674. methodnametable,intmessagetable,
  675. strmessagetable,classnamelabel : pasmlabel;
  676. storetypecanbeforward : boolean;
  677. vmtlist : taasmoutput;
  678. {$ifdef WITHDMT}
  679. dmtlabel : pasmlabel;
  680. {$endif WITHDMT}
  681. begin
  682. {Nowadays aktprocsym may already have a value, so we need to save
  683. it.}
  684. oldprocsym:=aktprocsym;
  685. { forward is resolved }
  686. if assigned(fd) then
  687. exclude(fd^.objectoptions,oo_is_forward);
  688. there_is_a_destructor:=false;
  689. actmembertype:=[sp_public];
  690. { objects and class types can't be declared local }
  691. if (symtablestack^.symtabletype<>globalsymtable) and
  692. (symtablestack^.symtabletype<>staticsymtable) then
  693. Message(parser_e_no_local_objects);
  694. storetypecanbeforward:=typecanbeforward;
  695. { for tp mode don't allow forward types }
  696. if m_tp in aktmodeswitches then
  697. typecanbeforward:=false;
  698. { distinguish classes and objects }
  699. if token=_OBJECT then
  700. begin
  701. is_a_class:=false;
  702. consume(_OBJECT)
  703. end
  704. else
  705. begin
  706. is_a_class:=true;
  707. consume(_CLASS);
  708. if not(assigned(fd)) and (token=_OF) then
  709. begin
  710. { a hack, but it's easy to handle }
  711. { class reference type }
  712. consume(_OF);
  713. single_type(tt,hs,typecanbeforward);
  714. { accept hp1, if is a forward def or a class }
  715. if (tt.def^.deftype=forwarddef) or
  716. ((tt.def^.deftype=objectdef) and pobjectdef(tt.def)^.is_class) then
  717. begin
  718. pcrd:=new(pclassrefdef,init(tt.def));
  719. object_dec:=pcrd;
  720. end
  721. else
  722. begin
  723. object_dec:=generrordef;
  724. Message1(type_e_class_type_expected,generrordef^.typename);
  725. end;
  726. typecanbeforward:=storetypecanbeforward;
  727. exit;
  728. end
  729. { forward class }
  730. else if not(assigned(fd)) and (token=_SEMICOLON) then
  731. begin
  732. { also anonym objects aren't allow (o : object a : longint; end;) }
  733. if n='' then
  734. begin
  735. Message(parser_f_no_anonym_objects)
  736. end;
  737. if n='TOBJECT' then
  738. begin
  739. aktclass:=new(pobjectdef,init(n,nil));
  740. class_tobject:=aktclass;
  741. end
  742. else
  743. aktclass:=new(pobjectdef,init(n,nil));
  744. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class,oo_is_forward];
  745. { all classes must have a vmt !! at offset zero }
  746. if not(oo_has_vmt in aktclass^.objectoptions) then
  747. aktclass^.insertvmt;
  748. object_dec:=aktclass;
  749. typecanbeforward:=storetypecanbeforward;
  750. exit;
  751. end;
  752. end;
  753. { also anonym objects aren't allow (o : object a : longint; end;) }
  754. if n='' then
  755. Message(parser_f_no_anonym_objects);
  756. { read the parent class }
  757. if token=_LKLAMMER then
  758. begin
  759. consume(_LKLAMMER);
  760. id_type(tt,pattern,false);
  761. childof:=pobjectdef(tt.def);
  762. if (childof^.deftype<>objectdef) then
  763. begin
  764. Message1(type_e_class_type_expected,childof^.typename);
  765. childof:=nil;
  766. aktclass:=new(pobjectdef,init(n,nil));
  767. end
  768. else
  769. begin
  770. { a mix of class and object isn't allowed }
  771. if (childof^.is_class and not is_a_class) or
  772. (not childof^.is_class and is_a_class) then
  773. Message(parser_e_mix_of_classes_and_objects);
  774. { the forward of the child must be resolved to get
  775. correct field addresses }
  776. if assigned(fd) then
  777. begin
  778. if (oo_is_forward in childof^.objectoptions) then
  779. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  780. aktclass:=fd;
  781. { we must inherit several options !!
  782. this was missing !!
  783. all is now done in set_parent
  784. including symtable datasize setting PM }
  785. fd^.set_parent(childof);
  786. end
  787. else
  788. aktclass:=new(pobjectdef,init(n,childof));
  789. end;
  790. consume(_RKLAMMER);
  791. end
  792. { if no parent class, then a class get tobject as parent }
  793. else if is_a_class then
  794. begin
  795. { is the current class tobject? }
  796. { so you could define your own tobject }
  797. if n='TOBJECT' then
  798. begin
  799. if assigned(fd) then
  800. aktclass:=fd
  801. else
  802. aktclass:=new(pobjectdef,init(n,nil));
  803. class_tobject:=aktclass;
  804. end
  805. else
  806. begin
  807. childof:=class_tobject;
  808. if assigned(fd) then
  809. begin
  810. { the forward of the child must be resolved to get
  811. correct field addresses
  812. }
  813. if (oo_is_forward in childof^.objectoptions) then
  814. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  815. aktclass:=fd;
  816. aktclass^.set_parent(childof);
  817. end
  818. else
  819. begin
  820. aktclass:=new(pobjectdef,init(n,childof));
  821. aktclass^.set_parent(childof);
  822. end;
  823. end;
  824. end
  825. else
  826. aktclass:=new(pobjectdef,init(n,nil));
  827. { default access is public }
  828. actmembertype:=[sp_public];
  829. { set the class attribute }
  830. if is_a_class then
  831. begin
  832. {$ifdef INCLUDEOK}
  833. include(aktclass^.objectoptions,oo_is_class);
  834. {$else}
  835. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class];
  836. {$endif}
  837. if (cs_generate_rtti in aktlocalswitches) or
  838. (assigned(aktclass^.childof) and
  839. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  840. begin
  841. include(aktclass^.objectoptions,oo_can_have_published);
  842. { in "publishable" classes the default access type is published }
  843. actmembertype:=[sp_published];
  844. { don't know if this is necessary (FK) }
  845. current_object_option:=[sp_published];
  846. end;
  847. end;
  848. aktobjectdef:=aktclass;
  849. aktclass^.symtable^.next:=symtablestack;
  850. symtablestack:=aktclass^.symtable;
  851. testcurobject:=1;
  852. curobjectname:=n;
  853. { new procinfo }
  854. oldprocinfo:=procinfo;
  855. new(procinfo,init);
  856. procinfo^._class:=aktclass;
  857. { short class declaration ? }
  858. if (not is_a_class) or (token<>_SEMICOLON) then
  859. begin
  860. { Parse componenten }
  861. repeat
  862. if (sp_private in actmembertype) then
  863. {$ifdef INCLUDEOK}
  864. include(aktclass^.objectoptions,oo_has_private);
  865. {$else}
  866. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_private];
  867. {$endif}
  868. if (sp_protected in actmembertype) then
  869. {$ifdef INCLUDEOK}
  870. include(aktclass^.objectoptions,oo_has_protected);
  871. {$else}
  872. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_protected];
  873. {$endif}
  874. case token of
  875. _ID : begin
  876. case idtoken of
  877. _PRIVATE : begin
  878. consume(_PRIVATE);
  879. actmembertype:=[sp_private];
  880. current_object_option:=[sp_private];
  881. end;
  882. _PROTECTED : begin
  883. consume(_PROTECTED);
  884. current_object_option:=[sp_protected];
  885. actmembertype:=[sp_protected];
  886. end;
  887. _PUBLIC : begin
  888. consume(_PUBLIC);
  889. current_object_option:=[sp_public];
  890. actmembertype:=[sp_public];
  891. end;
  892. _PUBLISHED : begin
  893. if not(oo_can_have_published in aktclass^.objectoptions) then
  894. Message(parser_e_cant_have_published);
  895. consume(_PUBLISHED);
  896. current_object_option:=[sp_published];
  897. actmembertype:=[sp_published];
  898. end;
  899. else
  900. read_var_decs(false,true,false);
  901. end;
  902. end;
  903. _PROPERTY : property_dec;
  904. _PROCEDURE,
  905. _FUNCTION,
  906. _CLASS : begin
  907. oldparse_only:=parse_only;
  908. parse_only:=true;
  909. parse_proc_dec;
  910. {$ifndef newcg}
  911. parse_object_proc_directives(aktprocsym);
  912. {$endif newcg}
  913. if (po_msgint in aktprocsym^.definition^.procoptions) then
  914. {$ifdef INCLUDEOK}
  915. include(aktclass^.objectoptions,oo_has_msgint);
  916. {$else}
  917. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgint];
  918. {$endif}
  919. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  920. {$ifdef INCLUDEOK}
  921. include(aktclass^.objectoptions,oo_has_msgstr);
  922. {$else}
  923. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_msgstr];
  924. {$endif}
  925. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  926. {$ifdef INCLUDEOK}
  927. include(aktclass^.objectoptions,oo_has_virtual);
  928. {$else}
  929. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  930. {$endif}
  931. parse_only:=oldparse_only;
  932. end;
  933. _CONSTRUCTOR : begin
  934. if not(sp_public in actmembertype) then
  935. Message(parser_w_constructor_should_be_public);
  936. oldparse_only:=parse_only;
  937. parse_only:=true;
  938. constructor_head;
  939. {$ifndef newcg}
  940. parse_object_proc_directives(aktprocsym);
  941. {$endif newcg}
  942. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  943. {$ifdef INCLUDEOK}
  944. include(aktclass^.objectoptions,oo_has_virtual);
  945. {$else}
  946. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  947. {$endif}
  948. parse_only:=oldparse_only;
  949. end;
  950. _DESTRUCTOR : begin
  951. if there_is_a_destructor then
  952. Message(parser_n_only_one_destructor);
  953. there_is_a_destructor:=true;
  954. if not(sp_public in actmembertype) then
  955. Message(parser_w_destructor_should_be_public);
  956. oldparse_only:=parse_only;
  957. parse_only:=true;
  958. destructor_head;
  959. {$ifndef newcg}
  960. parse_object_proc_directives(aktprocsym);
  961. {$endif newcg}
  962. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  963. {$ifdef INCLUDEOK}
  964. include(aktclass^.objectoptions,oo_has_virtual);
  965. {$else}
  966. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_has_virtual];
  967. {$endif}
  968. parse_only:=oldparse_only;
  969. end;
  970. _END : begin
  971. consume(_END);
  972. break;
  973. end;
  974. else
  975. consume(_ID); { Give a ident expected message, like tp7 }
  976. end;
  977. until false;
  978. current_object_option:=[sp_public];
  979. end;
  980. testcurobject:=0;
  981. curobjectname:='';
  982. typecanbeforward:=storetypecanbeforward;
  983. { generate vmt space if needed }
  984. if not(oo_has_vmt in aktclass^.objectoptions) and
  985. ([oo_has_virtual,oo_has_constructor,oo_has_destructor,oo_is_class]*aktclass^.objectoptions<>[]) then
  986. aktclass^.insertvmt;
  987. if (cs_create_smart in aktmoduleswitches) then
  988. datasegment^.concat(new(pai_cut,init));
  989. { Write the start of the VMT, wich is equal for classes and objects }
  990. if (oo_has_vmt in aktclass^.objectoptions) then
  991. begin
  992. {$ifdef WITHDMT}
  993. dmtlabel:=gendmt(aktclass);
  994. {$endif WITHDMT}
  995. { this generates the entries }
  996. vmtlist.init;
  997. genvmt(@vmtlist,aktclass);
  998. { write tables for classes, this must be done before the actual
  999. class is written, because we need the labels defined }
  1000. if is_a_class then
  1001. begin
  1002. methodnametable:=genpublishedmethodstable(aktclass);
  1003. { rtti }
  1004. if (oo_can_have_published in aktclass^.objectoptions) then
  1005. aktclass^.generate_rtti;
  1006. { write class name }
  1007. getdatalabel(classnamelabel);
  1008. datasegment^.concat(new(pai_label,init(classnamelabel)));
  1009. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  1010. datasegment^.concat(new(pai_string,init(aktclass^.objname^)));
  1011. { generate message and dynamic tables }
  1012. if (oo_has_msgstr in aktclass^.objectoptions) then
  1013. strmessagetable:=genstrmsgtab(aktclass);
  1014. if (oo_has_msgint in aktclass^.objectoptions) then
  1015. intmessagetable:=genintmsgtab(aktclass)
  1016. else
  1017. datasegment^.concat(new(pai_const,init_32bit(0)));
  1018. end;
  1019. { write debug info }
  1020. {$ifdef GDB}
  1021. if (cs_debuginfo in aktmoduleswitches) then
  1022. begin
  1023. do_count_dbx:=true;
  1024. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  1025. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  1026. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  1027. end;
  1028. {$endif GDB}
  1029. datasegment^.concat(new(pai_symbol,initname_global(aktclass^.vmt_mangledname,0)));
  1030. { determine the size with symtable^.datasize, because }
  1031. { size gives back 4 for classes }
  1032. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  1033. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  1034. {$ifdef WITHDMT}
  1035. if not(is_a_class) then
  1036. begin
  1037. if assigned(dmtlabel) then
  1038. datasegment^.concat(new(pai_const_symbol,init(dmtlabel)))
  1039. else
  1040. datasegment^.concat(new(pai_const,init_32bit(0)));
  1041. end;
  1042. {$endif WITHDMT}
  1043. { write pointer to parent VMT, this isn't implemented in TP }
  1044. { but this is not used in FPC ? (PM) }
  1045. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1046. { it is not written for parents that don't have any vmt !! }
  1047. if assigned(aktclass^.childof) and
  1048. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  1049. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  1050. else
  1051. datasegment^.concat(new(pai_const,init_32bit(0)));
  1052. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1053. if is_a_class then
  1054. begin
  1055. { pointer to class name string }
  1056. datasegment^.concat(new(pai_const_symbol,init(classnamelabel)));
  1057. { pointer to dynamic table }
  1058. if (oo_has_msgint in aktclass^.objectoptions) then
  1059. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  1060. else
  1061. datasegment^.concat(new(pai_const,init_32bit(0)));
  1062. { pointer to method table }
  1063. if assigned(methodnametable) then
  1064. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  1065. else
  1066. datasegment^.concat(new(pai_const,init_32bit(0)));
  1067. { pointer to field table }
  1068. datasegment^.concat(new(pai_const,init_32bit(0)));
  1069. { pointer to type info of published section }
  1070. if (oo_can_have_published in aktclass^.objectoptions) then
  1071. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  1072. else
  1073. datasegment^.concat(new(pai_const,init_32bit(0)));
  1074. { inittable for con-/destruction }
  1075. {
  1076. if aktclass^.needs_inittable then
  1077. }
  1078. { we generate the init table for classes always, because needs_inittable }
  1079. { for classes is always false, it applies only for objects }
  1080. datasegment^.concat(new(pai_const_symbol,init(aktclass^.get_inittable_label)));
  1081. {
  1082. else
  1083. datasegment^.concat(new(pai_const,init_32bit(0)));
  1084. }
  1085. { auto table }
  1086. datasegment^.concat(new(pai_const,init_32bit(0)));
  1087. { interface table }
  1088. datasegment^.concat(new(pai_const,init_32bit(0)));
  1089. { table for string messages }
  1090. if (oo_has_msgstr in aktclass^.objectoptions) then
  1091. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  1092. else
  1093. datasegment^.concat(new(pai_const,init_32bit(0)));
  1094. end;
  1095. datasegment^.concatlist(@vmtlist);
  1096. vmtlist.done;
  1097. { write the size of the VMT }
  1098. datasegment^.concat(new(pai_symbol_end,initname(aktclass^.vmt_mangledname)));
  1099. end;
  1100. { restore old state }
  1101. symtablestack:=symtablestack^.next;
  1102. aktobjectdef:=nil;
  1103. {Restore procinfo}
  1104. dispose(procinfo,done);
  1105. procinfo:=oldprocinfo;
  1106. {Restore the aktprocsym.}
  1107. aktprocsym:=oldprocsym;
  1108. object_dec:=aktclass;
  1109. end;
  1110. { reads a record declaration }
  1111. function record_dec : pdef;
  1112. var
  1113. symtable : psymtable;
  1114. storetypecanbeforward : boolean;
  1115. begin
  1116. { create recdef }
  1117. symtable:=new(psymtable,init(recordsymtable));
  1118. record_dec:=new(precorddef,init(symtable));
  1119. { update symtable stack }
  1120. symtable^.next:=symtablestack;
  1121. symtablestack:=symtable;
  1122. { parse record }
  1123. consume(_RECORD);
  1124. storetypecanbeforward:=typecanbeforward;
  1125. { for tp mode don't allow forward types }
  1126. if m_tp in aktmodeswitches then
  1127. typecanbeforward:=false;
  1128. read_var_decs(true,false,false);
  1129. consume(_END);
  1130. typecanbeforward:=storetypecanbeforward;
  1131. { may be scale record size to a size of n*4 ? }
  1132. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  1133. { restore symtable stack }
  1134. symtablestack:=symtable^.next;
  1135. end;
  1136. { reads a type definition and returns a pointer to it }
  1137. procedure read_type(var tt : ttype;const name : stringid);
  1138. var
  1139. pt : ptree;
  1140. tt2 : ttype;
  1141. aufdef : penumdef;
  1142. {aufsym : penumsym;}
  1143. ap : parraydef;
  1144. s : stringid;
  1145. l,v : longint;
  1146. oldaktpackrecords : tpackrecords;
  1147. hs : string;
  1148. defpos,storepos : tfileposinfo;
  1149. procedure expr_type;
  1150. var
  1151. pt1,pt2 : ptree;
  1152. begin
  1153. { use of current parsed object ? }
  1154. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  1155. begin
  1156. consume(_ID);
  1157. tt.setdef(aktobjectdef);
  1158. exit;
  1159. end;
  1160. { classes can be used also in classes }
  1161. if (curobjectname=pattern) and aktobjectdef^.is_class then
  1162. begin
  1163. tt.setdef(aktobjectdef);
  1164. consume(_ID);
  1165. exit;
  1166. end;
  1167. { we can't accept a equal in type }
  1168. pt1:=comp_expr(not(ignore_equal));
  1169. do_firstpass(pt1);
  1170. if (token=_POINTPOINT) then
  1171. begin
  1172. consume(_POINTPOINT);
  1173. { get high value of range }
  1174. pt2:=comp_expr(not(ignore_equal));
  1175. do_firstpass(pt2);
  1176. { both must be evaluated to constants now }
  1177. if (pt1^.treetype<>ordconstn) or (pt2^.treetype<>ordconstn) then
  1178. Message(sym_e_error_in_type_def)
  1179. else
  1180. begin
  1181. { check types }
  1182. if CheckTypes(pt1^.resulttype,pt2^.resulttype) then
  1183. begin
  1184. { Check bounds }
  1185. if pt2^.value<pt1^.value then
  1186. Message(cg_e_upper_lower_than_lower)
  1187. else
  1188. begin
  1189. { All checks passed, create the new def }
  1190. case pt1^.resulttype^.deftype of
  1191. enumdef : tt.setdef(new(penumdef,init_subrange(penumdef(pt1^.resulttype),pt1^.value,pt2^.value)));
  1192. orddef : begin
  1193. if is_char(pt1^.resulttype) then
  1194. tt.setdef(new(porddef,init(uchar,pt1^.value,pt2^.value)))
  1195. else
  1196. if is_boolean(pt1^.resulttype) then
  1197. tt.setdef(new(porddef,init(bool8bit,pt1^.value,pt2^.value)))
  1198. else
  1199. tt.setdef(new(porddef,init(uauto,pt1^.value,pt2^.value)));
  1200. end;
  1201. end;
  1202. end;
  1203. end;
  1204. end;
  1205. disposetree(pt2);
  1206. end
  1207. else
  1208. begin
  1209. { a simple type renaming }
  1210. if (pt1^.treetype=typen) then
  1211. begin
  1212. if assigned(pt1^.typenodesym) then
  1213. tt.setsym(pt1^.typenodesym)
  1214. else
  1215. tt.setdef(pt1^.resulttype);
  1216. end
  1217. else
  1218. Message(sym_e_error_in_type_def);
  1219. end;
  1220. disposetree(pt1);
  1221. end;
  1222. procedure array_dec;
  1223. var
  1224. lowval,
  1225. highval : longint;
  1226. arraytype : pdef;
  1227. begin
  1228. consume(_ARRAY);
  1229. consume(_LECKKLAMMER);
  1230. { defaults }
  1231. arraytype:=generrordef;
  1232. lowval:=$80000000;
  1233. highval:=$7fffffff;
  1234. tt.reset;
  1235. repeat
  1236. { read the expression and check it }
  1237. pt:=expr;
  1238. if pt^.treetype=typen then
  1239. begin
  1240. case pt^.resulttype^.deftype of
  1241. enumdef :
  1242. begin
  1243. lowval:=penumdef(pt^.resulttype)^.min;
  1244. highval:=penumdef(pt^.resulttype)^.max;
  1245. arraytype:=pt^.resulttype;
  1246. end;
  1247. orddef :
  1248. begin
  1249. lowval:=porddef(pt^.resulttype)^.low;
  1250. highval:=porddef(pt^.resulttype)^.high;
  1251. arraytype:=pt^.resulttype;
  1252. end;
  1253. else
  1254. Message(sym_e_error_in_type_def);
  1255. end;
  1256. end
  1257. else
  1258. begin
  1259. do_firstpass(pt);
  1260. if (pt^.treetype=rangen) then
  1261. begin
  1262. if (pt^.left^.treetype=ordconstn) and
  1263. (pt^.right^.treetype=ordconstn) then
  1264. begin
  1265. lowval:=pt^.left^.value;
  1266. highval:=pt^.right^.value;
  1267. if highval<lowval then
  1268. begin
  1269. Message(parser_e_array_lower_less_than_upper_bound);
  1270. highval:=lowval;
  1271. end;
  1272. arraytype:=pt^.right^.resulttype;
  1273. end
  1274. else
  1275. Message(type_e_cant_eval_constant_expr);
  1276. end
  1277. else
  1278. Message(sym_e_error_in_type_def)
  1279. end;
  1280. disposetree(pt);
  1281. { create arraydef }
  1282. if not assigned(tt.def) then
  1283. begin
  1284. ap:=new(parraydef,init(lowval,highval,arraytype));
  1285. tt.setdef(ap);
  1286. end
  1287. else
  1288. begin
  1289. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  1290. ap:=parraydef(ap^.elementtype.def);
  1291. end;
  1292. if token=_COMMA then
  1293. consume(_COMMA)
  1294. else
  1295. break;
  1296. until false;
  1297. consume(_RECKKLAMMER);
  1298. consume(_OF);
  1299. read_type(tt2,'');
  1300. { if no error, set element type }
  1301. if assigned(ap) then
  1302. ap^.elementtype:=tt2;
  1303. end;
  1304. begin
  1305. tt.reset;
  1306. case token of
  1307. _STRING,_FILE:
  1308. begin
  1309. single_type(tt,hs,false);
  1310. end;
  1311. _LKLAMMER:
  1312. begin
  1313. consume(_LKLAMMER);
  1314. { allow negativ value_str }
  1315. l:=-1;
  1316. {aufsym := Nil;}
  1317. aufdef:=new(penumdef,init);
  1318. repeat
  1319. s:=pattern;
  1320. defpos:=tokenpos;
  1321. consume(_ID);
  1322. if token=_ASSIGNMENT then
  1323. begin
  1324. consume(_ASSIGNMENT);
  1325. v:=get_intconst;
  1326. { please leave that a note, allows type save }
  1327. { declarations in the win32 units ! }
  1328. if v<=l then
  1329. Message(parser_n_duplicate_enum);
  1330. l:=v;
  1331. end
  1332. else
  1333. inc(l);
  1334. storepos:=tokenpos;
  1335. tokenpos:=defpos;
  1336. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1337. tokenpos:=storepos;
  1338. if token=_COMMA then
  1339. consume(_COMMA)
  1340. else
  1341. break;
  1342. until false;
  1343. {aufdef^.max:=l;
  1344. if we allow unordered enums
  1345. this can be wrong
  1346. min and max are now set in tenumsym.init PM }
  1347. tt.setdef(aufdef);
  1348. consume(_RKLAMMER);
  1349. end;
  1350. _ARRAY:
  1351. begin
  1352. array_dec;
  1353. end;
  1354. _SET:
  1355. begin
  1356. consume(_SET);
  1357. consume(_OF);
  1358. read_type(tt2,'');
  1359. if assigned(tt2.def) then
  1360. begin
  1361. case tt2.def^.deftype of
  1362. { don't forget that min can be negativ PM }
  1363. enumdef :
  1364. if penumdef(tt2.def)^.min>=0 then
  1365. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  1366. else
  1367. Message(sym_e_ill_type_decl_set);
  1368. orddef :
  1369. begin
  1370. case porddef(tt2.def)^.typ of
  1371. uchar :
  1372. tt.setdef(new(psetdef,init(tt2.def,255)));
  1373. u8bit,u16bit,u32bit,
  1374. s8bit,s16bit,s32bit :
  1375. begin
  1376. if (porddef(tt2.def)^.low>=0) then
  1377. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  1378. else
  1379. Message(sym_e_ill_type_decl_set);
  1380. end;
  1381. else
  1382. Message(sym_e_ill_type_decl_set);
  1383. end;
  1384. end;
  1385. else
  1386. Message(sym_e_ill_type_decl_set);
  1387. end;
  1388. end
  1389. else
  1390. tt.setdef(generrordef);
  1391. end;
  1392. _CARET:
  1393. begin
  1394. consume(_CARET);
  1395. single_type(tt2,hs,typecanbeforward);
  1396. tt.setdef(new(ppointerdef,init(tt2)));
  1397. end;
  1398. _RECORD:
  1399. begin
  1400. tt.setdef(record_dec);
  1401. end;
  1402. _PACKED:
  1403. begin
  1404. consume(_PACKED);
  1405. if token=_ARRAY then
  1406. array_dec
  1407. else
  1408. begin
  1409. oldaktpackrecords:=aktpackrecords;
  1410. aktpackrecords:=packrecord_1;
  1411. if token in [_CLASS,_OBJECT] then
  1412. tt.setdef(object_dec(name,nil))
  1413. else
  1414. tt.setdef(record_dec);
  1415. aktpackrecords:=oldaktpackrecords;
  1416. end;
  1417. end;
  1418. _CLASS,
  1419. _OBJECT:
  1420. begin
  1421. tt.setdef(object_dec(name,nil));
  1422. end;
  1423. _PROCEDURE:
  1424. begin
  1425. consume(_PROCEDURE);
  1426. tt.setdef(new(pprocvardef,init));
  1427. if token=_LKLAMMER then
  1428. parameter_dec(pprocvardef(tt.def));
  1429. if token=_OF then
  1430. begin
  1431. consume(_OF);
  1432. consume(_OBJECT);
  1433. {$ifdef INCLUDEOK}
  1434. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1435. {$else}
  1436. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1437. {$endif}
  1438. end;
  1439. end;
  1440. _FUNCTION:
  1441. begin
  1442. consume(_FUNCTION);
  1443. tt.def:=new(pprocvardef,init);
  1444. if token=_LKLAMMER then
  1445. parameter_dec(pprocvardef(tt.def));
  1446. consume(_COLON);
  1447. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  1448. if token=_OF then
  1449. begin
  1450. consume(_OF);
  1451. consume(_OBJECT);
  1452. {$ifdef INCLUDEOK}
  1453. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  1454. {$else}
  1455. pprocvardef(tt.def)^.procoptions:=pprocvardef(tt.def)^.procoptions+[po_methodpointer];
  1456. {$endif}
  1457. end;
  1458. end;
  1459. else
  1460. expr_type;
  1461. end;
  1462. if tt.def=nil then
  1463. tt.setdef(generrordef);
  1464. end;
  1465. end.
  1466. {
  1467. $Log$
  1468. Revision 1.21 2000-03-11 21:11:24 daniel
  1469. * Ported hcgdata to new symtable.
  1470. * Alignment code changed as suggested by Peter
  1471. + Usage of my is operator replacement, is_object
  1472. Revision 1.20 2000/02/24 18:41:39 peter
  1473. * removed warnings/notes
  1474. Revision 1.19 2000/02/21 22:17:49 florian
  1475. * fixed 819
  1476. Revision 1.18 2000/02/09 13:23:01 peter
  1477. * log truncated
  1478. Revision 1.17 2000/02/05 14:33:32 florian
  1479. * fixed init table generation for classes and arrays
  1480. Revision 1.16 2000/01/28 23:17:53 florian
  1481. * virtual XXXX; support for objects, only if -dWITHDMT is defined
  1482. Revision 1.15 2000/01/27 16:31:40 florian
  1483. * bug 738 fixed
  1484. Revision 1.14 2000/01/11 17:16:06 jonas
  1485. * removed a lot of memory leaks when an error is encountered (caused by
  1486. procinfo and pstringcontainers). There are still plenty left though :)
  1487. Revision 1.13 2000/01/07 01:14:34 peter
  1488. * updated copyright to 2000
  1489. Revision 1.12 1999/11/30 10:40:52 peter
  1490. + ttype, tsymlist
  1491. Revision 1.11 1999/11/26 00:19:12 peter
  1492. * property overriding dereference fix, but it need a bigger redesign
  1493. which i'll do tomorrow. This quick hack is for the lazarus ppl so
  1494. they can hack on mwcustomedit.
  1495. Revision 1.10 1999/11/17 17:05:03 pierre
  1496. * Notes/hints changes
  1497. Revision 1.9 1999/11/11 00:56:54 pierre
  1498. * Enum element reference location corrected
  1499. Revision 1.8 1999/11/09 23:43:09 pierre
  1500. * better browser info
  1501. Revision 1.7 1999/11/08 14:02:16 florian
  1502. * problem with "index X"-properties solved
  1503. * typed constants of class references are now allowed
  1504. Revision 1.6 1999/11/07 23:16:49 florian
  1505. * finally bug 517 solved ...
  1506. Revision 1.5 1999/10/27 16:04:06 peter
  1507. * fixed property reading
  1508. Revision 1.4 1999/10/27 14:17:08 florian
  1509. * property overriding fixed
  1510. Revision 1.3 1999/10/26 12:30:45 peter
  1511. * const parameter is now checked
  1512. * better and generic check if a node can be used for assigning
  1513. * export fixes
  1514. * procvar equal works now (it never had worked at least from 0.99.8)
  1515. * defcoll changed to linkedlist with pparaitem so it can easily be
  1516. walked both directions
  1517. Revision 1.2 1999/10/22 14:37:30 peter
  1518. * error when properties are passed to var parameters
  1519. Revision 1.1 1999/10/22 10:39:35 peter
  1520. * split type reading from pdecl to ptype unit
  1521. * parameter_dec routine is now used for procedure and procvars
  1522. }