pexpr.pas 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. symtype,
  23. node,
  24. cpuinfo;
  25. { reads a whole expression }
  26. function expr : tnode;
  27. { reads an expression without assignements and .. }
  28. function comp_expr(accept_equal : boolean):tnode;
  29. { reads a single factor }
  30. function factor(getaddr : boolean) : tnode;
  31. procedure string_dec(var t: ttype);
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  34. {$ifdef int64funcresok}
  35. function get_intconst:TConstExprInt;
  36. {$else int64funcresok}
  37. function get_intconst:longint;
  38. {$endif int64funcresok}
  39. function get_stringconst:string;
  40. implementation
  41. uses
  42. {$ifdef delphi}
  43. SysUtils,
  44. {$endif}
  45. { common }
  46. cutils,
  47. { global }
  48. globtype,globals,tokens,verbose,
  49. systems,widestr,
  50. { symtable }
  51. symconst,symbase,symdef,symsym,symtable,types,
  52. { pass 1 }
  53. pass_1,htypechk,
  54. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  55. { parser }
  56. scanner,
  57. pbase,
  58. { codegen }
  59. cgbase
  60. ;
  61. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  62. read as - (1**4) and not (-1)**4 PM }
  63. type
  64. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  65. const
  66. highest_precedence = oppower;
  67. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  68. const
  69. got_addrn : boolean = false;
  70. auto_inherited : boolean = false;
  71. procedure string_dec(var t: ttype);
  72. { reads a string type with optional length }
  73. { and returns a pointer to the string }
  74. { definition }
  75. var
  76. p : tnode;
  77. begin
  78. t:=cshortstringtype;
  79. consume(_STRING);
  80. if token=_LECKKLAMMER then
  81. begin
  82. consume(_LECKKLAMMER);
  83. p:=comp_expr(true);
  84. if not is_constintnode(p) then
  85. begin
  86. Message(cg_e_illegal_expression);
  87. { error recovery }
  88. consume(_RECKKLAMMER);
  89. end
  90. else
  91. begin
  92. if (tordconstnode(p).value<=0) then
  93. begin
  94. Message(parser_e_invalid_string_size);
  95. tordconstnode(p).value:=255;
  96. end;
  97. consume(_RECKKLAMMER);
  98. if tordconstnode(p).value>255 then
  99. t.setdef(tstringdef.createlong(tordconstnode(p).value))
  100. else
  101. if tordconstnode(p).value<>255 then
  102. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  103. end;
  104. p.free;
  105. end
  106. else
  107. begin
  108. if cs_ansistrings in aktlocalswitches then
  109. t:=cansistringtype
  110. else
  111. t:=cshortstringtype;
  112. end;
  113. end;
  114. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  115. var
  116. p1,p2 : tnode;
  117. end_of_paras : ttoken;
  118. prev_in_args : boolean;
  119. old_allow_array_constructor : boolean;
  120. begin
  121. if in_prop_paras then
  122. end_of_paras:=_RECKKLAMMER
  123. else
  124. end_of_paras:=_RKLAMMER;
  125. if token=end_of_paras then
  126. begin
  127. parse_paras:=nil;
  128. exit;
  129. end;
  130. { save old values }
  131. prev_in_args:=in_args;
  132. old_allow_array_constructor:=allow_array_constructor;
  133. { set para parsing values }
  134. in_args:=true;
  135. inc(parsing_para_level);
  136. allow_array_constructor:=true;
  137. p2:=nil;
  138. while true do
  139. begin
  140. p1:=comp_expr(true);
  141. p2:=ccallparanode.create(p1,p2);
  142. { it's for the str(l:5,s); }
  143. if __colon and (token=_COLON) then
  144. begin
  145. consume(_COLON);
  146. p1:=comp_expr(true);
  147. p2:=ccallparanode.create(p1,p2);
  148. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  149. if token=_COLON then
  150. begin
  151. consume(_COLON);
  152. p1:=comp_expr(true);
  153. p2:=ccallparanode.create(p1,p2);
  154. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  155. end
  156. end;
  157. if token=_COMMA then
  158. consume(_COMMA)
  159. else
  160. break;
  161. end;
  162. allow_array_constructor:=old_allow_array_constructor;
  163. dec(parsing_para_level);
  164. in_args:=prev_in_args;
  165. parse_paras:=p2;
  166. end;
  167. procedure check_tp_procvar(var p : tnode);
  168. var
  169. p1 : tnode;
  170. begin
  171. if (m_tp_procvar in aktmodeswitches) and
  172. (not got_addrn) and
  173. (not in_args) and
  174. (p.nodetype=loadn) then
  175. begin
  176. { support if procvar then for tp7 and many other expression like this }
  177. do_resulttypepass(p);
  178. set_varstate(p,false);
  179. { reset varstateset to maybe set used state later web bug769 PM }
  180. unset_varstate(p);
  181. if not(getprocvar) and (p.resulttype.def.deftype=procvardef) then
  182. begin
  183. p1:=ccallnode.create(nil,nil,nil,nil);
  184. tcallnode(p1).set_procvar(p);
  185. resulttypepass(p1);
  186. p:=p1;
  187. end;
  188. end;
  189. end;
  190. function statement_syssym(l : longint) : tnode;
  191. var
  192. p1,p2,paras : tnode;
  193. prev_in_args : boolean;
  194. begin
  195. prev_in_args:=in_args;
  196. case l of
  197. in_ord_x :
  198. begin
  199. consume(_LKLAMMER);
  200. in_args:=true;
  201. p1:=comp_expr(true);
  202. consume(_RKLAMMER);
  203. p1:=geninlinenode(in_ord_x,false,p1);
  204. statement_syssym := p1;
  205. end;
  206. in_break :
  207. begin
  208. statement_syssym:=cbreaknode.create;
  209. end;
  210. in_continue :
  211. begin
  212. statement_syssym:=ccontinuenode.create;
  213. end;
  214. in_typeof_x :
  215. begin
  216. consume(_LKLAMMER);
  217. in_args:=true;
  218. p1:=comp_expr(true);
  219. consume(_RKLAMMER);
  220. if p1.nodetype=typen then
  221. ttypenode(p1).allowed:=true;
  222. if p1.resulttype.def.deftype=objectdef then
  223. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  224. else
  225. begin
  226. Message(type_e_mismatch);
  227. p1.destroy;
  228. statement_syssym:=cerrornode.create;
  229. end;
  230. end;
  231. in_sizeof_x :
  232. begin
  233. consume(_LKLAMMER);
  234. in_args:=true;
  235. p1:=comp_expr(true);
  236. consume(_RKLAMMER);
  237. if (p1.nodetype<>typen) and
  238. (
  239. ((p1.resulttype.def.deftype=objectdef) and
  240. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  241. is_open_array(p1.resulttype.def) or
  242. is_open_string(p1.resulttype.def)
  243. ) then
  244. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  245. else
  246. begin
  247. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,s32bittype);
  248. { p1 not needed !}
  249. p1.destroy;
  250. end;
  251. end;
  252. in_typeinfo_x :
  253. begin
  254. consume(_LKLAMMER);
  255. in_args:=true;
  256. p1:=comp_expr(true);
  257. if p1.nodetype=typen then
  258. ttypenode(p1).allowed:=true
  259. else
  260. begin
  261. p1.destroy;
  262. p1:=cerrornode.create;
  263. Message(parser_e_illegal_parameter_list);
  264. end;
  265. consume(_RKLAMMER);
  266. p2:=ccallparanode.create(p1,nil);
  267. p2:=geninlinenode(in_typeinfo_x,false,p2);
  268. statement_syssym:=p2;
  269. end;
  270. in_assigned_x :
  271. begin
  272. consume(_LKLAMMER);
  273. in_args:=true;
  274. p1:=comp_expr(true);
  275. if not codegenerror then
  276. begin
  277. case p1.resulttype.def.deftype of
  278. pointerdef,
  279. procvardef,
  280. classrefdef : ;
  281. objectdef :
  282. if not is_class_or_interface(p1.resulttype.def) then
  283. Message(parser_e_illegal_parameter_list);
  284. else
  285. Message(parser_e_illegal_parameter_list);
  286. end;
  287. end;
  288. p2:=ccallparanode.create(p1,nil);
  289. p2:=geninlinenode(in_assigned_x,false,p2);
  290. consume(_RKLAMMER);
  291. statement_syssym:=p2;
  292. end;
  293. in_addr_x :
  294. begin
  295. consume(_LKLAMMER);
  296. in_args:=true;
  297. p1:=comp_expr(true);
  298. p1:=caddrnode.create(p1);
  299. consume(_RKLAMMER);
  300. statement_syssym:=p1;
  301. end;
  302. in_ofs_x :
  303. begin
  304. consume(_LKLAMMER);
  305. in_args:=true;
  306. p1:=comp_expr(true);
  307. p1:=caddrnode.create(p1);
  308. do_resulttypepass(p1);
  309. { Ofs() returns a cardinal, not a pointer }
  310. p1.resulttype:=u32bittype;
  311. consume(_RKLAMMER);
  312. statement_syssym:=p1;
  313. end;
  314. in_seg_x :
  315. begin
  316. consume(_LKLAMMER);
  317. in_args:=true;
  318. p1:=comp_expr(true);
  319. p1:=geninlinenode(in_seg_x,false,p1);
  320. consume(_RKLAMMER);
  321. statement_syssym:=p1;
  322. end;
  323. in_high_x,
  324. in_low_x :
  325. begin
  326. consume(_LKLAMMER);
  327. in_args:=true;
  328. p1:=comp_expr(true);
  329. p2:=geninlinenode(l,false,p1);
  330. consume(_RKLAMMER);
  331. statement_syssym:=p2;
  332. end;
  333. in_succ_x,
  334. in_pred_x :
  335. begin
  336. consume(_LKLAMMER);
  337. in_args:=true;
  338. p1:=comp_expr(true);
  339. p2:=geninlinenode(l,false,p1);
  340. consume(_RKLAMMER);
  341. statement_syssym:=p2;
  342. end;
  343. in_inc_x,
  344. in_dec_x :
  345. begin
  346. consume(_LKLAMMER);
  347. in_args:=true;
  348. p1:=comp_expr(true);
  349. if token=_COMMA then
  350. begin
  351. consume(_COMMA);
  352. p2:=ccallparanode.create(comp_expr(true),nil);
  353. end
  354. else
  355. p2:=nil;
  356. p2:=ccallparanode.create(p1,p2);
  357. statement_syssym:=geninlinenode(l,false,p2);
  358. consume(_RKLAMMER);
  359. end;
  360. in_finalize_x:
  361. begin
  362. consume(_LKLAMMER);
  363. in_args:=true;
  364. p1:=comp_expr(true);
  365. if token=_COMMA then
  366. begin
  367. consume(_COMMA);
  368. p2:=ccallparanode.create(comp_expr(true),nil);
  369. end
  370. else
  371. p2:=nil;
  372. p2:=ccallparanode.create(p1,p2);
  373. statement_syssym:=geninlinenode(in_finalize_x,false,p2);
  374. consume(_RKLAMMER);
  375. end;
  376. in_concat_x :
  377. begin
  378. consume(_LKLAMMER);
  379. in_args:=true;
  380. p2:=nil;
  381. while true do
  382. begin
  383. p1:=comp_expr(true);
  384. set_varstate(p1,true);
  385. if not((p1.resulttype.def.deftype=stringdef) or
  386. ((p1.resulttype.def.deftype=orddef) and
  387. (torddef(p1.resulttype.def).typ=uchar))) then
  388. Message(parser_e_illegal_parameter_list);
  389. if p2<>nil then
  390. p2:=caddnode.create(addn,p2,p1)
  391. else
  392. p2:=p1;
  393. if token=_COMMA then
  394. consume(_COMMA)
  395. else
  396. break;
  397. end;
  398. consume(_RKLAMMER);
  399. statement_syssym:=p2;
  400. end;
  401. in_read_x,
  402. in_readln_x :
  403. begin
  404. if token=_LKLAMMER then
  405. begin
  406. consume(_LKLAMMER);
  407. in_args:=true;
  408. paras:=parse_paras(false,false);
  409. consume(_RKLAMMER);
  410. end
  411. else
  412. paras:=nil;
  413. p1:=geninlinenode(l,false,paras);
  414. statement_syssym := p1;
  415. end;
  416. in_setlength_x:
  417. begin
  418. if token=_LKLAMMER then
  419. begin
  420. consume(_LKLAMMER);
  421. in_args:=true;
  422. paras:=parse_paras(false,false);
  423. consume(_RKLAMMER);
  424. end
  425. else
  426. paras:=nil;
  427. p1:=geninlinenode(l,false,paras);
  428. statement_syssym := p1;
  429. end;
  430. in_length_x:
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true);
  435. p2:=geninlinenode(l,false,p1);
  436. consume(_RKLAMMER);
  437. statement_syssym:=p2;
  438. end;
  439. in_write_x,
  440. in_writeln_x :
  441. begin
  442. if token=_LKLAMMER then
  443. begin
  444. consume(_LKLAMMER);
  445. in_args:=true;
  446. paras:=parse_paras(true,false);
  447. consume(_RKLAMMER);
  448. end
  449. else
  450. paras:=nil;
  451. p1 := geninlinenode(l,false,paras);
  452. statement_syssym := p1;
  453. end;
  454. in_str_x_string :
  455. begin
  456. consume(_LKLAMMER);
  457. in_args:=true;
  458. paras:=parse_paras(true,false);
  459. consume(_RKLAMMER);
  460. p1 := geninlinenode(l,false,paras);
  461. statement_syssym := p1;
  462. end;
  463. in_val_x:
  464. Begin
  465. consume(_LKLAMMER);
  466. in_args := true;
  467. p1:= ccallparanode.create(comp_expr(true), nil);
  468. consume(_COMMA);
  469. p2 := ccallparanode.create(comp_expr(true),p1);
  470. if (token = _COMMA) then
  471. Begin
  472. consume(_COMMA);
  473. p2 := ccallparanode.create(comp_expr(true),p2)
  474. End;
  475. consume(_RKLAMMER);
  476. p2 := geninlinenode(l,false,p2);
  477. statement_syssym := p2;
  478. End;
  479. in_include_x_y,
  480. in_exclude_x_y :
  481. begin
  482. consume(_LKLAMMER);
  483. in_args:=true;
  484. p1:=comp_expr(true);
  485. consume(_COMMA);
  486. p2:=comp_expr(true);
  487. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  488. consume(_RKLAMMER);
  489. end;
  490. in_assert_x_y :
  491. begin
  492. consume(_LKLAMMER);
  493. in_args:=true;
  494. p1:=comp_expr(true);
  495. if token=_COMMA then
  496. begin
  497. consume(_COMMA);
  498. p2:=comp_expr(true);
  499. end
  500. else
  501. begin
  502. { then insert an empty string }
  503. p2:=cstringconstnode.createstr('',st_default);
  504. end;
  505. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  506. consume(_RKLAMMER);
  507. end;
  508. else
  509. internalerror(15);
  510. end;
  511. in_args:=prev_in_args;
  512. end;
  513. { reads the parameter for a subroutine call }
  514. procedure do_proc_call(sym:tsym;st:tsymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  515. var
  516. prevafterassn : boolean;
  517. hs,hs1 : tvarsym;
  518. para,p2 : tnode;
  519. hst : tsymtable;
  520. begin
  521. prevafterassn:=afterassignment;
  522. afterassignment:=false;
  523. { want we only determine the address of }
  524. { a subroutine ? }
  525. if not(getaddr) then
  526. begin
  527. para:=nil;
  528. if auto_inherited then
  529. begin
  530. hst:=symtablestack;
  531. while assigned(hst) and (hst.symtabletype<>parasymtable) do
  532. hst:=hst.next;
  533. if assigned(hst) then
  534. begin
  535. hs:=tvarsym(hst.symindex.first);
  536. while assigned(hs) do
  537. begin
  538. if hs.typ<>varsym then
  539. internalerror(54382953);
  540. { if there is a localcopy then use that }
  541. if assigned(hs.localvarsym) then
  542. hs1:=hs.localvarsym
  543. else
  544. hs1:=hs;
  545. para:=ccallparanode.create(cloadnode.create(hs1,hs1.owner),para);
  546. hs:=tvarsym(hs.indexnext);
  547. end;
  548. end
  549. else
  550. internalerror(54382954);
  551. end
  552. else
  553. begin
  554. if token=_LKLAMMER then
  555. begin
  556. consume(_LKLAMMER);
  557. para:=parse_paras(false,false);
  558. consume(_RKLAMMER);
  559. end;
  560. end;
  561. p1:=ccallnode.create(para,tprocsym(sym),st,p1);
  562. end
  563. else
  564. begin
  565. { address operator @: }
  566. if not assigned(p1) then
  567. begin
  568. if (st.symtabletype=withsymtable) and
  569. (st.defowner.deftype=objectdef) then
  570. begin
  571. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  572. end
  573. else
  574. begin
  575. { we must provide a method pointer, if it isn't given, }
  576. { it is self }
  577. if (st.symtabletype=objectsymtable) then
  578. p1:=cselfnode.create(tobjectdef(st.defowner));
  579. end;
  580. end;
  581. { generate a methodcallnode or proccallnode }
  582. { we shouldn't convert things like @tcollection.load }
  583. p2:=cloadnode.create(sym,st);
  584. if assigned(p1) then
  585. tloadnode(p2).set_mp(p1);
  586. p1:=p2;
  587. { no postfix operators }
  588. again:=false;
  589. end;
  590. afterassignment:=prevafterassn;
  591. end;
  592. procedure handle_procvar(pv : tprocvardef;var p2 : tnode; getaddr: boolean);
  593. procedure doconv(procvar : tprocvardef;var t : tnode);
  594. var
  595. hp : tnode;
  596. begin
  597. hp:=nil;
  598. if (proc_to_procvar_equal(tprocsym(tcallnode(t).symtableprocentry).definition,procvar)) then
  599. begin
  600. hp:=cloadnode.create(tprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc);
  601. if (po_methodpointer in procvar.procoptions) then
  602. tloadnode(hp).set_mp(tnode(tcallnode(t).methodpointer).getcopy);
  603. end;
  604. if assigned(hp) then
  605. begin
  606. t.destroy;
  607. t:=hp;
  608. end;
  609. end;
  610. begin
  611. if ((m_tp_procvar in aktmodeswitches) or
  612. not getaddr) then
  613. if (p2.nodetype=calln) then
  614. doconv(pv,p2)
  615. else
  616. if (p2.nodetype=typeconvn) and
  617. (ttypeconvnode(p2).left.nodetype=calln) then
  618. doconv(pv,ttypeconvnode(p2).left);
  619. end;
  620. { the following procedure handles the access to a property symbol }
  621. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode; getaddr: boolean);
  622. procedure symlist_to_node(var p1:tnode;pl:tsymlist);
  623. var
  624. plist : psymlistitem;
  625. begin
  626. plist:=pl.firstsym;
  627. while assigned(plist) do
  628. begin
  629. case plist^.sltype of
  630. sl_load :
  631. begin
  632. { p1 can already contain the loadnode of
  633. the class variable. Then we need to use a
  634. subscriptn. If no tree is found (with block), then
  635. generate a loadn }
  636. if assigned(p1) then
  637. p1:=csubscriptnode.create(tvarsym(plist^.sym),p1)
  638. else
  639. p1:=cloadnode.create(tvarsym(plist^.sym),st);
  640. end;
  641. sl_subscript :
  642. p1:=csubscriptnode.create(tvarsym(plist^.sym),p1);
  643. sl_vec :
  644. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32bittype));
  645. else
  646. internalerror(200110205);
  647. end;
  648. plist:=plist^.next;
  649. end;
  650. include(p1.flags,nf_isproperty);
  651. end;
  652. var
  653. paras : tnode;
  654. p2 : tnode;
  655. begin
  656. paras:=nil;
  657. { property parameters? read them only if the property really }
  658. { has parameters }
  659. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  660. begin
  661. if token=_LECKKLAMMER then
  662. begin
  663. consume(_LECKKLAMMER);
  664. paras:=parse_paras(false,true);
  665. consume(_RECKKLAMMER);
  666. end;
  667. end;
  668. { indexed property }
  669. if (ppo_indexed in tpropertysym(sym).propoptions) then
  670. begin
  671. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype);
  672. paras:=ccallparanode.create(p2,paras);
  673. end;
  674. { we need only a write property if a := follows }
  675. { if not(afterassignment) and not(in_args) then }
  676. if token=_ASSIGNMENT then
  677. begin
  678. { write property: }
  679. if not tpropertysym(sym).writeaccess.empty then
  680. begin
  681. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  682. procsym :
  683. begin
  684. { generate the method call }
  685. p1:=ccallnode.create(paras,
  686. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  687. paras:=nil;
  688. consume(_ASSIGNMENT);
  689. { read the expression }
  690. getprocvar:=(tpropertysym(sym).proptype.def.deftype=procvardef);
  691. p2:=comp_expr(true);
  692. if getprocvar then
  693. handle_procvar(tprocvardef(tpropertysym(sym).proptype.def),p2,getaddr);
  694. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  695. include(tcallnode(p1).flags,nf_isproperty);
  696. getprocvar:=false;
  697. end;
  698. varsym :
  699. begin
  700. { generate access code }
  701. symlist_to_node(p1,tpropertysym(sym).writeaccess);
  702. consume(_ASSIGNMENT);
  703. { read the expression }
  704. p2:=comp_expr(true);
  705. p1:=cassignmentnode.create(p1,p2);
  706. end
  707. else
  708. begin
  709. p1:=cerrornode.create;
  710. Message(parser_e_no_procedure_to_access_property);
  711. end;
  712. end;
  713. end
  714. else
  715. begin
  716. p1:=cerrornode.create;
  717. Message(parser_e_no_procedure_to_access_property);
  718. end;
  719. end
  720. else
  721. begin
  722. { read property: }
  723. if not tpropertysym(sym).readaccess.empty then
  724. begin
  725. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  726. varsym :
  727. begin
  728. { generate access code }
  729. symlist_to_node(p1,tpropertysym(sym).readaccess);
  730. end;
  731. procsym :
  732. begin
  733. { generate the method call }
  734. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  735. paras:=nil;
  736. include(p1.flags,nf_isproperty);
  737. end
  738. else
  739. begin
  740. p1:=cerrornode.create;
  741. Message(type_e_mismatch);
  742. end;
  743. end;
  744. end
  745. else
  746. begin
  747. { error, no function to read property }
  748. p1:=cerrornode.create;
  749. Message(parser_e_no_procedure_to_access_property);
  750. end;
  751. end;
  752. { release paras if not used }
  753. if assigned(paras) then
  754. paras.free;
  755. end;
  756. { the ID token has to be consumed before calling this function }
  757. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  758. var
  759. static_name : string;
  760. isclassref : boolean;
  761. srsymtable : tsymtable;
  762. objdef : tobjectdef;
  763. begin
  764. if sym=nil then
  765. begin
  766. { pattern is still valid unless
  767. there is another ID just after the ID of sym }
  768. Message1(sym_e_id_no_member,pattern);
  769. p1.free;
  770. p1:=cerrornode.create;
  771. { try to clean up }
  772. again:=false;
  773. end
  774. else
  775. begin
  776. if assigned(p1) then
  777. begin
  778. if not assigned(p1.resulttype.def) then
  779. do_resulttypepass(p1);
  780. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  781. end
  782. else
  783. isclassref:=false;
  784. objdef:=tobjectdef(sym.owner.defowner);
  785. { check protected and private members }
  786. { please leave this code as it is, }
  787. { it has now the same behaviaor as TP/Delphi }
  788. if (sp_private in sym.symoptions) and
  789. (objdef.owner.symtabletype=globalsymtable) and
  790. (objdef.owner.unitid<>0) then
  791. Message(parser_e_cant_access_private_member);
  792. if (sp_protected in sym.symoptions) and
  793. (objdef.owner.symtabletype=globalsymtable) and
  794. (objdef.owner.unitid<>0) then
  795. begin
  796. if assigned(aktprocsym.definition._class) then
  797. begin
  798. if not aktprocsym.definition._class.is_related(objdef) then
  799. Message(parser_e_cant_access_protected_member);
  800. end
  801. else
  802. Message(parser_e_cant_access_protected_member);
  803. end;
  804. { we assume, that only procsyms and varsyms are in an object }
  805. { symbol table, for classes, properties are allowed }
  806. case sym.typ of
  807. procsym:
  808. begin
  809. do_proc_call(sym,sym.owner,
  810. getaddr or
  811. (getprocvar and
  812. ((block_type=bt_const) or
  813. ((m_tp_procvar in aktmodeswitches) and
  814. proc_to_procvar_equal(tprocsym(sym).definition,getprocvardef)
  815. )
  816. )
  817. ),again,p1);
  818. if (block_type=bt_const) and
  819. getprocvar then
  820. handle_procvar(getprocvardef,p1,getaddr);
  821. { we need to know which procedure is called }
  822. do_resulttypepass(p1);
  823. { now we know the real method e.g. we can check for a class method }
  824. if isclassref and
  825. assigned(tcallnode(p1).procdefinition) and
  826. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  827. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  828. Message(parser_e_only_class_methods_via_class_ref);
  829. end;
  830. varsym:
  831. begin
  832. if isclassref then
  833. Message(parser_e_only_class_methods_via_class_ref);
  834. if (sp_static in sym.symoptions) then
  835. begin
  836. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  837. searchsym(static_name,sym,srsymtable);
  838. p1.free;
  839. p1:=cloadnode.create(tvarsym(sym),srsymtable);
  840. end
  841. else
  842. p1:=csubscriptnode.create(tvarsym(sym),p1);
  843. end;
  844. propertysym:
  845. begin
  846. if isclassref then
  847. Message(parser_e_only_class_methods_via_class_ref);
  848. handle_propertysym(sym,sym.owner,p1,getaddr);
  849. end;
  850. else internalerror(16);
  851. end;
  852. end;
  853. end;
  854. {****************************************************************************
  855. Factor
  856. ****************************************************************************}
  857. {$ifdef fpc}
  858. {$maxfpuregisters 0}
  859. {$endif fpc}
  860. function factor(getaddr : boolean) : tnode;
  861. {---------------------------------------------
  862. Is_func_ret
  863. ---------------------------------------------}
  864. function is_func_ret(var p1:tnode;var sym : tsym;var srsymtable:tsymtable) : boolean;
  865. var
  866. p : pprocinfo;
  867. storesymtablestack : tsymtable;
  868. begin
  869. is_func_ret:=false;
  870. if not assigned(procinfo) or
  871. ((sym.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  872. exit;
  873. p:=procinfo;
  874. while assigned(p) do
  875. begin
  876. { is this an access to a function result? Accessing _RESULT is
  877. always allowed and funcretn is generated }
  878. if assigned(p^.procdef.funcretsym) and
  879. ((tfuncretsym(sym)=p^.procdef.resultfuncretsym) or
  880. ((tfuncretsym(sym)=p^.procdef.funcretsym) or
  881. ((tvarsym(sym)=otsym) and ((p^.flags and pi_operator)<>0))) and
  882. (not is_void(p^.procdef.rettype.def)) and
  883. (token<>_LKLAMMER) and
  884. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  885. ) then
  886. begin
  887. if ((tvarsym(sym)=otsym) and
  888. ((p^.flags and pi_operator)<>0)) then
  889. inc(otsym.refs);
  890. p1:=cfuncretnode.create(p^.procdef.funcretsym);
  891. is_func_ret:=true;
  892. if tfuncretsym(p^.procdef.funcretsym).funcretstate=vs_declared then
  893. begin
  894. tfuncretsym(p^.procdef.funcretsym).funcretstate:=vs_declared_and_first_found;
  895. include(p1.flags,nf_is_first_funcret);
  896. end;
  897. exit;
  898. end;
  899. p:=p^.parent;
  900. end;
  901. { we must use the function call, update the
  902. sym to be the procsym }
  903. if (sym.typ=funcretsym) then
  904. begin
  905. storesymtablestack:=symtablestack;
  906. symtablestack:=sym.owner.next;
  907. searchsym(sym.name,sym,srsymtable);
  908. if not assigned(sym) then
  909. sym:=generrorsym;
  910. if (sym.typ<>procsym) then
  911. Message(cg_e_illegal_expression);
  912. symtablestack:=storesymtablestack;
  913. end;
  914. end;
  915. {---------------------------------------------
  916. Factor_read_id
  917. ---------------------------------------------}
  918. procedure factor_read_id(var p1:tnode;var again:boolean);
  919. var
  920. pc : pchar;
  921. len : longint;
  922. srsym : tsym;
  923. possible_error : boolean;
  924. srsymtable : tsymtable;
  925. htype : ttype;
  926. static_name : string;
  927. begin
  928. { allow post fix operators }
  929. again:=true;
  930. consume_sym(srsym,srsymtable);
  931. if not is_func_ret(p1,srsym,srsymtable) then
  932. begin
  933. { check semantics of private }
  934. if (srsym.typ in [propertysym,procsym,varsym]) and
  935. (srsym.owner.symtabletype=objectsymtable) then
  936. begin
  937. if (sp_private in srsym.symoptions) and
  938. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  939. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  940. Message(parser_e_cant_access_private_member);
  941. end;
  942. case srsym.typ of
  943. absolutesym :
  944. begin
  945. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  946. end;
  947. varsym :
  948. begin
  949. { are we in a class method ? }
  950. if (srsym.owner.symtabletype=objectsymtable) and
  951. assigned(aktprocsym) and
  952. (po_classmethod in aktprocsym.definition.procoptions) then
  953. Message(parser_e_only_class_methods);
  954. if (sp_static in srsym.symoptions) then
  955. begin
  956. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  957. searchsym(static_name,srsym,srsymtable);
  958. end;
  959. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  960. if tvarsym(srsym).varstate=vs_declared then
  961. begin
  962. include(p1.flags,nf_first);
  963. { set special between first loaded until checked in resulttypepass }
  964. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  965. end;
  966. end;
  967. typedconstsym :
  968. begin
  969. p1:=cloadnode.create(srsym,srsymtable);
  970. end;
  971. syssym :
  972. p1:=statement_syssym(tsyssym(srsym).number);
  973. typesym :
  974. begin
  975. htype.setsym(srsym);
  976. if not assigned(htype.def) then
  977. begin
  978. again:=false;
  979. end
  980. else
  981. begin
  982. if token=_LKLAMMER then
  983. begin
  984. consume(_LKLAMMER);
  985. p1:=comp_expr(true);
  986. consume(_RKLAMMER);
  987. p1:=ctypeconvnode.create(p1,htype);
  988. include(p1.flags,nf_explizit);
  989. end
  990. else { not LKLAMMER }
  991. if (token=_POINT) and
  992. is_object(htype.def) then
  993. begin
  994. consume(_POINT);
  995. if assigned(procinfo) and
  996. assigned(procinfo^._class) and
  997. not(getaddr) then
  998. begin
  999. if procinfo^._class.is_related(tobjectdef(htype.def)) then
  1000. begin
  1001. p1:=ctypenode.create(htype);
  1002. { search also in inherited methods }
  1003. repeat
  1004. srsym:=tvarsym(tobjectdef(htype.def).symtable.search(pattern));
  1005. if assigned(srsym) then
  1006. break;
  1007. htype.def:=tobjectdef(htype.def).childof;
  1008. until not assigned(htype.def);
  1009. consume(_ID);
  1010. do_member_read(false,srsym,p1,again);
  1011. end
  1012. else
  1013. begin
  1014. Message(parser_e_no_super_class);
  1015. again:=false;
  1016. end;
  1017. end
  1018. else
  1019. begin
  1020. { allows @TObject.Load }
  1021. { also allows static methods and variables }
  1022. p1:=ctypenode.create(htype);
  1023. { TP allows also @TMenu.Load if Load is only }
  1024. { defined in an anchestor class }
  1025. srsym:=tvarsym(search_class_member(tobjectdef(htype.def),pattern));
  1026. if not assigned(srsym) then
  1027. Message1(sym_e_id_no_member,pattern)
  1028. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1029. Message(sym_e_only_static_in_static)
  1030. else
  1031. begin
  1032. consume(_ID);
  1033. do_member_read(getaddr,srsym,p1,again);
  1034. end;
  1035. end;
  1036. end
  1037. else
  1038. begin
  1039. { class reference ? }
  1040. if is_class(htype.def) then
  1041. begin
  1042. if getaddr and (token=_POINT) then
  1043. begin
  1044. consume(_POINT);
  1045. { allows @Object.Method }
  1046. { also allows static methods and variables }
  1047. p1:=ctypenode.create(htype);
  1048. { TP allows also @TMenu.Load if Load is only }
  1049. { defined in an anchestor class }
  1050. srsym:=tvarsym(search_class_member(tobjectdef(htype.def),pattern));
  1051. if not assigned(srsym) then
  1052. Message1(sym_e_id_no_member,pattern)
  1053. else
  1054. begin
  1055. consume(_ID);
  1056. do_member_read(getaddr,srsym,p1,again);
  1057. end;
  1058. end
  1059. else
  1060. begin
  1061. p1:=ctypenode.create(htype);
  1062. { For a type block we simply return only
  1063. the type. For all other blocks we return
  1064. a loadvmt node }
  1065. if (block_type<>bt_type) then
  1066. p1:=cloadvmtnode.create(p1);
  1067. end;
  1068. end
  1069. else
  1070. p1:=ctypenode.create(htype);
  1071. end;
  1072. end;
  1073. end;
  1074. enumsym :
  1075. begin
  1076. p1:=genenumnode(tenumsym(srsym));
  1077. end;
  1078. constsym :
  1079. begin
  1080. case tconstsym(srsym).consttyp of
  1081. constint :
  1082. begin
  1083. { do a very dirty trick to bootstrap this code }
  1084. if (tconstsym(srsym).valueord>=-(int64(2147483647)+int64(1))) and
  1085. (tconstsym(srsym).valueord<=2147483647) then
  1086. p1:=cordconstnode.create(tconstsym(srsym).valueord,s32bittype)
  1087. else if (tconstsym(srsym).valueord > maxlongint) and
  1088. (tconstsym(srsym).valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1089. p1:=cordconstnode.create(tconstsym(srsym).valueord,u32bittype)
  1090. else
  1091. p1:=cordconstnode.create(tconstsym(srsym).valueord,cs64bittype);
  1092. end;
  1093. conststring :
  1094. begin
  1095. len:=tconstsym(srsym).len;
  1096. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1097. len:=255;
  1098. getmem(pc,len+1);
  1099. move(pchar(tconstsym(srsym).valueptr)^,pc^,len);
  1100. pc[len]:=#0;
  1101. p1:=cstringconstnode.createpchar(pc,len);
  1102. end;
  1103. constchar :
  1104. p1:=cordconstnode.create(tconstsym(srsym).valueord,cchartype);
  1105. constreal :
  1106. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).valueptr)^,pbestrealtype^);
  1107. constbool :
  1108. p1:=cordconstnode.create(tconstsym(srsym).valueord,booltype);
  1109. constset :
  1110. p1:=csetconstnode.create(pconstset(tconstsym(srsym).valueptr),tconstsym(srsym).consttype);
  1111. constord :
  1112. p1:=cordconstnode.create(tconstsym(srsym).valueord,tconstsym(srsym).consttype);
  1113. constpointer :
  1114. p1:=cpointerconstnode.create(tconstsym(srsym).valueordptr,tconstsym(srsym).consttype);
  1115. constnil :
  1116. p1:=cnilnode.create;
  1117. constresourcestring:
  1118. begin
  1119. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  1120. do_resulttypepass(p1);
  1121. p1.resulttype:=cansistringtype;
  1122. end;
  1123. constguid :
  1124. p1:=cguidconstnode.create(pguid(tconstsym(srsym).valueptr)^);
  1125. end;
  1126. end;
  1127. procsym :
  1128. begin
  1129. { are we in a class method ? }
  1130. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1131. assigned(aktprocsym) and
  1132. (po_classmethod in aktprocsym.definition.procoptions);
  1133. do_proc_call(srsym,srsymtable,
  1134. getaddr or
  1135. (getprocvar and
  1136. ((block_type=bt_const) or
  1137. ((m_tp_procvar in aktmodeswitches) and
  1138. proc_to_procvar_equal(tprocsym(srsym).definition,getprocvardef)
  1139. )
  1140. )
  1141. ),again,p1);
  1142. if (block_type=bt_const) and
  1143. getprocvar then
  1144. handle_procvar(getprocvardef,p1,getaddr);
  1145. { we need to know which procedure is called }
  1146. if possible_error then
  1147. begin
  1148. do_resulttypepass(p1);
  1149. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1150. Message(parser_e_only_class_methods);
  1151. end;
  1152. end;
  1153. propertysym :
  1154. begin
  1155. { access to property in a method }
  1156. { are we in a class method ? }
  1157. if (srsym.owner.symtabletype=objectsymtable) and
  1158. assigned(aktprocsym) and
  1159. (po_classmethod in aktprocsym.definition.procoptions) then
  1160. Message(parser_e_only_class_methods);
  1161. { no method pointer }
  1162. p1:=nil;
  1163. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1164. end;
  1165. labelsym :
  1166. begin
  1167. consume(_COLON);
  1168. if tlabelsym(srsym).defined then
  1169. Message(sym_e_label_already_defined);
  1170. tlabelsym(srsym).defined:=true;
  1171. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1172. end;
  1173. errorsym :
  1174. begin
  1175. p1:=cerrornode.create;
  1176. if token=_LKLAMMER then
  1177. begin
  1178. consume(_LKLAMMER);
  1179. parse_paras(false,false);
  1180. consume(_RKLAMMER);
  1181. end;
  1182. end;
  1183. else
  1184. begin
  1185. p1:=cerrornode.create;
  1186. Message(cg_e_illegal_expression);
  1187. end;
  1188. end; { end case }
  1189. end;
  1190. end;
  1191. {---------------------------------------------
  1192. Factor_Read_Set
  1193. ---------------------------------------------}
  1194. { Read a set between [] }
  1195. function factor_read_set:tnode;
  1196. var
  1197. p1,p2 : tnode;
  1198. lastp,
  1199. buildp : tarrayconstructornode;
  1200. begin
  1201. buildp:=nil;
  1202. { be sure that a least one arrayconstructn is used, also for an
  1203. empty [] }
  1204. if token=_RECKKLAMMER then
  1205. buildp:=carrayconstructornode.create(nil,buildp)
  1206. else
  1207. begin
  1208. while true do
  1209. begin
  1210. p1:=comp_expr(true);
  1211. if token=_POINTPOINT then
  1212. begin
  1213. consume(_POINTPOINT);
  1214. p2:=comp_expr(true);
  1215. p1:=carrayconstructorrangenode.create(p1,p2);
  1216. end;
  1217. { insert at the end of the tree, to get the correct order }
  1218. if not assigned(buildp) then
  1219. begin
  1220. buildp:=carrayconstructornode.create(p1,nil);
  1221. lastp:=buildp;
  1222. end
  1223. else
  1224. begin
  1225. lastp.right:=carrayconstructornode.create(p1,nil);
  1226. lastp:=tarrayconstructornode(lastp.right);
  1227. end;
  1228. { there could be more elements }
  1229. if token=_COMMA then
  1230. consume(_COMMA)
  1231. else
  1232. break;
  1233. end;
  1234. end;
  1235. factor_read_set:=buildp;
  1236. end;
  1237. {---------------------------------------------
  1238. PostFixOperators
  1239. ---------------------------------------------}
  1240. procedure postfixoperators(var p1:tnode;var again:boolean);
  1241. { tries to avoid syntax errors after invalid qualifiers }
  1242. procedure recoverconsume_postfixops;
  1243. begin
  1244. while true do
  1245. begin
  1246. case token of
  1247. _CARET:
  1248. consume(_CARET);
  1249. _POINT:
  1250. begin
  1251. consume(_POINT);
  1252. if token=_ID then
  1253. consume(_ID);
  1254. end;
  1255. _LECKKLAMMER:
  1256. begin
  1257. consume(_LECKKLAMMER);
  1258. repeat
  1259. comp_expr(true);
  1260. if token=_COMMA then
  1261. consume(_COMMA)
  1262. else
  1263. break;
  1264. until false;
  1265. consume(_RECKKLAMMER);
  1266. end
  1267. else
  1268. break;
  1269. end;
  1270. end;
  1271. end;
  1272. var
  1273. store_static : boolean;
  1274. protsym : tpropertysym;
  1275. p2,p3 : tnode;
  1276. hsym : tsym;
  1277. classh : tobjectdef;
  1278. begin
  1279. again:=true;
  1280. while again do
  1281. begin
  1282. { we need the resulttype }
  1283. do_resulttypepass(p1);
  1284. if codegenerror then
  1285. begin
  1286. recoverconsume_postfixops;
  1287. exit;
  1288. end;
  1289. { handle token }
  1290. case token of
  1291. _CARET:
  1292. begin
  1293. consume(_CARET);
  1294. if (p1.resulttype.def.deftype<>pointerdef) then
  1295. begin
  1296. { ^ as binary operator is a problem!!!! (FK) }
  1297. again:=false;
  1298. Message(cg_e_invalid_qualifier);
  1299. recoverconsume_postfixops;
  1300. p1.destroy;
  1301. p1:=cerrornode.create;
  1302. end
  1303. else
  1304. begin
  1305. p1:=cderefnode.create(p1);
  1306. end;
  1307. end;
  1308. _LECKKLAMMER:
  1309. begin
  1310. if is_class_or_interface(p1.resulttype.def) then
  1311. begin
  1312. { default property }
  1313. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1314. if not(assigned(protsym)) then
  1315. begin
  1316. p1.destroy;
  1317. p1:=cerrornode.create;
  1318. again:=false;
  1319. message(parser_e_no_default_property_available);
  1320. end
  1321. else
  1322. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1323. end
  1324. else
  1325. begin
  1326. consume(_LECKKLAMMER);
  1327. repeat
  1328. case p1.resulttype.def.deftype of
  1329. pointerdef:
  1330. begin
  1331. { support delphi autoderef }
  1332. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1333. (m_autoderef in aktmodeswitches) then
  1334. begin
  1335. p1:=cderefnode.create(p1);
  1336. end;
  1337. p2:=comp_expr(true);
  1338. p1:=cvecnode.create(p1,p2);
  1339. end;
  1340. stringdef :
  1341. begin
  1342. p2:=comp_expr(true);
  1343. p1:=cvecnode.create(p1,p2);
  1344. end;
  1345. arraydef :
  1346. begin
  1347. p2:=comp_expr(true);
  1348. { support SEG:OFS for go32v2 Mem[] }
  1349. if (target_info.target=target_i386_go32v2) and
  1350. (p1.nodetype=loadn) and
  1351. assigned(tloadnode(p1).symtableentry) and
  1352. assigned(tloadnode(p1).symtableentry.owner.name) and
  1353. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1354. ((tloadnode(p1).symtableentry.name='MEM') or
  1355. (tloadnode(p1).symtableentry.name='MEMW') or
  1356. (tloadnode(p1).symtableentry.name='MEML')) then
  1357. begin
  1358. if (token=_COLON) then
  1359. begin
  1360. consume(_COLON);
  1361. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype),p2);
  1362. p2:=comp_expr(true);
  1363. p2:=caddnode.create(addn,p2,p3);
  1364. p1:=cvecnode.create(p1,p2);
  1365. include(tvecnode(p1).flags,nf_memseg);
  1366. include(tvecnode(p1).flags,nf_memindex);
  1367. end
  1368. else
  1369. begin
  1370. p1:=cvecnode.create(p1,p2);
  1371. include(tvecnode(p1).flags,nf_memindex);
  1372. end;
  1373. end
  1374. else
  1375. p1:=cvecnode.create(p1,p2);
  1376. end;
  1377. else
  1378. begin
  1379. Message(cg_e_invalid_qualifier);
  1380. p1.destroy;
  1381. p1:=cerrornode.create;
  1382. comp_expr(true);
  1383. again:=false;
  1384. end;
  1385. end;
  1386. do_resulttypepass(p1);
  1387. if token=_COMMA then
  1388. consume(_COMMA)
  1389. else
  1390. break;
  1391. until false;
  1392. consume(_RECKKLAMMER);
  1393. end;
  1394. end;
  1395. _POINT :
  1396. begin
  1397. consume(_POINT);
  1398. if (p1.resulttype.def.deftype=pointerdef) and
  1399. (m_autoderef in aktmodeswitches) then
  1400. begin
  1401. p1:=cderefnode.create(p1);
  1402. do_resulttypepass(p1);
  1403. end;
  1404. case p1.resulttype.def.deftype of
  1405. recorddef:
  1406. begin
  1407. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1408. if assigned(hsym) and
  1409. (hsym.typ=varsym) then
  1410. p1:=csubscriptnode.create(tvarsym(hsym),p1)
  1411. else
  1412. begin
  1413. Message1(sym_e_illegal_field,pattern);
  1414. p1.destroy;
  1415. p1:=cerrornode.create;
  1416. end;
  1417. consume(_ID);
  1418. end;
  1419. classrefdef:
  1420. begin
  1421. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1422. hsym:=nil;
  1423. while assigned(classh) do
  1424. begin
  1425. hsym:=tsym(classh.symtable.search(pattern));
  1426. if assigned(hsym) then
  1427. break;
  1428. classh:=classh.childof;
  1429. end;
  1430. if hsym=nil then
  1431. begin
  1432. Message1(sym_e_id_no_member,pattern);
  1433. p1.destroy;
  1434. p1:=cerrornode.create;
  1435. { try to clean up }
  1436. consume(_ID);
  1437. end
  1438. else
  1439. begin
  1440. consume(_ID);
  1441. do_member_read(getaddr,hsym,p1,again);
  1442. end;
  1443. end;
  1444. objectdef:
  1445. begin
  1446. classh:=tobjectdef(p1.resulttype.def);
  1447. hsym:=nil;
  1448. store_static:=allow_only_static;
  1449. allow_only_static:=false;
  1450. while assigned(classh) do
  1451. begin
  1452. hsym:=tsym(classh.symtable.search(pattern));
  1453. if assigned(hsym) then
  1454. break;
  1455. classh:=classh.childof;
  1456. end;
  1457. allow_only_static:=store_static;
  1458. if hsym=nil then
  1459. begin
  1460. Message1(sym_e_id_no_member,pattern);
  1461. p1.destroy;
  1462. p1:=cerrornode.create;
  1463. { try to clean up }
  1464. consume(_ID);
  1465. end
  1466. else
  1467. begin
  1468. consume(_ID);
  1469. do_member_read(getaddr,hsym,p1,again);
  1470. end;
  1471. end;
  1472. pointerdef:
  1473. begin
  1474. Message(cg_e_invalid_qualifier);
  1475. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1476. Message(parser_h_maybe_deref_caret_missing);
  1477. end;
  1478. else
  1479. begin
  1480. Message(cg_e_invalid_qualifier);
  1481. p1.destroy;
  1482. p1:=cerrornode.create;
  1483. consume(_ID);
  1484. end;
  1485. end;
  1486. end;
  1487. else
  1488. begin
  1489. { is this a procedure variable ? }
  1490. if assigned(p1.resulttype.def) then
  1491. begin
  1492. if (p1.resulttype.def.deftype=procvardef) then
  1493. begin
  1494. if getprocvar and is_equal(p1.resulttype.def,getprocvardef) then
  1495. again:=false
  1496. else
  1497. if (token=_LKLAMMER) or
  1498. ((tprocvardef(p1.resulttype.def).para.empty) and
  1499. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1500. (not afterassignment) and
  1501. (not in_args)) then
  1502. begin
  1503. { do this in a strange way }
  1504. { it's not a clean solution }
  1505. p2:=p1;
  1506. p1:=ccallnode.create(nil,nil,nil,nil);
  1507. tcallnode(p1).set_procvar(p2);
  1508. {$ifdef TEST_PROCSYMS}
  1509. p1.unit_specific:=unit_specific;
  1510. {$endif TEST_PROCSYMS}
  1511. {tcallnode(p1).symtableprocentry:=tprocsym(sym);}
  1512. if token=_LKLAMMER then
  1513. begin
  1514. consume(_LKLAMMER);
  1515. tcallnode(p1).left:=parse_paras(false,false);
  1516. consume(_RKLAMMER);
  1517. end;
  1518. { proc():= is never possible }
  1519. if token=_ASSIGNMENT then
  1520. begin
  1521. Message(cg_e_illegal_expression);
  1522. p1:=cerrornode.create;
  1523. again:=false;
  1524. end;
  1525. end
  1526. else
  1527. again:=false;
  1528. end
  1529. else
  1530. again:=false;
  1531. end
  1532. else
  1533. again:=false;
  1534. end;
  1535. end;
  1536. end; { while again }
  1537. end;
  1538. {---------------------------------------------
  1539. Factor (Main)
  1540. ---------------------------------------------}
  1541. var
  1542. l : longint;
  1543. card : cardinal;
  1544. ic : TConstExprInt;
  1545. oldp1,
  1546. p1,p2 : tnode;
  1547. code : integer;
  1548. {$ifdef TEST_PROCSYMS}
  1549. unit_specific,
  1550. {$endif TEST_PROCSYMS}
  1551. again : boolean;
  1552. sym : tsym;
  1553. classh : tobjectdef;
  1554. d : bestreal;
  1555. hs : string;
  1556. htype : ttype;
  1557. filepos : tfileposinfo;
  1558. {---------------------------------------------
  1559. Helpers
  1560. ---------------------------------------------}
  1561. procedure check_tokenpos;
  1562. begin
  1563. if (p1<>oldp1) then
  1564. begin
  1565. if assigned(p1) then
  1566. p1.set_tree_filepos(filepos);
  1567. oldp1:=p1;
  1568. filepos:=akttokenpos;
  1569. end;
  1570. end;
  1571. begin
  1572. oldp1:=nil;
  1573. p1:=nil;
  1574. filepos:=akttokenpos;
  1575. again:=false;
  1576. if token=_ID then
  1577. begin
  1578. if idtoken=_NEW then
  1579. begin
  1580. consume(_NEW);
  1581. consume(_LKLAMMER);
  1582. p1:=factor(false);
  1583. if p1.nodetype<>typen then
  1584. begin
  1585. Message(type_e_type_id_expected);
  1586. p1.destroy;
  1587. p1:=cerrornode.create;
  1588. do_resulttypepass(p1);
  1589. end;
  1590. if (p1.resulttype.def.deftype<>pointerdef) then
  1591. Message1(type_e_pointer_type_expected,p1.resulttype.def.typename)
  1592. else
  1593. if token=_RKLAMMER then
  1594. begin
  1595. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=objectdef) and
  1596. (oo_has_vmt in tobjectdef(tpointerdef(p1.resulttype.def).pointertype.def).objectoptions) then
  1597. Message(parser_w_use_extended_syntax_for_objects);
  1598. p2:=cnewnode.create(nil);
  1599. do_resulttypepass(p2);
  1600. p2.resulttype:=p1.resulttype;
  1601. p1.destroy;
  1602. p1:=p2;
  1603. consume(_RKLAMMER);
  1604. end
  1605. else
  1606. begin
  1607. p2:=chnewnode.create;
  1608. do_resulttypepass(p2);
  1609. p2.resulttype:=tpointerdef(p1.resulttype.def).pointertype;
  1610. consume(_COMMA);
  1611. afterassignment:=false;
  1612. { determines the current object defintion }
  1613. classh:=tobjectdef(p2.resulttype.def);
  1614. if classh.deftype<>objectdef then
  1615. Message(parser_e_pointer_to_class_expected)
  1616. else
  1617. begin
  1618. { check for an abstract class }
  1619. if (oo_has_abstract in classh.objectoptions) then
  1620. Message(sym_e_no_instance_of_abstract_object);
  1621. { search the constructor also in the symbol tables of
  1622. the parents }
  1623. sym:=nil;
  1624. while assigned(classh) do
  1625. begin
  1626. sym:=tsym(classh.symtable.search(pattern));
  1627. if assigned(sym) then
  1628. break;
  1629. classh:=classh.childof;
  1630. end;
  1631. consume(_ID);
  1632. do_member_read(false,sym,p2,again);
  1633. { we need to know which procedure is called }
  1634. do_resulttypepass(p2);
  1635. if (p2.nodetype<>calln) or
  1636. (assigned(tcallnode(p2).procdefinition) and
  1637. (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor)) then
  1638. Message(parser_e_expr_have_to_be_constructor_call);
  1639. end;
  1640. p2:=cnewnode.create(p2);
  1641. do_resulttypepass(p2);
  1642. p2.resulttype:=p1.resulttype;
  1643. p1.destroy;
  1644. p1:=p2;
  1645. consume(_RKLAMMER);
  1646. end;
  1647. postfixoperators(p1,again);
  1648. end
  1649. else
  1650. begin
  1651. factor_read_id(p1,again);
  1652. if again then
  1653. begin
  1654. check_tokenpos;
  1655. { handle post fix operators }
  1656. postfixoperators(p1,again);
  1657. end;
  1658. end;
  1659. end
  1660. else
  1661. case token of
  1662. _SELF :
  1663. begin
  1664. again:=true;
  1665. consume(_SELF);
  1666. if not assigned(procinfo^._class) then
  1667. begin
  1668. p1:=cerrornode.create;
  1669. again:=false;
  1670. Message(parser_e_self_not_in_method);
  1671. end
  1672. else
  1673. begin
  1674. if (po_classmethod in aktprocsym.definition.procoptions) then
  1675. begin
  1676. { self in class methods is a class reference type }
  1677. htype.setdef(procinfo^._class);
  1678. p1:=cselfnode.create(tobjectdef(tclassrefdef.create(htype)));
  1679. end
  1680. else
  1681. p1:=cselfnode.create(procinfo^._class);
  1682. postfixoperators(p1,again);
  1683. end;
  1684. end;
  1685. _INHERITED :
  1686. begin
  1687. again:=true;
  1688. consume(_INHERITED);
  1689. if assigned(procinfo^._class) then
  1690. begin
  1691. { if inherited; only then we need the method with
  1692. the same name }
  1693. if token=_SEMICOLON then
  1694. begin
  1695. hs:=aktprocsym.name;
  1696. auto_inherited:=true
  1697. end
  1698. else
  1699. begin
  1700. hs:=pattern;
  1701. consume(_ID);
  1702. auto_inherited:=false;
  1703. end;
  1704. classh:=procinfo^._class.childof;
  1705. while assigned(classh) do
  1706. begin
  1707. sym:=tsym(tobjectdef(classh).symtable.search(hs));
  1708. if assigned(sym) then
  1709. begin
  1710. if sym.typ=procsym then
  1711. begin
  1712. htype.setdef(classh);
  1713. p1:=ctypenode.create(htype);
  1714. end;
  1715. do_member_read(false,sym,p1,again);
  1716. break;
  1717. end;
  1718. classh:=classh.childof;
  1719. end;
  1720. if classh=nil then
  1721. begin
  1722. Message1(sym_e_id_no_member,hs);
  1723. again:=false;
  1724. p1:=cerrornode.create;
  1725. end;
  1726. { turn auto inheriting off }
  1727. auto_inherited:=false;
  1728. end
  1729. else
  1730. begin
  1731. Message(parser_e_generic_methods_only_in_methods);
  1732. again:=false;
  1733. p1:=cerrornode.create;
  1734. end;
  1735. postfixoperators(p1,again);
  1736. end;
  1737. _INTCONST :
  1738. begin
  1739. { try cardinal first }
  1740. val(pattern,card,code);
  1741. if code<>0 then
  1742. begin
  1743. { then longint }
  1744. valint(pattern,l,code);
  1745. if code <> 0 then
  1746. begin
  1747. { then int64 }
  1748. val(pattern,ic,code);
  1749. if code<>0 then
  1750. begin
  1751. {finally float }
  1752. val(pattern,d,code);
  1753. if code<>0 then
  1754. begin
  1755. Message(cg_e_invalid_integer);
  1756. consume(_INTCONST);
  1757. l:=1;
  1758. p1:=cordconstnode.create(l,s32bittype);
  1759. end
  1760. else
  1761. begin
  1762. consume(_INTCONST);
  1763. p1:=crealconstnode.create(d,pbestrealtype^);
  1764. end;
  1765. end
  1766. else
  1767. begin
  1768. consume(_INTCONST);
  1769. p1:=cordconstnode.create(ic,cs64bittype);
  1770. end
  1771. end
  1772. else
  1773. begin
  1774. consume(_INTCONST);
  1775. p1:=cordconstnode.create(l,s32bittype)
  1776. end
  1777. end
  1778. else
  1779. begin
  1780. consume(_INTCONST);
  1781. { check whether the value isn't in the longint range as well }
  1782. { (longint is easier to perform calculations with) (JM) }
  1783. if card <= $7fffffff then
  1784. { no sign extension necessary, so not longint typecast (JM) }
  1785. p1:=cordconstnode.create(card,s32bittype)
  1786. else
  1787. p1:=cordconstnode.create(card,u32bittype)
  1788. end;
  1789. end;
  1790. _REALNUMBER :
  1791. begin
  1792. val(pattern,d,code);
  1793. if code<>0 then
  1794. begin
  1795. Message(parser_e_error_in_real);
  1796. d:=1.0;
  1797. end;
  1798. consume(_REALNUMBER);
  1799. p1:=crealconstnode.create(d,pbestrealtype^);
  1800. end;
  1801. _STRING :
  1802. begin
  1803. string_dec(htype);
  1804. { STRING can be also a type cast }
  1805. if token=_LKLAMMER then
  1806. begin
  1807. consume(_LKLAMMER);
  1808. p1:=comp_expr(true);
  1809. consume(_RKLAMMER);
  1810. p1:=ctypeconvnode.create(p1,htype);
  1811. include(p1.flags,nf_explizit);
  1812. { handle postfix operators here e.g. string(a)[10] }
  1813. again:=true;
  1814. postfixoperators(p1,again);
  1815. end
  1816. else
  1817. p1:=ctypenode.create(htype);
  1818. end;
  1819. _FILE :
  1820. begin
  1821. htype:=cfiletype;
  1822. consume(_FILE);
  1823. { FILE can be also a type cast }
  1824. if token=_LKLAMMER then
  1825. begin
  1826. consume(_LKLAMMER);
  1827. p1:=comp_expr(true);
  1828. consume(_RKLAMMER);
  1829. p1:=ctypeconvnode.create(p1,htype);
  1830. include(p1.flags,nf_explizit);
  1831. { handle postfix operators here e.g. string(a)[10] }
  1832. again:=true;
  1833. postfixoperators(p1,again);
  1834. end
  1835. else
  1836. begin
  1837. p1:=ctypenode.create(htype);
  1838. end;
  1839. end;
  1840. _CSTRING :
  1841. begin
  1842. p1:=cstringconstnode.createstr(pattern,st_default);
  1843. consume(_CSTRING);
  1844. end;
  1845. _CCHAR :
  1846. begin
  1847. p1:=cordconstnode.create(ord(pattern[1]),cchartype);
  1848. consume(_CCHAR);
  1849. end;
  1850. _CWSTRING:
  1851. begin
  1852. p1:=cstringconstnode.createwstr(patternw);
  1853. consume(_CWSTRING);
  1854. end;
  1855. _CWCHAR:
  1856. begin
  1857. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype);
  1858. consume(_CWCHAR);
  1859. end;
  1860. _KLAMMERAFFE :
  1861. begin
  1862. consume(_KLAMMERAFFE);
  1863. got_addrn:=true;
  1864. { support both @<x> and @(<x>) }
  1865. if token=_LKLAMMER then
  1866. begin
  1867. consume(_LKLAMMER);
  1868. p1:=factor(true);
  1869. consume(_RKLAMMER);
  1870. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1871. begin
  1872. again:=true;
  1873. postfixoperators(p1,again);
  1874. end;
  1875. end
  1876. else
  1877. p1:=factor(true);
  1878. got_addrn:=false;
  1879. p1:=caddrnode.create(p1);
  1880. end;
  1881. _LKLAMMER :
  1882. begin
  1883. consume(_LKLAMMER);
  1884. p1:=comp_expr(true);
  1885. consume(_RKLAMMER);
  1886. { it's not a good solution }
  1887. { but (a+b)^ makes some problems }
  1888. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1889. begin
  1890. again:=true;
  1891. postfixoperators(p1,again);
  1892. end;
  1893. end;
  1894. _LECKKLAMMER :
  1895. begin
  1896. consume(_LECKKLAMMER);
  1897. p1:=factor_read_set;
  1898. consume(_RECKKLAMMER);
  1899. end;
  1900. _PLUS :
  1901. begin
  1902. consume(_PLUS);
  1903. p1:=factor(false);
  1904. end;
  1905. _MINUS :
  1906. begin
  1907. consume(_MINUS);
  1908. p1:=sub_expr(oppower,false);
  1909. p1:=cunaryminusnode.create(p1);
  1910. end;
  1911. _OP_NOT :
  1912. begin
  1913. consume(_OP_NOT);
  1914. p1:=factor(false);
  1915. p1:=cnotnode.create(p1);
  1916. end;
  1917. _TRUE :
  1918. begin
  1919. consume(_TRUE);
  1920. p1:=cordconstnode.create(1,booltype);
  1921. end;
  1922. _FALSE :
  1923. begin
  1924. consume(_FALSE);
  1925. p1:=cordconstnode.create(0,booltype);
  1926. end;
  1927. _NIL :
  1928. begin
  1929. consume(_NIL);
  1930. p1:=cnilnode.create;
  1931. end;
  1932. else
  1933. begin
  1934. p1:=cerrornode.create;
  1935. consume(token);
  1936. Message(cg_e_illegal_expression);
  1937. end;
  1938. end;
  1939. { generate error node if no node is created }
  1940. if not assigned(p1) then
  1941. begin
  1942. {$ifdef EXTDEBUG}
  1943. Comment(V_Warning,'factor: p1=nil');
  1944. {$endif}
  1945. p1:=cerrornode.create;
  1946. end;
  1947. { get the resulttype for the node }
  1948. if (not assigned(p1.resulttype.def)) then
  1949. do_resulttypepass(p1);
  1950. { tp7 procvar handling, but not if the next token
  1951. will be a := }
  1952. if (m_tp_procvar in aktmodeswitches) and
  1953. (token<>_ASSIGNMENT) then
  1954. check_tp_procvar(p1);
  1955. factor:=p1;
  1956. check_tokenpos;
  1957. end;
  1958. {$ifdef fpc}
  1959. {$maxfpuregisters default}
  1960. {$endif fpc}
  1961. {****************************************************************************
  1962. Sub_Expr
  1963. ****************************************************************************}
  1964. const
  1965. { Warning these stay be ordered !! }
  1966. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1967. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1968. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1969. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1970. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1971. [_STARSTAR] );
  1972. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1973. {Reads a subexpression while the operators are of the current precedence
  1974. level, or any higher level. Replaces the old term, simpl_expr and
  1975. simpl2_expr.}
  1976. var
  1977. p1,p2 : tnode;
  1978. oldt : Ttoken;
  1979. filepos : tfileposinfo;
  1980. begin
  1981. if pred_level=highest_precedence then
  1982. p1:=factor(false)
  1983. else
  1984. p1:=sub_expr(succ(pred_level),true);
  1985. repeat
  1986. if (token in operator_levels[pred_level]) and
  1987. ((token<>_EQUAL) or accept_equal) then
  1988. begin
  1989. oldt:=token;
  1990. filepos:=akttokenpos;
  1991. consume(token);
  1992. if pred_level=highest_precedence then
  1993. p2:=factor(false)
  1994. else
  1995. p2:=sub_expr(succ(pred_level),true);
  1996. case oldt of
  1997. _PLUS :
  1998. p1:=caddnode.create(addn,p1,p2);
  1999. _MINUS :
  2000. p1:=caddnode.create(subn,p1,p2);
  2001. _STAR :
  2002. p1:=caddnode.create(muln,p1,p2);
  2003. _SLASH :
  2004. p1:=caddnode.create(slashn,p1,p2);
  2005. _EQUAL :
  2006. p1:=caddnode.create(equaln,p1,p2);
  2007. _GT :
  2008. p1:=caddnode.create(gtn,p1,p2);
  2009. _LT :
  2010. p1:=caddnode.create(ltn,p1,p2);
  2011. _GTE :
  2012. p1:=caddnode.create(gten,p1,p2);
  2013. _LTE :
  2014. p1:=caddnode.create(lten,p1,p2);
  2015. _SYMDIF :
  2016. p1:=caddnode.create(symdifn,p1,p2);
  2017. _STARSTAR :
  2018. p1:=caddnode.create(starstarn,p1,p2);
  2019. _OP_AS :
  2020. p1:=casnode.create(p1,p2);
  2021. _OP_IN :
  2022. p1:=cinnode.create(p1,p2);
  2023. _OP_IS :
  2024. p1:=cisnode.create(p1,p2);
  2025. _OP_OR :
  2026. p1:=caddnode.create(orn,p1,p2);
  2027. _OP_AND :
  2028. p1:=caddnode.create(andn,p1,p2);
  2029. _OP_DIV :
  2030. p1:=cmoddivnode.create(divn,p1,p2);
  2031. _OP_NOT :
  2032. p1:=cnotnode.create(p1);
  2033. _OP_MOD :
  2034. p1:=cmoddivnode.create(modn,p1,p2);
  2035. _OP_SHL :
  2036. p1:=cshlshrnode.create(shln,p1,p2);
  2037. _OP_SHR :
  2038. p1:=cshlshrnode.create(shrn,p1,p2);
  2039. _OP_XOR :
  2040. p1:=caddnode.create(xorn,p1,p2);
  2041. _ASSIGNMENT :
  2042. p1:=cassignmentnode.create(p1,p2);
  2043. _CARET :
  2044. p1:=caddnode.create(caretn,p1,p2);
  2045. _UNEQUAL :
  2046. p1:=caddnode.create(unequaln,p1,p2);
  2047. end;
  2048. p1.set_tree_filepos(filepos);
  2049. end
  2050. else
  2051. break;
  2052. until false;
  2053. sub_expr:=p1;
  2054. end;
  2055. function comp_expr(accept_equal : boolean):tnode;
  2056. var
  2057. oldafterassignment : boolean;
  2058. p1 : tnode;
  2059. begin
  2060. oldafterassignment:=afterassignment;
  2061. afterassignment:=true;
  2062. p1:=sub_expr(opcompare,accept_equal);
  2063. { get the resulttype for this expression }
  2064. if not assigned(p1.resulttype.def) then
  2065. do_resulttypepass(p1);
  2066. afterassignment:=oldafterassignment;
  2067. comp_expr:=p1;
  2068. end;
  2069. function expr : tnode;
  2070. var
  2071. p1,p2 : tnode;
  2072. oldafterassignment : boolean;
  2073. oldp1 : tnode;
  2074. filepos : tfileposinfo;
  2075. begin
  2076. oldafterassignment:=afterassignment;
  2077. p1:=sub_expr(opcompare,true);
  2078. { get the resulttype for this expression }
  2079. if not assigned(p1.resulttype.def) then
  2080. do_resulttypepass(p1);
  2081. filepos:=akttokenpos;
  2082. if (m_tp_procvar in aktmodeswitches) and
  2083. (token<>_ASSIGNMENT) then
  2084. check_tp_procvar(p1);
  2085. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2086. afterassignment:=true;
  2087. oldp1:=p1;
  2088. case token of
  2089. _POINTPOINT :
  2090. begin
  2091. consume(_POINTPOINT);
  2092. p2:=sub_expr(opcompare,true);
  2093. p1:=crangenode.create(p1,p2);
  2094. end;
  2095. _ASSIGNMENT :
  2096. begin
  2097. consume(_ASSIGNMENT);
  2098. if (m_tp_procvar in aktmodeswitches) and
  2099. (p1.resulttype.def.deftype=procvardef) then
  2100. begin
  2101. getprocvar:=true;
  2102. getprocvardef:=tprocvardef(p1.resulttype.def);
  2103. end;
  2104. p2:=sub_expr(opcompare,true);
  2105. if getprocvar then
  2106. handle_procvar(getprocvardef,p2,true);
  2107. getprocvar:=false;
  2108. p1:=cassignmentnode.create(p1,p2);
  2109. end;
  2110. _PLUSASN :
  2111. begin
  2112. consume(_PLUSASN);
  2113. p2:=sub_expr(opcompare,true);
  2114. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2115. end;
  2116. _MINUSASN :
  2117. begin
  2118. consume(_MINUSASN);
  2119. p2:=sub_expr(opcompare,true);
  2120. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2121. end;
  2122. _STARASN :
  2123. begin
  2124. consume(_STARASN );
  2125. p2:=sub_expr(opcompare,true);
  2126. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2127. end;
  2128. _SLASHASN :
  2129. begin
  2130. consume(_SLASHASN );
  2131. p2:=sub_expr(opcompare,true);
  2132. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2133. end;
  2134. end;
  2135. { get the resulttype for this expression }
  2136. if not assigned(p1.resulttype.def) then
  2137. do_resulttypepass(p1);
  2138. afterassignment:=oldafterassignment;
  2139. if p1<>oldp1 then
  2140. p1.set_tree_filepos(filepos);
  2141. expr:=p1;
  2142. end;
  2143. {$ifdef int64funcresok}
  2144. function get_intconst:TConstExprInt;
  2145. {$else int64funcresok}
  2146. function get_intconst:longint;
  2147. {$endif int64funcresok}
  2148. {Reads an expression, tries to evalute it and check if it is an integer
  2149. constant. Then the constant is returned.}
  2150. var
  2151. p:tnode;
  2152. begin
  2153. p:=comp_expr(true);
  2154. if not codegenerror then
  2155. begin
  2156. if (p.nodetype<>ordconstn) or
  2157. not(is_integer(p.resulttype.def)) then
  2158. Message(cg_e_illegal_expression)
  2159. else
  2160. get_intconst:=tordconstnode(p).value;
  2161. end;
  2162. p.free;
  2163. end;
  2164. function get_stringconst:string;
  2165. {Reads an expression, tries to evaluate it and checks if it is a string
  2166. constant. Then the constant is returned.}
  2167. var
  2168. p:tnode;
  2169. begin
  2170. get_stringconst:='';
  2171. p:=comp_expr(true);
  2172. if p.nodetype<>stringconstn then
  2173. begin
  2174. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2175. get_stringconst:=char(tordconstnode(p).value)
  2176. else
  2177. Message(cg_e_illegal_expression);
  2178. end
  2179. else
  2180. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2181. p.free;
  2182. end;
  2183. end.
  2184. {
  2185. $Log$
  2186. Revision 1.46 2001-10-21 13:10:51 peter
  2187. * better support for indexed properties
  2188. Revision 1.45 2001/10/21 12:33:07 peter
  2189. * array access for properties added
  2190. Revision 1.44 2001/10/20 19:28:39 peter
  2191. * interface 2 guid support
  2192. * guid constants support
  2193. Revision 1.43 2001/10/18 16:30:38 jonas
  2194. * property parameters are now fully parsed by the firstcall code to
  2195. check for the correct amount and types (merged)
  2196. Revision 1.42 2001/09/02 21:18:28 peter
  2197. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  2198. is used for holding target platform pointer values. As those can be
  2199. bigger than the source platform.
  2200. Revision 1.41 2001/08/26 13:36:45 florian
  2201. * some cg reorganisation
  2202. * some PPC updates
  2203. Revision 1.40 2001/08/22 21:16:21 florian
  2204. * some interfaces related problems regarding
  2205. mapping of interface implementions fixed
  2206. Revision 1.39 2001/08/06 21:40:47 peter
  2207. * funcret moved from tprocinfo to tprocdef
  2208. Revision 1.38 2001/07/09 21:15:41 peter
  2209. * Length made internal
  2210. * Add array support for Length
  2211. Revision 1.37 2001/06/29 14:16:57 jonas
  2212. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  2213. required to assign the address of a procedure to a procvar, sometimes
  2214. not. Now it is always required) (merged)
  2215. Revision 1.36 2001/06/04 18:16:42 peter
  2216. * fixed tp procvar support in parameters of a called procvar
  2217. * typenode cleanup, no special handling needed anymore for bt_type
  2218. Revision 1.35 2001/06/04 11:45:35 peter
  2219. * parse const after .. using bt_const block to allow expressions, this
  2220. is Delphi compatible
  2221. Revision 1.34 2001/05/19 21:15:53 peter
  2222. * allow typenodes for typeinfo and typeof
  2223. * tp procvar fixes for properties
  2224. Revision 1.33 2001/05/19 12:23:59 peter
  2225. * fixed crash with auto dereferencing
  2226. Revision 1.32 2001/05/09 19:52:51 peter
  2227. * removed unused allow_type
  2228. Revision 1.31 2001/05/04 15:52:03 florian
  2229. * some Delphi incompatibilities fixed:
  2230. - out, dispose and new can be used as idenfiers now
  2231. - const p = apointerype(nil); is supported now
  2232. + support for const p = apointertype(pointer(1234)); added
  2233. Revision 1.30 2001/04/14 14:07:10 peter
  2234. * moved more code from pass_1 to det_resulttype
  2235. Revision 1.29 2001/04/13 23:50:24 peter
  2236. * fpc mode now requires @ also when left of assignment is an procvardef
  2237. Revision 1.28 2001/04/13 01:22:12 peter
  2238. * symtable change to classes
  2239. * range check generation and errors fixed, make cycle DEBUG=1 works
  2240. * memory leaks fixed
  2241. Revision 1.27 2001/04/04 22:43:52 peter
  2242. * remove unnecessary calls to firstpass
  2243. Revision 1.26 2001/04/02 21:20:33 peter
  2244. * resulttype rewrite
  2245. Revision 1.25 2001/03/11 22:58:50 peter
  2246. * getsym redesign, removed the globals srsym,srsymtable
  2247. Revision 1.24 2000/12/25 00:07:27 peter
  2248. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2249. tlinkedlist objects)
  2250. Revision 1.23 2000/12/19 20:36:03 peter
  2251. * cardinal const expr fix from jonas
  2252. Revision 1.22 2000/12/17 14:00:18 peter
  2253. * fixed static variables
  2254. Revision 1.21 2000/12/15 13:26:01 jonas
  2255. * only return int64's from functions if it int64funcresok is defined
  2256. + added int64funcresok define to options.pas
  2257. Revision 1.20 2000/12/15 12:13:52 michael
  2258. + Fix from Peter
  2259. Revision 1.19 2000/12/07 17:19:42 jonas
  2260. * new constant handling: from now on, hex constants >$7fffffff are
  2261. parsed as unsigned constants (otherwise, $80000000 got sign extended
  2262. and became $ffffffff80000000), all constants in the longint range
  2263. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  2264. are cardinals and the rest are int64's.
  2265. * added lots of longint typecast to prevent range check errors in the
  2266. compiler and rtl
  2267. * type casts of symbolic ordinal constants are now preserved
  2268. * fixed bug where the original resulttype.def wasn't restored correctly
  2269. after doing a 64bit rangecheck
  2270. Revision 1.18 2000/11/29 00:30:36 florian
  2271. * unused units removed from uses clause
  2272. * some changes for widestrings
  2273. Revision 1.17 2000/11/09 17:46:55 florian
  2274. * System.TypeInfo fixed
  2275. + System.Finalize implemented
  2276. + some new keywords for interface support added
  2277. Revision 1.16 2000/11/06 20:30:55 peter
  2278. * more fixes to get make cycle working
  2279. Revision 1.15 2000/11/04 14:25:20 florian
  2280. + merged Attila's changes for interfaces, not tested yet
  2281. Revision 1.14 2000/10/31 22:02:49 peter
  2282. * symtable splitted, no real code changes
  2283. Revision 1.13 2000/10/26 23:40:54 peter
  2284. * fixed crash with call from type decl which is not allowed (merged)
  2285. Revision 1.12 2000/10/21 18:16:12 florian
  2286. * a lot of changes:
  2287. - basic dyn. array support
  2288. - basic C++ support
  2289. - some work for interfaces done
  2290. ....
  2291. Revision 1.11 2000/10/14 10:14:51 peter
  2292. * moehrendorf oct 2000 rewrite
  2293. Revision 1.10 2000/10/01 19:48:25 peter
  2294. * lot of compile updates for cg11
  2295. Revision 1.9 2000/09/24 21:19:50 peter
  2296. * delphi compile fixes
  2297. Revision 1.8 2000/09/24 15:06:22 peter
  2298. * use defines.inc
  2299. Revision 1.7 2000/08/27 16:11:51 peter
  2300. * moved some util functions from globals,cobjects to cutils
  2301. * splitted files into finput,fmodule
  2302. Revision 1.6 2000/08/20 15:12:49 peter
  2303. * auto derefence mode for array pointer (merged)
  2304. Revision 1.5 2000/08/16 18:33:53 peter
  2305. * splitted namedobjectitem.next into indexnext and listnext so it
  2306. can be used in both lists
  2307. * don't allow "word = word" type definitions (merged)
  2308. Revision 1.4 2000/08/16 13:06:06 florian
  2309. + support of 64 bit integer constants
  2310. Revision 1.3 2000/08/04 22:00:52 peter
  2311. * merges from fixes
  2312. Revision 1.2 2000/07/13 11:32:44 michael
  2313. + removed logs
  2314. }