pexpr.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Does parsing of expression 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 pexpr;
  19. interface
  20. uses symtable,tree;
  21. { reads a whole expression }
  22. function expr : ptree;
  23. { reads an expression without assignements and .. }
  24. function comp_expr(accept_equal : boolean):Ptree;
  25. { reads a single factor }
  26. function factor(getaddr : boolean) : ptree;
  27. { the ID token has to be consumed before calling this function }
  28. procedure do_member_read(const sym : psym;var p1 : ptree;
  29. var pd : pdef;var again : boolean);
  30. function get_intconst:longint;
  31. function get_stringconst:string;
  32. implementation
  33. uses
  34. cobjects,globals,scanner,aasm,pass_1,systems,
  35. hcodegen,types,verbose
  36. { parser specific stuff }
  37. ,pbase,pdecl
  38. { processor specific stuff }
  39. {$ifdef i386}
  40. ,i386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  47. var
  48. p1,p2 : ptree;
  49. end_of_paras : ttoken;
  50. begin
  51. if in_prop_paras then
  52. end_of_paras:=RECKKLAMMER
  53. else
  54. end_of_paras:=RKLAMMER;
  55. if token=end_of_paras then
  56. begin
  57. parse_paras:=nil;
  58. exit;
  59. end;
  60. p2:=nil;
  61. inc(parsing_para_level);
  62. while true do
  63. begin
  64. p1:=expr;
  65. p2:=gencallparanode(p1,p2);
  66. { it's for the str(l:5,s); }
  67. if _colon and (token=COLON) then
  68. begin
  69. consume(COLON);
  70. p1:=expr;
  71. p2:=gencallparanode(p1,p2);
  72. p2^.is_colon_para:=true;
  73. if token=COLON then
  74. begin
  75. consume(COLON);
  76. p1:=expr;
  77. p2:=gencallparanode(p1,p2);
  78. p2^.is_colon_para:=true;
  79. end
  80. end;
  81. if token=COMMA then
  82. consume(COMMA)
  83. else
  84. break;
  85. end;
  86. dec(parsing_para_level);
  87. parse_paras:=p2;
  88. end;
  89. function statement_syssym(l : longint;var pd : pdef) : ptree;
  90. { const regnames:array[R_EAX..R_EDI] of string[3]=
  91. ('EAX','ECX','EDX','EBX','','','ESI','EDI'); }
  92. var
  93. p1,p2 : ptree;
  94. paras : ptree;
  95. prev_in_args : boolean;
  96. Store_valid : boolean;
  97. begin
  98. prev_in_args:=in_args;
  99. Store_valid:=Must_be_valid;
  100. case l of
  101. in_ord_x:
  102. begin
  103. consume(LKLAMMER);
  104. in_args:=true;
  105. Must_be_valid:=true;
  106. p1:=expr;
  107. consume(RKLAMMER);
  108. do_firstpass(p1);
  109. p1:=geninlinenode(in_ord_x,p1);
  110. do_firstpass(p1);
  111. statement_syssym := p1;
  112. pd:=p1^.resulttype;
  113. end;
  114. in_break:
  115. begin
  116. statement_syssym:=genzeronode(breakn);
  117. pd:=voiddef;
  118. end;
  119. in_continue:
  120. begin
  121. statement_syssym:=genzeronode(continuen);
  122. pd:=voiddef;
  123. end;
  124. in_typeof_x : begin
  125. consume(LKLAMMER);
  126. in_args:=true;
  127. p1:=expr;
  128. consume(RKLAMMER);
  129. pd:=voidpointerdef;
  130. if p1^.treetype=typen then
  131. begin
  132. if (p1^.resulttype=nil) then
  133. begin
  134. Message(sym_e_type_mismatch);
  135. statement_syssym:=genzeronode(errorn);
  136. end
  137. else
  138. if p1^.resulttype^.deftype=objectdef then
  139. statement_syssym:=geninlinenode(in_typeof_x,p1)
  140. else
  141. begin
  142. Message(sym_e_type_mismatch);
  143. statement_syssym:=genzeronode(errorn);
  144. end;
  145. end
  146. else
  147. begin
  148. Must_be_valid:=false;
  149. do_firstpass(p1);
  150. if (p1^.resulttype=nil) then
  151. begin
  152. Message(sym_e_type_mismatch);
  153. statement_syssym:=genzeronode(errorn)
  154. end
  155. else
  156. if p1^.resulttype^.deftype=objectdef then
  157. statement_syssym:=geninlinenode(in_typeof_x,p1)
  158. else
  159. begin
  160. Message(sym_e_type_mismatch);
  161. statement_syssym:=genzeronode(errorn)
  162. end;
  163. end;
  164. end;
  165. in_sizeof_x : begin
  166. consume(LKLAMMER);
  167. in_args:=true;
  168. p1:=expr;
  169. consume(RKLAMMER);
  170. pd:=s32bitdef;
  171. if p1^.treetype=typen then
  172. begin
  173. statement_syssym:=genordinalconstnode(
  174. p1^.resulttype^.size,pd);
  175. { p1 not needed !}
  176. disposetree(p1);
  177. end
  178. else
  179. begin
  180. Must_be_valid:=false;
  181. do_firstpass(p1);
  182. if p1^.resulttype^.deftype<>objectdef then
  183. begin
  184. statement_syssym:=genordinalconstnode(
  185. p1^.resulttype^.size,pd);
  186. { p1 not needed !}
  187. disposetree(p1);
  188. end
  189. else
  190. begin
  191. statement_syssym:=geninlinenode(in_sizeof_x,p1);
  192. end;
  193. end;
  194. end;
  195. in_assigned_x : begin
  196. consume(LKLAMMER);
  197. in_args:=true;
  198. p1:=expr;
  199. Must_be_valid:=true;
  200. do_firstpass(p1);
  201. case p1^.resulttype^.deftype of
  202. pointerdef,procvardef,
  203. classrefdef:
  204. ;
  205. objectdef:
  206. if not(pobjectdef(p1^.resulttype)^.isclass) then
  207. Message(parser_e_illegal_parameter_list);
  208. else Message(parser_e_illegal_parameter_list);
  209. end;
  210. p2:=gencallparanode(p1,nil);
  211. p2:=geninlinenode(in_assigned_x,p2);
  212. consume(RKLAMMER);
  213. pd:=booldef;
  214. statement_syssym:=p2;
  215. end;
  216. in_ofs_x : begin
  217. consume(LKLAMMER);
  218. in_args:=true;
  219. p1:=expr;
  220. p1:=gensinglenode(addrn,p1);
  221. Must_be_valid:=false;
  222. do_firstpass(p1);
  223. { Ofs() returns a longint, not a pointer }
  224. p1^.resulttype:=u32bitdef;
  225. pd:=p1^.resulttype;
  226. consume(RKLAMMER);
  227. statement_syssym:=p1;
  228. end;
  229. in_seg_x : begin
  230. consume(LKLAMMER);
  231. in_args:=true;
  232. p1:=expr;
  233. do_firstpass(p1);
  234. if p1^.location.loc<>LOC_REFERENCE then
  235. Message(cg_e_illegal_expression);
  236. p1:=genordinalconstnode(0,s32bitdef);
  237. Must_be_valid:=false;
  238. pd:=s32bitdef;
  239. consume(RKLAMMER);
  240. statement_syssym:=p1;
  241. end;
  242. in_high_x,
  243. in_low_x : begin
  244. consume(LKLAMMER);
  245. in_args:=true;
  246. p1:=expr;
  247. do_firstpass(p1);
  248. Must_be_valid:=false;
  249. p2:=geninlinenode(l,p1);
  250. consume(RKLAMMER);
  251. pd:=s32bitdef;
  252. statement_syssym:=p2;
  253. end;
  254. in_succ_x,
  255. in_pred_x : begin
  256. consume(LKLAMMER);
  257. in_args:=true;
  258. p1:=expr;
  259. do_firstpass(p1);
  260. Must_be_valid:=false;
  261. p2:=geninlinenode(l,p1);
  262. consume(RKLAMMER);
  263. pd:=p1^.resulttype;
  264. statement_syssym:=p2;
  265. end;
  266. in_inc_x,
  267. in_dec_x : begin
  268. consume(LKLAMMER);
  269. in_args:=true;
  270. p1:=expr;
  271. p2:=gencallparanode(p1,nil);
  272. Must_be_valid:=false;
  273. if token=COMMA then
  274. begin
  275. consume(COMMA);
  276. p1:=expr;
  277. p2:=gencallparanode(p1,p2);
  278. end;
  279. statement_syssym:=geninlinenode(l,p2);
  280. consume(RKLAMMER);
  281. pd:=voiddef;
  282. end;
  283. in_concat_x : begin
  284. consume(LKLAMMER);
  285. in_args:=true;
  286. p2:=nil;
  287. while true do
  288. begin
  289. p1:=expr;
  290. Must_be_valid:=true;
  291. do_firstpass(p1);
  292. if not((p1^.resulttype^.deftype=stringdef) or
  293. ((p1^.resulttype^.deftype=orddef) and
  294. (porddef(p1^.resulttype)^.typ=uchar)
  295. )
  296. ) then Message(parser_e_illegal_parameter_list);
  297. if p2<>nil then
  298. p2:=gennode(addn,p2,p1)
  299. else p2:=p1;
  300. if token=COMMA then
  301. consume(COMMA)
  302. else break;
  303. end;
  304. consume(RKLAMMER);
  305. pd:=cstringdef;
  306. statement_syssym:=p2;
  307. end;
  308. in_read_x,
  309. in_readln_x : begin
  310. if token=LKLAMMER then
  311. begin
  312. consume(LKLAMMER);
  313. in_args:=true;
  314. Must_be_valid:=false;
  315. paras:=parse_paras(false,false);
  316. consume(RKLAMMER);
  317. end
  318. else
  319. paras:=nil;
  320. pd:=voiddef;
  321. p1:=geninlinenode(l,paras);
  322. do_firstpass(p1);
  323. statement_syssym := p1;
  324. end;
  325. in_write_x,
  326. in_writeln_x : begin
  327. if token=LKLAMMER then
  328. begin
  329. consume(LKLAMMER);
  330. in_args:=true;
  331. Must_be_valid:=true;
  332. paras:=parse_paras(true,false);
  333. consume(RKLAMMER);
  334. end
  335. else
  336. paras:=nil;
  337. pd:=voiddef;
  338. p1 := geninlinenode(l,paras);
  339. do_firstpass(p1);
  340. statement_syssym := p1;
  341. end;
  342. in_str_x_string : begin
  343. consume(LKLAMMER);
  344. in_args:=true;
  345. paras:=parse_paras(true,false);
  346. consume(RKLAMMER);
  347. p1 := geninlinenode(l,paras);
  348. do_firstpass(p1);
  349. statement_syssym := p1;
  350. pd:=voiddef;
  351. end;
  352. in_include_x_y,
  353. in_exclude_x_y:
  354. begin
  355. consume(LKLAMMER);
  356. in_args:=true;
  357. p1:=expr;
  358. Must_be_valid:=false;
  359. consume(COMMA);
  360. p2:=expr;
  361. { just a bit lisp feeling }
  362. statement_syssym:=geninlinenode(l,
  363. gencallparanode(p1,gencallparanode(p2,nil)));
  364. consume(RKLAMMER);
  365. pd:=voiddef;
  366. end;
  367. {in_val_x : begin
  368. consume(LKLAMMER);
  369. paras:=parse_paras(false);
  370. consume(RKLAMMER);
  371. p1 := geninlinenode(l,paras);
  372. do_firstpass(p1);
  373. statement_syssym := p1;
  374. pd:=voiddef;
  375. end; }
  376. else internalerror(15);
  377. end;
  378. in_args:=prev_in_args;
  379. Must_be_valid:=Store_valid;
  380. end;
  381. { reads the parameter for a subroutine call }
  382. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  383. var
  384. prev_in_args : boolean;
  385. prevafterassn : boolean;
  386. begin
  387. prev_in_args:=in_args;
  388. prevafterassn:=afterassignment;
  389. afterassignment:=false;
  390. { want we only determine the address of }
  391. { a subroutine }
  392. if not(getaddr) then
  393. begin
  394. if token=LKLAMMER then
  395. begin
  396. consume(LKLAMMER);
  397. in_args:=true;
  398. p1^.left:=parse_paras(false,false);
  399. consume(RKLAMMER);
  400. end
  401. else p1^.left:=nil;
  402. { do firstpass because we need the }
  403. { result type }
  404. do_firstpass(p1);
  405. end
  406. else
  407. begin
  408. { address operator @: }
  409. p1^.left:=nil;
  410. { forget pd }
  411. pd:=nil;
  412. { no postfix operators }
  413. again:=false;
  414. end;
  415. pd:=p1^.resulttype;
  416. in_args:=prev_in_args;
  417. afterassignment:=prevafterassn;
  418. end;
  419. { the following procedure handles the access to a property symbol }
  420. procedure handle_propertysym(sym : psym;var p1 : ptree;
  421. var pd : pdef);
  422. var
  423. paras : ptree;
  424. oldafterassignment : boolean;
  425. p2 : ptree;
  426. begin
  427. paras:=nil;
  428. { property parameters? }
  429. if token=LECKKLAMMER then
  430. begin
  431. consume(LECKKLAMMER);
  432. paras:=parse_paras(false,true);
  433. consume(RECKKLAMMER);
  434. end;
  435. { indexed property }
  436. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  437. begin
  438. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  439. paras:=gencallparanode(p2,paras);
  440. end;
  441. if not(afterassignment) and not(in_args) then
  442. begin
  443. { write property: }
  444. { no result }
  445. pd:=voiddef;
  446. if assigned(ppropertysym(sym)^.writeaccesssym) then
  447. begin
  448. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  449. begin
  450. { generate the method call }
  451. p1:=genmethodcallnode(pprocsym(
  452. ppropertysym(sym)^.writeaccesssym),
  453. ppropertysym(sym)^.writeaccesssym^.owner,p1);
  454. { we know the procedure to call, so
  455. force the usage of that procedure }
  456. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  457. p1^.left:=paras;
  458. { to be on the save side }
  459. oldafterassignment:=afterassignment;
  460. consume(ASSIGNMENT);
  461. { read the expression }
  462. afterassignment:=true;
  463. p2:=expr;
  464. p1^.left:=gencallparanode(p2,p1^.left);
  465. afterassignment:=oldafterassignment;
  466. end
  467. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  468. begin
  469. if assigned(paras) then
  470. message(parser_e_no_paras_allowed);
  471. { subscribed access? }
  472. if p1=nil then
  473. begin
  474. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  475. ppropertysym(sym)^.readaccesssym^.owner);
  476. end
  477. else
  478. p1:=gensubscriptnode(pvarsym(
  479. ppropertysym(sym)^.readaccesssym),p1);
  480. { to be on the save side }
  481. oldafterassignment:=afterassignment;
  482. consume(ASSIGNMENT);
  483. { read the expression }
  484. afterassignment:=true;
  485. p2:=expr;
  486. p1:=gennode(assignn,p1,p2);
  487. afterassignment:=oldafterassignment;
  488. end
  489. else
  490. begin
  491. p1:=genzeronode(errorn);
  492. Message(parser_e_no_procedure_to_access_property);
  493. end;
  494. end
  495. else
  496. begin
  497. p1:=genzeronode(errorn);
  498. Message(parser_e_no_procedure_to_access_property);
  499. end;
  500. end
  501. else
  502. begin
  503. { read property: }
  504. pd:=ppropertysym(sym)^.proptype;
  505. if assigned(ppropertysym(sym)^.readaccesssym) then
  506. begin
  507. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  508. begin
  509. if assigned(paras) then
  510. message(parser_e_no_paras_allowed);
  511. { subscribed access? }
  512. if p1=nil then
  513. begin
  514. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  515. ppropertysym(sym)^.readaccesssym^.owner);
  516. end
  517. else
  518. p1:=gensubscriptnode(pvarsym(
  519. ppropertysym(sym)^.readaccesssym),p1);
  520. end
  521. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  522. begin
  523. { generate the method call }
  524. p1:=genmethodcallnode(pprocsym(
  525. ppropertysym(sym)^.readaccesssym),
  526. ppropertysym(sym)^.readaccesssym^.owner,p1);
  527. { we know the procedure to call, so
  528. force the usage of that procedure }
  529. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  530. { insert paras }
  531. p1^.left:=paras;
  532. { if we should be delphi compatible }
  533. { then force type conversion }
  534. { }
  535. { isn't neccessary, the result types }
  536. { have to match excatly }
  537. {if cs_delphi2_compatible in aktswitches then
  538. p1:=gentypeconvnode(p1,pd);
  539. }
  540. end
  541. else
  542. begin
  543. p1:=genzeronode(errorn);
  544. Message(sym_e_type_mismatch);
  545. end;
  546. end
  547. else
  548. begin
  549. { error, no function to read property }
  550. p1:=genzeronode(errorn);
  551. Message(parser_e_no_procedure_to_access_property);
  552. end;
  553. end;
  554. end;
  555. { the ID token has to be consumed before calling this function }
  556. procedure do_member_read(const sym : psym;var p1 : ptree;
  557. var pd : pdef;var again : boolean);
  558. var
  559. static_name : string;
  560. isclassref : boolean;
  561. begin
  562. if sym=nil then
  563. begin
  564. Message(sym_e_id_no_member);
  565. disposetree(p1);
  566. p1:=genzeronode(errorn);
  567. { try to clean up }
  568. pd:=generrordef;
  569. again:=false;
  570. end
  571. else
  572. begin
  573. isclassref:=pd^.deftype=classrefdef;
  574. { check protected and private members }
  575. if ((sym^.properties and sp_private)<>0) and
  576. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  577. Message(parser_e_cant_access_private_member);
  578. if ((sym^.properties and sp_protected)<>0) and
  579. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  580. Message(parser_e_cant_access_protected_member);
  581. { we assume, that only procsyms and varsyms are in an object }
  582. { symbol table, for classes, properties are allowed }
  583. case sym^.typ of
  584. procsym:
  585. begin
  586. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  587. do_proc_call(false,again,p1,pd);
  588. { now we know the real method e.g. we can check for }
  589. { a class method }
  590. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  591. Message(parser_e_only_class_methods_via_class_ref);
  592. end;
  593. varsym:
  594. begin
  595. if isclassref then
  596. Message(parser_e_only_class_methods_via_class_ref);
  597. if (sym^.properties and sp_static)<>0 then
  598. begin
  599. static_name:=lowercase(srsymtable^.name^)+'_'+sym^.name;
  600. getsym(static_name,true);
  601. disposetree(p1);
  602. p1:=genloadnode(pvarsym(srsym),srsymtable);
  603. end
  604. else
  605. p1:=gensubscriptnode(pvarsym(sym),p1);
  606. pd:=pvarsym(sym)^.definition;
  607. end;
  608. propertysym:
  609. begin
  610. if isclassref then
  611. Message(parser_e_only_class_methods_via_class_ref);
  612. handle_propertysym(sym,p1,pd);
  613. end;
  614. else internalerror(16);
  615. end;
  616. end;
  617. end;
  618. function factor(getaddr : boolean) : ptree;
  619. var
  620. l : longint;
  621. p1,p2,p3 : ptree;
  622. code : word;
  623. pd,pd2 : pdef;
  624. unit_specific, again : boolean;
  625. static_name : string;
  626. sym : pvarsym;
  627. classh : pobjectdef;
  628. d : bestreal;
  629. constset : pconstset;
  630. propsym : ppropertysym;
  631. { p1 and p2 must contain valid values }
  632. procedure postfixoperators;
  633. begin
  634. while again do
  635. begin
  636. case token of
  637. CARET:
  638. begin
  639. consume(CARET);
  640. if pd^.deftype<>pointerdef then
  641. begin
  642. { ^ as binary operator is a problem!!!! (FK) }
  643. again:=false;
  644. Message(cg_e_invalid_qualifier);
  645. disposetree(p1);
  646. p1:=genzeronode(errorn);
  647. end
  648. else
  649. begin
  650. p1:=gensinglenode(derefn,p1);
  651. pd:=ppointerdef(pd)^.definition;
  652. end;
  653. end;
  654. LECKKLAMMER:
  655. begin
  656. if (pd^.deftype=objectdef) and
  657. pobjectdef(pd)^.isclass then
  658. begin
  659. { default property }
  660. propsym:=search_default_property(pobjectdef(pd));
  661. if not(assigned(propsym)) then
  662. begin
  663. disposetree(p1);
  664. p1:=genzeronode(errorn);
  665. again:=false;
  666. message(parser_e_no_default_property_available);
  667. end
  668. else
  669. begin
  670. p1:=nil;
  671. handle_propertysym(propsym,p1,pd);
  672. end;
  673. end
  674. else
  675. begin
  676. consume(LECKKLAMMER);
  677. repeat
  678. if (pd^.deftype<>arraydef) and
  679. (pd^.deftype<>stringdef) and
  680. (pd^.deftype<>pointerdef) then
  681. begin
  682. Message(cg_e_invalid_qualifier);
  683. disposetree(p1);
  684. p1:=genzeronode(errorn);
  685. again:=false;
  686. end
  687. else if (pd^.deftype=pointerdef) then
  688. begin
  689. p2:=expr;
  690. p1:=gennode(vecn,p1,p2);
  691. pd:=ppointerdef(pd)^.definition;
  692. end
  693. else
  694. begin
  695. p2:=expr;
  696. { support SEG:OFS for go32v2 Mem[] }
  697. if (target_info.target=target_GO32V2) and
  698. (p1^.treetype=loadn) and
  699. assigned(p1^.symtableentry) and
  700. assigned(p1^.symtableentry^.owner^.name) and
  701. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  702. ((p1^.symtableentry^.name='MEM') or
  703. (p1^.symtableentry^.name='MEMW') or
  704. (p1^.symtableentry^.name='MEML')) then
  705. begin
  706. if (token=COLON) then
  707. begin
  708. consume(COLON);
  709. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  710. p2:=expr;
  711. p2:=gennode(addn,p2,p3);
  712. p1:=gennode(vecn,p1,p2);
  713. p1^.memseg:=true;
  714. p1^.memindex:=true;
  715. end
  716. else
  717. begin
  718. p1:=gennode(vecn,p1,p2);
  719. p1^.memindex:=true;
  720. end;
  721. end
  722. { else
  723. if (target_info.target=target_GO32V2) and
  724. assigned(p1^.symtableentry) and
  725. assigned(p1^.symtableentry^.owner^.name) and
  726. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  727. ((p1^.symtableentry^.name='PORT') or
  728. (p1^.symtableentry^.name='PORTW') or
  729. (p1^.symtableentry^.name='PORTL')) then
  730. begin
  731. p1:=gennode(vecn,p1,p2);
  732. p1^.portindex:=true;
  733. p
  734. end;
  735. end }
  736. else
  737. p1:=gennode(vecn,p1,p2);
  738. if pd^.deftype=stringdef then
  739. pd:=cchardef
  740. else
  741. pd:=parraydef(pd)^.definition;
  742. end;
  743. if token=COMMA then consume(COMMA)
  744. else break;
  745. until false;
  746. consume(RECKKLAMMER);
  747. end;
  748. end;
  749. POINT:
  750. begin
  751. consume(POINT);
  752. case pd^.deftype of
  753. recorddef:
  754. begin
  755. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  756. consume(ID);
  757. if sym=nil then
  758. begin
  759. Message(sym_e_illegal_field);
  760. disposetree(p1);
  761. p1:=genzeronode(errorn);
  762. end
  763. else
  764. begin
  765. p1:=gensubscriptnode(sym,p1);
  766. pd:=sym^.definition;
  767. end;
  768. end;
  769. classrefdef:
  770. begin
  771. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  772. sym:=nil;
  773. while assigned(classh) do
  774. begin
  775. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  776. srsymtable:=classh^.publicsyms;
  777. if assigned(sym) then
  778. break;
  779. classh:=classh^.childof;
  780. end;
  781. consume(ID);
  782. do_member_read(sym,p1,pd,again);
  783. end;
  784. objectdef:
  785. begin
  786. classh:=pobjectdef(pd);
  787. sym:=nil;
  788. while assigned(classh) do
  789. begin
  790. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  791. srsymtable:=classh^.publicsyms;
  792. if assigned(sym) then
  793. break;
  794. classh:=classh^.childof;
  795. end;
  796. consume(ID);
  797. do_member_read(sym,p1,pd,again);
  798. end;
  799. pointerdef:
  800. begin
  801. if ppointerdef(pd)^.definition^.deftype
  802. in [recorddef,objectdef,classrefdef] then
  803. begin
  804. Message(cg_e_invalid_qualifier);
  805. { exterror:=strpnew(' may be pointer deref ^ is missing');
  806. error(invalid_qualifizier); }
  807. Comment(V_hint,' may be pointer deref ^ is missing');
  808. end
  809. else
  810. Message(cg_e_invalid_qualifier);
  811. end
  812. else
  813. begin
  814. Message(cg_e_invalid_qualifier);
  815. disposetree(p1);
  816. p1:=genzeronode(errorn);
  817. end;
  818. end;
  819. end;
  820. else
  821. begin
  822. { is this a procedure variable ? }
  823. if assigned(pd) then
  824. begin
  825. if (pd^.deftype=procvardef) then
  826. begin
  827. if getprocvar then
  828. again:=false
  829. else
  830. if (token=LKLAMMER) or
  831. ((pprocvardef(pd)^.para1=nil) and
  832. (token<>ASSIGNMENT) and (not in_args)) then
  833. begin
  834. { do this in a strange way }
  835. { it's not a clean solution }
  836. p2:=p1;
  837. p1:=gencallnode(nil,
  838. nil);
  839. p1^.right:=p2;
  840. p1^.unit_specific:=unit_specific;
  841. if token=LKLAMMER then
  842. begin
  843. consume(LKLAMMER);
  844. p1^.left:=parse_paras(false,false);
  845. consume(RKLAMMER);
  846. end;
  847. pd:=pprocvardef(pd)^.retdef;
  848. p1^.resulttype:=pd;
  849. end
  850. else again:=false;
  851. p1^.resulttype:=pd;
  852. end
  853. else again:=false;
  854. end
  855. else again:=false;
  856. end;
  857. end;
  858. end;
  859. end;
  860. procedure do_set(p : pconstset;pos : longint);
  861. var
  862. l : longint;
  863. begin
  864. if (pos>255) or
  865. (pos<0) then
  866. Message(parser_e_illegal_set_expr);
  867. l:=pos div 8;
  868. { do we allow the same twice }
  869. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  870. Message(parser_e_illegal_set_expr);
  871. p^[l]:=p^[l] or (1 shl (pos mod 8));
  872. end;
  873. var
  874. possible_error : boolean;
  875. storesymtablestack : psymtable;
  876. actprocsym : pprocsym;
  877. begin
  878. case token of
  879. ID:
  880. begin
  881. { allow post fix operators }
  882. again:=true;
  883. if (cs_delphi2_compatible in aktswitches) and
  884. (pattern='RESULT') and
  885. assigned(aktprocsym) and
  886. (procinfo.retdef<>pdef(voiddef)) then
  887. begin
  888. consume(ID);
  889. p1:=genzeronode(funcretn);
  890. pd:=procinfo.retdef;
  891. {$ifdef TEST_FUNCRET}
  892. p1^.funcretprocinfo:=pointer(@procinfo);
  893. p1^.retdef:=pd;
  894. {$endif TEST_FUNCRET}
  895. end
  896. else
  897. begin
  898. getsym(pattern,true);
  899. consume(ID);
  900. { is this an access to a function result ? }
  901. if assigned(aktprocsym) and
  902. ((srsym^.name=aktprocsym^.name) or
  903. ((pvarsym(srsym)=opsym) and
  904. ((procinfo.flags and pi_operator)<>0))) and
  905. (procinfo.retdef<>pdef(voiddef)) and
  906. (token<>LKLAMMER) and
  907. (not ((cs_tp_compatible in aktswitches) and
  908. (afterassignment or in_args))) then
  909. begin
  910. p1:=genzeronode(funcretn);
  911. pd:=procinfo.retdef;
  912. {$ifdef TEST_FUNCRET}
  913. p1^.funcretprocinfo:=pointer(@procinfo);
  914. p1^.retdef:=pd;
  915. {$endif TEST_FUNCRET}
  916. end
  917. else
  918. { else it's a normal symbol }
  919. begin
  920. if srsym^.typ=unitsym then
  921. begin
  922. consume(POINT);
  923. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  924. unit_specific:=true;
  925. consume(ID);
  926. end
  927. else
  928. unit_specific:=false;
  929. { check semantics of private }
  930. if srsymtable^.symtabletype=objectsymtable then
  931. begin
  932. if ((srsym^.properties and sp_private)<>0) and
  933. (pobjectdef(srsym^.owner^.defowner)^.
  934. owner^.symtabletype=unitsymtable) then
  935. Message(parser_e_cant_access_private_member);
  936. end;
  937. case srsym^.typ of
  938. absolutesym:
  939. begin
  940. p1:=genloadnode(pvarsym(srsym),srsymtable);
  941. pd:=pabsolutesym(srsym)^.definition;
  942. end;
  943. varsym:
  944. begin
  945. { are we in a class method ? }
  946. if (srsymtable^.symtabletype=objectsymtable) and
  947. assigned(aktprocsym) and
  948. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  949. Message(parser_e_only_class_methods);
  950. if (srsym^.properties and sp_static)<>0 then
  951. begin
  952. static_name:=lowercase(srsymtable^.name^)+'_'+srsym^.name;
  953. getsym(static_name,true);
  954. end;
  955. p1:=genloadnode(pvarsym(srsym),srsymtable);
  956. if pvarsym(srsym)^.is_valid=0 then
  957. begin
  958. p1^.is_first := true;
  959. { set special between first loaded
  960. until checked in firstpass }
  961. pvarsym(srsym)^.is_valid:=2;
  962. end;
  963. pd:=pvarsym(srsym)^.definition;
  964. end;
  965. typedconstsym:
  966. begin
  967. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  968. pd:=ptypedconstsym(srsym)^.definition;
  969. end;
  970. syssym:
  971. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  972. typesym:
  973. begin
  974. pd:=ptypesym(srsym)^.definition;
  975. { if we read a type declaration }
  976. { we have to return the type and }
  977. { nothing else }
  978. if block_type=bt_type then
  979. begin
  980. p1:=genzeronode(typen);
  981. p1^.resulttype:=pd;
  982. pd:=voiddef;
  983. end
  984. else
  985. begin
  986. if token=LKLAMMER then
  987. begin
  988. consume(LKLAMMER);
  989. p1:=expr;
  990. consume(RKLAMMER);
  991. p1:=gentypeconvnode(p1,pd);
  992. p1^.explizit:=true;
  993. end
  994. else if (token=POINT) and
  995. (pd^.deftype=objectdef) and
  996. ((pobjectdef(pd)^.options and oois_class)=0) then
  997. begin
  998. consume(POINT);
  999. if assigned(procinfo._class) then
  1000. begin
  1001. if procinfo._class^.isrelated(pobjectdef(pd)) then
  1002. begin
  1003. p1:=genzeronode(typen);
  1004. p1^.resulttype:=pd;
  1005. srsymtable:=pobjectdef(pd)^.publicsyms;
  1006. sym:=pvarsym(srsymtable^.search(pattern));
  1007. consume(ID);
  1008. do_member_read(sym,p1,pd,again);
  1009. end
  1010. else
  1011. begin
  1012. Message(parser_e_no_super_class);
  1013. pd:=generrordef;
  1014. again:=false;
  1015. end;
  1016. end
  1017. else
  1018. begin
  1019. { allows @TObject.Load }
  1020. { also allows static methods and variables }
  1021. p1:=genzeronode(typen);
  1022. p1^.resulttype:=pd;
  1023. srsymtable:=pobjectdef(pd)^.publicsyms;
  1024. sym:=pvarsym(srsymtable^.search(pattern));
  1025. if not(getaddr) and
  1026. ((sym^.properties and sp_static)=0) then
  1027. Message(sym_e_only_static_in_static)
  1028. else
  1029. begin
  1030. consume(ID);
  1031. do_member_read(sym,p1,pd,again);
  1032. end;
  1033. end
  1034. end
  1035. else
  1036. begin
  1037. { class reference ? }
  1038. if (pd^.deftype=objectdef)
  1039. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1040. begin
  1041. p1:=genzeronode(typen);
  1042. p1^.resulttype:=pd;
  1043. pd:=new(pclassrefdef,init(pd));
  1044. p1:=gensinglenode(loadvmtn,p1);
  1045. p1^.resulttype:=pd;
  1046. end
  1047. else
  1048. begin
  1049. { generate a type node }
  1050. { (for typeof etc) }
  1051. p1:=genzeronode(typen);
  1052. p1^.resulttype:=pd;
  1053. pd:=voiddef;
  1054. end;
  1055. end;
  1056. end;
  1057. end;
  1058. enumsym:
  1059. begin
  1060. p1:=genenumnode(penumsym(srsym));
  1061. pd:=p1^.resulttype;
  1062. end;
  1063. constsym:
  1064. begin
  1065. case pconstsym(srsym)^.consttype of
  1066. constint:
  1067. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1068. conststring:
  1069. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1070. constchar:
  1071. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1072. constreal:
  1073. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1074. constbool:
  1075. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1076. constseta:
  1077. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1078. psetdef(pconstsym(srsym)^.definition));
  1079. constord:
  1080. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1081. pconstsym(srsym)^.definition);
  1082. end;
  1083. pd:=p1^.resulttype;
  1084. end;
  1085. procsym:
  1086. begin
  1087. { are we in a class method ? }
  1088. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1089. assigned(aktprocsym) and
  1090. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1091. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1092. p1^.unit_specific:=unit_specific;
  1093. do_proc_call(getaddr,again,p1,pd);
  1094. if possible_error and
  1095. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1096. Message(parser_e_only_class_methods);
  1097. end;
  1098. propertysym:
  1099. begin
  1100. { access to property in a method }
  1101. { are we in a class method ? }
  1102. if (srsymtable^.symtabletype=objectsymtable) and
  1103. assigned(aktprocsym) and
  1104. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1105. Message(parser_e_only_class_methods);
  1106. { no method pointer }
  1107. p1:=nil;
  1108. handle_propertysym(srsym,p1,pd);
  1109. end;
  1110. errorsym:
  1111. begin
  1112. p1:=genzeronode(errorn);
  1113. pd:=generrordef;
  1114. if token=LKLAMMER then
  1115. begin
  1116. consume(LKLAMMER);
  1117. parse_paras(false,false);
  1118. consume(RKLAMMER);
  1119. end;
  1120. end;
  1121. else
  1122. begin
  1123. p1:=genzeronode(errorn);
  1124. pd:=generrordef;
  1125. Message(cg_e_illegal_expression);
  1126. end;
  1127. end;
  1128. end;
  1129. end;
  1130. { handle post fix operators }
  1131. postfixoperators;
  1132. end;
  1133. _NEW : begin
  1134. consume(_NEW);
  1135. consume(LKLAMMER);
  1136. p1:=factor(false);
  1137. if p1^.treetype<>typen then
  1138. Message(sym_e_type_id_expected);
  1139. pd:=p1^.resulttype;
  1140. pd2:=pd;
  1141. if (pd^.deftype<>pointerdef) or
  1142. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1143. begin
  1144. Message(parser_e_pointer_to_class_expected);
  1145. { if an error occurs, read til the end of the new }
  1146. { statement }
  1147. p1:=genzeronode(errorn);
  1148. l:=1;
  1149. while true do
  1150. begin
  1151. case token of
  1152. LKLAMMER : inc(l);
  1153. RKLAMMER : dec(l);
  1154. end;
  1155. consume(token);
  1156. if l=0 then
  1157. break;
  1158. end;
  1159. end
  1160. else
  1161. begin
  1162. disposetree(p1);
  1163. p1:=genzeronode(hnewn);
  1164. p1^.resulttype:=ppointerdef(pd)^.definition;
  1165. consume(COMMA);
  1166. afterassignment:=false;
  1167. { determines the current object defintion }
  1168. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1169. { check for an abstract class }
  1170. if (classh^.options and oois_abstract)<>0 then
  1171. Message(sym_e_no_instance_of_abstract_object);
  1172. { search the constructor also in the symbol tables of }
  1173. { the parents }
  1174. { no constructor found }
  1175. sym:=nil;
  1176. while assigned(classh) do
  1177. begin
  1178. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1179. srsymtable:=classh^.publicsyms;
  1180. if assigned(sym) then
  1181. break;
  1182. classh:=classh^.childof;
  1183. end;
  1184. consume(ID);
  1185. do_member_read(sym,p1,pd,again);
  1186. if (p1^.treetype<>calln) or
  1187. (assigned(p1^.procdefinition) and
  1188. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1189. Message(parser_e_expr_have_to_be_constructor_call);
  1190. p1:=gensinglenode(newn,p1);
  1191. { set the resulttype }
  1192. p1^.resulttype:=pd2;
  1193. consume(RKLAMMER);
  1194. end;
  1195. end;
  1196. _SELF:
  1197. begin
  1198. again:=true;
  1199. consume(_SELF);
  1200. if not assigned(procinfo._class) then
  1201. begin
  1202. p1:=genzeronode(errorn);
  1203. pd:=generrordef;
  1204. again:=false;
  1205. Message(parser_e_self_not_in_method);
  1206. end
  1207. else
  1208. begin
  1209. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1210. begin
  1211. { self in class methods is a class reference type }
  1212. pd:=new(pclassrefdef,init(procinfo._class));
  1213. p1:=genselfnode(pd);
  1214. p1^.resulttype:=pd;
  1215. end
  1216. else
  1217. begin
  1218. p1:=genselfnode(procinfo._class);
  1219. p1^.resulttype:=procinfo._class;
  1220. end;
  1221. pd:=p1^.resulttype;
  1222. postfixoperators;
  1223. end;
  1224. end;
  1225. _INHERITED : begin
  1226. again:=true;
  1227. consume(_INHERITED);
  1228. if assigned(procinfo._class) then
  1229. begin
  1230. classh:=procinfo._class^.childof;
  1231. while assigned(classh) do
  1232. begin
  1233. srsymtable:=pobjectdef(classh)^.publicsyms;
  1234. sym:=pvarsym(srsymtable^.search(pattern));
  1235. if assigned(sym) then
  1236. begin
  1237. p1:=genzeronode(typen);
  1238. p1^.resulttype:=classh;
  1239. pd:=p1^.resulttype;
  1240. consume(ID);
  1241. do_member_read(sym,p1,pd,again);
  1242. break;
  1243. end;
  1244. classh:=classh^.childof;
  1245. end;
  1246. if classh=nil then
  1247. begin
  1248. Message1(sym_e_id_no_member,pattern);
  1249. again:=false;
  1250. pd:=generrordef;
  1251. p1:=genzeronode(errorn);
  1252. end;
  1253. end
  1254. else
  1255. Message(parser_e_generic_methods_only_in_methods);
  1256. postfixoperators;
  1257. end;
  1258. INTCONST : begin
  1259. valint(pattern,l,code);
  1260. if code<>0 then
  1261. begin
  1262. val(pattern,d,code);
  1263. if code<>0 then
  1264. begin
  1265. Message(cg_e_invalid_integer);
  1266. l:=1;
  1267. consume(INTCONST);
  1268. p1:=genordinalconstnode(l,s32bitdef);
  1269. end
  1270. else
  1271. begin
  1272. consume(INTCONST);
  1273. p1:=genrealconstnode(d);
  1274. end;
  1275. end
  1276. else
  1277. begin
  1278. consume(INTCONST);
  1279. p1:=genordinalconstnode(l,s32bitdef);
  1280. end;
  1281. end;
  1282. REALNUMBER : begin
  1283. val(pattern,d,code);
  1284. if code<>0 then
  1285. begin
  1286. Message(parser_e_error_in_real);
  1287. d:=1.0;
  1288. end;
  1289. consume(REALNUMBER);
  1290. p1:=genrealconstnode(d);
  1291. end;
  1292. { FILE and STRING can be also a type cast }
  1293. _STRING:
  1294. begin
  1295. pd:=stringtype;
  1296. consume(LKLAMMER);
  1297. p1:=expr;
  1298. consume(RKLAMMER);
  1299. p1:=gentypeconvnode(p1,pd);
  1300. p1^.explizit:=true;
  1301. { handle postfix operators here e.g. string(a)[10] }
  1302. again:=true;
  1303. postfixoperators;
  1304. end;
  1305. _FILE:
  1306. begin
  1307. pd:=cfiledef;
  1308. consume(_FILE);
  1309. consume(LKLAMMER);
  1310. p1:=expr;
  1311. consume(RKLAMMER);
  1312. p1:=gentypeconvnode(p1,pd);
  1313. p1^.explizit:=true;
  1314. { handle postfix operators here e.g. string(a)[10] }
  1315. again:=true;
  1316. postfixoperators;
  1317. end;
  1318. CSTRING:
  1319. begin
  1320. p1:=genstringconstnode(pattern);
  1321. consume(CSTRING);
  1322. end;
  1323. CCHAR:
  1324. begin
  1325. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1326. consume(CCHAR);
  1327. end;
  1328. KLAMMERAFFE : begin
  1329. consume(KLAMMERAFFE);
  1330. p1:=factor(true);
  1331. p1:=gensinglenode(addrn,p1);
  1332. end;
  1333. LKLAMMER : begin
  1334. consume(LKLAMMER);
  1335. p1:=expr;
  1336. consume(RKLAMMER);
  1337. { it's not a good solution }
  1338. { but (a+b)^ makes some problems }
  1339. case token of
  1340. CARET,POINT,LECKKLAMMER:
  1341. begin
  1342. { we need the resulttype }
  1343. { of the expression in pd }
  1344. do_firstpass(p1);
  1345. pd:=p1^.resulttype;
  1346. again:=true;
  1347. postfixoperators;
  1348. end;
  1349. end;
  1350. end;
  1351. LECKKLAMMER : begin
  1352. consume(LECKKLAMMER);
  1353. new(constset);
  1354. for l:=0 to 31 do
  1355. constset^[l]:=0;
  1356. p2:=nil;
  1357. pd:=nil;
  1358. if token<>RECKKLAMMER then
  1359. while true do
  1360. begin
  1361. p1:=expr;
  1362. do_firstpass(p1);
  1363. case p1^.treetype of
  1364. ordconstn : begin
  1365. if pd=nil then
  1366. pd:=p1^.resulttype;
  1367. if not(is_equal(pd,p1^.resulttype)) then
  1368. Message(parser_e_typeconflict_in_set)
  1369. else
  1370. do_set(constset,p1^.value);
  1371. disposetree(p1);
  1372. end;
  1373. rangen : begin
  1374. if pd=nil then
  1375. pd:=p1^.left^.resulttype;
  1376. if not(is_equal(pd,p1^.left^.resulttype)) then
  1377. Message(parser_e_typeconflict_in_set)
  1378. else
  1379. for l:=p1^.left^.value to p1^.right^.value do
  1380. do_set(constset,l);
  1381. disposetree(p1);
  1382. end;
  1383. stringconstn : begin
  1384. if pd=nil then
  1385. pd:=cchardef;
  1386. if not(is_equal(pd,cchardef)) then
  1387. Message(parser_e_typeconflict_in_set)
  1388. else
  1389. for l:=1 to length(pstring(p1^.values)^) do
  1390. do_set(constset,ord(pstring(p1^.values)^[l]));
  1391. disposetree(p1);
  1392. end;
  1393. else
  1394. begin
  1395. if pd=nil then
  1396. pd:=p1^.resulttype;
  1397. if not(is_equal(pd,p1^.resulttype)) then
  1398. Message(parser_e_typeconflict_in_set);
  1399. p2:=gennode(setelen,p1,p2);
  1400. end;
  1401. end;
  1402. if token=COMMA then
  1403. consume(COMMA)
  1404. else break;
  1405. end;
  1406. consume(RECKKLAMMER);
  1407. p1:=gensinglenode(setconstrn,p2);
  1408. p1^.resulttype:=new(psetdef,init(pd,255));
  1409. p1^.constset:=constset;
  1410. end;
  1411. PLUS : begin
  1412. consume(PLUS);
  1413. p1:=factor(false);
  1414. end;
  1415. MINUS : begin
  1416. consume(MINUS);
  1417. p1:=factor(false);
  1418. p1:=gensinglenode(umminusn,p1);
  1419. end;
  1420. _NOT : begin
  1421. consume(_NOT);
  1422. p1:=factor(false);
  1423. p1:=gensinglenode(notn,p1);
  1424. end;
  1425. _TRUE : begin
  1426. consume(_TRUE);
  1427. p1:=genordinalconstnode(1,booldef);
  1428. end;
  1429. _FALSE : begin
  1430. consume(_FALSE);
  1431. p1:=genordinalconstnode(0,booldef);
  1432. end;
  1433. _NIL : begin
  1434. consume(_NIL);
  1435. p1:=genzeronode(niln);
  1436. end;
  1437. else
  1438. begin
  1439. p1:=genzeronode(errorn);
  1440. consume(token);
  1441. Message(cg_e_illegal_expression);
  1442. end;
  1443. end;
  1444. factor:=p1;
  1445. end;
  1446. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1447. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1448. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1449. isn,asn,inn,
  1450. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1451. nothingn,nothingn,nothingn,nothingn,nothingn,
  1452. nothingn,nothingn,nothingn,nothingn,nothingn,
  1453. nothingn,nothingn,nothingn,nothingn,nothingn,
  1454. nothingn,andn,nothingn,nothingn,nothingn,
  1455. nothingn,nothingn,nothingn,nothingn,nothingn,
  1456. nothingn,nothingn,divn,nothingn,nothingn,
  1457. nothingn,nothingn,nothingn,nothingn,nothingn,
  1458. nothingn,nothingn,nothingn,nothingn,nothingn,
  1459. nothingn,nothingn,nothingn,nothingn,nothingn,
  1460. modn,nothingn,nothingn,nothingn,nothingn,
  1461. nothingn,nothingn,orn,
  1462. nothingn,nothingn,nothingn,nothingn,nothingn,
  1463. nothingn,nothingn,shln,shrn,
  1464. nothingn,nothingn,nothingn,nothingn,nothingn,
  1465. nothingn,nothingn,nothingn,nothingn,nothingn,
  1466. nothingn,xorn);
  1467. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1468. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1469. [PLUS,MINUS,_OR,_XOR],
  1470. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1471. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1472. {Reads a subexpression while the operators are of the current precedence
  1473. level, or any higher level. Replaces the old term, simpl_expr and
  1474. simpl2_expr.}
  1475. var p1,p2:Ptree;
  1476. oldt:Ttoken;
  1477. begin
  1478. { if pred_level=high(Toperator_precedence) then }
  1479. if pred_level=opmultiply then
  1480. p1:=factor(getprocvar)
  1481. else
  1482. p1:=sub_expr(succ(pred_level),true);
  1483. repeat
  1484. { aweful hack to support const a : 1..2=1; }
  1485. { disadvantage of tables :) FK }
  1486. if (token in operator_levels[pred_level]) and
  1487. ((token<>EQUAL) or accept_equal) then
  1488. begin
  1489. oldt:=token;
  1490. consume(token);
  1491. { if pred_level=high(Toperator_precedence) then }
  1492. if pred_level=opmultiply then
  1493. p2:=factor(getprocvar)
  1494. else
  1495. p2:=sub_expr(succ(pred_level),true);
  1496. p1:=gennode(tok2node[oldt],p1,p2);
  1497. end
  1498. else
  1499. break;
  1500. until false;
  1501. sub_expr:=p1;
  1502. end;
  1503. function comp_expr(accept_equal : boolean):Ptree;
  1504. begin
  1505. comp_expr:=sub_expr(opcompare,accept_equal);
  1506. end;
  1507. function expr : ptree;
  1508. var
  1509. p1,p2 : ptree;
  1510. oldafterassignment : boolean;
  1511. begin
  1512. oldafterassignment:=afterassignment;
  1513. p1:=sub_expr(opcompare,true);
  1514. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1515. afterassignment:=true;
  1516. case token of
  1517. POINTPOINT : begin
  1518. consume(POINTPOINT);
  1519. p2:=sub_expr(opcompare,true);
  1520. p1:=gennode(rangen,p1,p2);
  1521. end;
  1522. ASSIGNMENT : begin
  1523. consume(ASSIGNMENT);
  1524. { avoid a firstpass of a procedure if
  1525. it must be assigned to a procvar }
  1526. { should be recursive for a:=b:=c !!! }
  1527. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1528. getprocvar:=true;
  1529. p2:=sub_expr(opcompare,true);
  1530. if getprocvar and (p2^.treetype=calln) then
  1531. begin
  1532. p2^.treetype:=loadn;
  1533. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1534. p2^.symtableentry:=p2^.symtableprocentry;
  1535. end;
  1536. getprocvar:=false;
  1537. p1:=gennode(assignn,p1,p2);
  1538. end;
  1539. { this is the code for C like assignements }
  1540. { from an improvement of Peter Schaefer }
  1541. _PLUSASN : begin
  1542. consume(_PLUSASN );
  1543. p2:=sub_expr(opcompare,true);
  1544. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1545. { was first
  1546. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1547. but disposetree assumes that we have a real
  1548. *** tree *** }
  1549. end;
  1550. _MINUSASN : begin
  1551. consume(_MINUSASN );
  1552. p2:=sub_expr(opcompare,true);
  1553. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1554. end;
  1555. _STARASN : begin
  1556. consume(_STARASN );
  1557. p2:=sub_expr(opcompare,true);
  1558. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1559. end;
  1560. _SLASHASN : begin
  1561. consume(_SLASHASN );
  1562. p2:=sub_expr(opcompare,true);
  1563. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1564. end;
  1565. end;
  1566. afterassignment:=oldafterassignment;
  1567. expr:=p1;
  1568. end;
  1569. function get_intconst:longint;
  1570. {Reads an expression, tries to evalute it and check if it is an integer
  1571. constant. Then the constant is returned.}
  1572. var p:Ptree;
  1573. begin
  1574. p:=expr;
  1575. do_firstpass(p);
  1576. if (p^.treetype<>ordconstn) and
  1577. (p^.resulttype^.deftype=orddef) and
  1578. not (Porddef(p^.resulttype)^.typ in
  1579. [uvoid,uchar,bool8bit]) then
  1580. Message(cg_e_illegal_expression)
  1581. else
  1582. get_intconst:=p^.value;
  1583. disposetree(p);
  1584. end;
  1585. function get_stringconst:string;
  1586. {Reads an expression, tries to evaluate it and checks if it is a string
  1587. constant. Then the constant is returned.}
  1588. var p:Ptree;
  1589. begin
  1590. get_stringconst:='';
  1591. p:=expr;
  1592. do_firstpass(p);
  1593. if p^.treetype<>stringconstn then
  1594. if (p^.treetype=ordconstn) and
  1595. (p^.resulttype^.deftype=orddef) and
  1596. (Porddef(p^.resulttype)^.typ=uchar) then
  1597. get_stringconst:=char(p^.value)
  1598. else
  1599. Message(cg_e_illegal_expression)
  1600. else
  1601. get_stringconst:=p^.values^;
  1602. disposetree(p);
  1603. end;
  1604. end.
  1605. {
  1606. $Log$
  1607. Revision 1.11 1998-05-04 11:22:26 florian
  1608. * problem with DOM solved: it crashes when accessing a property in a method
  1609. Revision 1.10 1998/05/01 16:38:45 florian
  1610. * handling of private and protected fixed
  1611. + change_keywords_to_tp implemented to remove
  1612. keywords which aren't supported by tp
  1613. * break and continue are now symbols of the system unit
  1614. + widestring, longstring and ansistring type released
  1615. Revision 1.9 1998/04/29 10:33:58 pierre
  1616. + added some code for ansistring (not complete nor working yet)
  1617. * corrected operator overloading
  1618. * corrected nasm output
  1619. + started inline procedures
  1620. + added starstarn : use ** for exponentiation (^ gave problems)
  1621. + started UseTokenInfo cond to get accurate positions
  1622. Revision 1.8 1998/04/14 23:27:03 florian
  1623. + exclude/include with constant second parameter added
  1624. Revision 1.7 1998/04/09 23:02:15 florian
  1625. * small problems solved to get remake3 work
  1626. Revision 1.6 1998/04/09 22:16:35 florian
  1627. * problem with previous REGALLOC solved
  1628. * improved property support
  1629. Revision 1.5 1998/04/08 10:26:09 florian
  1630. * correct error handling of virtual constructors
  1631. * problem with new type declaration handling fixed
  1632. Revision 1.4 1998/04/07 22:45:05 florian
  1633. * bug0092, bug0115 and bug0121 fixed
  1634. + packed object/class/array
  1635. Revision 1.3 1998/04/07 13:19:46 pierre
  1636. * bugfixes for reset_gdb_info
  1637. in MEM parsing for go32v2
  1638. better external symbol creation
  1639. support for rhgdb.exe (lowercase file names)
  1640. Revision 1.2 1998/03/26 11:18:31 florian
  1641. - switch -Sa removed
  1642. - support of a:=b:=0 removed
  1643. Revision 1.1.1.1 1998/03/25 11:18:14 root
  1644. * Restored version
  1645. Revision 1.26 1998/03/24 21:48:33 florian
  1646. * just a couple of fixes applied:
  1647. - problem with fixed16 solved
  1648. - internalerror 10005 problem fixed
  1649. - patch for assembler reading
  1650. - small optimizer fix
  1651. - mem is now supported
  1652. Revision 1.25 1998/03/21 23:59:39 florian
  1653. * indexed properties fixed
  1654. * ppu i/o of properties fixed
  1655. * field can be also used for write access
  1656. * overriding of properties
  1657. Revision 1.24 1998/03/16 22:42:21 florian
  1658. * some fixes of Peter applied:
  1659. ofs problem, profiler support
  1660. Revision 1.23 1998/03/11 11:23:57 florian
  1661. * bug0081 and bug0109 fixed
  1662. Revision 1.22 1998/03/10 16:27:42 pierre
  1663. * better line info in stabs debug
  1664. * symtabletype and lexlevel separated into two fields of tsymtable
  1665. + ifdef MAKELIB for direct library output, not complete
  1666. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  1667. working
  1668. + ifdef TESTFUNCRET for setting func result in underfunction, not
  1669. working
  1670. Revision 1.21 1998/03/10 01:17:24 peter
  1671. * all files have the same header
  1672. * messages are fully implemented, EXTDEBUG uses Comment()
  1673. + AG... files for the Assembler generation
  1674. Revision 1.20 1998/03/06 00:52:44 peter
  1675. * replaced all old messages from errore.msg, only ExtDebug and some
  1676. Comment() calls are left
  1677. * fixed options.pas
  1678. Revision 1.19 1998/03/02 01:49:02 peter
  1679. * renamed target_DOS to target_GO32V1
  1680. + new verbose system, merged old errors and verbose units into one new
  1681. verbose.pas, so errors.pas is obsolete
  1682. Revision 1.18 1998/03/01 22:46:18 florian
  1683. + some win95 linking stuff
  1684. * a couple of bugs fixed:
  1685. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  1686. Revision 1.17 1998/02/27 21:24:06 florian
  1687. * dll support changed (dll name can be also a string contants)
  1688. Revision 1.16 1998/02/24 00:19:17 peter
  1689. * makefile works again (btw. linux does like any char after a \ )
  1690. * removed circular unit with assemble and files
  1691. * fixed a sigsegv in pexpr
  1692. * pmodule init unit/program is the almost the same, merged them
  1693. Revision 1.15 1998/02/13 10:35:24 daniel
  1694. * Made Motorola version compilable.
  1695. * Fixed optimizer
  1696. Revision 1.14 1998/02/12 17:19:20 florian
  1697. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  1698. also that aktswitches isn't a pointer)
  1699. Revision 1.13 1998/02/12 11:50:26 daniel
  1700. Yes! Finally! After three retries, my patch!
  1701. Changes:
  1702. Complete rewrite of psub.pas.
  1703. Added support for DLL's.
  1704. Compiler requires less memory.
  1705. Platform units for each platform.
  1706. Revision 1.12 1998/02/11 21:56:37 florian
  1707. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1708. Revision 1.11 1998/02/01 22:41:11 florian
  1709. * clean up
  1710. + system.assigned([class])
  1711. + system.assigned([class of xxxx])
  1712. * first fixes of as and is-operator
  1713. Revision 1.10 1998/02/01 15:04:15 florian
  1714. * better error recovering
  1715. * some clean up
  1716. Revision 1.9 1998/01/30 21:27:05 carl
  1717. * partial bugfix #88, #89 and typeof and other inline functions
  1718. (these bugs have a deeper nesting level, and therefore i only fixed
  1719. the parser crashes - there is also a tree crash).
  1720. Revision 1.8 1998/01/26 17:31:01 florian
  1721. * stupid bug with self in class methods fixed
  1722. Revision 1.7 1998/01/25 22:29:02 florian
  1723. * a lot bug fixes on the DOM
  1724. Revision 1.6 1998/01/23 10:46:41 florian
  1725. * small problems with FCL object model fixed, objpas?.inc is compilable
  1726. Revision 1.5 1998/01/16 22:34:42 michael
  1727. * Changed 'conversation' to 'conversion'. Waayyy too much chatting going on
  1728. in this compiler :)
  1729. Revision 1.4 1998/01/16 18:03:15 florian
  1730. * small bug fixes, some stuff of delphi styled constructores added
  1731. Revision 1.3 1998/01/13 23:11:14 florian
  1732. + class methods
  1733. Revision 1.2 1998/01/09 09:09:59 michael
  1734. + Initial implementation, second try
  1735. }