pexpr.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  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,
  36. symconst,aasm,
  37. hcodegen,types,verbose,strings,
  38. {$ifndef newcg}
  39. tccal,
  40. {$endif newcg}
  41. pass_1,
  42. { parser specific stuff }
  43. pbase,pdecl,
  44. { processor specific stuff }
  45. cpubase,cpuinfo;
  46. const
  47. allow_type : boolean = true;
  48. got_addrn : boolean = false;
  49. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  50. var
  51. p1,p2 : ptree;
  52. end_of_paras : ttoken;
  53. begin
  54. if in_prop_paras then
  55. end_of_paras:=RECKKLAMMER
  56. else
  57. end_of_paras:=RKLAMMER;
  58. if token=end_of_paras then
  59. begin
  60. parse_paras:=nil;
  61. exit;
  62. end;
  63. p2:=nil;
  64. inc(parsing_para_level);
  65. while true do
  66. begin
  67. p1:=comp_expr(true);
  68. p2:=gencallparanode(p1,p2);
  69. { it's for the str(l:5,s); }
  70. if _colon and (token=COLON) then
  71. begin
  72. consume(COLON);
  73. p1:=comp_expr(true);
  74. p2:=gencallparanode(p1,p2);
  75. p2^.is_colon_para:=true;
  76. if token=COLON then
  77. begin
  78. consume(COLON);
  79. p1:=comp_expr(true);
  80. p2:=gencallparanode(p1,p2);
  81. p2^.is_colon_para:=true;
  82. end
  83. end;
  84. if token=COMMA then
  85. consume(COMMA)
  86. else
  87. break;
  88. end;
  89. dec(parsing_para_level);
  90. parse_paras:=p2;
  91. end;
  92. procedure check_tp_procvar(var p : ptree);
  93. var
  94. p1 : ptree;
  95. Store_valid : boolean;
  96. begin
  97. if (m_tp_procvar in aktmodeswitches) and
  98. (not got_addrn) and
  99. (not in_args) and
  100. (p^.treetype=loadn) then
  101. begin
  102. { support if procvar then for tp7 and many other expression like this }
  103. Store_valid:=Must_be_valid;
  104. Must_be_valid:=false;
  105. do_firstpass(p);
  106. Must_be_valid:=Store_valid;
  107. if not(getprocvar) and (p^.resulttype^.deftype=procvardef) then
  108. begin
  109. p1:=gencallnode(nil,nil);
  110. p1^.right:=p;
  111. p1^.resulttype:=pprocvardef(p^.resulttype)^.retdef;
  112. firstpass(p1);
  113. p:=p1;
  114. end;
  115. end;
  116. end;
  117. function statement_syssym(l : longint;var pd : pdef) : ptree;
  118. var
  119. p1,p2,paras : ptree;
  120. prev_in_args : boolean;
  121. Store_valid : boolean;
  122. begin
  123. prev_in_args:=in_args;
  124. Store_valid:=Must_be_valid;
  125. case l of
  126. in_ord_x :
  127. begin
  128. consume(LKLAMMER);
  129. in_args:=true;
  130. Must_be_valid:=true;
  131. p1:=comp_expr(true);
  132. consume(RKLAMMER);
  133. do_firstpass(p1);
  134. p1:=geninlinenode(in_ord_x,false,p1);
  135. do_firstpass(p1);
  136. statement_syssym := p1;
  137. pd:=p1^.resulttype;
  138. end;
  139. in_break :
  140. begin
  141. statement_syssym:=genzeronode(breakn);
  142. pd:=voiddef;
  143. end;
  144. in_continue :
  145. begin
  146. statement_syssym:=genzeronode(continuen);
  147. pd:=voiddef;
  148. end;
  149. in_typeof_x :
  150. begin
  151. consume(LKLAMMER);
  152. in_args:=true;
  153. {allow_type:=true;}
  154. p1:=comp_expr(true);
  155. {allow_type:=false;}
  156. consume(RKLAMMER);
  157. pd:=voidpointerdef;
  158. if p1^.treetype=typen then
  159. begin
  160. if (p1^.typenodetype=nil) then
  161. begin
  162. Message(type_e_mismatch);
  163. statement_syssym:=genzeronode(errorn);
  164. end
  165. else
  166. if p1^.typenodetype^.deftype=objectdef then
  167. begin
  168. { we can use resulttype in pass_2 (PM) }
  169. p1^.resulttype:=p1^.typenodetype;
  170. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  171. end
  172. else
  173. begin
  174. Message(type_e_mismatch);
  175. disposetree(p1);
  176. statement_syssym:=genzeronode(errorn);
  177. end;
  178. end
  179. else { not a type node }
  180. begin
  181. Must_be_valid:=false;
  182. do_firstpass(p1);
  183. if (p1^.resulttype=nil) then
  184. begin
  185. Message(type_e_mismatch);
  186. disposetree(p1);
  187. statement_syssym:=genzeronode(errorn)
  188. end
  189. else
  190. if p1^.resulttype^.deftype=objectdef then
  191. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  192. else
  193. begin
  194. Message(type_e_mismatch);
  195. statement_syssym:=genzeronode(errorn);
  196. disposetree(p1);
  197. end;
  198. end;
  199. end;
  200. in_sizeof_x :
  201. begin
  202. consume(LKLAMMER);
  203. in_args:=true;
  204. {allow_type:=true;}
  205. p1:=comp_expr(true);
  206. {allow_type:=false; }
  207. consume(RKLAMMER);
  208. pd:=s32bitdef;
  209. if p1^.treetype=typen then
  210. begin
  211. statement_syssym:=genordinalconstnode(p1^.typenodetype^.size,pd);
  212. { p1 not needed !}
  213. disposetree(p1);
  214. end
  215. else
  216. begin
  217. Must_be_valid:=false;
  218. do_firstpass(p1);
  219. if ((p1^.resulttype^.deftype=objectdef) and
  220. (oo_has_constructor in pobjectdef(p1^.resulttype)^.objectoptions)) or
  221. is_open_array(p1^.resulttype) or
  222. is_open_string(p1^.resulttype) then
  223. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  224. else
  225. begin
  226. statement_syssym:=genordinalconstnode(p1^.resulttype^.size,pd);
  227. { p1 not needed !}
  228. disposetree(p1);
  229. end;
  230. end;
  231. end;
  232. in_assigned_x :
  233. begin
  234. consume(LKLAMMER);
  235. in_args:=true;
  236. p1:=comp_expr(true);
  237. Must_be_valid:=true;
  238. do_firstpass(p1);
  239. case p1^.resulttype^.deftype of
  240. pointerdef,
  241. procvardef,
  242. classrefdef : ;
  243. objectdef : if not(pobjectdef(p1^.resulttype)^.is_class) then
  244. Message(parser_e_illegal_parameter_list);
  245. else
  246. Message(parser_e_illegal_parameter_list);
  247. end;
  248. p2:=gencallparanode(p1,nil);
  249. p2:=geninlinenode(in_assigned_x,false,p2);
  250. consume(RKLAMMER);
  251. pd:=booldef;
  252. statement_syssym:=p2;
  253. end;
  254. in_ofs_x :
  255. begin
  256. consume(LKLAMMER);
  257. in_args:=true;
  258. p1:=comp_expr(true);
  259. p1:=gensinglenode(addrn,p1);
  260. Must_be_valid:=false;
  261. do_firstpass(p1);
  262. { Ofs() returns a longint, not a pointer }
  263. p1^.resulttype:=u32bitdef;
  264. pd:=p1^.resulttype;
  265. consume(RKLAMMER);
  266. statement_syssym:=p1;
  267. end;
  268. in_addr_x :
  269. begin
  270. consume(LKLAMMER);
  271. in_args:=true;
  272. p1:=comp_expr(true);
  273. p1:=gensinglenode(addrn,p1);
  274. Must_be_valid:=false;
  275. do_firstpass(p1);
  276. pd:=p1^.resulttype;
  277. consume(RKLAMMER);
  278. statement_syssym:=p1;
  279. end;
  280. in_seg_x :
  281. begin
  282. consume(LKLAMMER);
  283. in_args:=true;
  284. p1:=comp_expr(true);
  285. do_firstpass(p1);
  286. if p1^.location.loc<>LOC_REFERENCE then
  287. Message(cg_e_illegal_expression);
  288. p1:=genordinalconstnode(0,s32bitdef);
  289. Must_be_valid:=false;
  290. pd:=s32bitdef;
  291. consume(RKLAMMER);
  292. statement_syssym:=p1;
  293. end;
  294. in_high_x,
  295. in_low_x :
  296. begin
  297. consume(LKLAMMER);
  298. in_args:=true;
  299. {allow_type:=true;}
  300. p1:=comp_expr(true);
  301. {allow_type:=false;}
  302. do_firstpass(p1);
  303. if p1^.treetype=typen then
  304. p1^.resulttype:=p1^.typenodetype;
  305. Must_be_valid:=false;
  306. p2:=geninlinenode(l,false,p1);
  307. consume(RKLAMMER);
  308. pd:=s32bitdef;
  309. statement_syssym:=p2;
  310. end;
  311. in_succ_x,
  312. in_pred_x :
  313. begin
  314. consume(LKLAMMER);
  315. in_args:=true;
  316. p1:=comp_expr(true);
  317. do_firstpass(p1);
  318. Must_be_valid:=false;
  319. p2:=geninlinenode(l,false,p1);
  320. consume(RKLAMMER);
  321. pd:=p1^.resulttype;
  322. statement_syssym:=p2;
  323. end;
  324. in_inc_x,
  325. in_dec_x :
  326. begin
  327. consume(LKLAMMER);
  328. in_args:=true;
  329. p1:=comp_expr(true);
  330. Must_be_valid:=false;
  331. if token=COMMA then
  332. begin
  333. consume(COMMA);
  334. p2:=gencallparanode(comp_expr(true),nil);
  335. end
  336. else
  337. p2:=nil;
  338. p2:=gencallparanode(p1,p2);
  339. statement_syssym:=geninlinenode(l,false,p2);
  340. consume(RKLAMMER);
  341. pd:=voiddef;
  342. end;
  343. in_concat_x :
  344. begin
  345. consume(LKLAMMER);
  346. in_args:=true;
  347. p2:=nil;
  348. while true do
  349. begin
  350. p1:=comp_expr(true);
  351. Must_be_valid:=true;
  352. do_firstpass(p1);
  353. if not((p1^.resulttype^.deftype=stringdef) or
  354. ((p1^.resulttype^.deftype=orddef) and
  355. (porddef(p1^.resulttype)^.typ=uchar))) then
  356. Message(parser_e_illegal_parameter_list);
  357. if p2<>nil then
  358. p2:=gennode(addn,p2,p1)
  359. else
  360. p2:=p1;
  361. if token=COMMA then
  362. consume(COMMA)
  363. else
  364. break;
  365. end;
  366. consume(RKLAMMER);
  367. pd:=cshortstringdef;
  368. statement_syssym:=p2;
  369. end;
  370. in_read_x,
  371. in_readln_x :
  372. begin
  373. if token=LKLAMMER then
  374. begin
  375. consume(LKLAMMER);
  376. in_args:=true;
  377. Must_be_valid:=false;
  378. paras:=parse_paras(false,false);
  379. consume(RKLAMMER);
  380. end
  381. else
  382. paras:=nil;
  383. pd:=voiddef;
  384. p1:=geninlinenode(l,false,paras);
  385. do_firstpass(p1);
  386. statement_syssym := p1;
  387. end;
  388. in_write_x,
  389. in_writeln_x :
  390. begin
  391. if token=LKLAMMER then
  392. begin
  393. consume(LKLAMMER);
  394. in_args:=true;
  395. Must_be_valid:=true;
  396. paras:=parse_paras(true,false);
  397. consume(RKLAMMER);
  398. end
  399. else
  400. paras:=nil;
  401. pd:=voiddef;
  402. p1 := geninlinenode(l,false,paras);
  403. do_firstpass(p1);
  404. statement_syssym := p1;
  405. end;
  406. in_str_x_string :
  407. begin
  408. consume(LKLAMMER);
  409. in_args:=true;
  410. paras:=parse_paras(true,false);
  411. consume(RKLAMMER);
  412. p1 := geninlinenode(l,false,paras);
  413. do_firstpass(p1);
  414. statement_syssym := p1;
  415. pd:=voiddef;
  416. end;
  417. in_val_x:
  418. Begin
  419. consume(LKLAMMER);
  420. in_args := true;
  421. p1:= gencallparanode(comp_expr(true), nil);
  422. Must_be_valid := False;
  423. consume(COMMA);
  424. p2 := gencallparanode(comp_expr(true),p1);
  425. if (token = COMMA) then
  426. Begin
  427. consume(COMMA);
  428. p2 := gencallparanode(comp_expr(true),p2)
  429. End;
  430. consume(RKLAMMER);
  431. p2 := geninlinenode(l,false,p2);
  432. do_firstpass(p2);
  433. statement_syssym := p2;
  434. pd := voiddef;
  435. End;
  436. in_include_x_y,
  437. in_exclude_x_y :
  438. begin
  439. consume(LKLAMMER);
  440. in_args:=true;
  441. p1:=comp_expr(true);
  442. Must_be_valid:=false;
  443. consume(COMMA);
  444. p2:=comp_expr(true);
  445. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  446. consume(RKLAMMER);
  447. pd:=voiddef;
  448. end;
  449. in_assert_x_y :
  450. begin
  451. consume(LKLAMMER);
  452. in_args:=true;
  453. p1:=comp_expr(true);
  454. if token=COMMA then
  455. begin
  456. consume(COMMA);
  457. p2:=comp_expr(true);
  458. end
  459. else
  460. begin
  461. { then insert an empty string }
  462. p2:=genstringconstnode('');
  463. end;
  464. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  465. consume(RKLAMMER);
  466. pd:=voiddef;
  467. end;
  468. else
  469. internalerror(15);
  470. end;
  471. in_args:=prev_in_args;
  472. Must_be_valid:=Store_valid;
  473. end;
  474. { reads the parameter for a subroutine call }
  475. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  476. var
  477. prev_in_args : boolean;
  478. prevafterassn : boolean;
  479. begin
  480. prev_in_args:=in_args;
  481. prevafterassn:=afterassignment;
  482. afterassignment:=false;
  483. { want we only determine the address of }
  484. { a subroutine ? }
  485. if not(getaddr) then
  486. begin
  487. if token=LKLAMMER then
  488. begin
  489. consume(LKLAMMER);
  490. in_args:=true;
  491. p1^.left:=parse_paras(false,false);
  492. consume(RKLAMMER);
  493. end
  494. else p1^.left:=nil;
  495. { do firstpass because we need the }
  496. { result type }
  497. do_firstpass(p1);
  498. end
  499. else
  500. begin
  501. { address operator @: }
  502. p1^.left:=nil;
  503. { forget pd }
  504. pd:=nil;
  505. if (p1^.symtableproc^.symtabletype=withsymtable) and
  506. (p1^.symtableproc^.defowner^.deftype=objectdef) then
  507. begin
  508. p1^.methodpointer:=getcopy(pwithsymtable(p1^.symtableproc)^.withrefnode);
  509. end
  510. else if not(assigned(p1^.methodpointer)) then
  511. begin
  512. { we must provide a method pointer, if it isn't given, }
  513. { it is self }
  514. p1^.methodpointer:=genselfnode(procinfo._class);
  515. p1^.methodpointer^.resulttype:=procinfo._class;
  516. end;
  517. { no postfix operators }
  518. again:=false;
  519. end;
  520. pd:=p1^.resulttype;
  521. in_args:=prev_in_args;
  522. afterassignment:=prevafterassn;
  523. end;
  524. procedure handle_procvar(procvar : pprocvardef;var t : ptree);
  525. var
  526. hp : ptree;
  527. begin
  528. hp:=nil;
  529. if (proc_to_procvar_equal(pprocsym(t^.symtableentry)^.definition,procvar)) then
  530. begin
  531. if (po_methodpointer in procvar^.procoptions) then
  532. hp:=genloadmethodcallnode(pprocsym(t^.symtableprocentry),t^.symtable,getcopy(t^.methodpointer))
  533. else
  534. hp:=genloadcallnode(pprocsym(t^.symtableprocentry),t^.symtable);
  535. end;
  536. if assigned(hp) then
  537. begin
  538. disposetree(t);
  539. t:=hp;
  540. end;
  541. end;
  542. { the following procedure handles the access to a property symbol }
  543. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : ptree;
  544. var pd : pdef);
  545. var
  546. paras : ptree;
  547. p2 : ptree;
  548. begin
  549. paras:=nil;
  550. { property parameters? }
  551. if token=LECKKLAMMER then
  552. begin
  553. consume(LECKKLAMMER);
  554. paras:=parse_paras(false,true);
  555. consume(RECKKLAMMER);
  556. end;
  557. { indexed property }
  558. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  559. begin
  560. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  561. paras:=gencallparanode(p2,paras);
  562. end;
  563. { we need only a write property if a := follows }
  564. { if not(afterassignment) and not(in_args) then }
  565. if token=ASSIGNMENT then
  566. begin
  567. { write property: }
  568. { no result }
  569. pd:=voiddef;
  570. if assigned(ppropertysym(sym)^.writeaccesssym) then
  571. begin
  572. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  573. begin
  574. { generate the method call }
  575. p1:=genmethodcallnode(pprocsym(
  576. ppropertysym(sym)^.writeaccesssym),st,p1);
  577. { we know the procedure to call, so
  578. force the usage of that procedure }
  579. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  580. p1^.left:=paras;
  581. consume(ASSIGNMENT);
  582. { read the expression }
  583. getprocvar:=ppropertysym(sym)^.proptype^.deftype=procvardef;
  584. p2:=comp_expr(true);
  585. if getprocvar then
  586. begin
  587. if (p2^.treetype=calln) then
  588. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2)
  589. else
  590. if (p2^.treetype=typeconvn) and
  591. (p2^.left^.treetype=calln) then
  592. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype),p2^.left);
  593. end;
  594. p1^.left:=gencallparanode(p2,p1^.left);
  595. getprocvar:=false;
  596. end
  597. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  598. begin
  599. if assigned(paras) then
  600. message(parser_e_no_paras_allowed);
  601. { subscribed access? }
  602. if p1=nil then
  603. p1:=genloadnode(pvarsym(ppropertysym(sym)^.writeaccesssym),st)
  604. else
  605. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.writeaccesssym),p1);
  606. consume(ASSIGNMENT);
  607. { read the expression }
  608. p2:=comp_expr(true);
  609. p1:=gennode(assignn,p1,p2);
  610. end
  611. else
  612. begin
  613. p1:=genzeronode(errorn);
  614. Message(parser_e_no_procedure_to_access_property);
  615. end;
  616. end
  617. else
  618. begin
  619. p1:=genzeronode(errorn);
  620. Message(parser_e_no_procedure_to_access_property);
  621. end;
  622. end
  623. else
  624. begin
  625. { read property: }
  626. pd:=ppropertysym(sym)^.proptype;
  627. if assigned(ppropertysym(sym)^.readaccesssym) then
  628. begin
  629. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  630. begin
  631. if assigned(paras) then
  632. message(parser_e_no_paras_allowed);
  633. { subscribed access? }
  634. if p1=nil then
  635. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),st)
  636. else
  637. p1:=gensubscriptnode(pvarsym(ppropertysym(sym)^.readaccesssym),p1);
  638. end
  639. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  640. begin
  641. { generate the method call }
  642. p1:=genmethodcallnode(pprocsym(ppropertysym(sym)^.readaccesssym),st,p1);
  643. { we know the procedure to call, so
  644. force the usage of that procedure }
  645. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  646. { insert paras }
  647. p1^.left:=paras;
  648. end
  649. else
  650. begin
  651. p1:=genzeronode(errorn);
  652. Message(type_e_mismatch);
  653. end;
  654. end
  655. else
  656. begin
  657. { error, no function to read property }
  658. p1:=genzeronode(errorn);
  659. Message(parser_e_no_procedure_to_access_property);
  660. end;
  661. end;
  662. end;
  663. { the ID token has to be consumed before calling this function }
  664. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  665. var pd : pdef;var again : boolean);
  666. var
  667. static_name : string;
  668. isclassref : boolean;
  669. begin
  670. if sym=nil then
  671. begin
  672. { pattern is still valid unless
  673. there is another ID just after the ID of sym }
  674. Message1(sym_e_id_no_member,pattern);
  675. disposetree(p1);
  676. p1:=genzeronode(errorn);
  677. { try to clean up }
  678. pd:=generrordef;
  679. again:=false;
  680. end
  681. else
  682. begin
  683. isclassref:=pd^.deftype=classrefdef;
  684. { check protected and private members }
  685. { please leave this code as it is, }
  686. { it has now the same behaviaor as TP/Delphi }
  687. if (sp_private in sym^.symoptions) and
  688. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  689. Message(parser_e_cant_access_private_member);
  690. if (sp_protected in sym^.symoptions) and
  691. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  692. begin
  693. if assigned(aktprocsym^.definition^._class) then
  694. begin
  695. if not aktprocsym^.definition^._class^.is_related(pobjectdef(sym^.owner^.defowner)) then
  696. Message(parser_e_cant_access_protected_member);
  697. end
  698. else
  699. Message(parser_e_cant_access_protected_member);
  700. end;
  701. { we assume, that only procsyms and varsyms are in an object }
  702. { symbol table, for classes, properties are allowed }
  703. case sym^.typ of
  704. procsym:
  705. begin
  706. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  707. do_proc_call(getaddr or
  708. (getprocvar and
  709. (m_tp_procvar in aktmodeswitches) and
  710. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef))
  711. ,again,p1,pd);
  712. { now we know the real method e.g. we can check for }
  713. { a class method }
  714. if isclassref and
  715. not(po_classmethod in p1^.procdefinition^.procoptions) and
  716. not(p1^.procdefinition^.proctypeoption=potype_constructor) 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 (sp_static in sym^.symoptions) 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 (sp_private in srsym^.symoptions) 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. (po_classmethod in aktprocsym^.definition^.procoptions) then
  886. Message(parser_e_only_class_methods);
  887. if (sp_static in srsym^.symoptions) 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)^.varstate=vs_declared then
  894. begin
  895. p1^.is_first := true;
  896. { set special between first loaded until checked in firstpass }
  897. pvarsym(srsym)^.varstate:=vs_declared2;
  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)^.is_class) then
  946. begin
  947. consume(POINT);
  948. if assigned(procinfo._class) then
  949. begin
  950. if procinfo._class^.is_related(pobjectdef(pd)) then
  951. begin
  952. p1:=gentypenode(pd,ptypesym(srsym));
  953. p1^.resulttype:=pd;
  954. srsymtable:=pobjectdef(pd)^.symtable;
  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)^.symtable;
  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)^.symtable;
  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 not(sp_static in sym^.symoptions) 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)^.is_class 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. (po_classmethod in aktprocsym^.definition^.procoptions);
  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. not(po_classmethod in p1^.procdefinition^.procoptions) 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. (po_classmethod in aktprocsym^.definition^.procoptions) 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)^.is_class 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(precorddef(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^.symtable^.search(pattern));
  1340. srsymtable:=classh^.symtable;
  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^.symtable^.search(pattern));
  1357. srsymtable:=classh^.symtable;
  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. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.definition)^.objectoptions) 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 (oo_has_abstract in classh^.objectoptions) 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^.symtable^.search(pattern));
  1509. srsymtable:=classh^.symtable;
  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^.proctypeoption<>potype_constructor)) 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 (po_classmethod in aktprocsym^.definition^.procoptions) 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)^.symtable;
  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.130 1999-08-04 00:23:12 florian
  1959. * renamed i386asm and i386base to cpuasm and cpubase
  1960. Revision 1.129 1999/08/03 22:02:59 peter
  1961. * moved bitmask constants to sets
  1962. * some other type/const renamings
  1963. Revision 1.128 1999/08/03 13:50:17 michael
  1964. + Changes for alpha
  1965. Revision 1.127 1999/08/01 18:28:13 florian
  1966. * modifications for the new code generator
  1967. Revision 1.126 1999/07/30 12:28:40 peter
  1968. * fixed crash with unknown id and colon parameter in write
  1969. Revision 1.125 1999/07/27 23:42:14 peter
  1970. * indirect type referencing is now allowed
  1971. Revision 1.124 1999/07/23 21:31:42 peter
  1972. * fixed crash with resourcestring
  1973. Revision 1.123 1999/07/23 11:37:46 peter
  1974. * error for illegal type reference, instead of 10998
  1975. Revision 1.122 1999/07/22 09:37:52 florian
  1976. + resourcestring implemented
  1977. + start of longstring support
  1978. Revision 1.121 1999/07/16 10:04:35 peter
  1979. * merged
  1980. Revision 1.120 1999/07/06 22:38:11 florian
  1981. * another fix for TP/Delphi styled procedure variables
  1982. Revision 1.119 1999/07/05 20:13:16 peter
  1983. * removed temp defines
  1984. Revision 1.118 1999/07/01 21:33:57 peter
  1985. * merged
  1986. Revision 1.117 1999/06/30 15:43:20 florian
  1987. * two bugs regarding method variables fixed
  1988. - if you take in a method the address of another method
  1989. don't need self anymore
  1990. - if the class pointer was in a register, wrong code for a method
  1991. variable load was generated
  1992. Revision 1.116 1999/06/26 00:24:53 pierre
  1993. * mereg from fixes-0_99_12 branch
  1994. Revision 1.112.2.8 1999/07/16 09:54:57 peter
  1995. * @procvar support in tp7 mode works again
  1996. Revision 1.112.2.7 1999/07/07 07:53:10 michael
  1997. + Merged patches from florian
  1998. Revision 1.112.2.6 1999/07/01 21:31:59 peter
  1999. * procvar fixes again
  2000. Revision 1.112.2.5 1999/07/01 15:17:17 peter
  2001. * methoidpointer fixes from florian
  2002. Revision 1.112.2.4 1999/06/26 00:22:30 pierre
  2003. * wrong warnings in -So mode suppressed
  2004. Revision 1.112.2.3 1999/06/17 12:51:44 pierre
  2005. * changed is_assignment_overloaded into
  2006. function assignment_overloaded : pprocdef
  2007. to allow overloading of assignment with only different result type
  2008. Revision 1.112.2.2 1999/06/15 18:54:52 peter
  2009. * more procvar fixes
  2010. Revision 1.112.2.1 1999/06/13 22:38:09 peter
  2011. * tp_procvar check for loading of procvars when getaddr=false
  2012. Revision 1.112 1999/06/02 22:44:11 pierre
  2013. * previous wrong log corrected
  2014. Revision 1.111 1999/06/02 22:25:43 pierre
  2015. * changed $ifdef FPC @ into $ifndef TP
  2016. * changes for correct procvar handling under tp mode
  2017. Revision 1.110 1999/06/01 19:27:55 peter
  2018. * better checks for procvar and methodpointer
  2019. Revision 1.109 1999/05/27 19:44:46 peter
  2020. * removed oldasm
  2021. * plabel -> pasmlabel
  2022. * -a switches to source writing automaticly
  2023. * assembler readers OOPed
  2024. * asmsymbol automaticly external
  2025. * jumptables and other label fixes for asm readers
  2026. Revision 1.108 1999/05/18 14:15:54 peter
  2027. * containsself fixes
  2028. * checktypes()
  2029. Revision 1.107 1999/05/18 09:52:18 peter
  2030. * procedure of object and addrn fixes
  2031. Revision 1.106 1999/05/16 17:06:31 peter
  2032. * remove firstcallparan which looks obsolete
  2033. Revision 1.105 1999/05/12 22:36:09 florian
  2034. * override isn't allowed in objects!
  2035. Revision 1.104 1999/05/07 10:35:23 florian
  2036. * first fix for a problem with method pointer properties, still doesn't work
  2037. with WITH
  2038. Revision 1.103 1999/05/06 21:40:16 peter
  2039. * fixed crash
  2040. Revision 1.101 1999/05/06 09:05:21 peter
  2041. * generic write_float and str_float
  2042. * fixed constant float conversions
  2043. Revision 1.100 1999/05/04 21:44:57 florian
  2044. * changes to compile it with Delphi 4.0
  2045. Revision 1.99 1999/05/01 13:24:31 peter
  2046. * merged nasm compiler
  2047. * old asm moved to oldasm/
  2048. Revision 1.98 1999/04/26 18:29:56 peter
  2049. * farpointerdef moved into pointerdef.is_far
  2050. Revision 1.97 1999/04/19 09:27:48 peter
  2051. * removed my property fix
  2052. Revision 1.96 1999/04/19 09:13:47 peter
  2053. * class property without write support
  2054. Revision 1.95 1999/04/19 06:10:08 florian
  2055. * property problem fixed: a propertysym is only a write
  2056. access if it is followed by a assignment token
  2057. Revision 1.94 1999/04/17 13:12:17 peter
  2058. * addr() internal
  2059. Revision 1.93 1999/04/15 09:00:08 peter
  2060. * fixed property write
  2061. Revision 1.92 1999/04/08 20:59:43 florian
  2062. * fixed problem with default properties which are a class
  2063. * case bug (from the mailing list with -O2) fixed, the
  2064. distance of the case labels can be greater than the positive
  2065. range of a longint => it is now a dword for fpc
  2066. Revision 1.91 1999/04/06 11:21:56 peter
  2067. * more use of ttoken
  2068. Revision 1.90 1999/03/31 13:55:12 peter
  2069. * assembler inlining working for ag386bin
  2070. Revision 1.89 1999/03/26 00:05:36 peter
  2071. * released valintern
  2072. + deffile is now removed when compiling is finished
  2073. * ^( compiles now correct
  2074. + static directive
  2075. * shrd fixed
  2076. Revision 1.88 1999/03/24 23:17:15 peter
  2077. * fixed bugs 212,222,225,227,229,231,233
  2078. Revision 1.87 1999/03/16 17:52:52 jonas
  2079. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  2080. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  2081. * in cgai386: also small fixes to emitrangecheck
  2082. Revision 1.86 1999/03/04 13:55:44 pierre
  2083. * some m68k fixes (still not compilable !)
  2084. * new(tobj) does not give warning if tobj has no VMT !
  2085. Revision 1.85 1999/02/22 15:09:39 florian
  2086. * behaviaor of PROTECTED and PRIVATE fixed, works now like TP/Delphi
  2087. Revision 1.84 1999/02/22 02:15:26 peter
  2088. * updates for ag386bin
  2089. Revision 1.83 1999/02/11 09:46:25 pierre
  2090. * fix for normal method calls inside static methods :
  2091. WARNING there were both parser and codegen errors !!
  2092. added static_call boolean to calln tree
  2093. Revision 1.82 1999/01/28 14:06:47 florian
  2094. * small fix for method pointers
  2095. * found the annoying strpas bug, mainly nested call to type cast which
  2096. use ansistrings crash
  2097. Revision 1.81 1999/01/27 00:13:55 florian
  2098. * "procedure of object"-stuff fixed
  2099. Revision 1.80 1999/01/21 16:41:01 pierre
  2100. * fix for constructor inside with statements
  2101. Revision 1.79 1998/12/30 22:15:48 peter
  2102. + farpointer type
  2103. * absolutesym now also stores if its far
  2104. Revision 1.78 1998/12/11 00:03:32 peter
  2105. + globtype,tokens,version unit splitted from globals
  2106. Revision 1.77 1998/12/04 10:18:09 florian
  2107. * some stuff for procedures of object added
  2108. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  2109. Revision 1.76 1998/11/27 14:50:40 peter
  2110. + open strings, $P switch support
  2111. Revision 1.75 1998/11/25 19:12:51 pierre
  2112. * var:=new(pointer_type) support added
  2113. Revision 1.74 1998/11/13 10:18:11 peter
  2114. + nil constants
  2115. Revision 1.73 1998/11/05 12:02:52 peter
  2116. * released useansistring
  2117. * removed -Sv, its now available in fpc modes
  2118. Revision 1.72 1998/11/04 10:11:41 peter
  2119. * ansistring fixes
  2120. Revision 1.71 1998/10/22 23:57:29 peter
  2121. * fixed filedef for typenodetype
  2122. Revision 1.70 1998/10/21 15:12:54 pierre
  2123. * bug fix for IOCHECK inside a procedure with iocheck modifier
  2124. * removed the GPF for unexistant overloading
  2125. (firstcall was called with procedinition=nil !)
  2126. * changed typen to what Florian proposed
  2127. gentypenode(p : pdef) sets the typenodetype field
  2128. and resulttype is only set if inside bt_type block !
  2129. Revision 1.69 1998/10/20 15:10:19 pierre
  2130. * type ptree only allowed inside expression
  2131. if following sizeof typeof low high or as first arg of new !!
  2132. Revision 1.68 1998/10/20 11:15:44 pierre
  2133. * calling of private method allowed inside child object method
  2134. Revision 1.67 1998/10/19 08:54:57 pierre
  2135. * wrong stabs info corrected once again !!
  2136. + variable vmt offset with vmt field only if required
  2137. implemented now !!!
  2138. Revision 1.66 1998/10/15 15:13:28 pierre
  2139. + added oo_hasconstructor and oo_hasdestructor
  2140. for objects options
  2141. Revision 1.65 1998/10/13 13:10:24 peter
  2142. * new style for m68k/i386 infos and enums
  2143. Revision 1.64 1998/10/12 12:20:55 pierre
  2144. + added tai_const_symbol_offset
  2145. for r : pointer = @var.field;
  2146. * better message for different arg names on implementation
  2147. of function
  2148. Revision 1.63 1998/10/12 10:28:30 florian
  2149. + auto dereferencing of pointers to structured types in delphi mode
  2150. Revision 1.62 1998/10/12 10:05:41 peter
  2151. * fixed mem leak with arrayconstrutor
  2152. Revision 1.61 1998/10/05 13:57:15 peter
  2153. * crash preventions
  2154. Revision 1.60 1998/10/05 12:32:46 peter
  2155. + assert() support
  2156. Revision 1.59 1998/10/01 14:56:24 peter
  2157. * crash preventions
  2158. Revision 1.58 1998/09/30 07:40:35 florian
  2159. * better error recovering
  2160. Revision 1.57 1998/09/28 16:18:16 florian
  2161. * two fixes to get ansi strings work
  2162. Revision 1.56 1998/09/26 17:45:36 peter
  2163. + idtoken and only one token table
  2164. Revision 1.55 1998/09/24 23:49:10 peter
  2165. + aktmodeswitches
  2166. Revision 1.54 1998/09/23 15:46:39 florian
  2167. * problem with with and classes fixed
  2168. Revision 1.53 1998/09/23 09:58:54 peter
  2169. * first working array of const things
  2170. Revision 1.52 1998/09/20 09:38:45 florian
  2171. * hasharray for defs fixed
  2172. * ansistring code generation corrected (init/final, assignement)
  2173. Revision 1.51 1998/09/18 16:03:43 florian
  2174. * some changes to compile with Delphi
  2175. Revision 1.50 1998/09/17 13:41:18 pierre
  2176. sizeof(TPOINT) problem
  2177. Revision 1.49.2.1 1998/09/17 08:42:31 pierre
  2178. TPOINT sizeof fix
  2179. Revision 1.49 1998/09/09 11:50:53 pierre
  2180. * forward def are not put in record or objects
  2181. + added check for forwards also in record and objects
  2182. * dummy parasymtable for unit initialization removed from
  2183. symtable stack
  2184. Revision 1.48 1998/09/07 22:25:53 peter
  2185. * fixed str(boolean,string) which was allowed
  2186. * fixed write(' ':<int expression>) only constants where allowed :(
  2187. Revision 1.47 1998/09/07 18:46:10 peter
  2188. * update smartlinking, uses getdatalabel
  2189. * renamed ptree.value vars to value_str,value_real,value_set
  2190. Revision 1.46 1998/09/04 08:42:03 peter
  2191. * updated some error messages
  2192. Revision 1.45 1998/09/01 17:39:49 peter
  2193. + internal constant functions
  2194. Revision 1.44 1998/08/28 10:54:24 peter
  2195. * fixed smallset generation from elements, it has never worked before!
  2196. Revision 1.43 1998/08/23 16:07:24 florian
  2197. * internalerror with mod/div fixed
  2198. Revision 1.42 1998/08/21 14:08:50 pierre
  2199. + TEST_FUNCRET now default (old code removed)
  2200. works also for m68k (at least compiles)
  2201. Revision 1.41 1998/08/20 21:36:39 peter
  2202. * fixed 'with object do' bug
  2203. Revision 1.40 1998/08/20 09:26:41 pierre
  2204. + funcret setting in underproc testing
  2205. compile with _dTEST_FUNCRET
  2206. Revision 1.39 1998/08/18 16:48:48 pierre
  2207. * bug for -So proc assignment to p^rocvar fixed
  2208. Revision 1.38 1998/08/18 14:17:09 pierre
  2209. * bug about assigning the return value of a function to
  2210. a procvar fixed : warning
  2211. assigning a proc to a procvar need @ in FPC mode !!
  2212. * missing file/line info restored
  2213. Revision 1.37 1998/08/18 09:24:43 pierre
  2214. * small warning position bug fixed
  2215. * support_mmx switches splitting was missing
  2216. * rhide error and warning output corrected
  2217. Revision 1.36 1998/08/15 16:50:29 peter
  2218. * fixed proc()=expr which was not allowed anymore by my previous fix
  2219. Revision 1.35 1998/08/14 18:18:46 peter
  2220. + dynamic set contruction
  2221. * smallsets are now working (always longint size)
  2222. Revision 1.34 1998/08/13 11:00:12 peter
  2223. * fixed procedure<>procedure construct
  2224. Revision 1.33 1998/08/11 15:31:39 peter
  2225. * write extended to ppu file
  2226. * new version 0.99.7
  2227. Revision 1.32 1998/08/11 14:05:32 peter
  2228. * fixed sizeof(array of char)
  2229. Revision 1.31 1998/08/10 14:50:11 peter
  2230. + localswitches, moduleswitches, globalswitches splitting
  2231. Revision 1.30 1998/07/28 21:52:54 florian
  2232. + implementation of raise and try..finally
  2233. + some misc. exception stuff
  2234. Revision 1.29 1998/07/27 21:57:13 florian
  2235. * fix to allow tv like stream registration:
  2236. @tmenu.load doesn't work if load had parameters or if load was only
  2237. declared in an anchestor class of tmenu
  2238. Revision 1.28 1998/07/14 21:46:51 peter
  2239. * updated messages file
  2240. Revision 1.27 1998/06/25 14:04:23 peter
  2241. + internal inc/dec
  2242. Revision 1.26 1998/06/09 16:01:46 pierre
  2243. + added procedure directive parsing for procvars
  2244. (accepted are popstack cdecl and pascal)
  2245. + added C vars with the following syntax
  2246. var C calias 'true_c_name';(can be followed by external)
  2247. reason is that you must add the Cprefix
  2248. which is target dependent
  2249. Revision 1.25 1998/06/05 14:37:33 pierre
  2250. * fixes for inline for operators
  2251. * inline procedure more correctly restricted
  2252. Revision 1.24 1998/06/04 23:51:52 peter
  2253. * m68k compiles
  2254. + .def file creation moved to gendef.pas so it could also be used
  2255. for win32
  2256. Revision 1.23 1998/06/04 09:55:40 pierre
  2257. * demangled name of procsym reworked to become independant of the mangling scheme
  2258. Revision 1.22 1998/06/02 17:03:03 pierre
  2259. * with node corrected for objects
  2260. * small bugs for SUPPORT_MMX fixed
  2261. Revision 1.21 1998/05/27 19:45:05 peter
  2262. * symtable.pas splitted into includefiles
  2263. * symtable adapted for $ifdef NEWPPU
  2264. Revision 1.20 1998/05/26 07:53:59 pierre
  2265. * bug fix for empty sets (nil pd was dereferenced )
  2266. Revision 1.19 1998/05/25 17:11:43 pierre
  2267. * firstpasscount bug fixed
  2268. now all is already set correctly the first time
  2269. under EXTDEBUG try -gp to skip all other firstpasses
  2270. it works !!
  2271. * small bug fixes
  2272. - for smallsets with -dTESTSMALLSET
  2273. - some warnings removed (by correcting code !)
  2274. Revision 1.18 1998/05/23 01:21:20 peter
  2275. + aktasmmode, aktoptprocessor, aktoutputformat
  2276. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  2277. + $LIBNAME to set the library name where the unit will be put in
  2278. * splitted cgi386 a bit (codeseg to large for bp7)
  2279. * nasm, tasm works again. nasm moved to ag386nsm.pas
  2280. Revision 1.17 1998/05/22 12:37:03 carl
  2281. * crash bugfix (patched msanually to main branch)
  2282. Revision 1.16 1998/05/21 19:33:32 peter
  2283. + better procedure directive handling and only one table
  2284. Revision 1.15 1998/05/20 09:42:35 pierre
  2285. + UseTokenInfo now default
  2286. * unit in interface uses and implementation uses gives error now
  2287. * only one error for unknown symbol (uses lastsymknown boolean)
  2288. the problem came from the label code !
  2289. + first inlined procedures and function work
  2290. (warning there might be allowed cases were the result is still wrong !!)
  2291. * UseBrower updated gives a global list of all position of all used symbols
  2292. with switch -gb
  2293. Revision 1.14 1998/05/11 13:07:56 peter
  2294. + $ifdef NEWPPU for the new ppuformat
  2295. + $define GDB not longer required
  2296. * removed all warnings and stripped some log comments
  2297. * no findfirst/findnext anymore to remove smartlink *.o files
  2298. Revision 1.13 1998/05/06 08:38:45 pierre
  2299. * better position info with UseTokenInfo
  2300. UseTokenInfo greatly simplified
  2301. + added check for changed tree after first time firstpass
  2302. (if we could remove all the cases were it happen
  2303. we could skip all firstpass if firstpasscount > 1)
  2304. Only with ExtDebug
  2305. Revision 1.12 1998/05/05 12:05:42 florian
  2306. * problems with properties fixed
  2307. * crash fixed: i:=l when i and l are undefined, was a problem with
  2308. implementation of private/protected
  2309. Revision 1.11 1998/05/04 11:22:26 florian
  2310. * problem with DOM solved: it crashes when accessing a property in a method
  2311. Revision 1.10 1998/05/01 16:38:45 florian
  2312. * handling of private and protected fixed
  2313. + change_keywords_to_tp implemented to remove
  2314. keywords which aren't supported by tp
  2315. * break and continue are now symbols of the system unit
  2316. + widestring, longstring and ansistring type released
  2317. Revision 1.9 1998/04/29 10:33:58 pierre
  2318. + added some code for ansistring (not complete nor working yet)
  2319. * corrected operator overloading
  2320. * corrected nasm output
  2321. + started inline procedures
  2322. + added starstarn : use ** for exponentiation (^ gave problems)
  2323. + started UseTokenInfo cond to get accurate positions
  2324. Revision 1.8 1998/04/14 23:27:03 florian
  2325. + exclude/include with constant second parameter added
  2326. Revision 1.7 1998/04/09 23:02:15 florian
  2327. * small problems solved to get remake3 work
  2328. Revision 1.6 1998/04/09 22:16:35 florian
  2329. * problem with previous REGALLOC solved
  2330. * improved property support
  2331. Revision 1.5 1998/04/08 10:26:09 florian
  2332. * correct error handling of virtual constructors
  2333. * problem with new type declaration handling fixed
  2334. Revision 1.4 1998/04/07 22:45:05 florian
  2335. * bug0092, bug0115 and bug0121 fixed
  2336. + packed object/class/array
  2337. Revision 1.3 1998/04/07 13:19:46 pierre
  2338. * bugfixes for reset_gdb_info
  2339. in MEM parsing for go32v2
  2340. better external symbol creation
  2341. support for rhgdb.exe (lowercase file names)
  2342. }