pexpr.pas 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  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(getaddr : boolean;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. globtype,systems,tokens,
  35. cobjects,globals,scanner,aasm,pass_1,
  36. hcodegen,types,verbose,strings,tccal
  37. { parser specific stuff }
  38. ,pbase,pdecl
  39. { processor specific stuff }
  40. {$ifdef i386}
  41. ,i386base
  42. {$endif}
  43. {$ifdef m68k}
  44. ,m68k
  45. {$endif}
  46. ;
  47. const
  48. allow_type : boolean = true;
  49. got_addrn : boolean = false;
  50. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  51. var
  52. p1,p2 : ptree;
  53. end_of_paras : ttoken;
  54. begin
  55. if in_prop_paras then
  56. end_of_paras:=RECKKLAMMER
  57. else
  58. end_of_paras:=RKLAMMER;
  59. if token=end_of_paras then
  60. begin
  61. parse_paras:=nil;
  62. exit;
  63. end;
  64. p2:=nil;
  65. inc(parsing_para_level);
  66. while true do
  67. begin
  68. p1:=comp_expr(true);
  69. p2:=gencallparanode(p1,p2);
  70. { it's for the str(l:5,s); }
  71. if _colon and (token=COLON) then
  72. begin
  73. consume(COLON);
  74. p1:=comp_expr(true);
  75. p2:=gencallparanode(p1,p2);
  76. p2^.is_colon_para:=true;
  77. if token=COLON then
  78. begin
  79. consume(COLON);
  80. p1:=comp_expr(true);
  81. p2:=gencallparanode(p1,p2);
  82. p2^.is_colon_para:=true;
  83. end
  84. end;
  85. if token=COMMA then
  86. consume(COMMA)
  87. else
  88. break;
  89. end;
  90. dec(parsing_para_level);
  91. parse_paras:=p2;
  92. end;
  93. procedure check_tp_procvar(var p : ptree);
  94. var
  95. p1 : ptree;
  96. Store_valid : boolean;
  97. begin
  98. if (m_tp_procvar in aktmodeswitches) and
  99. (not got_addrn) and
  100. (not in_args) and
  101. (p^.treetype=loadn) then
  102. begin
  103. { support if procvar then for tp7 and many other expression like this }
  104. Store_valid:=Must_be_valid;
  105. Must_be_valid:=false;
  106. firstpass(p);
  107. Must_be_valid:=Store_valid;
  108. if not(getprocvar) and (p^.resulttype^.deftype=procvardef) then
  109. begin
  110. p1:=gencallnode(nil,nil);
  111. p1^.right:=p;
  112. p1^.resulttype:=pprocvardef(p^.resulttype)^.retdef;
  113. firstpass(p1);
  114. p:=p1;
  115. end;
  116. end;
  117. end;
  118. function statement_syssym(l : longint;var pd : pdef) : ptree;
  119. var
  120. p1,p2,paras : ptree;
  121. prev_in_args : boolean;
  122. Store_valid : boolean;
  123. begin
  124. prev_in_args:=in_args;
  125. Store_valid:=Must_be_valid;
  126. case l of
  127. in_ord_x :
  128. begin
  129. consume(LKLAMMER);
  130. in_args:=true;
  131. Must_be_valid:=true;
  132. p1:=comp_expr(true);
  133. consume(RKLAMMER);
  134. do_firstpass(p1);
  135. p1:=geninlinenode(in_ord_x,false,p1);
  136. do_firstpass(p1);
  137. statement_syssym := p1;
  138. pd:=p1^.resulttype;
  139. end;
  140. in_break :
  141. begin
  142. statement_syssym:=genzeronode(breakn);
  143. pd:=voiddef;
  144. end;
  145. in_continue :
  146. begin
  147. statement_syssym:=genzeronode(continuen);
  148. pd:=voiddef;
  149. end;
  150. in_typeof_x :
  151. begin
  152. consume(LKLAMMER);
  153. in_args:=true;
  154. {allow_type:=true;}
  155. p1:=comp_expr(true);
  156. {allow_type:=false;}
  157. consume(RKLAMMER);
  158. pd:=voidpointerdef;
  159. if p1^.treetype=typen then
  160. begin
  161. if (p1^.typenodetype=nil) then
  162. begin
  163. Message(type_e_mismatch);
  164. statement_syssym:=genzeronode(errorn);
  165. end
  166. else
  167. if p1^.typenodetype^.deftype=objectdef then
  168. begin
  169. { we can use resulttype in pass_2 (PM) }
  170. p1^.resulttype:=p1^.typenodetype;
  171. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  172. end
  173. else
  174. begin
  175. Message(type_e_mismatch);
  176. disposetree(p1);
  177. statement_syssym:=genzeronode(errorn);
  178. end;
  179. end
  180. else { not a type node }
  181. begin
  182. Must_be_valid:=false;
  183. do_firstpass(p1);
  184. if (p1^.resulttype=nil) then
  185. begin
  186. Message(type_e_mismatch);
  187. disposetree(p1);
  188. statement_syssym:=genzeronode(errorn)
  189. end
  190. else
  191. if p1^.resulttype^.deftype=objectdef then
  192. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  193. else
  194. begin
  195. Message(type_e_mismatch);
  196. statement_syssym:=genzeronode(errorn);
  197. disposetree(p1);
  198. end;
  199. end;
  200. end;
  201. in_sizeof_x :
  202. begin
  203. consume(LKLAMMER);
  204. in_args:=true;
  205. {allow_type:=true;}
  206. p1:=comp_expr(true);
  207. {allow_type:=false; }
  208. consume(RKLAMMER);
  209. pd:=s32bitdef;
  210. if p1^.treetype=typen then
  211. begin
  212. statement_syssym:=genordinalconstnode(p1^.typenodetype^.size,pd);
  213. { p1 not needed !}
  214. disposetree(p1);
  215. end
  216. else
  217. begin
  218. Must_be_valid:=false;
  219. do_firstpass(p1);
  220. if ((p1^.resulttype^.deftype=objectdef) and
  221. ((pobjectdef(p1^.resulttype)^.options and oo_hasconstructor)<>0)) or
  222. is_open_array(p1^.resulttype) or
  223. is_open_string(p1^.resulttype) then
  224. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  225. else
  226. begin
  227. statement_syssym:=genordinalconstnode(p1^.resulttype^.size,pd);
  228. { p1 not needed !}
  229. disposetree(p1);
  230. end;
  231. end;
  232. end;
  233. in_assigned_x :
  234. begin
  235. consume(LKLAMMER);
  236. in_args:=true;
  237. p1:=comp_expr(true);
  238. Must_be_valid:=true;
  239. do_firstpass(p1);
  240. case p1^.resulttype^.deftype of
  241. pointerdef,
  242. procvardef,
  243. classrefdef : ;
  244. objectdef : if not(pobjectdef(p1^.resulttype)^.isclass) then
  245. Message(parser_e_illegal_parameter_list);
  246. else
  247. Message(parser_e_illegal_parameter_list);
  248. end;
  249. p2:=gencallparanode(p1,nil);
  250. p2:=geninlinenode(in_assigned_x,false,p2);
  251. consume(RKLAMMER);
  252. pd:=booldef;
  253. statement_syssym:=p2;
  254. end;
  255. in_ofs_x :
  256. begin
  257. consume(LKLAMMER);
  258. in_args:=true;
  259. p1:=comp_expr(true);
  260. p1:=gensinglenode(addrn,p1);
  261. Must_be_valid:=false;
  262. do_firstpass(p1);
  263. { Ofs() returns a longint, not a pointer }
  264. p1^.resulttype:=u32bitdef;
  265. pd:=p1^.resulttype;
  266. consume(RKLAMMER);
  267. statement_syssym:=p1;
  268. end;
  269. in_addr_x :
  270. begin
  271. consume(LKLAMMER);
  272. in_args:=true;
  273. p1:=comp_expr(true);
  274. p1:=gensinglenode(addrn,p1);
  275. Must_be_valid:=false;
  276. do_firstpass(p1);
  277. pd:=p1^.resulttype;
  278. consume(RKLAMMER);
  279. statement_syssym:=p1;
  280. end;
  281. in_seg_x :
  282. begin
  283. consume(LKLAMMER);
  284. in_args:=true;
  285. p1:=comp_expr(true);
  286. do_firstpass(p1);
  287. if p1^.location.loc<>LOC_REFERENCE then
  288. Message(cg_e_illegal_expression);
  289. p1:=genordinalconstnode(0,s32bitdef);
  290. Must_be_valid:=false;
  291. pd:=s32bitdef;
  292. consume(RKLAMMER);
  293. statement_syssym:=p1;
  294. end;
  295. in_high_x,
  296. in_low_x :
  297. begin
  298. consume(LKLAMMER);
  299. in_args:=true;
  300. {allow_type:=true;}
  301. p1:=comp_expr(true);
  302. {allow_type:=false;}
  303. do_firstpass(p1);
  304. if p1^.treetype=typen then
  305. p1^.resulttype:=p1^.typenodetype;
  306. Must_be_valid:=false;
  307. p2:=geninlinenode(l,false,p1);
  308. consume(RKLAMMER);
  309. pd:=s32bitdef;
  310. statement_syssym:=p2;
  311. end;
  312. in_succ_x,
  313. in_pred_x :
  314. begin
  315. consume(LKLAMMER);
  316. in_args:=true;
  317. p1:=comp_expr(true);
  318. do_firstpass(p1);
  319. Must_be_valid:=false;
  320. p2:=geninlinenode(l,false,p1);
  321. consume(RKLAMMER);
  322. pd:=p1^.resulttype;
  323. statement_syssym:=p2;
  324. end;
  325. in_inc_x,
  326. in_dec_x :
  327. begin
  328. consume(LKLAMMER);
  329. in_args:=true;
  330. p1:=comp_expr(true);
  331. Must_be_valid:=false;
  332. if token=COMMA then
  333. begin
  334. consume(COMMA);
  335. p2:=gencallparanode(comp_expr(true),nil);
  336. end
  337. else
  338. p2:=nil;
  339. p2:=gencallparanode(p1,p2);
  340. statement_syssym:=geninlinenode(l,false,p2);
  341. consume(RKLAMMER);
  342. pd:=voiddef;
  343. end;
  344. in_concat_x :
  345. begin
  346. consume(LKLAMMER);
  347. in_args:=true;
  348. p2:=nil;
  349. while true do
  350. begin
  351. p1:=comp_expr(true);
  352. Must_be_valid:=true;
  353. do_firstpass(p1);
  354. if not((p1^.resulttype^.deftype=stringdef) or
  355. ((p1^.resulttype^.deftype=orddef) and
  356. (porddef(p1^.resulttype)^.typ=uchar))) then
  357. Message(parser_e_illegal_parameter_list);
  358. if p2<>nil then
  359. p2:=gennode(addn,p2,p1)
  360. else
  361. p2:=p1;
  362. if token=COMMA then
  363. consume(COMMA)
  364. else
  365. break;
  366. end;
  367. consume(RKLAMMER);
  368. pd:=cshortstringdef;
  369. statement_syssym:=p2;
  370. end;
  371. in_read_x,
  372. in_readln_x :
  373. begin
  374. if token=LKLAMMER then
  375. begin
  376. consume(LKLAMMER);
  377. in_args:=true;
  378. Must_be_valid:=false;
  379. paras:=parse_paras(false,false);
  380. consume(RKLAMMER);
  381. end
  382. else
  383. paras:=nil;
  384. pd:=voiddef;
  385. p1:=geninlinenode(l,false,paras);
  386. do_firstpass(p1);
  387. statement_syssym := p1;
  388. end;
  389. in_write_x,
  390. in_writeln_x :
  391. begin
  392. if token=LKLAMMER then
  393. begin
  394. consume(LKLAMMER);
  395. in_args:=true;
  396. Must_be_valid:=true;
  397. paras:=parse_paras(true,false);
  398. consume(RKLAMMER);
  399. end
  400. else
  401. paras:=nil;
  402. pd:=voiddef;
  403. p1 := geninlinenode(l,false,paras);
  404. do_firstpass(p1);
  405. statement_syssym := p1;
  406. end;
  407. in_str_x_string :
  408. begin
  409. consume(LKLAMMER);
  410. in_args:=true;
  411. paras:=parse_paras(true,false);
  412. consume(RKLAMMER);
  413. p1 := geninlinenode(l,false,paras);
  414. do_firstpass(p1);
  415. statement_syssym := p1;
  416. pd:=voiddef;
  417. end;
  418. in_val_x:
  419. Begin
  420. consume(LKLAMMER);
  421. in_args := true;
  422. p1:= gencallparanode(comp_expr(true), nil);
  423. Must_be_valid := False;
  424. consume(COMMA);
  425. p2 := gencallparanode(comp_expr(true),p1);
  426. if (token = COMMA) then
  427. Begin
  428. consume(COMMA);
  429. p2 := gencallparanode(comp_expr(true),p2)
  430. End;
  431. consume(RKLAMMER);
  432. p2 := geninlinenode(l,false,p2);
  433. do_firstpass(p2);
  434. statement_syssym := p2;
  435. pd := voiddef;
  436. End;
  437. in_include_x_y,
  438. in_exclude_x_y :
  439. begin
  440. consume(LKLAMMER);
  441. in_args:=true;
  442. p1:=comp_expr(true);
  443. Must_be_valid:=false;
  444. consume(COMMA);
  445. p2:=comp_expr(true);
  446. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  447. consume(RKLAMMER);
  448. pd:=voiddef;
  449. end;
  450. in_assert_x_y :
  451. begin
  452. consume(LKLAMMER);
  453. in_args:=true;
  454. p1:=comp_expr(true);
  455. if token=COMMA then
  456. begin
  457. consume(COMMA);
  458. p2:=comp_expr(true);
  459. end
  460. else
  461. begin
  462. { then insert an empty string }
  463. p2:=genstringconstnode('');
  464. end;
  465. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  466. consume(RKLAMMER);
  467. pd:=voiddef;
  468. end;
  469. else
  470. internalerror(15);
  471. end;
  472. in_args:=prev_in_args;
  473. Must_be_valid:=Store_valid;
  474. end;
  475. { reads the parameter for a subroutine call }
  476. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  477. var
  478. prev_in_args : boolean;
  479. prevafterassn : boolean;
  480. begin
  481. prev_in_args:=in_args;
  482. prevafterassn:=afterassignment;
  483. afterassignment:=false;
  484. { want we only determine the address of }
  485. { a subroutine ? }
  486. if not(getaddr) then
  487. begin
  488. if token=LKLAMMER then
  489. begin
  490. consume(LKLAMMER);
  491. in_args:=true;
  492. p1^.left:=parse_paras(false,false);
  493. consume(RKLAMMER);
  494. end
  495. else p1^.left:=nil;
  496. { do firstpass because we need the }
  497. { result type }
  498. do_firstpass(p1);
  499. end
  500. else
  501. begin
  502. { address operator @: }
  503. p1^.left:=nil;
  504. { forget pd }
  505. pd:=nil;
  506. if (p1^.symtableproc^.symtabletype=withsymtable) and
  507. (p1^.symtableproc^.defowner^.deftype=objectdef) then
  508. begin
  509. p1^.methodpointer:=getcopy(pwithsymtable(p1^.symtableproc)^.withrefnode);
  510. end
  511. else if not(assigned(p1^.methodpointer)) then
  512. begin
  513. { we must provide a method pointer, if it isn't given, }
  514. { it is self }
  515. p1^.methodpointer:=genselfnode(procinfo._class);
  516. p1^.methodpointer^.resulttype:=procinfo._class;
  517. end;
  518. { no postfix operators }
  519. again:=false;
  520. end;
  521. pd:=p1^.resulttype;
  522. in_args:=prev_in_args;
  523. afterassignment:=prevafterassn;
  524. end;
  525. procedure handle_procvar(procvar : pprocvardef;var t : ptree);
  526. var
  527. hp : ptree;
  528. begin
  529. hp:=nil;
  530. if (proc_to_procvar_equal(pprocsym(t^.symtableentry)^.definition,procvar)) then
  531. begin
  532. if ((procvar^.options and pomethodpointer)<>0) then
  533. hp:=genloadmethodcallnode(pprocsym(t^.symtableprocentry),t^.symtable,getcopy(t^.methodpointer))
  534. else
  535. hp:=genloadcallnode(pprocsym(t^.symtableprocentry),t^.symtable);
  536. end;
  537. if assigned(hp) then
  538. begin
  539. disposetree(t);
  540. t:=hp;
  541. end;
  542. end;
  543. { the following procedure handles the access to a property symbol }
  544. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : ptree;
  545. var pd : pdef);
  546. var
  547. paras : ptree;
  548. p2 : ptree;
  549. begin
  550. paras:=nil;
  551. { property parameters? }
  552. if token=LECKKLAMMER then
  553. begin
  554. consume(LECKKLAMMER);
  555. paras:=parse_paras(false,true);
  556. consume(RECKKLAMMER);
  557. end;
  558. { indexed property }
  559. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  560. begin
  561. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  562. paras:=gencallparanode(p2,paras);
  563. end;
  564. { we need only a write property if a := follows }
  565. { if not(afterassignment) and not(in_args) then }
  566. if token=ASSIGNMENT then
  567. begin
  568. { write property: }
  569. { no result }
  570. pd:=voiddef;
  571. if assigned(ppropertysym(sym)^.writeaccesssym) then
  572. begin
  573. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  574. begin
  575. { generate the method call }
  576. p1:=genmethodcallnode(pprocsym(
  577. ppropertysym(sym)^.writeaccesssym),st,p1);
  578. { we know the procedure to call, so
  579. force the usage of that procedure }
  580. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  581. p1^.left:=paras;
  582. consume(ASSIGNMENT);
  583. { read the expression }
  584. getprocvar:=ppropertysym(sym)^.proptype^.deftype=procvardef;
  585. p2:=comp_expr(true);
  586. if getprocvar then
  587. begin
  588. if (p2^.treetype=calln) then
  589. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2)
  590. else
  591. if (p2^.treetype=typeconvn) and
  592. (p2^.left^.treetype=calln) then
  593. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2^.left);
  594. end;
  595. p1^.left:=gencallparanode(p2,p1^.left);
  596. getprocvar:=false;
  597. end
  598. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  599. begin
  600. if assigned(paras) then
  601. message(parser_e_no_paras_allowed);
  602. { subscribed access? }
  603. if p1=nil then
  604. p1:=genloadnode(pvarsym(ppropertysym(sym)^.writeaccesssym),st)
  605. else
  606. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.writeaccesssym),p1);
  607. consume(ASSIGNMENT);
  608. { read the expression }
  609. p2:=comp_expr(true);
  610. p1:=gennode(assignn,p1,p2);
  611. end
  612. else
  613. begin
  614. p1:=genzeronode(errorn);
  615. Message(parser_e_no_procedure_to_access_property);
  616. end;
  617. end
  618. else
  619. begin
  620. p1:=genzeronode(errorn);
  621. Message(parser_e_no_procedure_to_access_property);
  622. end;
  623. end
  624. else
  625. begin
  626. { read property: }
  627. pd:=ppropertysym(sym)^.proptype;
  628. if assigned(ppropertysym(sym)^.readaccesssym) then
  629. begin
  630. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  631. begin
  632. if assigned(paras) then
  633. message(parser_e_no_paras_allowed);
  634. { subscribed access? }
  635. if p1=nil then
  636. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),st)
  637. else
  638. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.readaccesssym),p1);
  639. end
  640. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  641. begin
  642. { generate the method call }
  643. p1:=genmethodcallnode(pprocsym(ppropertysym(sym)^.readaccesssym),st,p1);
  644. { we know the procedure to call, so
  645. force the usage of that procedure }
  646. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  647. { insert paras }
  648. p1^.left:=paras;
  649. end
  650. else
  651. begin
  652. p1:=genzeronode(errorn);
  653. Message(type_e_mismatch);
  654. end;
  655. end
  656. else
  657. begin
  658. { error, no function to read property }
  659. p1:=genzeronode(errorn);
  660. Message(parser_e_no_procedure_to_access_property);
  661. end;
  662. end;
  663. end;
  664. { the ID token has to be consumed before calling this function }
  665. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  666. var pd : pdef;var again : boolean);
  667. var
  668. static_name : string;
  669. isclassref : boolean;
  670. begin
  671. if sym=nil then
  672. begin
  673. { pattern is still valid unless
  674. there is another ID just after the ID of sym }
  675. Message1(sym_e_id_no_member,pattern);
  676. disposetree(p1);
  677. p1:=genzeronode(errorn);
  678. { try to clean up }
  679. pd:=generrordef;
  680. again:=false;
  681. end
  682. else
  683. begin
  684. isclassref:=pd^.deftype=classrefdef;
  685. { check protected and private members }
  686. { please leave this code as it is, }
  687. { it has now the same behaviaor as TP/Delphi }
  688. if ((sym^.properties and sp_private)<>0) and
  689. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  690. Message(parser_e_cant_access_private_member);
  691. if ((sym^.properties and sp_protected)<>0) and
  692. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  693. begin
  694. if assigned(aktprocsym^.definition^._class) then
  695. begin
  696. if not aktprocsym^.definition^._class^.isrelated(
  697. pobjectdef(sym^.owner^.defowner)) then
  698. Message(parser_e_cant_access_protected_member);
  699. end
  700. else
  701. Message(parser_e_cant_access_protected_member);
  702. end;
  703. { we assume, that only procsyms and varsyms are in an object }
  704. { symbol table, for classes, properties are allowed }
  705. case sym^.typ of
  706. procsym:
  707. begin
  708. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  709. do_proc_call(getaddr or
  710. (getprocvar and
  711. (m_tp_procvar in aktmodeswitches) and
  712. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef))
  713. ,again,p1,pd);
  714. { now we know the real method e.g. we can check for }
  715. { a class method }
  716. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  717. Message(parser_e_only_class_methods_via_class_ref);
  718. end;
  719. varsym:
  720. begin
  721. if isclassref then
  722. Message(parser_e_only_class_methods_via_class_ref);
  723. if (sym^.properties and sp_static)<>0 then
  724. begin
  725. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  726. this is wrong for static field in with symtable (PM) }
  727. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  728. getsym(static_name,true);
  729. disposetree(p1);
  730. p1:=genloadnode(pvarsym(srsym),srsymtable);
  731. end
  732. else
  733. p1:=gensubscriptnode(pvarsym(sym),p1);
  734. pd:=pvarsym(sym)^.definition;
  735. end;
  736. propertysym:
  737. begin
  738. if isclassref then
  739. Message(parser_e_only_class_methods_via_class_ref);
  740. handle_propertysym(sym,srsymtable,p1,pd);
  741. end;
  742. else internalerror(16);
  743. end;
  744. end;
  745. end;
  746. {****************************************************************************
  747. Factor
  748. ****************************************************************************}
  749. function factor(getaddr : boolean) : ptree;
  750. var
  751. l : longint;
  752. oldp1,
  753. p1,p2,p3 : ptree;
  754. code : integer;
  755. pd,pd2 : pdef;
  756. possible_error,
  757. unit_specific,
  758. again : boolean;
  759. sym : pvarsym;
  760. classh : pobjectdef;
  761. d : bestreal;
  762. static_name : string;
  763. propsym : ppropertysym;
  764. filepos : tfileposinfo;
  765. {---------------------------------------------
  766. Is_func_ret
  767. ---------------------------------------------}
  768. function is_func_ret(sym : psym) : boolean;
  769. var
  770. p : pprocinfo;
  771. storesymtablestack : psymtable;
  772. begin
  773. is_func_ret:=false;
  774. if (sym^.typ<>funcretsym) and ((procinfo.flags and pi_operator)=0) then
  775. exit;
  776. p:=@procinfo;
  777. while system.assigned(p) do
  778. begin
  779. { is this an access to a function result ? }
  780. if assigned(p^.funcretsym) and
  781. ((pfuncretsym(sym)=p^.funcretsym) or
  782. ((pvarsym(sym)=opsym) and
  783. ((p^.flags and pi_operator)<>0))) and
  784. (p^.retdef<>pdef(voiddef)) and
  785. (token<>LKLAMMER) and
  786. (not ((m_tp in aktmodeswitches) and
  787. (afterassignment or in_args))) then
  788. begin
  789. if ((pvarsym(sym)=opsym) and
  790. ((p^.flags and pi_operator)<>0)) then
  791. inc(opsym^.refs);
  792. if ((pvarsym(sym)=opsym) and
  793. ((p^.flags and pi_operator)<>0)) then
  794. inc(opsym^.refs);
  795. p1:=genzeronode(funcretn);
  796. pd:=p^.retdef;
  797. p1^.funcretprocinfo:=p;
  798. p1^.retdef:=pd;
  799. is_func_ret:=true;
  800. exit;
  801. end;
  802. p:=p^.parent;
  803. end;
  804. { we must use the function call }
  805. if(sym^.typ=funcretsym) then
  806. begin
  807. storesymtablestack:=symtablestack;
  808. symtablestack:=srsymtable^.next;
  809. getsym(sym^.name,true);
  810. if srsym^.typ<>procsym then
  811. Message(cg_e_illegal_expression);
  812. symtablestack:=storesymtablestack;
  813. end;
  814. end;
  815. {---------------------------------------------
  816. Factor_read_id
  817. ---------------------------------------------}
  818. procedure factor_read_id;
  819. var
  820. pc : pchar;
  821. len : longint;
  822. begin
  823. { allow post fix operators }
  824. again:=true;
  825. if (m_result in aktmodeswitches) and
  826. (idtoken=_RESULT) and
  827. assigned(aktprocsym) and
  828. (procinfo.retdef<>pdef(voiddef)) then
  829. begin
  830. consume(ID);
  831. p1:=genzeronode(funcretn);
  832. pd:=procinfo.retdef;
  833. p1^.funcretprocinfo:=pointer(@procinfo);
  834. p1^.retdef:=pd;
  835. end
  836. else
  837. begin
  838. if lastsymknown then
  839. begin
  840. srsym:=lastsrsym;
  841. srsymtable:=lastsrsymtable;
  842. lastsymknown:=false;
  843. end
  844. else
  845. getsym(pattern,true);
  846. consume(ID);
  847. if not is_func_ret(srsym) then
  848. { else it's a normal symbol }
  849. begin
  850. { is it defined like UNIT.SYMBOL ? }
  851. if srsym^.typ=unitsym then
  852. begin
  853. consume(POINT);
  854. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  855. unit_specific:=true;
  856. consume(ID);
  857. end
  858. else
  859. unit_specific:=false;
  860. if not assigned(srsym) then
  861. Begin
  862. p1:=genzeronode(errorn);
  863. { try to clean up }
  864. pd:=generrordef;
  865. end
  866. else
  867. Begin
  868. { check semantics of private }
  869. if (srsym^.typ in [propertysym,procsym,varsym]) and
  870. (srsymtable^.symtabletype=objectsymtable) then
  871. begin
  872. if ((srsym^.properties and sp_private)<>0) and
  873. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  874. Message(parser_e_cant_access_private_member);
  875. end;
  876. case srsym^.typ of
  877. absolutesym : begin
  878. p1:=genloadnode(pvarsym(srsym),srsymtable);
  879. pd:=pabsolutesym(srsym)^.definition;
  880. end;
  881. varsym : begin
  882. { are we in a class method ? }
  883. if (srsymtable^.symtabletype=objectsymtable) and
  884. assigned(aktprocsym) and
  885. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  886. Message(parser_e_only_class_methods);
  887. if (srsym^.properties and sp_static)<>0 then
  888. begin
  889. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  890. getsym(static_name,true);
  891. end;
  892. p1:=genloadnode(pvarsym(srsym),srsymtable);
  893. if pvarsym(srsym)^.is_valid=0 then
  894. begin
  895. p1^.is_first := true;
  896. { set special between first loaded until checked in firstpass }
  897. pvarsym(srsym)^.is_valid:=2;
  898. end;
  899. pd:=pvarsym(srsym)^.definition;
  900. end;
  901. typedconstsym : begin
  902. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  903. pd:=ptypedconstsym(srsym)^.definition;
  904. end;
  905. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  906. typesym : begin
  907. pd:=ptypesym(srsym)^.definition;
  908. if not assigned(pd) then
  909. begin
  910. pd:=generrordef;
  911. again:=false;
  912. end
  913. else
  914. begin
  915. { if we read a type declaration }
  916. { we have to return the type and }
  917. { nothing else }
  918. if block_type=bt_type then
  919. begin
  920. { we don't need sym reference when it's in the
  921. current unit or system unit, because those
  922. units are always loaded (PFV) }
  923. if (pd^.owner^.unitid=0) or
  924. (pd^.owner^.unitid=1) then
  925. p1:=gentypenode(pd,nil)
  926. else
  927. p1:=gentypenode(pd,ptypesym(srsym));
  928. { here we can also set resulttype !! }
  929. p1^.resulttype:=pd;
  930. pd:=voiddef;
  931. end
  932. else { not type block }
  933. begin
  934. if token=LKLAMMER then
  935. begin
  936. consume(LKLAMMER);
  937. p1:=comp_expr(true);
  938. consume(RKLAMMER);
  939. p1:=gentypeconvnode(p1,pd);
  940. p1^.explizit:=true;
  941. end
  942. else { not LKLAMMER}
  943. if (token=POINT) and
  944. (pd^.deftype=objectdef) and
  945. not(pobjectdef(pd)^.isclass) then
  946. begin
  947. consume(POINT);
  948. if assigned(procinfo._class) then
  949. begin
  950. if procinfo._class^.isrelated(pobjectdef(pd)) then
  951. begin
  952. p1:=gentypenode(pd,ptypesym(srsym));
  953. p1^.resulttype:=pd;
  954. srsymtable:=pobjectdef(pd)^.publicsyms;
  955. sym:=pvarsym(srsymtable^.search(pattern));
  956. { search also in inherited methods }
  957. while sym=nil do
  958. begin
  959. pd:=pobjectdef(pd)^.childof;
  960. srsymtable:=pobjectdef(pd)^.publicsyms;
  961. sym:=pvarsym(srsymtable^.search(pattern));
  962. end;
  963. consume(ID);
  964. do_member_read(false,sym,p1,pd,again);
  965. end
  966. else
  967. begin
  968. Message(parser_e_no_super_class);
  969. pd:=generrordef;
  970. again:=false;
  971. end;
  972. end
  973. else
  974. begin
  975. { allows @TObject.Load }
  976. { also allows static methods and variables }
  977. p1:=genzeronode(typen);
  978. p1^.resulttype:=pd;
  979. { srsymtable:=pobjectdef(pd)^.publicsyms;
  980. sym:=pvarsym(srsymtable^.search(pattern)); }
  981. { TP allows also @TMenu.Load if Load is only }
  982. { defined in an anchestor class }
  983. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  984. if not assigned(sym) then
  985. Message1(sym_e_id_no_member,pattern)
  986. else if not(getaddr) and ((sym^.properties and sp_static)=0) then
  987. Message(sym_e_only_static_in_static)
  988. else
  989. begin
  990. consume(ID);
  991. do_member_read(getaddr,sym,p1,pd,again);
  992. end;
  993. end;
  994. end
  995. else
  996. begin
  997. { class reference ? }
  998. if (pd^.deftype=objectdef)
  999. and pobjectdef(pd)^.isclass then
  1000. begin
  1001. p1:=gentypenode(pd,nil);
  1002. p1^.resulttype:=pd;
  1003. pd:=new(pclassrefdef,init(pd));
  1004. p1:=gensinglenode(loadvmtn,p1);
  1005. p1^.resulttype:=pd;
  1006. end
  1007. else
  1008. begin
  1009. { generate a type node }
  1010. { (for typeof etc) }
  1011. if allow_type then
  1012. begin
  1013. p1:=gentypenode(pd,nil);
  1014. { here we must use typenodetype explicitly !! PM
  1015. p1^.resulttype:=pd; }
  1016. pd:=voiddef;
  1017. end
  1018. else
  1019. Message(parser_e_no_type_not_allowed_here);
  1020. end;
  1021. end;
  1022. end;
  1023. end;
  1024. end;
  1025. enumsym : begin
  1026. p1:=genenumnode(penumsym(srsym));
  1027. pd:=p1^.resulttype;
  1028. end;
  1029. constsym : begin
  1030. case pconstsym(srsym)^.consttype of
  1031. constint :
  1032. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1033. conststring :
  1034. begin
  1035. len:=pconstsym(srsym)^.len;
  1036. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1037. len:=255;
  1038. getmem(pc,len+1);
  1039. move(pchar(pconstsym(srsym)^.value)^,pc^,len);
  1040. pc[len]:=#0;
  1041. p1:=genpcharconstnode(pc,len);
  1042. end;
  1043. constchar :
  1044. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1045. constreal :
  1046. p1:=genrealconstnode(pbestreal(pconstsym(srsym)^.value)^,bestrealdef^);
  1047. constbool :
  1048. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1049. constset :
  1050. p1:=gensetconstnode(pconstset(pconstsym(srsym)^.value),
  1051. psetdef(pconstsym(srsym)^.definition));
  1052. constord :
  1053. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1054. pconstsym(srsym)^.definition);
  1055. constnil :
  1056. p1:=genzeronode(niln);
  1057. constresourcestring:
  1058. begin
  1059. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1060. p1^.resulttype:=cansistringdef;
  1061. end;
  1062. end;
  1063. pd:=p1^.resulttype;
  1064. end;
  1065. procsym : begin
  1066. { are we in a class method ? }
  1067. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1068. assigned(aktprocsym) and
  1069. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1070. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1071. p1^.unit_specific:=unit_specific;
  1072. do_proc_call(getaddr or
  1073. (getprocvar and
  1074. (m_tp_procvar in aktmodeswitches) and
  1075. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)),
  1076. again,p1,pd);
  1077. if possible_error and
  1078. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1079. Message(parser_e_only_class_methods);
  1080. end;
  1081. propertysym : begin
  1082. { access to property in a method }
  1083. { are we in a class method ? }
  1084. if (srsymtable^.symtabletype=objectsymtable) and
  1085. assigned(aktprocsym) and
  1086. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1087. Message(parser_e_only_class_methods);
  1088. { no method pointer }
  1089. p1:=nil;
  1090. handle_propertysym(srsym,srsymtable,p1,pd);
  1091. end;
  1092. errorsym : begin
  1093. p1:=genzeronode(errorn);
  1094. p1^.resulttype:=generrordef;
  1095. pd:=generrordef;
  1096. if token=LKLAMMER then
  1097. begin
  1098. consume(LKLAMMER);
  1099. parse_paras(false,false);
  1100. consume(RKLAMMER);
  1101. end;
  1102. end;
  1103. else
  1104. begin
  1105. p1:=genzeronode(errorn);
  1106. pd:=generrordef;
  1107. Message(cg_e_illegal_expression);
  1108. end;
  1109. end; { end case }
  1110. end;
  1111. end;
  1112. end;
  1113. end;
  1114. {---------------------------------------------
  1115. Factor_Read_Set
  1116. ---------------------------------------------}
  1117. { Read a set between [] }
  1118. function factor_read_set:ptree;
  1119. var
  1120. p1,
  1121. lastp,
  1122. buildp : ptree;
  1123. begin
  1124. buildp:=nil;
  1125. { be sure that a least one arrayconstructn is used, also for an
  1126. empty [] }
  1127. if token=RECKKLAMMER then
  1128. buildp:=gennode(arrayconstructn,nil,buildp)
  1129. else
  1130. begin
  1131. while true do
  1132. begin
  1133. p1:=comp_expr(true);
  1134. if token=POINTPOINT then
  1135. begin
  1136. consume(POINTPOINT);
  1137. p2:=comp_expr(true);
  1138. p1:=gennode(arrayconstructrangen,p1,p2);
  1139. end;
  1140. { insert at the end of the tree, to get the correct order }
  1141. if not assigned(buildp) then
  1142. begin
  1143. buildp:=gennode(arrayconstructn,p1,nil);
  1144. lastp:=buildp;
  1145. end
  1146. else
  1147. begin
  1148. lastp^.right:=gennode(arrayconstructn,p1,nil);
  1149. lastp:=lastp^.right;
  1150. end;
  1151. { there could be more elements }
  1152. if token=COMMA then
  1153. consume(COMMA)
  1154. else
  1155. break;
  1156. end;
  1157. end;
  1158. factor_read_set:=buildp;
  1159. end;
  1160. {---------------------------------------------
  1161. Helpers
  1162. ---------------------------------------------}
  1163. procedure check_tokenpos;
  1164. begin
  1165. if (p1<>oldp1) then
  1166. begin
  1167. if assigned(p1) then
  1168. set_tree_filepos(p1,filepos);
  1169. oldp1:=p1;
  1170. filepos:=tokenpos;
  1171. end;
  1172. end;
  1173. {---------------------------------------------
  1174. PostFixOperators
  1175. ---------------------------------------------}
  1176. procedure postfixoperators;
  1177. var
  1178. store_static : boolean;
  1179. { p1 and p2 must contain valid value_str }
  1180. begin
  1181. check_tokenpos;
  1182. while again do
  1183. begin
  1184. { prevent crashes with unknown types }
  1185. if not assigned(pd) then
  1186. begin
  1187. { try to recover }
  1188. repeat
  1189. case token of
  1190. CARET:
  1191. consume(CARET);
  1192. POINT:
  1193. begin
  1194. consume(POINT);
  1195. consume(ID);
  1196. end;
  1197. LECKKLAMMER:
  1198. begin
  1199. repeat
  1200. consume(token);
  1201. until token in [RECKKLAMMER,SEMICOLON];
  1202. end;
  1203. else
  1204. break;
  1205. end;
  1206. until false;
  1207. exit;
  1208. end;
  1209. { handle token }
  1210. case token of
  1211. CARET:
  1212. begin
  1213. consume(CARET);
  1214. if (pd^.deftype<>pointerdef) then
  1215. begin
  1216. { ^ as binary operator is a problem!!!! (FK) }
  1217. again:=false;
  1218. Message(cg_e_invalid_qualifier);
  1219. disposetree(p1);
  1220. p1:=genzeronode(errorn);
  1221. end
  1222. else
  1223. begin
  1224. p1:=gensinglenode(derefn,p1);
  1225. pd:=ppointerdef(pd)^.definition;
  1226. end;
  1227. end;
  1228. LECKKLAMMER:
  1229. begin
  1230. if (pd^.deftype=objectdef) and pobjectdef(pd)^.isclass then
  1231. begin
  1232. { default property }
  1233. propsym:=search_default_property(pobjectdef(pd));
  1234. if not(assigned(propsym)) then
  1235. begin
  1236. disposetree(p1);
  1237. p1:=genzeronode(errorn);
  1238. again:=false;
  1239. message(parser_e_no_default_property_available);
  1240. end
  1241. else
  1242. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1243. end
  1244. else
  1245. begin
  1246. consume(LECKKLAMMER);
  1247. repeat
  1248. case pd^.deftype of
  1249. pointerdef:
  1250. begin
  1251. p2:=comp_expr(true);
  1252. p1:=gennode(vecn,p1,p2);
  1253. pd:=ppointerdef(pd)^.definition;
  1254. end;
  1255. stringdef : begin
  1256. p2:=comp_expr(true);
  1257. p1:=gennode(vecn,p1,p2);
  1258. pd:=cchardef
  1259. end;
  1260. arraydef : begin
  1261. p2:=comp_expr(true);
  1262. { support SEG:OFS for go32v2 Mem[] }
  1263. if (target_info.target=target_i386_go32v2) and
  1264. (p1^.treetype=loadn) and
  1265. assigned(p1^.symtableentry) and
  1266. assigned(p1^.symtableentry^.owner^.name) and
  1267. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  1268. ((p1^.symtableentry^.name='MEM') or
  1269. (p1^.symtableentry^.name='MEMW') or
  1270. (p1^.symtableentry^.name='MEML')) then
  1271. begin
  1272. if (token=COLON) then
  1273. begin
  1274. consume(COLON);
  1275. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  1276. p2:=comp_expr(true);
  1277. p2:=gennode(addn,p2,p3);
  1278. p1:=gennode(vecn,p1,p2);
  1279. p1^.memseg:=true;
  1280. p1^.memindex:=true;
  1281. end
  1282. else
  1283. begin
  1284. p1:=gennode(vecn,p1,p2);
  1285. p1^.memindex:=true;
  1286. end;
  1287. end
  1288. else
  1289. p1:=gennode(vecn,p1,p2);
  1290. pd:=parraydef(pd)^.definition;
  1291. end;
  1292. else
  1293. begin
  1294. Message(cg_e_invalid_qualifier);
  1295. disposetree(p1);
  1296. p1:=genzeronode(errorn);
  1297. again:=false;
  1298. end;
  1299. end;
  1300. if token=COMMA then
  1301. consume(COMMA)
  1302. else
  1303. break;
  1304. until false;
  1305. consume(RECKKLAMMER);
  1306. end;
  1307. end;
  1308. POINT : begin
  1309. consume(POINT);
  1310. if (pd^.deftype=pointerdef) and
  1311. (m_autoderef in aktmodeswitches) then
  1312. begin
  1313. p1:=gensinglenode(derefn,p1);
  1314. pd:=ppointerdef(pd)^.definition;
  1315. end;
  1316. case pd^.deftype of
  1317. recorddef:
  1318. begin
  1319. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  1320. if sym=nil then
  1321. begin
  1322. Message1(sym_e_illegal_field,pattern);
  1323. disposetree(p1);
  1324. p1:=genzeronode(errorn);
  1325. end
  1326. else
  1327. begin
  1328. p1:=gensubscriptnode(sym,p1);
  1329. pd:=sym^.definition;
  1330. end;
  1331. consume(ID);
  1332. end;
  1333. classrefdef:
  1334. begin
  1335. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  1336. sym:=nil;
  1337. while assigned(classh) do
  1338. begin
  1339. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1340. srsymtable:=classh^.publicsyms;
  1341. if assigned(sym) then
  1342. break;
  1343. classh:=classh^.childof;
  1344. end;
  1345. consume(ID);
  1346. do_member_read(getaddr,sym,p1,pd,again);
  1347. end;
  1348. objectdef:
  1349. begin
  1350. classh:=pobjectdef(pd);
  1351. sym:=nil;
  1352. store_static:=allow_only_static;
  1353. allow_only_static:=false;
  1354. while assigned(classh) do
  1355. begin
  1356. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1357. srsymtable:=classh^.publicsyms;
  1358. if assigned(sym) then
  1359. break;
  1360. classh:=classh^.childof;
  1361. end;
  1362. allow_only_static:=store_static;
  1363. consume(ID);
  1364. do_member_read(getaddr,sym,p1,pd,again);
  1365. end;
  1366. pointerdef:
  1367. begin
  1368. Message(cg_e_invalid_qualifier);
  1369. if ppointerdef(pd)^.definition^.deftype in [recorddef,objectdef,classrefdef] then
  1370. Message(parser_h_maybe_deref_caret_missing);
  1371. end;
  1372. else
  1373. begin
  1374. Message(cg_e_invalid_qualifier);
  1375. disposetree(p1);
  1376. p1:=genzeronode(errorn);
  1377. end;
  1378. end;
  1379. end;
  1380. else
  1381. begin
  1382. { is this a procedure variable ? }
  1383. if assigned(pd) then
  1384. begin
  1385. if (pd^.deftype=procvardef) then
  1386. begin
  1387. if getprocvar and is_equal(pd,getprocvardef) then
  1388. again:=false
  1389. else
  1390. if (token=LKLAMMER) or
  1391. ((pprocvardef(pd)^.para1=nil) and
  1392. (not((token in [ASSIGNMENT,UNEQUAL,EQUAL]))) and
  1393. (not afterassignment) and
  1394. (not in_args)) then
  1395. begin
  1396. { do this in a strange way }
  1397. { it's not a clean solution }
  1398. p2:=p1;
  1399. p1:=gencallnode(nil,nil);
  1400. p1^.right:=p2;
  1401. p1^.unit_specific:=unit_specific;
  1402. p1^.symtableprocentry:=pprocsym(sym);
  1403. if token=LKLAMMER then
  1404. begin
  1405. consume(LKLAMMER);
  1406. p1^.left:=parse_paras(false,false);
  1407. consume(RKLAMMER);
  1408. end;
  1409. pd:=pprocvardef(pd)^.retdef;
  1410. { proc():= is never possible }
  1411. if token=ASSIGNMENT then
  1412. begin
  1413. Message(cg_e_illegal_expression);
  1414. p1:=genzeronode(errorn);
  1415. again:=false;
  1416. end;
  1417. p1^.resulttype:=pd;
  1418. end
  1419. else
  1420. again:=false;
  1421. p1^.resulttype:=pd;
  1422. end
  1423. else
  1424. again:=false;
  1425. end
  1426. else
  1427. again:=false;
  1428. end;
  1429. end;
  1430. check_tokenpos;
  1431. end; { while again }
  1432. end;
  1433. {---------------------------------------------
  1434. Factor (Main)
  1435. ---------------------------------------------}
  1436. begin
  1437. oldp1:=nil;
  1438. p1:=nil;
  1439. filepos:=tokenpos;
  1440. if token=ID then
  1441. begin
  1442. factor_read_id;
  1443. { handle post fix operators }
  1444. postfixoperators;
  1445. end
  1446. else
  1447. case token of
  1448. _NEW : begin
  1449. consume(_NEW);
  1450. consume(LKLAMMER);
  1451. {allow_type:=true;}
  1452. p1:=factor(false);
  1453. {allow_type:=false;}
  1454. if p1^.treetype<>typen then
  1455. begin
  1456. Message(type_e_type_id_expected);
  1457. disposetree(p1);
  1458. pd:=generrordef;
  1459. end
  1460. else
  1461. pd:=p1^.typenodetype;
  1462. pd2:=pd;
  1463. if (pd^.deftype<>pointerdef) then
  1464. Message(type_e_pointer_type_expected)
  1465. else if {(ppointerdef(pd)^.definition^.deftype<>objectdef)}
  1466. token=RKLAMMER then
  1467. begin
  1468. if (ppointerdef(pd)^.definition^.deftype=objectdef) and
  1469. ((pobjectdef(ppointerdef(pd)^.definition)^.options and oo_hasvmt) <> 0) then
  1470. Message(parser_w_use_extended_syntax_for_objects);
  1471. p1:=gensinglenode(newn,nil);
  1472. p1^.resulttype:=pd2;
  1473. consume(RKLAMMER);
  1474. (*Message(parser_e_pointer_to_class_expected);
  1475. { if an error occurs, read til the end of
  1476. the new statement }
  1477. p1:=genzeronode(errorn);
  1478. l:=1;
  1479. while true do
  1480. begin
  1481. case token of
  1482. LKLAMMER : inc(l);
  1483. RKLAMMER : dec(l);
  1484. end;
  1485. consume(token);
  1486. if l=0 then
  1487. break;
  1488. end;*)
  1489. end
  1490. else
  1491. begin
  1492. disposetree(p1);
  1493. p1:=genzeronode(hnewn);
  1494. p1^.resulttype:=ppointerdef(pd)^.definition;
  1495. consume(COMMA);
  1496. afterassignment:=false;
  1497. { determines the current object defintion }
  1498. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1499. { check for an abstract class }
  1500. if (classh^.options and oo_is_abstract)<>0 then
  1501. Message(sym_e_no_instance_of_abstract_object);
  1502. { search the constructor also in the symbol tables of
  1503. the parents }
  1504. { no constructor found }
  1505. sym:=nil;
  1506. while assigned(classh) do
  1507. begin
  1508. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1509. srsymtable:=classh^.publicsyms;
  1510. if assigned(sym) then
  1511. break;
  1512. classh:=classh^.childof;
  1513. end;
  1514. consume(ID);
  1515. do_member_read(false,sym,p1,pd,again);
  1516. if (p1^.treetype<>calln) or
  1517. (assigned(p1^.procdefinition) and
  1518. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1519. Message(parser_e_expr_have_to_be_constructor_call);
  1520. p1:=gensinglenode(newn,p1);
  1521. { set the resulttype }
  1522. p1^.resulttype:=pd2;
  1523. consume(RKLAMMER);
  1524. end;
  1525. end;
  1526. _SELF : begin
  1527. again:=true;
  1528. consume(_SELF);
  1529. if not assigned(procinfo._class) then
  1530. begin
  1531. p1:=genzeronode(errorn);
  1532. pd:=generrordef;
  1533. again:=false;
  1534. Message(parser_e_self_not_in_method);
  1535. end
  1536. else
  1537. begin
  1538. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1539. begin
  1540. { self in class methods is a class reference type }
  1541. pd:=new(pclassrefdef,init(procinfo._class));
  1542. p1:=genselfnode(pd);
  1543. p1^.resulttype:=pd;
  1544. end
  1545. else
  1546. begin
  1547. p1:=genselfnode(procinfo._class);
  1548. p1^.resulttype:=procinfo._class;
  1549. end;
  1550. pd:=p1^.resulttype;
  1551. postfixoperators;
  1552. end;
  1553. end;
  1554. _INHERITED : begin
  1555. again:=true;
  1556. consume(_INHERITED);
  1557. if assigned(procinfo._class) then
  1558. begin
  1559. classh:=procinfo._class^.childof;
  1560. while assigned(classh) do
  1561. begin
  1562. srsymtable:=pobjectdef(classh)^.publicsyms;
  1563. sym:=pvarsym(srsymtable^.search(pattern));
  1564. if assigned(sym) then
  1565. begin
  1566. p1:=genzeronode(typen);
  1567. p1^.resulttype:=classh;
  1568. pd:=p1^.resulttype;
  1569. consume(ID);
  1570. do_member_read(false,sym,p1,pd,again);
  1571. break;
  1572. end;
  1573. classh:=classh^.childof;
  1574. end;
  1575. if classh=nil then
  1576. begin
  1577. Message1(sym_e_id_no_member,pattern);
  1578. again:=false;
  1579. pd:=generrordef;
  1580. p1:=genzeronode(errorn);
  1581. end;
  1582. end
  1583. else
  1584. begin
  1585. Message(parser_e_generic_methods_only_in_methods);
  1586. again:=false;
  1587. pd:=generrordef;
  1588. p1:=genzeronode(errorn);
  1589. end;
  1590. postfixoperators;
  1591. end;
  1592. INTCONST : begin
  1593. valint(pattern,l,code);
  1594. if code<>0 then
  1595. begin
  1596. val(pattern,d,code);
  1597. if code<>0 then
  1598. begin
  1599. Message(cg_e_invalid_integer);
  1600. consume(INTCONST);
  1601. l:=1;
  1602. p1:=genordinalconstnode(l,s32bitdef);
  1603. end
  1604. else
  1605. begin
  1606. consume(INTCONST);
  1607. p1:=genrealconstnode(d,bestrealdef^);
  1608. end;
  1609. end
  1610. else
  1611. begin
  1612. consume(INTCONST);
  1613. p1:=genordinalconstnode(l,s32bitdef);
  1614. end;
  1615. end;
  1616. REALNUMBER : begin
  1617. val(pattern,d,code);
  1618. if code<>0 then
  1619. begin
  1620. Message(parser_e_error_in_real);
  1621. d:=1.0;
  1622. end;
  1623. consume(REALNUMBER);
  1624. p1:=genrealconstnode(d,bestrealdef^);
  1625. end;
  1626. _STRING : begin
  1627. pd:=stringtype;
  1628. { STRING can be also a type cast }
  1629. if token=LKLAMMER then
  1630. begin
  1631. consume(LKLAMMER);
  1632. p1:=comp_expr(true);
  1633. consume(RKLAMMER);
  1634. p1:=gentypeconvnode(p1,pd);
  1635. p1^.explizit:=true;
  1636. { handle postfix operators here e.g. string(a)[10] }
  1637. again:=true;
  1638. postfixoperators;
  1639. end
  1640. else
  1641. p1:=gentypenode(pd,nil);
  1642. end;
  1643. _FILE : begin
  1644. pd:=cfiledef;
  1645. consume(_FILE);
  1646. { FILE can be also a type cast }
  1647. if token=LKLAMMER then
  1648. begin
  1649. consume(LKLAMMER);
  1650. p1:=comp_expr(true);
  1651. consume(RKLAMMER);
  1652. p1:=gentypeconvnode(p1,pd);
  1653. p1^.explizit:=true;
  1654. { handle postfix operators here e.g. string(a)[10] }
  1655. again:=true;
  1656. postfixoperators;
  1657. end
  1658. else
  1659. p1:=gentypenode(pd,nil);
  1660. end;
  1661. CSTRING : begin
  1662. p1:=genstringconstnode(pattern);
  1663. consume(CSTRING);
  1664. end;
  1665. CCHAR : begin
  1666. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1667. consume(CCHAR);
  1668. end;
  1669. KLAMMERAFFE : begin
  1670. consume(KLAMMERAFFE);
  1671. got_addrn:=true;
  1672. p1:=factor(true);
  1673. got_addrn:=false;
  1674. p1:=gensinglenode(addrn,p1);
  1675. end;
  1676. LKLAMMER : begin
  1677. consume(LKLAMMER);
  1678. p1:=comp_expr(true);
  1679. consume(RKLAMMER);
  1680. { it's not a good solution }
  1681. { but (a+b)^ makes some problems }
  1682. if token in [CARET,POINT,LECKKLAMMER] then
  1683. begin
  1684. { we need the resulttype }
  1685. { of the expression in pd }
  1686. do_firstpass(p1);
  1687. pd:=p1^.resulttype;
  1688. again:=true;
  1689. postfixoperators;
  1690. end;
  1691. end;
  1692. LECKKLAMMER : begin
  1693. consume(LECKKLAMMER);
  1694. p1:=factor_read_set;
  1695. consume(RECKKLAMMER);
  1696. end;
  1697. PLUS : begin
  1698. consume(PLUS);
  1699. p1:=factor(false);
  1700. end;
  1701. MINUS : begin
  1702. consume(MINUS);
  1703. p1:=factor(false);
  1704. p1:=gensinglenode(umminusn,p1);
  1705. end;
  1706. _NOT : begin
  1707. consume(_NOT);
  1708. p1:=factor(false);
  1709. p1:=gensinglenode(notn,p1);
  1710. end;
  1711. _TRUE : begin
  1712. consume(_TRUE);
  1713. p1:=genordinalconstnode(1,booldef);
  1714. end;
  1715. _FALSE : begin
  1716. consume(_FALSE);
  1717. p1:=genordinalconstnode(0,booldef);
  1718. end;
  1719. _NIL : begin
  1720. consume(_NIL);
  1721. p1:=genzeronode(niln);
  1722. end;
  1723. else
  1724. begin
  1725. p1:=genzeronode(errorn);
  1726. consume(token);
  1727. Message(cg_e_illegal_expression);
  1728. end;
  1729. end;
  1730. { generate error node if no node is created }
  1731. if not assigned(p1) then
  1732. p1:=genzeronode(errorn);
  1733. { tp7 procvar handling, but not if the next token
  1734. will be a := }
  1735. if (m_tp_procvar in aktmodeswitches) and
  1736. (token<>ASSIGNMENT) then
  1737. check_tp_procvar(p1);
  1738. factor:=p1;
  1739. check_tokenpos;
  1740. end;
  1741. {****************************************************************************
  1742. Sub_Expr
  1743. ****************************************************************************}
  1744. type
  1745. Toperator_precedence=(opcompare,opaddition,opmultiply);
  1746. Ttok2nodeRec=record
  1747. tok : ttoken;
  1748. nod : ttreetyp;
  1749. end;
  1750. const
  1751. tok2nodes=23;
  1752. tok2node:array[1..tok2nodes] of ttok2noderec=(
  1753. (tok:PLUS ;nod:addn),
  1754. (tok:MINUS ;nod:subn),
  1755. (tok:STAR ;nod:muln),
  1756. (tok:SLASH ;nod:slashn),
  1757. (tok:EQUAL ;nod:equaln),
  1758. (tok:GT ;nod:gtn),
  1759. (tok:LT ;nod:ltn),
  1760. (tok:GTE ;nod:gten),
  1761. (tok:LTE ;nod:lten),
  1762. (tok:SYMDIF ;nod:symdifn),
  1763. (tok:STARSTAR;nod:starstarn),
  1764. (tok:CARET ;nod:caretn),
  1765. (tok:UNEQUAL ;nod:unequaln),
  1766. (tok:_AS ;nod:asn),
  1767. (tok:_IN ;nod:inn),
  1768. (tok:_IS ;nod:isn),
  1769. (tok:_OR ;nod:orn),
  1770. (tok:_AND ;nod:andn),
  1771. (tok:_DIV ;nod:divn),
  1772. (tok:_MOD ;nod:modn),
  1773. (tok:_SHL ;nod:shln),
  1774. (tok:_SHR ;nod:shrn),
  1775. (tok:_XOR ;nod:xorn)
  1776. );
  1777. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1778. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1779. [PLUS,MINUS,_OR,_XOR],
  1780. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1781. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1782. {Reads a subexpression while the operators are of the current precedence
  1783. level, or any higher level. Replaces the old term, simpl_expr and
  1784. simpl2_expr.}
  1785. var
  1786. low,high,mid : longint;
  1787. p1,p2 : Ptree;
  1788. oldt : Ttoken;
  1789. filepos : tfileposinfo;
  1790. begin
  1791. if pred_level=opmultiply then
  1792. p1:=factor(false)
  1793. else
  1794. p1:=sub_expr(succ(pred_level),true);
  1795. repeat
  1796. if (token in operator_levels[pred_level]) and
  1797. ((token<>EQUAL) or accept_equal) then
  1798. begin
  1799. oldt:=token;
  1800. filepos:=tokenpos;
  1801. consume(token);
  1802. if pred_level=opmultiply then
  1803. p2:=factor(false)
  1804. else
  1805. p2:=sub_expr(succ(pred_level),true);
  1806. low:=1;
  1807. high:=tok2nodes;
  1808. while (low<high) do
  1809. begin
  1810. mid:=(low+high+1) shr 1;
  1811. if oldt<tok2node[mid].tok then
  1812. high:=mid-1
  1813. else
  1814. low:=mid;
  1815. end;
  1816. if tok2node[high].tok=oldt then
  1817. p1:=gennode(tok2node[high].nod,p1,p2)
  1818. else
  1819. p1:=gennode(nothingn,p1,p2);
  1820. set_tree_filepos(p1,filepos);
  1821. end
  1822. else
  1823. break;
  1824. until false;
  1825. sub_expr:=p1;
  1826. end;
  1827. function comp_expr(accept_equal : boolean):Ptree;
  1828. var
  1829. oldafterassignment : boolean;
  1830. p1 : ptree;
  1831. begin
  1832. oldafterassignment:=afterassignment;
  1833. afterassignment:=true;
  1834. p1:=sub_expr(opcompare,accept_equal);
  1835. afterassignment:=oldafterassignment;
  1836. comp_expr:=p1;
  1837. end;
  1838. function expr : ptree;
  1839. var
  1840. p1,p2 : ptree;
  1841. oldafterassignment : boolean;
  1842. oldp1 : ptree;
  1843. filepos : tfileposinfo;
  1844. begin
  1845. oldafterassignment:=afterassignment;
  1846. p1:=sub_expr(opcompare,true);
  1847. filepos:=tokenpos;
  1848. if (m_tp_procvar in aktmodeswitches) and
  1849. (token<>ASSIGNMENT) then
  1850. check_tp_procvar(p1);
  1851. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1852. afterassignment:=true;
  1853. oldp1:=p1;
  1854. case token of
  1855. POINTPOINT : begin
  1856. consume(POINTPOINT);
  1857. p2:=sub_expr(opcompare,true);
  1858. p1:=gennode(rangen,p1,p2);
  1859. end;
  1860. ASSIGNMENT : begin
  1861. consume(ASSIGNMENT);
  1862. { avoid a firstpass of a procedure if
  1863. it must be assigned to a procvar }
  1864. { should be recursive for a:=b:=c !!! }
  1865. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1866. begin
  1867. getprocvar:=true;
  1868. getprocvardef:=pprocvardef(p1^.resulttype);
  1869. end;
  1870. p2:=sub_expr(opcompare,true);
  1871. if getprocvar then
  1872. begin
  1873. if (p2^.treetype=calln) then
  1874. handle_procvar(getprocvardef,p2)
  1875. else
  1876. { also allow p:= proc(t); !! (PM) }
  1877. if (p2^.treetype=typeconvn) and
  1878. (p2^.left^.treetype=calln) then
  1879. handle_procvar(getprocvardef,p2^.left);
  1880. end;
  1881. getprocvar:=false;
  1882. p1:=gennode(assignn,p1,p2);
  1883. end;
  1884. { this is the code for C like assignements }
  1885. { from an improvement of Peter Schaefer }
  1886. _PLUSASN : begin
  1887. consume(_PLUSASN );
  1888. p2:=sub_expr(opcompare,true);
  1889. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1890. { was first
  1891. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1892. but disposetree assumes that we have a real
  1893. *** tree *** }
  1894. end;
  1895. _MINUSASN : begin
  1896. consume(_MINUSASN );
  1897. p2:=sub_expr(opcompare,true);
  1898. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1899. end;
  1900. _STARASN : begin
  1901. consume(_STARASN );
  1902. p2:=sub_expr(opcompare,true);
  1903. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1904. end;
  1905. _SLASHASN : begin
  1906. consume(_SLASHASN );
  1907. p2:=sub_expr(opcompare,true);
  1908. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1909. end;
  1910. end;
  1911. afterassignment:=oldafterassignment;
  1912. if p1<>oldp1 then
  1913. set_tree_filepos(p1,filepos);
  1914. expr:=p1;
  1915. end;
  1916. function get_intconst:longint;
  1917. {Reads an expression, tries to evalute it and check if it is an integer
  1918. constant. Then the constant is returned.}
  1919. var
  1920. p:Ptree;
  1921. begin
  1922. p:=comp_expr(true);
  1923. do_firstpass(p);
  1924. if not codegenerror then
  1925. begin
  1926. if (p^.treetype<>ordconstn) and
  1927. (p^.resulttype^.deftype=orddef) and
  1928. not(Porddef(p^.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  1929. Message(cg_e_illegal_expression)
  1930. else
  1931. get_intconst:=p^.value;
  1932. end;
  1933. disposetree(p);
  1934. end;
  1935. function get_stringconst:string;
  1936. {Reads an expression, tries to evaluate it and checks if it is a string
  1937. constant. Then the constant is returned.}
  1938. var
  1939. p:Ptree;
  1940. begin
  1941. get_stringconst:='';
  1942. p:=comp_expr(true);
  1943. do_firstpass(p);
  1944. if p^.treetype<>stringconstn then
  1945. begin
  1946. if (p^.treetype=ordconstn) and is_char(p^.resulttype) then
  1947. get_stringconst:=char(p^.value)
  1948. else
  1949. Message(cg_e_illegal_expression);
  1950. end
  1951. else
  1952. get_stringconst:=strpas(p^.value_str);
  1953. disposetree(p);
  1954. end;
  1955. end.
  1956. {
  1957. $Log$
  1958. Revision 1.126 1999-07-30 12:28:40 peter
  1959. * fixed crash with unknown id and colon parameter in write
  1960. Revision 1.125 1999/07/27 23:42:14 peter
  1961. * indirect type referencing is now allowed
  1962. Revision 1.124 1999/07/23 21:31:42 peter
  1963. * fixed crash with resourcestring
  1964. Revision 1.123 1999/07/23 11:37:46 peter
  1965. * error for illegal type reference, instead of 10998
  1966. Revision 1.122 1999/07/22 09:37:52 florian
  1967. + resourcestring implemented
  1968. + start of longstring support
  1969. Revision 1.121 1999/07/16 10:04:35 peter
  1970. * merged
  1971. Revision 1.120 1999/07/06 22:38:11 florian
  1972. * another fix for TP/Delphi styled procedure variables
  1973. Revision 1.119 1999/07/05 20:13:16 peter
  1974. * removed temp defines
  1975. Revision 1.118 1999/07/01 21:33:57 peter
  1976. * merged
  1977. Revision 1.117 1999/06/30 15:43:20 florian
  1978. * two bugs regarding method variables fixed
  1979. - if you take in a method the address of another method
  1980. don't need self anymore
  1981. - if the class pointer was in a register, wrong code for a method
  1982. variable load was generated
  1983. Revision 1.116 1999/06/26 00:24:53 pierre
  1984. * mereg from fixes-0_99_12 branch
  1985. Revision 1.112.2.8 1999/07/16 09:54:57 peter
  1986. * @procvar support in tp7 mode works again
  1987. Revision 1.112.2.7 1999/07/07 07:53:10 michael
  1988. + Merged patches from florian
  1989. Revision 1.112.2.6 1999/07/01 21:31:59 peter
  1990. * procvar fixes again
  1991. Revision 1.112.2.5 1999/07/01 15:17:17 peter
  1992. * methoidpointer fixes from florian
  1993. Revision 1.112.2.4 1999/06/26 00:22:30 pierre
  1994. * wrong warnings in -So mode suppressed
  1995. Revision 1.112.2.3 1999/06/17 12:51:44 pierre
  1996. * changed is_assignment_overloaded into
  1997. function assignment_overloaded : pprocdef
  1998. to allow overloading of assignment with only different result type
  1999. Revision 1.112.2.2 1999/06/15 18:54:52 peter
  2000. * more procvar fixes
  2001. Revision 1.112.2.1 1999/06/13 22:38:09 peter
  2002. * tp_procvar check for loading of procvars when getaddr=false
  2003. Revision 1.112 1999/06/02 22:44:11 pierre
  2004. * previous wrong log corrected
  2005. Revision 1.111 1999/06/02 22:25:43 pierre
  2006. * changed $ifdef FPC @ into $ifndef TP
  2007. * changes for correct procvar handling under tp mode
  2008. Revision 1.110 1999/06/01 19:27:55 peter
  2009. * better checks for procvar and methodpointer
  2010. Revision 1.109 1999/05/27 19:44:46 peter
  2011. * removed oldasm
  2012. * plabel -> pasmlabel
  2013. * -a switches to source writing automaticly
  2014. * assembler readers OOPed
  2015. * asmsymbol automaticly external
  2016. * jumptables and other label fixes for asm readers
  2017. Revision 1.108 1999/05/18 14:15:54 peter
  2018. * containsself fixes
  2019. * checktypes()
  2020. Revision 1.107 1999/05/18 09:52:18 peter
  2021. * procedure of object and addrn fixes
  2022. Revision 1.106 1999/05/16 17:06:31 peter
  2023. * remove firstcallparan which looks obsolete
  2024. Revision 1.105 1999/05/12 22:36:09 florian
  2025. * override isn't allowed in objects!
  2026. Revision 1.104 1999/05/07 10:35:23 florian
  2027. * first fix for a problem with method pointer properties, still doesn't work
  2028. with WITH
  2029. Revision 1.103 1999/05/06 21:40:16 peter
  2030. * fixed crash
  2031. Revision 1.101 1999/05/06 09:05:21 peter
  2032. * generic write_float and str_float
  2033. * fixed constant float conversions
  2034. Revision 1.100 1999/05/04 21:44:57 florian
  2035. * changes to compile it with Delphi 4.0
  2036. Revision 1.99 1999/05/01 13:24:31 peter
  2037. * merged nasm compiler
  2038. * old asm moved to oldasm/
  2039. Revision 1.98 1999/04/26 18:29:56 peter
  2040. * farpointerdef moved into pointerdef.is_far
  2041. Revision 1.97 1999/04/19 09:27:48 peter
  2042. * removed my property fix
  2043. Revision 1.96 1999/04/19 09:13:47 peter
  2044. * class property without write support
  2045. Revision 1.95 1999/04/19 06:10:08 florian
  2046. * property problem fixed: a propertysym is only a write
  2047. access if it is followed by a assignment token
  2048. Revision 1.94 1999/04/17 13:12:17 peter
  2049. * addr() internal
  2050. Revision 1.93 1999/04/15 09:00:08 peter
  2051. * fixed property write
  2052. Revision 1.92 1999/04/08 20:59:43 florian
  2053. * fixed problem with default properties which are a class
  2054. * case bug (from the mailing list with -O2) fixed, the
  2055. distance of the case labels can be greater than the positive
  2056. range of a longint => it is now a dword for fpc
  2057. Revision 1.91 1999/04/06 11:21:56 peter
  2058. * more use of ttoken
  2059. Revision 1.90 1999/03/31 13:55:12 peter
  2060. * assembler inlining working for ag386bin
  2061. Revision 1.89 1999/03/26 00:05:36 peter
  2062. * released valintern
  2063. + deffile is now removed when compiling is finished
  2064. * ^( compiles now correct
  2065. + static directive
  2066. * shrd fixed
  2067. Revision 1.88 1999/03/24 23:17:15 peter
  2068. * fixed bugs 212,222,225,227,229,231,233
  2069. Revision 1.87 1999/03/16 17:52:52 jonas
  2070. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  2071. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  2072. * in cgai386: also small fixes to emitrangecheck
  2073. Revision 1.86 1999/03/04 13:55:44 pierre
  2074. * some m68k fixes (still not compilable !)
  2075. * new(tobj) does not give warning if tobj has no VMT !
  2076. Revision 1.85 1999/02/22 15:09:39 florian
  2077. * behaviaor of PROTECTED and PRIVATE fixed, works now like TP/Delphi
  2078. Revision 1.84 1999/02/22 02:15:26 peter
  2079. * updates for ag386bin
  2080. Revision 1.83 1999/02/11 09:46:25 pierre
  2081. * fix for normal method calls inside static methods :
  2082. WARNING there were both parser and codegen errors !!
  2083. added static_call boolean to calln tree
  2084. Revision 1.82 1999/01/28 14:06:47 florian
  2085. * small fix for method pointers
  2086. * found the annoying strpas bug, mainly nested call to type cast which
  2087. use ansistrings crash
  2088. Revision 1.81 1999/01/27 00:13:55 florian
  2089. * "procedure of object"-stuff fixed
  2090. Revision 1.80 1999/01/21 16:41:01 pierre
  2091. * fix for constructor inside with statements
  2092. Revision 1.79 1998/12/30 22:15:48 peter
  2093. + farpointer type
  2094. * absolutesym now also stores if its far
  2095. Revision 1.78 1998/12/11 00:03:32 peter
  2096. + globtype,tokens,version unit splitted from globals
  2097. Revision 1.77 1998/12/04 10:18:09 florian
  2098. * some stuff for procedures of object added
  2099. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  2100. Revision 1.76 1998/11/27 14:50:40 peter
  2101. + open strings, $P switch support
  2102. Revision 1.75 1998/11/25 19:12:51 pierre
  2103. * var:=new(pointer_type) support added
  2104. Revision 1.74 1998/11/13 10:18:11 peter
  2105. + nil constants
  2106. Revision 1.73 1998/11/05 12:02:52 peter
  2107. * released useansistring
  2108. * removed -Sv, its now available in fpc modes
  2109. Revision 1.72 1998/11/04 10:11:41 peter
  2110. * ansistring fixes
  2111. Revision 1.71 1998/10/22 23:57:29 peter
  2112. * fixed filedef for typenodetype
  2113. Revision 1.70 1998/10/21 15:12:54 pierre
  2114. * bug fix for IOCHECK inside a procedure with iocheck modifier
  2115. * removed the GPF for unexistant overloading
  2116. (firstcall was called with procedinition=nil !)
  2117. * changed typen to what Florian proposed
  2118. gentypenode(p : pdef) sets the typenodetype field
  2119. and resulttype is only set if inside bt_type block !
  2120. Revision 1.69 1998/10/20 15:10:19 pierre
  2121. * type ptree only allowed inside expression
  2122. if following sizeof typeof low high or as first arg of new !!
  2123. Revision 1.68 1998/10/20 11:15:44 pierre
  2124. * calling of private method allowed inside child object method
  2125. Revision 1.67 1998/10/19 08:54:57 pierre
  2126. * wrong stabs info corrected once again !!
  2127. + variable vmt offset with vmt field only if required
  2128. implemented now !!!
  2129. Revision 1.66 1998/10/15 15:13:28 pierre
  2130. + added oo_hasconstructor and oo_hasdestructor
  2131. for objects options
  2132. Revision 1.65 1998/10/13 13:10:24 peter
  2133. * new style for m68k/i386 infos and enums
  2134. Revision 1.64 1998/10/12 12:20:55 pierre
  2135. + added tai_const_symbol_offset
  2136. for r : pointer = @var.field;
  2137. * better message for different arg names on implementation
  2138. of function
  2139. Revision 1.63 1998/10/12 10:28:30 florian
  2140. + auto dereferencing of pointers to structured types in delphi mode
  2141. Revision 1.62 1998/10/12 10:05:41 peter
  2142. * fixed mem leak with arrayconstrutor
  2143. Revision 1.61 1998/10/05 13:57:15 peter
  2144. * crash preventions
  2145. Revision 1.60 1998/10/05 12:32:46 peter
  2146. + assert() support
  2147. Revision 1.59 1998/10/01 14:56:24 peter
  2148. * crash preventions
  2149. Revision 1.58 1998/09/30 07:40:35 florian
  2150. * better error recovering
  2151. Revision 1.57 1998/09/28 16:18:16 florian
  2152. * two fixes to get ansi strings work
  2153. Revision 1.56 1998/09/26 17:45:36 peter
  2154. + idtoken and only one token table
  2155. Revision 1.55 1998/09/24 23:49:10 peter
  2156. + aktmodeswitches
  2157. Revision 1.54 1998/09/23 15:46:39 florian
  2158. * problem with with and classes fixed
  2159. Revision 1.53 1998/09/23 09:58:54 peter
  2160. * first working array of const things
  2161. Revision 1.52 1998/09/20 09:38:45 florian
  2162. * hasharray for defs fixed
  2163. * ansistring code generation corrected (init/final, assignement)
  2164. Revision 1.51 1998/09/18 16:03:43 florian
  2165. * some changes to compile with Delphi
  2166. Revision 1.50 1998/09/17 13:41:18 pierre
  2167. sizeof(TPOINT) problem
  2168. Revision 1.49.2.1 1998/09/17 08:42:31 pierre
  2169. TPOINT sizeof fix
  2170. Revision 1.49 1998/09/09 11:50:53 pierre
  2171. * forward def are not put in record or objects
  2172. + added check for forwards also in record and objects
  2173. * dummy parasymtable for unit initialization removed from
  2174. symtable stack
  2175. Revision 1.48 1998/09/07 22:25:53 peter
  2176. * fixed str(boolean,string) which was allowed
  2177. * fixed write(' ':<int expression>) only constants where allowed :(
  2178. Revision 1.47 1998/09/07 18:46:10 peter
  2179. * update smartlinking, uses getdatalabel
  2180. * renamed ptree.value vars to value_str,value_real,value_set
  2181. Revision 1.46 1998/09/04 08:42:03 peter
  2182. * updated some error messages
  2183. Revision 1.45 1998/09/01 17:39:49 peter
  2184. + internal constant functions
  2185. Revision 1.44 1998/08/28 10:54:24 peter
  2186. * fixed smallset generation from elements, it has never worked before!
  2187. Revision 1.43 1998/08/23 16:07:24 florian
  2188. * internalerror with mod/div fixed
  2189. Revision 1.42 1998/08/21 14:08:50 pierre
  2190. + TEST_FUNCRET now default (old code removed)
  2191. works also for m68k (at least compiles)
  2192. Revision 1.41 1998/08/20 21:36:39 peter
  2193. * fixed 'with object do' bug
  2194. Revision 1.40 1998/08/20 09:26:41 pierre
  2195. + funcret setting in underproc testing
  2196. compile with _dTEST_FUNCRET
  2197. Revision 1.39 1998/08/18 16:48:48 pierre
  2198. * bug for -So proc assignment to p^rocvar fixed
  2199. Revision 1.38 1998/08/18 14:17:09 pierre
  2200. * bug about assigning the return value of a function to
  2201. a procvar fixed : warning
  2202. assigning a proc to a procvar need @ in FPC mode !!
  2203. * missing file/line info restored
  2204. Revision 1.37 1998/08/18 09:24:43 pierre
  2205. * small warning position bug fixed
  2206. * support_mmx switches splitting was missing
  2207. * rhide error and warning output corrected
  2208. Revision 1.36 1998/08/15 16:50:29 peter
  2209. * fixed proc()=expr which was not allowed anymore by my previous fix
  2210. Revision 1.35 1998/08/14 18:18:46 peter
  2211. + dynamic set contruction
  2212. * smallsets are now working (always longint size)
  2213. Revision 1.34 1998/08/13 11:00:12 peter
  2214. * fixed procedure<>procedure construct
  2215. Revision 1.33 1998/08/11 15:31:39 peter
  2216. * write extended to ppu file
  2217. * new version 0.99.7
  2218. Revision 1.32 1998/08/11 14:05:32 peter
  2219. * fixed sizeof(array of char)
  2220. Revision 1.31 1998/08/10 14:50:11 peter
  2221. + localswitches, moduleswitches, globalswitches splitting
  2222. Revision 1.30 1998/07/28 21:52:54 florian
  2223. + implementation of raise and try..finally
  2224. + some misc. exception stuff
  2225. Revision 1.29 1998/07/27 21:57:13 florian
  2226. * fix to allow tv like stream registration:
  2227. @tmenu.load doesn't work if load had parameters or if load was only
  2228. declared in an anchestor class of tmenu
  2229. Revision 1.28 1998/07/14 21:46:51 peter
  2230. * updated messages file
  2231. Revision 1.27 1998/06/25 14:04:23 peter
  2232. + internal inc/dec
  2233. Revision 1.26 1998/06/09 16:01:46 pierre
  2234. + added procedure directive parsing for procvars
  2235. (accepted are popstack cdecl and pascal)
  2236. + added C vars with the following syntax
  2237. var C calias 'true_c_name';(can be followed by external)
  2238. reason is that you must add the Cprefix
  2239. which is target dependent
  2240. Revision 1.25 1998/06/05 14:37:33 pierre
  2241. * fixes for inline for operators
  2242. * inline procedure more correctly restricted
  2243. Revision 1.24 1998/06/04 23:51:52 peter
  2244. * m68k compiles
  2245. + .def file creation moved to gendef.pas so it could also be used
  2246. for win32
  2247. Revision 1.23 1998/06/04 09:55:40 pierre
  2248. * demangled name of procsym reworked to become independant of the mangling scheme
  2249. Revision 1.22 1998/06/02 17:03:03 pierre
  2250. * with node corrected for objects
  2251. * small bugs for SUPPORT_MMX fixed
  2252. Revision 1.21 1998/05/27 19:45:05 peter
  2253. * symtable.pas splitted into includefiles
  2254. * symtable adapted for $ifdef NEWPPU
  2255. Revision 1.20 1998/05/26 07:53:59 pierre
  2256. * bug fix for empty sets (nil pd was dereferenced )
  2257. Revision 1.19 1998/05/25 17:11:43 pierre
  2258. * firstpasscount bug fixed
  2259. now all is already set correctly the first time
  2260. under EXTDEBUG try -gp to skip all other firstpasses
  2261. it works !!
  2262. * small bug fixes
  2263. - for smallsets with -dTESTSMALLSET
  2264. - some warnings removed (by correcting code !)
  2265. Revision 1.18 1998/05/23 01:21:20 peter
  2266. + aktasmmode, aktoptprocessor, aktoutputformat
  2267. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  2268. + $LIBNAME to set the library name where the unit will be put in
  2269. * splitted cgi386 a bit (codeseg to large for bp7)
  2270. * nasm, tasm works again. nasm moved to ag386nsm.pas
  2271. Revision 1.17 1998/05/22 12:37:03 carl
  2272. * crash bugfix (patched msanually to main branch)
  2273. Revision 1.16 1998/05/21 19:33:32 peter
  2274. + better procedure directive handling and only one table
  2275. Revision 1.15 1998/05/20 09:42:35 pierre
  2276. + UseTokenInfo now default
  2277. * unit in interface uses and implementation uses gives error now
  2278. * only one error for unknown symbol (uses lastsymknown boolean)
  2279. the problem came from the label code !
  2280. + first inlined procedures and function work
  2281. (warning there might be allowed cases were the result is still wrong !!)
  2282. * UseBrower updated gives a global list of all position of all used symbols
  2283. with switch -gb
  2284. Revision 1.14 1998/05/11 13:07:56 peter
  2285. + $ifdef NEWPPU for the new ppuformat
  2286. + $define GDB not longer required
  2287. * removed all warnings and stripped some log comments
  2288. * no findfirst/findnext anymore to remove smartlink *.o files
  2289. Revision 1.13 1998/05/06 08:38:45 pierre
  2290. * better position info with UseTokenInfo
  2291. UseTokenInfo greatly simplified
  2292. + added check for changed tree after first time firstpass
  2293. (if we could remove all the cases were it happen
  2294. we could skip all firstpass if firstpasscount > 1)
  2295. Only with ExtDebug
  2296. Revision 1.12 1998/05/05 12:05:42 florian
  2297. * problems with properties fixed
  2298. * crash fixed: i:=l when i and l are undefined, was a problem with
  2299. implementation of private/protected
  2300. Revision 1.11 1998/05/04 11:22:26 florian
  2301. * problem with DOM solved: it crashes when accessing a property in a method
  2302. Revision 1.10 1998/05/01 16:38:45 florian
  2303. * handling of private and protected fixed
  2304. + change_keywords_to_tp implemented to remove
  2305. keywords which aren't supported by tp
  2306. * break and continue are now symbols of the system unit
  2307. + widestring, longstring and ansistring type released
  2308. Revision 1.9 1998/04/29 10:33:58 pierre
  2309. + added some code for ansistring (not complete nor working yet)
  2310. * corrected operator overloading
  2311. * corrected nasm output
  2312. + started inline procedures
  2313. + added starstarn : use ** for exponentiation (^ gave problems)
  2314. + started UseTokenInfo cond to get accurate positions
  2315. Revision 1.8 1998/04/14 23:27:03 florian
  2316. + exclude/include with constant second parameter added
  2317. Revision 1.7 1998/04/09 23:02:15 florian
  2318. * small problems solved to get remake3 work
  2319. Revision 1.6 1998/04/09 22:16:35 florian
  2320. * problem with previous REGALLOC solved
  2321. * improved property support
  2322. Revision 1.5 1998/04/08 10:26:09 florian
  2323. * correct error handling of virtual constructors
  2324. * problem with new type declaration handling fixed
  2325. Revision 1.4 1998/04/07 22:45:05 florian
  2326. * bug0092, bug0115 and bug0121 fixed
  2327. + packed object/class/array
  2328. Revision 1.3 1998/04/07 13:19:46 pierre
  2329. * bugfixes for reset_gdb_info
  2330. in MEM parsing for go32v2
  2331. better external symbol creation
  2332. support for rhgdb.exe (lowercase file names)
  2333. }