ptype.pas 63 KB

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