pexpr.pas 89 KB

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