ptype.pas 61 KB

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