pexpr.pas 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  24. { reads a whole expression }
  25. function expr : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if (token=_LECKKLAMMER) then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. consume(_RECKKLAMMER);
  97. if tordconstnode(p).value>255 then
  98. begin
  99. { longstring is currently unsupported (CEC)! }
  100. { t:=tstringdef.createlong(tordconstnode(p).value))}
  101. Message(parser_e_invalid_string_size);
  102. tordconstnode(p).value:=255;
  103. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  104. end
  105. else
  106. if tordconstnode(p).value<>255 then
  107. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  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. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true);
  267. consume(_RKLAMMER);
  268. if (block_type=bt_except) then
  269. begin
  270. Message(parser_e_exit_with_argument_not__possible);
  271. { recovery }
  272. p1.free;
  273. p1:=nil;
  274. end
  275. else if (not assigned(current_procinfo) or
  276. is_void(current_procinfo.procdef.returndef)) then
  277. begin
  278. Message(parser_e_void_function);
  279. { recovery }
  280. p1.free;
  281. p1:=nil;
  282. end;
  283. end
  284. else
  285. p1:=nil;
  286. end
  287. else
  288. begin
  289. if not (current_procinfo.procdef.procsym.name = pattern) then
  290. Message(parser_e_macpas_exit_wrong_param);
  291. consume(_ID);
  292. consume(_RKLAMMER);
  293. p1:=nil;
  294. end
  295. end
  296. else
  297. p1:=nil;
  298. statement_syssym:=cexitnode.create(p1);
  299. end;
  300. in_break :
  301. begin
  302. statement_syssym:=cbreaknode.create
  303. end;
  304. in_continue :
  305. begin
  306. statement_syssym:=ccontinuenode.create
  307. end;
  308. in_leave :
  309. begin
  310. if m_mac in current_settings.modeswitches then
  311. statement_syssym:=cbreaknode.create
  312. else
  313. begin
  314. Message1(sym_e_id_not_found, orgpattern);
  315. statement_syssym:=cerrornode.create;
  316. end;
  317. end;
  318. in_cycle :
  319. begin
  320. if m_mac in current_settings.modeswitches then
  321. statement_syssym:=ccontinuenode.create
  322. else
  323. begin
  324. Message1(sym_e_id_not_found, orgpattern);
  325. statement_syssym:=cerrornode.create;
  326. end;
  327. end;
  328. in_typeof_x :
  329. begin
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. consume(_RKLAMMER);
  334. if p1.nodetype=typen then
  335. ttypenode(p1).allowed:=true;
  336. { Allow classrefdef, which is required for
  337. Typeof(self) in static class methods }
  338. if (p1.resultdef.typ = objectdef) or
  339. (assigned(current_procinfo) and
  340. ((po_classmethod in current_procinfo.procdef.procoptions) or
  341. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  342. (p1.resultdef.typ=classrefdef)) then
  343. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  344. else
  345. begin
  346. Message(parser_e_class_id_expected);
  347. p1.destroy;
  348. statement_syssym:=cerrornode.create;
  349. end;
  350. end;
  351. in_sizeof_x,
  352. in_bitsizeof_x :
  353. begin
  354. consume(_LKLAMMER);
  355. in_args:=true;
  356. p1:=comp_expr(true);
  357. consume(_RKLAMMER);
  358. if (p1.nodetype<>typen) and
  359. (
  360. (is_object(p1.resultdef) and
  361. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  362. is_open_array(p1.resultdef) or
  363. is_array_of_const(p1.resultdef) or
  364. is_open_string(p1.resultdef)
  365. ) then
  366. begin
  367. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  368. { no packed bit support for these things }
  369. if (l = in_bitsizeof_x) then
  370. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  371. end
  372. else
  373. begin
  374. if (l = in_sizeof_x) or
  375. (not((p1.nodetype = vecn) and
  376. is_packed_array(tvecnode(p1).left.resultdef)) and
  377. not((p1.nodetype = subscriptn) and
  378. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  379. begin
  380. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  381. if (l = in_bitsizeof_x) then
  382. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  383. end
  384. else
  385. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  386. { p1 not needed !}
  387. p1.destroy;
  388. end;
  389. end;
  390. in_typeinfo_x :
  391. begin
  392. consume(_LKLAMMER);
  393. in_args:=true;
  394. p1:=comp_expr(true);
  395. { When reading a class type it is parsed as loadvmtaddrn,
  396. typeinfo only needs the type so we remove the loadvmtaddrn }
  397. if p1.nodetype=loadvmtaddrn then
  398. begin
  399. p2:=tloadvmtaddrnode(p1).left;
  400. tloadvmtaddrnode(p1).left:=nil;
  401. p1.free;
  402. p1:=p2;
  403. end;
  404. if p1.nodetype=typen then
  405. ttypenode(p1).allowed:=true;
  406. { else
  407. begin
  408. p1.destroy;
  409. p1:=cerrornode.create;
  410. Message(parser_e_illegal_parameter_list);
  411. end;}
  412. consume(_RKLAMMER);
  413. p2:=geninlinenode(in_typeinfo_x,false,p1);
  414. statement_syssym:=p2;
  415. end;
  416. in_unaligned_x :
  417. begin
  418. err:=false;
  419. consume(_LKLAMMER);
  420. in_args:=true;
  421. p1:=comp_expr(true);
  422. p2:=ccallparanode.create(p1,nil);
  423. p2:=geninlinenode(in_unaligned_x,false,p2);
  424. consume(_RKLAMMER);
  425. statement_syssym:=p2;
  426. end;
  427. in_assigned_x :
  428. begin
  429. err:=false;
  430. consume(_LKLAMMER);
  431. in_args:=true;
  432. p1:=comp_expr(true);
  433. { When reading a class type it is parsed as loadvmtaddrn,
  434. typeinfo only needs the type so we remove the loadvmtaddrn }
  435. if p1.nodetype=loadvmtaddrn then
  436. begin
  437. p2:=tloadvmtaddrnode(p1).left;
  438. tloadvmtaddrnode(p1).left:=nil;
  439. p1.free;
  440. p1:=p2;
  441. end;
  442. if not codegenerror then
  443. begin
  444. case p1.resultdef.typ of
  445. procdef, { procvar }
  446. pointerdef,
  447. procvardef,
  448. classrefdef : ;
  449. objectdef :
  450. if not is_class_or_interface(p1.resultdef) then
  451. begin
  452. Message(parser_e_illegal_parameter_list);
  453. err:=true;
  454. end;
  455. arraydef :
  456. if not is_dynamic_array(p1.resultdef) then
  457. begin
  458. Message(parser_e_illegal_parameter_list);
  459. err:=true;
  460. end;
  461. else
  462. begin
  463. Message(parser_e_illegal_parameter_list);
  464. err:=true;
  465. end;
  466. end;
  467. end
  468. else
  469. err:=true;
  470. if not err then
  471. begin
  472. p2:=ccallparanode.create(p1,nil);
  473. p2:=geninlinenode(in_assigned_x,false,p2);
  474. end
  475. else
  476. begin
  477. p1.free;
  478. p2:=cerrornode.create;
  479. end;
  480. consume(_RKLAMMER);
  481. statement_syssym:=p2;
  482. end;
  483. in_addr_x :
  484. begin
  485. consume(_LKLAMMER);
  486. in_args:=true;
  487. p1:=comp_expr(true);
  488. p1:=caddrnode.create(p1);
  489. if cs_typed_addresses in current_settings.localswitches then
  490. include(p1.flags,nf_typedaddr);
  491. consume(_RKLAMMER);
  492. statement_syssym:=p1;
  493. end;
  494. in_ofs_x :
  495. begin
  496. consume(_LKLAMMER);
  497. in_args:=true;
  498. p1:=comp_expr(true);
  499. p1:=caddrnode.create(p1);
  500. do_typecheckpass(p1);
  501. { Ofs() returns a cardinal/qword, not a pointer }
  502. p1.resultdef:=uinttype;
  503. consume(_RKLAMMER);
  504. statement_syssym:=p1;
  505. end;
  506. in_seg_x :
  507. begin
  508. consume(_LKLAMMER);
  509. in_args:=true;
  510. p1:=comp_expr(true);
  511. p1:=geninlinenode(in_seg_x,false,p1);
  512. consume(_RKLAMMER);
  513. statement_syssym:=p1;
  514. end;
  515. in_high_x,
  516. in_low_x :
  517. begin
  518. consume(_LKLAMMER);
  519. in_args:=true;
  520. p1:=comp_expr(true);
  521. p2:=geninlinenode(l,false,p1);
  522. consume(_RKLAMMER);
  523. statement_syssym:=p2;
  524. end;
  525. in_succ_x,
  526. in_pred_x :
  527. begin
  528. consume(_LKLAMMER);
  529. in_args:=true;
  530. p1:=comp_expr(true);
  531. p2:=geninlinenode(l,false,p1);
  532. consume(_RKLAMMER);
  533. statement_syssym:=p2;
  534. end;
  535. in_inc_x,
  536. in_dec_x :
  537. begin
  538. consume(_LKLAMMER);
  539. in_args:=true;
  540. p1:=comp_expr(true);
  541. if try_to_consume(_COMMA) then
  542. p2:=ccallparanode.create(comp_expr(true),nil)
  543. else
  544. p2:=nil;
  545. p2:=ccallparanode.create(p1,p2);
  546. statement_syssym:=geninlinenode(l,false,p2);
  547. consume(_RKLAMMER);
  548. end;
  549. in_slice_x:
  550. begin
  551. if not(in_args) then
  552. begin
  553. message(parser_e_illegal_slice);
  554. consume(_LKLAMMER);
  555. in_args:=true;
  556. comp_expr(true).free;
  557. if try_to_consume(_COMMA) then
  558. comp_expr(true).free;
  559. statement_syssym:=cerrornode.create;
  560. consume(_RKLAMMER);
  561. end
  562. else
  563. begin
  564. consume(_LKLAMMER);
  565. in_args:=true;
  566. p1:=comp_expr(true);
  567. Consume(_COMMA);
  568. if not(codegenerror) then
  569. p2:=ccallparanode.create(comp_expr(true),nil)
  570. else
  571. p2:=cerrornode.create;
  572. p2:=ccallparanode.create(p1,p2);
  573. statement_syssym:=geninlinenode(l,false,p2);
  574. consume(_RKLAMMER);
  575. end;
  576. end;
  577. in_initialize_x:
  578. begin
  579. statement_syssym:=inline_initialize;
  580. end;
  581. in_finalize_x:
  582. begin
  583. statement_syssym:=inline_finalize;
  584. end;
  585. in_copy_x:
  586. begin
  587. statement_syssym:=inline_copy;
  588. end;
  589. in_concat_x :
  590. begin
  591. consume(_LKLAMMER);
  592. in_args:=true;
  593. { Translate to x:=x+y[+z]. The addnode will do the
  594. type checking }
  595. p2:=nil;
  596. repeat
  597. p1:=comp_expr(true);
  598. if p2<>nil then
  599. p2:=caddnode.create(addn,p2,p1)
  600. else
  601. begin
  602. { Force string type if it isn't yet }
  603. if not(
  604. (p1.resultdef.typ=stringdef) or
  605. is_chararray(p1.resultdef) or
  606. is_char(p1.resultdef)
  607. ) then
  608. inserttypeconv(p1,cshortstringtype);
  609. p2:=p1;
  610. end;
  611. until not try_to_consume(_COMMA);
  612. consume(_RKLAMMER);
  613. statement_syssym:=p2;
  614. end;
  615. in_read_x,
  616. in_readln_x,
  617. in_readstr_x:
  618. begin
  619. if try_to_consume(_LKLAMMER) then
  620. begin
  621. paras:=parse_paras(false,false,_RKLAMMER);
  622. consume(_RKLAMMER);
  623. end
  624. else
  625. paras:=nil;
  626. p1:=geninlinenode(l,false,paras);
  627. statement_syssym := p1;
  628. end;
  629. in_setlength_x:
  630. begin
  631. statement_syssym := inline_setlength;
  632. end;
  633. in_objc_selector_x:
  634. begin
  635. if (m_objectivec1 in current_settings.modeswitches) then
  636. begin
  637. consume(_LKLAMMER);
  638. in_args:=true;
  639. { don't turn procsyms into calls (getaddr = true) }
  640. p1:=factor(true);
  641. p2:=geninlinenode(l,false,p1);
  642. consume(_RKLAMMER);
  643. statement_syssym:=p2;
  644. end
  645. else
  646. begin
  647. Message1(sym_e_id_not_found, orgpattern);
  648. statement_syssym:=cerrornode.create;
  649. end;
  650. end;
  651. in_length_x:
  652. begin
  653. consume(_LKLAMMER);
  654. in_args:=true;
  655. p1:=comp_expr(true);
  656. p2:=geninlinenode(l,false,p1);
  657. consume(_RKLAMMER);
  658. statement_syssym:=p2;
  659. end;
  660. in_write_x,
  661. in_writeln_x,
  662. in_writestr_x :
  663. begin
  664. if try_to_consume(_LKLAMMER) then
  665. begin
  666. paras:=parse_paras(true,false,_RKLAMMER);
  667. consume(_RKLAMMER);
  668. end
  669. else
  670. paras:=nil;
  671. p1 := geninlinenode(l,false,paras);
  672. statement_syssym := p1;
  673. end;
  674. in_str_x_string :
  675. begin
  676. consume(_LKLAMMER);
  677. paras:=parse_paras(true,false,_RKLAMMER);
  678. consume(_RKLAMMER);
  679. p1 := geninlinenode(l,false,paras);
  680. statement_syssym := p1;
  681. end;
  682. in_val_x:
  683. Begin
  684. consume(_LKLAMMER);
  685. in_args := true;
  686. p1:= ccallparanode.create(comp_expr(true), nil);
  687. consume(_COMMA);
  688. p2 := ccallparanode.create(comp_expr(true),p1);
  689. if try_to_consume(_COMMA) then
  690. p2 := ccallparanode.create(comp_expr(true),p2);
  691. consume(_RKLAMMER);
  692. p2 := geninlinenode(l,false,p2);
  693. statement_syssym := p2;
  694. End;
  695. in_include_x_y,
  696. in_exclude_x_y :
  697. begin
  698. consume(_LKLAMMER);
  699. in_args:=true;
  700. p1:=comp_expr(true);
  701. consume(_COMMA);
  702. p2:=comp_expr(true);
  703. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  704. consume(_RKLAMMER);
  705. end;
  706. in_pack_x_y_z,
  707. in_unpack_x_y_z :
  708. begin
  709. consume(_LKLAMMER);
  710. in_args:=true;
  711. p1:=comp_expr(true);
  712. consume(_COMMA);
  713. p2:=comp_expr(true);
  714. consume(_COMMA);
  715. paras:=comp_expr(true);
  716. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  717. consume(_RKLAMMER);
  718. end;
  719. in_assert_x_y :
  720. begin
  721. consume(_LKLAMMER);
  722. in_args:=true;
  723. p1:=comp_expr(true);
  724. if try_to_consume(_COMMA) then
  725. p2:=comp_expr(true)
  726. else
  727. begin
  728. { then insert an empty string }
  729. p2:=cstringconstnode.createstr('');
  730. end;
  731. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  732. consume(_RKLAMMER);
  733. end;
  734. in_get_frame:
  735. begin
  736. statement_syssym:=geninlinenode(l,false,nil);
  737. end;
  738. (*
  739. in_get_caller_frame:
  740. begin
  741. if try_to_consume(_LKLAMMER) then
  742. begin
  743. {You used to call get_caller_frame as get_caller_frame(get_frame),
  744. however, as a stack frame may not exist, it does more harm than
  745. good, so ignore it.}
  746. in_args:=true;
  747. p1:=comp_expr(true);
  748. p1.destroy;
  749. consume(_RKLAMMER);
  750. end;
  751. statement_syssym:=geninlinenode(l,false,nil);
  752. end;
  753. *)
  754. else
  755. internalerror(15);
  756. end;
  757. in_args:=prev_in_args;
  758. end;
  759. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  760. begin
  761. maybe_load_methodpointer:=false;
  762. if not assigned(p1) then
  763. begin
  764. case st.symtabletype of
  765. withsymtable :
  766. begin
  767. if (st.defowner.typ=objectdef) then
  768. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  769. end;
  770. ObjectSymtable :
  771. begin
  772. p1:=load_self_node;
  773. { We are calling a member }
  774. maybe_load_methodpointer:=true;
  775. end;
  776. end;
  777. end;
  778. end;
  779. { reads the parameter for a subroutine call }
  780. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  781. var
  782. membercall,
  783. prevafterassn : boolean;
  784. i : integer;
  785. para,p2 : tnode;
  786. currpara : tparavarsym;
  787. aprocdef : tprocdef;
  788. begin
  789. prevafterassn:=afterassignment;
  790. afterassignment:=false;
  791. membercall:=false;
  792. aprocdef:=nil;
  793. { when it is a call to a member we need to load the
  794. methodpointer first
  795. (except if we are in a static class method)
  796. }
  797. membercall:=
  798. not(assigned(current_procinfo) and
  799. ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) and
  800. maybe_load_methodpointer(st,p1);
  801. { When we are expecting a procvar we also need
  802. to get the address in some cases }
  803. if assigned(getprocvardef) then
  804. begin
  805. if (block_type=bt_const) or
  806. getaddr then
  807. begin
  808. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  809. getaddr:=true;
  810. end
  811. else
  812. if (m_tp_procvar in current_settings.modeswitches) or
  813. (m_mac_procvar in current_settings.modeswitches) then
  814. begin
  815. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  816. if assigned(aprocdef) then
  817. getaddr:=true;
  818. end;
  819. end;
  820. { only need to get the address of the procedure? }
  821. if getaddr then
  822. begin
  823. { Retrieve info which procvar to call. For tp_procvar the
  824. aprocdef is already loaded above so we can reuse it }
  825. if not assigned(aprocdef) and
  826. assigned(getprocvardef) then
  827. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  828. { generate a methodcallnode or proccallnode }
  829. { we shouldn't convert things like @tcollection.load }
  830. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  831. if assigned(p1) then
  832. begin
  833. { for loading methodpointer of an inherited function
  834. we use self as instance and load the address of
  835. the function directly and not through the vmt (PFV) }
  836. if (cnf_inherited in callflags) then
  837. begin
  838. include(p2.flags,nf_inherited);
  839. p1.free;
  840. p1:=load_self_node;
  841. end;
  842. if (p1.nodetype<>typen) then
  843. tloadnode(p2).set_mp(p1)
  844. else
  845. p1.free;
  846. end;
  847. p1:=p2;
  848. { no postfix operators }
  849. again:=false;
  850. end
  851. else
  852. begin
  853. para:=nil;
  854. if anon_inherited then
  855. begin
  856. if not assigned(current_procinfo) then
  857. internalerror(200305054);
  858. for i:=0 to current_procinfo.procdef.paras.count-1 do
  859. begin
  860. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  861. if not(vo_is_hidden_para in currpara.varoptions) then
  862. begin
  863. { inheritance by msgint? }
  864. if assigned(srdef) then
  865. { anonymous inherited via msgid calls only require a var parameter for
  866. both methods, so we need some type casting here }
  867. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  868. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  869. para)
  870. else
  871. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  872. end;
  873. end;
  874. end
  875. else
  876. begin
  877. if try_to_consume(_LKLAMMER) then
  878. begin
  879. para:=parse_paras(false,false,_RKLAMMER);
  880. consume(_RKLAMMER);
  881. end;
  882. end;
  883. { indicate if this call was generated by a member and
  884. no explicit self is used, this is needed to determine
  885. how to handle a destructor call (PFV) }
  886. if membercall then
  887. include(callflags,cnf_member_call);
  888. if assigned(obj) then
  889. begin
  890. if (st.symtabletype<>ObjectSymtable) then
  891. internalerror(200310031);
  892. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  893. end
  894. else
  895. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  896. end;
  897. afterassignment:=prevafterassn;
  898. end;
  899. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  900. var
  901. hp,hp2 : tnode;
  902. hpp : ^tnode;
  903. currprocdef : tprocdef;
  904. begin
  905. if not assigned(pv) then
  906. internalerror(200301121);
  907. if (m_tp_procvar in current_settings.modeswitches) or
  908. (m_mac_procvar in current_settings.modeswitches) then
  909. begin
  910. hp:=p2;
  911. hpp:=@p2;
  912. while assigned(hp) and
  913. (hp.nodetype=typeconvn) do
  914. begin
  915. hp:=ttypeconvnode(hp).left;
  916. { save orignal address of the old tree so we can replace the node }
  917. hpp:=@hp;
  918. end;
  919. if (hp.nodetype=calln) and
  920. { a procvar can't have parameters! }
  921. not assigned(tcallnode(hp).left) then
  922. begin
  923. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  924. if assigned(currprocdef) then
  925. begin
  926. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  927. if (po_methodpointer in pv.procoptions) then
  928. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  929. hp.destroy;
  930. { replace the old callnode with the new loadnode }
  931. hpp^:=hp2;
  932. end;
  933. end;
  934. end;
  935. end;
  936. { the following procedure handles the access to a property symbol }
  937. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  938. var
  939. paras : tnode;
  940. p2 : tnode;
  941. membercall : boolean;
  942. callflags : tcallnodeflags;
  943. propaccesslist : tpropaccesslist;
  944. function getpropaccesslist(pap:tpropaccesslisttypes):boolean;
  945. var
  946. hpropsym : tpropertysym;
  947. begin
  948. result:=false;
  949. { find property in the overriden list }
  950. hpropsym:=propsym;
  951. repeat
  952. propaccesslist:=hpropsym.propaccesslist[pap];
  953. if not propaccesslist.empty then
  954. begin
  955. result:=true;
  956. exit;
  957. end;
  958. hpropsym:=hpropsym.overridenpropsym;
  959. until not assigned(hpropsym);
  960. end;
  961. begin
  962. { property parameters? read them only if the property really }
  963. { has parameters }
  964. paras:=nil;
  965. if (ppo_hasparameters in propsym.propoptions) then
  966. begin
  967. if try_to_consume(_LECKKLAMMER) then
  968. begin
  969. paras:=parse_paras(false,false,_RECKKLAMMER);
  970. consume(_RECKKLAMMER);
  971. end;
  972. end;
  973. { indexed property }
  974. if (ppo_indexed in propsym.propoptions) then
  975. begin
  976. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  977. paras:=ccallparanode.create(p2,paras);
  978. end;
  979. { we need only a write property if a := follows }
  980. { if not(afterassignment) and not(in_args) then }
  981. if token=_ASSIGNMENT then
  982. begin
  983. if getpropaccesslist(palt_write) then
  984. begin
  985. case propaccesslist.firstsym^.sym.typ of
  986. procsym :
  987. begin
  988. callflags:=[];
  989. { generate the method call }
  990. membercall:=maybe_load_methodpointer(st,p1);
  991. if membercall then
  992. include(callflags,cnf_member_call);
  993. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  994. addsymref(propaccesslist.firstsym^.sym);
  995. paras:=nil;
  996. consume(_ASSIGNMENT);
  997. { read the expression }
  998. if propsym.propdef.typ=procvardef then
  999. getprocvardef:=tprocvardef(propsym.propdef);
  1000. p2:=comp_expr(true);
  1001. if assigned(getprocvardef) then
  1002. handle_procvar(getprocvardef,p2);
  1003. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1004. { mark as property, both the tcallnode and the real call block }
  1005. include(p1.flags,nf_isproperty);
  1006. getprocvardef:=nil;
  1007. end;
  1008. fieldvarsym :
  1009. begin
  1010. { generate access code }
  1011. propaccesslist_to_node(p1,st,propaccesslist);
  1012. include(p1.flags,nf_isproperty);
  1013. consume(_ASSIGNMENT);
  1014. { read the expression }
  1015. p2:=comp_expr(true);
  1016. p1:=cassignmentnode.create(p1,p2);
  1017. end
  1018. else
  1019. begin
  1020. p1:=cerrornode.create;
  1021. Message(parser_e_no_procedure_to_access_property);
  1022. end;
  1023. end;
  1024. end
  1025. else
  1026. begin
  1027. p1:=cerrornode.create;
  1028. Message(parser_e_no_procedure_to_access_property);
  1029. end;
  1030. end
  1031. else
  1032. begin
  1033. if getpropaccesslist(palt_read) then
  1034. begin
  1035. case propaccesslist.firstsym^.sym.typ of
  1036. fieldvarsym :
  1037. begin
  1038. { generate access code }
  1039. propaccesslist_to_node(p1,st,propaccesslist);
  1040. include(p1.flags,nf_isproperty);
  1041. end;
  1042. procsym :
  1043. begin
  1044. callflags:=[];
  1045. { generate the method call }
  1046. membercall:=maybe_load_methodpointer(st,p1);
  1047. if membercall then
  1048. include(callflags,cnf_member_call);
  1049. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  1050. paras:=nil;
  1051. include(p1.flags,nf_isproperty);
  1052. end
  1053. else
  1054. begin
  1055. p1:=cerrornode.create;
  1056. Message(type_e_mismatch);
  1057. end;
  1058. end;
  1059. end
  1060. else
  1061. begin
  1062. { error, no function to read property }
  1063. p1:=cerrornode.create;
  1064. Message(parser_e_no_procedure_to_access_property);
  1065. end;
  1066. end;
  1067. { release paras if not used }
  1068. if assigned(paras) then
  1069. paras.free;
  1070. end;
  1071. { the ID token has to be consumed before calling this function }
  1072. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1073. var
  1074. static_name : string;
  1075. isclassref : boolean;
  1076. srsymtable : TSymtable;
  1077. begin
  1078. if sym=nil then
  1079. begin
  1080. { pattern is still valid unless
  1081. there is another ID just after the ID of sym }
  1082. Message1(sym_e_id_no_member,orgpattern);
  1083. p1.free;
  1084. p1:=cerrornode.create;
  1085. { try to clean up }
  1086. again:=false;
  1087. end
  1088. else
  1089. begin
  1090. if assigned(p1) then
  1091. begin
  1092. if not assigned(p1.resultdef) then
  1093. do_typecheckpass(p1);
  1094. isclassref:=(p1.resultdef.typ=classrefdef);
  1095. end
  1096. else
  1097. isclassref:=false;
  1098. { we assume, that only procsyms and varsyms are in an object }
  1099. { symbol table, for classes, properties are allowed }
  1100. case sym.typ of
  1101. procsym:
  1102. begin
  1103. do_proc_call(sym,sym.owner,classh,
  1104. (getaddr and not(token in [_CARET,_POINT])),
  1105. again,p1,callflags);
  1106. { we need to know which procedure is called }
  1107. do_typecheckpass(p1);
  1108. { calling using classref? }
  1109. if isclassref and
  1110. (p1.nodetype=calln) and
  1111. assigned(tcallnode(p1).procdefinition) and
  1112. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1113. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1114. Message(parser_e_only_class_methods_via_class_ref);
  1115. end;
  1116. fieldvarsym:
  1117. begin
  1118. if (sp_static in sym.symoptions) then
  1119. begin
  1120. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1121. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1122. if assigned(sym) then
  1123. check_hints(sym,sym.symoptions);
  1124. p1.free;
  1125. p1:=nil;
  1126. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1127. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1128. end
  1129. else
  1130. begin
  1131. if isclassref then
  1132. if assigned(p1) and
  1133. is_self_node(p1) then
  1134. Message(parser_e_only_class_methods)
  1135. else
  1136. Message(parser_e_only_class_methods_via_class_ref);
  1137. p1:=csubscriptnode.create(sym,p1);
  1138. end;
  1139. end;
  1140. propertysym:
  1141. begin
  1142. if isclassref then
  1143. Message(parser_e_only_class_methods_via_class_ref);
  1144. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1145. end;
  1146. typesym:
  1147. begin
  1148. p1:=ctypenode.create(ttypesym(sym).typedef);
  1149. end;
  1150. else internalerror(16);
  1151. end;
  1152. end;
  1153. end;
  1154. {****************************************************************************
  1155. Factor
  1156. ****************************************************************************}
  1157. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1158. out memberparentdef: tdef): boolean;
  1159. var
  1160. hdef : tdef;
  1161. begin
  1162. result:=true;
  1163. memberparentdef:=nil;
  1164. case st.symtabletype of
  1165. ObjectSymtable:
  1166. begin
  1167. memberparentdef:=tdef(st.defowner);
  1168. exit;
  1169. end;
  1170. WithSymtable:
  1171. begin
  1172. if assigned(p1) then
  1173. internalerror(2007012002);
  1174. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1175. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1176. if not(hdef.typ in [objectdef,classrefdef]) then
  1177. exit;
  1178. if (hdef.typ=classrefdef) then
  1179. hdef:=tclassrefdef(hdef).pointeddef;
  1180. memberparentdef:=hdef;
  1181. end;
  1182. else
  1183. result:=false;
  1184. end;
  1185. end;
  1186. {$maxfpuregisters 0}
  1187. function factor(getaddr : boolean) : tnode;
  1188. {---------------------------------------------
  1189. Factor_read_id
  1190. ---------------------------------------------}
  1191. procedure factor_read_id(out p1:tnode;var again:boolean);
  1192. var
  1193. pc : pchar;
  1194. srsym : tsym;
  1195. unit_found : boolean;
  1196. srsymtable : TSymtable;
  1197. hdef : tdef;
  1198. orgstoredpattern,
  1199. storedpattern : string;
  1200. len : longint;
  1201. t : ttoken;
  1202. begin
  1203. { allow post fix operators }
  1204. again:=true;
  1205. { first check for identifier }
  1206. if token<>_ID then
  1207. begin
  1208. srsym:=generrorsym;
  1209. srsymtable:=nil;
  1210. consume(_ID);
  1211. end
  1212. else
  1213. begin
  1214. searchsym(pattern,srsym,srsymtable);
  1215. { handle unit specification like System.Writeln }
  1216. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1217. storedpattern:=pattern;
  1218. orgstoredpattern:=orgpattern;
  1219. consume(t);
  1220. { named parameter support }
  1221. found_arg_name:=false;
  1222. if not(unit_found) and
  1223. named_args_allowed and
  1224. (token=_ASSIGNMENT) then
  1225. begin
  1226. found_arg_name:=true;
  1227. p1:=cstringconstnode.createstr(storedpattern);
  1228. consume(_ASSIGNMENT);
  1229. exit;
  1230. end;
  1231. { if nothing found give error and return errorsym }
  1232. if assigned(srsym) then
  1233. check_hints(srsym,srsym.symoptions)
  1234. else
  1235. begin
  1236. identifier_not_found(orgstoredpattern);
  1237. srsym:=generrorsym;
  1238. srsymtable:=nil;
  1239. end;
  1240. end;
  1241. { Access to funcret or need to call the function? }
  1242. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1243. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1244. { result(x) is not allowed }
  1245. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1246. (
  1247. (token=_LKLAMMER) or
  1248. (
  1249. (
  1250. (m_tp7 in current_settings.modeswitches) or
  1251. (m_delphi in current_settings.modeswitches)
  1252. ) and
  1253. (afterassignment or in_args)
  1254. )
  1255. ) then
  1256. begin
  1257. hdef:=tdef(srsym.owner.defowner);
  1258. if assigned(hdef) and
  1259. (hdef.typ=procdef) then
  1260. srsym:=tprocdef(hdef).procsym
  1261. else
  1262. begin
  1263. Message(parser_e_illegal_expression);
  1264. srsym:=generrorsym;
  1265. end;
  1266. srsymtable:=srsym.owner;
  1267. end;
  1268. begin
  1269. case srsym.typ of
  1270. absolutevarsym :
  1271. begin
  1272. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1273. begin
  1274. p1:=nil;
  1275. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1276. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1277. include(p1.flags,nf_absolute);
  1278. end
  1279. else
  1280. p1:=cloadnode.create(srsym,srsymtable);
  1281. end;
  1282. staticvarsym,
  1283. localvarsym,
  1284. paravarsym,
  1285. fieldvarsym :
  1286. begin
  1287. { check if we are reading a field of an object/class/ }
  1288. { record. is_member_read() will deal with withsymtables }
  1289. { if needed. }
  1290. if is_member_read(srsym,srsymtable,p1,hdef) then
  1291. begin
  1292. { if the field was originally found in an }
  1293. { objectsymtable, it means it's part of self }
  1294. if (srsymtable.symtabletype=ObjectSymtable) then
  1295. p1:=load_self_node;
  1296. { now, if the field itself is part of an objectsymtab }
  1297. { (it can be even if it was found in a withsymtable, }
  1298. { e.g., "with classinstance do field := 5"), then }
  1299. { let do_member_read handle it }
  1300. if (srsym.owner.symtabletype=ObjectSymtable) then
  1301. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1302. else
  1303. { otherwise it's a regular record subscript }
  1304. p1:=csubscriptnode.create(srsym,p1);
  1305. end
  1306. else
  1307. { regular non-field load }
  1308. p1:=cloadnode.create(srsym,srsymtable);
  1309. end;
  1310. syssym :
  1311. begin
  1312. p1:=statement_syssym(tsyssym(srsym).number);
  1313. end;
  1314. typesym :
  1315. begin
  1316. hdef:=ttypesym(srsym).typedef;
  1317. if not assigned(hdef) then
  1318. begin
  1319. again:=false;
  1320. end
  1321. else
  1322. begin
  1323. { We need to know if this unit uses Variants }
  1324. if (hdef=cvarianttype) and
  1325. not(cs_compilesystem in current_settings.moduleswitches) then
  1326. current_module.flags:=current_module.flags or uf_uses_variants;
  1327. if try_to_consume(_LKLAMMER) then
  1328. begin
  1329. p1:=comp_expr(true);
  1330. consume(_RKLAMMER);
  1331. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1332. end
  1333. else { not LKLAMMER }
  1334. if (token=_POINT) and
  1335. is_object(hdef) then
  1336. begin
  1337. consume(_POINT);
  1338. if assigned(current_objectdef) and
  1339. not(getaddr) then
  1340. begin
  1341. if current_objectdef.is_related(tobjectdef(hdef)) then
  1342. begin
  1343. p1:=ctypenode.create(hdef);
  1344. { search also in inherited methods }
  1345. searchsym_in_class(tobjectdef(hdef),current_objectdef,pattern,srsym,srsymtable);
  1346. if assigned(srsym) then
  1347. check_hints(srsym,srsym.symoptions);
  1348. consume(_ID);
  1349. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1350. end
  1351. else
  1352. begin
  1353. Message(parser_e_no_super_class);
  1354. again:=false;
  1355. end;
  1356. end
  1357. else
  1358. begin
  1359. { allows @TObject.Load }
  1360. { also allows static methods and variables }
  1361. p1:=ctypenode.create(hdef);
  1362. { TP allows also @TMenu.Load if Load is only }
  1363. { defined in an anchestor class }
  1364. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1365. if assigned(srsym) then
  1366. begin
  1367. check_hints(srsym,srsym.symoptions);
  1368. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1369. Message(sym_e_only_static_in_static)
  1370. else
  1371. begin
  1372. consume(_ID);
  1373. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1374. end;
  1375. end
  1376. else
  1377. Message1(sym_e_id_no_member,orgpattern);
  1378. end;
  1379. end
  1380. else
  1381. begin
  1382. { class reference ? }
  1383. if is_class(hdef) then
  1384. begin
  1385. if getaddr and (token=_POINT) then
  1386. begin
  1387. consume(_POINT);
  1388. { allows @Object.Method }
  1389. { also allows static methods and variables }
  1390. p1:=ctypenode.create(hdef);
  1391. { TP allows also @TMenu.Load if Load is only }
  1392. { defined in an anchestor class }
  1393. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1394. if assigned(srsym) then
  1395. begin
  1396. check_hints(srsym,srsym.symoptions);
  1397. consume(_ID);
  1398. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1399. end
  1400. else
  1401. begin
  1402. Message1(sym_e_id_no_member,orgpattern);
  1403. consume(_ID);
  1404. end;
  1405. end
  1406. else
  1407. begin
  1408. p1:=ctypenode.create(hdef);
  1409. { For a type block we simply return only
  1410. the type. For all other blocks we return
  1411. a loadvmt node }
  1412. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1413. p1:=cloadvmtaddrnode.create(p1);
  1414. end;
  1415. end
  1416. else
  1417. p1:=ctypenode.create(hdef);
  1418. end;
  1419. end;
  1420. end;
  1421. enumsym :
  1422. begin
  1423. p1:=genenumnode(tenumsym(srsym));
  1424. end;
  1425. constsym :
  1426. begin
  1427. case tconstsym(srsym).consttyp of
  1428. constord :
  1429. begin
  1430. if tconstsym(srsym).constdef=nil then
  1431. internalerror(200403232);
  1432. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).constdef,true);
  1433. end;
  1434. conststring :
  1435. begin
  1436. len:=tconstsym(srsym).value.len;
  1437. if not(cs_ansistrings in current_settings.localswitches) and (len>255) then
  1438. len:=255;
  1439. getmem(pc,len+1);
  1440. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1441. pc[len]:=#0;
  1442. p1:=cstringconstnode.createpchar(pc,len);
  1443. end;
  1444. constwstring :
  1445. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1446. constreal :
  1447. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1448. constset :
  1449. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).constdef);
  1450. constpointer :
  1451. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).constdef);
  1452. constnil :
  1453. p1:=cnilnode.create;
  1454. constresourcestring:
  1455. begin
  1456. p1:=cloadnode.create(srsym,srsymtable);
  1457. do_typecheckpass(p1);
  1458. p1.resultdef:=cansistringtype;
  1459. end;
  1460. constguid :
  1461. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1462. else
  1463. internalerror(200507181);
  1464. end;
  1465. end;
  1466. procsym :
  1467. begin
  1468. { check if it's a method/class method }
  1469. if is_member_read(srsym,srsymtable,p1,hdef) then
  1470. begin
  1471. { not srsymtable.symtabletype since that can be }
  1472. { withsymtable as well }
  1473. if (srsym.owner.symtabletype=ObjectSymtable) then
  1474. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1475. else
  1476. { no procsyms in records (yet) }
  1477. internalerror(2007012006);
  1478. end
  1479. else
  1480. { regular procedure/function call }
  1481. do_proc_call(srsym,srsymtable,nil,
  1482. (getaddr and not(token in [_CARET,_POINT])),
  1483. again,p1,[]);
  1484. end;
  1485. propertysym :
  1486. begin
  1487. { property of a class/object? }
  1488. if is_member_read(srsym,srsymtable,p1,hdef) then
  1489. begin
  1490. if (srsymtable.symtabletype=ObjectSymtable) then
  1491. p1:=load_self_node;
  1492. { not srsymtable.symtabletype since that can be }
  1493. { withsymtable as well }
  1494. if (srsym.owner.symtabletype=ObjectSymtable) then
  1495. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1496. else
  1497. { no propertysyms in records (yet) }
  1498. internalerror(2007012006);
  1499. end
  1500. else
  1501. { no method pointer }
  1502. begin
  1503. p1:=nil;
  1504. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1505. end;
  1506. end;
  1507. labelsym :
  1508. begin
  1509. { Support @label }
  1510. if getaddr then
  1511. begin
  1512. if srsym.owner<>current_procinfo.procdef.localst then
  1513. CGMessage(parser_e_label_outside_proc);
  1514. p1:=cloadnode.create(srsym,srsym.owner)
  1515. end
  1516. else
  1517. begin
  1518. consume(_COLON);
  1519. if tlabelsym(srsym).defined then
  1520. Message(sym_e_label_already_defined);
  1521. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1522. begin
  1523. tlabelsym(srsym).nonlocal:=true;
  1524. exclude(current_procinfo.procdef.procoptions,po_inline);
  1525. end;
  1526. tlabelsym(srsym).defined:=true;
  1527. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1528. tlabelsym(srsym).code:=p1;
  1529. end;
  1530. end;
  1531. errorsym :
  1532. begin
  1533. p1:=cerrornode.create;
  1534. if try_to_consume(_LKLAMMER) then
  1535. begin
  1536. parse_paras(false,false,_RKLAMMER);
  1537. consume(_RKLAMMER);
  1538. end;
  1539. end;
  1540. else
  1541. begin
  1542. p1:=cerrornode.create;
  1543. Message(parser_e_illegal_expression);
  1544. end;
  1545. end; { end case }
  1546. end;
  1547. end;
  1548. {---------------------------------------------
  1549. Factor_Read_Set
  1550. ---------------------------------------------}
  1551. { Read a set between [] }
  1552. function factor_read_set:tnode;
  1553. var
  1554. p1,p2 : tnode;
  1555. lastp,
  1556. buildp : tarrayconstructornode;
  1557. begin
  1558. buildp:=nil;
  1559. { be sure that a least one arrayconstructn is used, also for an
  1560. empty [] }
  1561. if token=_RECKKLAMMER then
  1562. buildp:=carrayconstructornode.create(nil,buildp)
  1563. else
  1564. repeat
  1565. p1:=comp_expr(true);
  1566. if try_to_consume(_POINTPOINT) then
  1567. begin
  1568. p2:=comp_expr(true);
  1569. p1:=carrayconstructorrangenode.create(p1,p2);
  1570. end;
  1571. { insert at the end of the tree, to get the correct order }
  1572. if not assigned(buildp) then
  1573. begin
  1574. buildp:=carrayconstructornode.create(p1,nil);
  1575. lastp:=buildp;
  1576. end
  1577. else
  1578. begin
  1579. lastp.right:=carrayconstructornode.create(p1,nil);
  1580. lastp:=tarrayconstructornode(lastp.right);
  1581. end;
  1582. { there could be more elements }
  1583. until not try_to_consume(_COMMA);
  1584. factor_read_set:=buildp;
  1585. end;
  1586. {---------------------------------------------
  1587. PostFixOperators
  1588. ---------------------------------------------}
  1589. { returns whether or not p1 has been changed }
  1590. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1591. { tries to avoid syntax errors after invalid qualifiers }
  1592. procedure recoverconsume_postfixops;
  1593. begin
  1594. repeat
  1595. if not try_to_consume(_CARET) then
  1596. if try_to_consume(_POINT) then
  1597. try_to_consume(_ID)
  1598. else if try_to_consume(_LECKKLAMMER) then
  1599. begin
  1600. repeat
  1601. comp_expr(true);
  1602. until not try_to_consume(_COMMA);
  1603. consume(_RECKKLAMMER);
  1604. end
  1605. else if try_to_consume(_LKLAMMER) then
  1606. begin
  1607. repeat
  1608. comp_expr(true);
  1609. until not try_to_consume(_COMMA);
  1610. consume(_RKLAMMER);
  1611. end
  1612. else
  1613. break;
  1614. until false;
  1615. end;
  1616. procedure handle_variantarray;
  1617. var
  1618. p4 : tnode;
  1619. newstatement : tstatementnode;
  1620. tempresultvariant,
  1621. temp : ttempcreatenode;
  1622. paras : tcallparanode;
  1623. newblock : tnode;
  1624. countindices : aint;
  1625. begin
  1626. { create statements with call initialize the arguments and
  1627. call fpc_dynarr_setlength }
  1628. newblock:=internalstatements(newstatement);
  1629. { get temp for array of indicies,
  1630. we set the real size later }
  1631. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1632. addstatement(newstatement,temp);
  1633. countindices:=0;
  1634. repeat
  1635. p4:=comp_expr(true);
  1636. addstatement(newstatement,cassignmentnode.create(
  1637. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1638. inc(countindices);
  1639. until not try_to_consume(_COMMA);
  1640. { set real size }
  1641. temp.size:=countindices*s32inttype.size;
  1642. consume(_RECKKLAMMER);
  1643. { we need only a write access if a := follows }
  1644. if token=_ASSIGNMENT then
  1645. begin
  1646. consume(_ASSIGNMENT);
  1647. p4:=comp_expr(true);
  1648. { create call to fpc_vararray_put }
  1649. paras:=ccallparanode.create(cordconstnode.create
  1650. (countindices,s32inttype,true),
  1651. ccallparanode.create(caddrnode.create_internal
  1652. (ctemprefnode.create(temp)),
  1653. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1654. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1655. ,nil))));
  1656. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1657. addstatement(newstatement,ctempdeletenode.create(temp));
  1658. end
  1659. else
  1660. begin
  1661. { create temp for result }
  1662. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1663. addstatement(newstatement,tempresultvariant);
  1664. { create call to fpc_vararray_get }
  1665. paras:=ccallparanode.create(cordconstnode.create
  1666. (countindices,s32inttype,true),
  1667. ccallparanode.create(caddrnode.create_internal
  1668. (ctemprefnode.create(temp)),
  1669. ccallparanode.create(p1,
  1670. ccallparanode.create(
  1671. ctemprefnode.create(tempresultvariant)
  1672. ,nil))));
  1673. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1674. addstatement(newstatement,ctempdeletenode.create(temp));
  1675. { the last statement should return the value as
  1676. location and type, this is done be referencing the
  1677. temp and converting it first from a persistent temp to
  1678. normal temp }
  1679. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1680. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1681. end;
  1682. p1:=newblock;
  1683. end;
  1684. var
  1685. protsym : tpropertysym;
  1686. p2,p3 : tnode;
  1687. srsym : tsym;
  1688. srsymtable : TSymtable;
  1689. classh : tobjectdef;
  1690. { shouldn't be used that often, so the extra overhead is ok to save
  1691. stack space }
  1692. dispatchstring : ansistring;
  1693. nodechanged : boolean;
  1694. label
  1695. skipreckklammercheck;
  1696. begin
  1697. result:=false;
  1698. again:=true;
  1699. while again do
  1700. begin
  1701. { we need the resultdef }
  1702. do_typecheckpass_changed(p1,nodechanged);
  1703. result:=result or nodechanged;
  1704. if codegenerror then
  1705. begin
  1706. recoverconsume_postfixops;
  1707. exit;
  1708. end;
  1709. { handle token }
  1710. case token of
  1711. _CARET:
  1712. begin
  1713. consume(_CARET);
  1714. { support tp/mac procvar^ if the procvar returns a
  1715. pointer type }
  1716. if ((m_tp_procvar in current_settings.modeswitches) or
  1717. (m_mac_procvar in current_settings.modeswitches)) and
  1718. (p1.resultdef.typ=procvardef) and
  1719. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1720. begin
  1721. p1:=ccallnode.create_procvar(nil,p1);
  1722. typecheckpass(p1);
  1723. end;
  1724. if (p1.resultdef.typ<>pointerdef) then
  1725. begin
  1726. { ^ as binary operator is a problem!!!! (FK) }
  1727. again:=false;
  1728. Message(parser_e_invalid_qualifier);
  1729. recoverconsume_postfixops;
  1730. p1.destroy;
  1731. p1:=cerrornode.create;
  1732. end
  1733. else
  1734. p1:=cderefnode.create(p1);
  1735. end;
  1736. _LECKKLAMMER:
  1737. begin
  1738. if is_class_or_interface_or_object(p1.resultdef) then
  1739. begin
  1740. { default property }
  1741. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1742. if not(assigned(protsym)) then
  1743. begin
  1744. p1.destroy;
  1745. p1:=cerrornode.create;
  1746. again:=false;
  1747. message(parser_e_no_default_property_available);
  1748. end
  1749. else
  1750. begin
  1751. { The property symbol is referenced indirect }
  1752. protsym.IncRefCount;
  1753. handle_propertysym(protsym,protsym.owner,p1);
  1754. end;
  1755. end
  1756. else
  1757. begin
  1758. consume(_LECKKLAMMER);
  1759. repeat
  1760. { in all of the cases below, p1 is changed }
  1761. case p1.resultdef.typ of
  1762. pointerdef:
  1763. begin
  1764. { support delphi autoderef }
  1765. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1766. (m_autoderef in current_settings.modeswitches) then
  1767. p1:=cderefnode.create(p1);
  1768. p2:=comp_expr(true);
  1769. { Support Pbytevar[0..9] which returns array [0..9].}
  1770. if try_to_consume(_POINTPOINT) then
  1771. p2:=crangenode.create(p2,comp_expr(true));
  1772. p1:=cvecnode.create(p1,p2);
  1773. end;
  1774. variantdef:
  1775. begin
  1776. handle_variantarray;
  1777. { the RECKKLAMMER is already read }
  1778. goto skipreckklammercheck;
  1779. end;
  1780. stringdef :
  1781. begin
  1782. p2:=comp_expr(true);
  1783. { Support string[0..9] which returns array [0..9] of char.}
  1784. if try_to_consume(_POINTPOINT) then
  1785. p2:=crangenode.create(p2,comp_expr(true));
  1786. p1:=cvecnode.create(p1,p2);
  1787. end;
  1788. arraydef:
  1789. begin
  1790. p2:=comp_expr(true);
  1791. { support SEG:OFS for go32v2 Mem[] }
  1792. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1793. (p1.nodetype=loadn) and
  1794. assigned(tloadnode(p1).symtableentry) and
  1795. assigned(tloadnode(p1).symtableentry.owner.name) and
  1796. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1797. ((tloadnode(p1).symtableentry.name='MEM') or
  1798. (tloadnode(p1).symtableentry.name='MEMW') or
  1799. (tloadnode(p1).symtableentry.name='MEML')) then
  1800. begin
  1801. if try_to_consume(_COLON) then
  1802. begin
  1803. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1804. p2:=comp_expr(true);
  1805. p2:=caddnode.create(addn,p2,p3);
  1806. if try_to_consume(_POINTPOINT) then
  1807. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1808. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1809. p1:=cvecnode.create(p1,p2);
  1810. include(tvecnode(p1).flags,nf_memseg);
  1811. include(tvecnode(p1).flags,nf_memindex);
  1812. end
  1813. else
  1814. begin
  1815. if try_to_consume(_POINTPOINT) then
  1816. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1817. p2:=crangenode.create(p2,comp_expr(true));
  1818. p1:=cvecnode.create(p1,p2);
  1819. include(tvecnode(p1).flags,nf_memindex);
  1820. end;
  1821. end
  1822. else
  1823. begin
  1824. if try_to_consume(_POINTPOINT) then
  1825. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1826. p2:=crangenode.create(p2,comp_expr(true));
  1827. p1:=cvecnode.create(p1,p2);
  1828. end;
  1829. end;
  1830. else
  1831. begin
  1832. if p1.resultdef.typ<>undefineddef then
  1833. Message(parser_e_invalid_qualifier);
  1834. p1.destroy;
  1835. p1:=cerrornode.create;
  1836. comp_expr(true);
  1837. again:=false;
  1838. end;
  1839. end;
  1840. do_typecheckpass(p1);
  1841. until not try_to_consume(_COMMA);
  1842. consume(_RECKKLAMMER);
  1843. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1844. skipreckklammercheck:
  1845. end;
  1846. end;
  1847. _POINT :
  1848. begin
  1849. consume(_POINT);
  1850. if (p1.resultdef.typ=pointerdef) and
  1851. (m_autoderef in current_settings.modeswitches) then
  1852. begin
  1853. p1:=cderefnode.create(p1);
  1854. do_typecheckpass(p1);
  1855. end;
  1856. case p1.resultdef.typ of
  1857. recorddef:
  1858. begin
  1859. if token=_ID then
  1860. begin
  1861. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1862. if assigned(srsym) and
  1863. (srsym.typ=fieldvarsym) then
  1864. begin
  1865. check_hints(srsym,srsym.symoptions);
  1866. p1:=csubscriptnode.create(srsym,p1)
  1867. end
  1868. else
  1869. begin
  1870. Message1(sym_e_illegal_field,pattern);
  1871. p1.destroy;
  1872. p1:=cerrornode.create;
  1873. end;
  1874. end;
  1875. consume(_ID);
  1876. end;
  1877. variantdef:
  1878. begin
  1879. { dispatch call? }
  1880. if token=_ID then
  1881. begin
  1882. dispatchstring:=orgpattern;
  1883. consume(_ID);
  1884. if try_to_consume(_LKLAMMER) then
  1885. begin
  1886. p2:=parse_paras(false,true,_RKLAMMER);
  1887. consume(_RKLAMMER);
  1888. end
  1889. else
  1890. p2:=nil;
  1891. { property setter? }
  1892. if (token=_ASSIGNMENT) and not(afterassignment) then
  1893. begin
  1894. consume(_ASSIGNMENT);
  1895. { read the expression }
  1896. p3:=comp_expr(true);
  1897. { concat value parameter too }
  1898. p2:=ccallparanode.create(p3,p2);
  1899. { passing p3 here is only for information purposes }
  1900. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,false);
  1901. end
  1902. else
  1903. begin
  1904. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,
  1905. { this is only an approximation
  1906. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1907. afterassignment or in_args or (token<>_SEMICOLON));
  1908. end;
  1909. end
  1910. else { Error }
  1911. Consume(_ID);
  1912. end;
  1913. classrefdef:
  1914. begin
  1915. if token=_ID then
  1916. begin
  1917. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1918. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1919. if assigned(srsym) then
  1920. begin
  1921. check_hints(srsym,srsym.symoptions);
  1922. consume(_ID);
  1923. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1924. end
  1925. else
  1926. begin
  1927. Message1(sym_e_id_no_member,orgpattern);
  1928. p1.destroy;
  1929. p1:=cerrornode.create;
  1930. { try to clean up }
  1931. consume(_ID);
  1932. end;
  1933. end
  1934. else { Error }
  1935. Consume(_ID);
  1936. end;
  1937. objectdef:
  1938. begin
  1939. if token=_ID then
  1940. begin
  1941. classh:=tobjectdef(p1.resultdef);
  1942. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1943. if assigned(srsym) then
  1944. begin
  1945. check_hints(srsym,srsym.symoptions);
  1946. consume(_ID);
  1947. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1948. end
  1949. else
  1950. begin
  1951. Message1(sym_e_id_no_member,orgpattern);
  1952. p1.destroy;
  1953. p1:=cerrornode.create;
  1954. { try to clean up }
  1955. consume(_ID);
  1956. end;
  1957. end
  1958. else { Error }
  1959. Consume(_ID);
  1960. end;
  1961. pointerdef:
  1962. begin
  1963. Message(parser_e_invalid_qualifier);
  1964. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  1965. Message(parser_h_maybe_deref_caret_missing);
  1966. end;
  1967. else
  1968. begin
  1969. if p1.resultdef.typ<>undefineddef then
  1970. Message(parser_e_invalid_qualifier);
  1971. p1.destroy;
  1972. p1:=cerrornode.create;
  1973. { Error }
  1974. consume(_ID);
  1975. end;
  1976. end;
  1977. end;
  1978. else
  1979. begin
  1980. { is this a procedure variable ? }
  1981. if assigned(p1.resultdef) and
  1982. (p1.resultdef.typ=procvardef) then
  1983. begin
  1984. { Typenode for typecasting or expecting a procvar }
  1985. if (p1.nodetype=typen) or
  1986. (
  1987. assigned(getprocvardef) and
  1988. equal_defs(p1.resultdef,getprocvardef)
  1989. ) then
  1990. begin
  1991. if try_to_consume(_LKLAMMER) then
  1992. begin
  1993. p1:=comp_expr(true);
  1994. consume(_RKLAMMER);
  1995. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  1996. end
  1997. else
  1998. again:=false
  1999. end
  2000. else
  2001. begin
  2002. if try_to_consume(_LKLAMMER) then
  2003. begin
  2004. p2:=parse_paras(false,false,_RKLAMMER);
  2005. consume(_RKLAMMER);
  2006. p1:=ccallnode.create_procvar(p2,p1);
  2007. { proc():= is never possible }
  2008. if token=_ASSIGNMENT then
  2009. begin
  2010. Message(parser_e_illegal_expression);
  2011. p1.free;
  2012. p1:=cerrornode.create;
  2013. again:=false;
  2014. end;
  2015. end
  2016. else
  2017. again:=false;
  2018. end;
  2019. end
  2020. else
  2021. again:=false;
  2022. end;
  2023. end;
  2024. { we only try again if p1 was changed }
  2025. if again or
  2026. (p1.nodetype=errorn) then
  2027. result:=true;
  2028. end; { while again }
  2029. end;
  2030. {---------------------------------------------
  2031. Factor (Main)
  2032. ---------------------------------------------}
  2033. var
  2034. l : longint;
  2035. ic : int64;
  2036. qc : qword;
  2037. p1 : tnode;
  2038. code : integer;
  2039. srsym : tsym;
  2040. srsymtable : TSymtable;
  2041. pd : tprocdef;
  2042. hclassdef : tobjectdef;
  2043. d : bestreal;
  2044. cur : currency;
  2045. hs,hsorg : string;
  2046. hdef : tdef;
  2047. filepos : tfileposinfo;
  2048. again,
  2049. updatefpos,
  2050. nodechanged : boolean;
  2051. begin
  2052. { can't keep a copy of p1 and compare pointers afterwards, because
  2053. p1 may be freed and reallocated in the same place! }
  2054. updatefpos:=false;
  2055. p1:=nil;
  2056. filepos:=current_tokenpos;
  2057. again:=false;
  2058. if token=_ID then
  2059. begin
  2060. again:=true;
  2061. { Handle references to self }
  2062. if (idtoken=_SELF) and
  2063. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2064. assigned(current_objectdef) then
  2065. begin
  2066. p1:=load_self_node;
  2067. consume(_ID);
  2068. again:=true;
  2069. end
  2070. else
  2071. factor_read_id(p1,again);
  2072. if assigned(p1) then
  2073. begin
  2074. { factor_read_id will set the filepos to after the id,
  2075. and in case of _SELF the filepos will already be the
  2076. same as filepos (so setting it again doesn't hurt). }
  2077. p1.fileinfo:=filepos;
  2078. filepos:=current_tokenpos;
  2079. end;
  2080. { handle post fix operators }
  2081. updatefpos:=postfixoperators(p1,again);
  2082. end
  2083. else
  2084. begin
  2085. updatefpos:=true;
  2086. case token of
  2087. _RETURN :
  2088. begin
  2089. consume(_RETURN);
  2090. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2091. p1 := cexitnode.create(comp_expr(true))
  2092. else
  2093. p1 := cexitnode.create(nil);
  2094. end;
  2095. _INHERITED :
  2096. begin
  2097. again:=true;
  2098. consume(_INHERITED);
  2099. if assigned(current_procinfo) and
  2100. assigned(current_objectdef) then
  2101. begin
  2102. hclassdef:=current_objectdef.childof;
  2103. { if inherited; only then we need the method with
  2104. the same name }
  2105. if token in endtokens then
  2106. begin
  2107. hs:=current_procinfo.procdef.procsym.name;
  2108. hsorg:=current_procinfo.procdef.procsym.realname;
  2109. anon_inherited:=true;
  2110. { For message methods we need to search using the message
  2111. number or string }
  2112. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2113. srdef:=nil;
  2114. if (po_msgint in pd.procoptions) then
  2115. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2116. else
  2117. if (po_msgstr in pd.procoptions) then
  2118. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2119. else
  2120. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2121. end
  2122. else
  2123. begin
  2124. hs:=pattern;
  2125. hsorg:=orgpattern;
  2126. consume(_ID);
  2127. anon_inherited:=false;
  2128. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2129. end;
  2130. if assigned(srsym) then
  2131. begin
  2132. check_hints(srsym,srsym.symoptions);
  2133. { load the procdef from the inherited class and
  2134. not from self }
  2135. case srsym.typ of
  2136. procsym:
  2137. begin
  2138. hdef:=hclassdef;
  2139. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2140. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2141. hdef:=tclassrefdef.create(hdef);
  2142. p1:=ctypenode.create(hdef);
  2143. end;
  2144. propertysym:
  2145. ;
  2146. else
  2147. begin
  2148. Message(parser_e_methode_id_expected);
  2149. p1:=cerrornode.create;
  2150. end;
  2151. end;
  2152. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2153. end
  2154. else
  2155. begin
  2156. if anon_inherited then
  2157. begin
  2158. { For message methods we need to call DefaultHandler }
  2159. if (po_msgint in pd.procoptions) or
  2160. (po_msgstr in pd.procoptions) then
  2161. begin
  2162. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2163. if not assigned(srsym) or
  2164. (srsym.typ<>procsym) then
  2165. internalerror(200303171);
  2166. p1:=nil;
  2167. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2168. end
  2169. else
  2170. begin
  2171. { we need to ignore the inherited; }
  2172. p1:=cnothingnode.create;
  2173. end;
  2174. end
  2175. else
  2176. begin
  2177. Message1(sym_e_id_no_member,hsorg);
  2178. p1:=cerrornode.create;
  2179. end;
  2180. again:=false;
  2181. end;
  2182. { turn auto inheriting off }
  2183. anon_inherited:=false;
  2184. end
  2185. else
  2186. begin
  2187. Message(parser_e_generic_methods_only_in_methods);
  2188. again:=false;
  2189. p1:=cerrornode.create;
  2190. end;
  2191. postfixoperators(p1,again);
  2192. end;
  2193. _INTCONST :
  2194. begin
  2195. {Try first wether the value fits in an int64.}
  2196. val(pattern,ic,code);
  2197. if code=0 then
  2198. begin
  2199. consume(_INTCONST);
  2200. int_to_type(ic,hdef);
  2201. p1:=cordconstnode.create(ic,hdef,true);
  2202. end
  2203. else
  2204. begin
  2205. { try qword next }
  2206. val(pattern,qc,code);
  2207. if code=0 then
  2208. begin
  2209. consume(_INTCONST);
  2210. int_to_type(qc,hdef);
  2211. p1:=cordconstnode.create(qc,hdef,true);
  2212. end;
  2213. end;
  2214. if code<>0 then
  2215. begin
  2216. { finally float }
  2217. val(pattern,d,code);
  2218. if code<>0 then
  2219. begin
  2220. Message(parser_e_invalid_integer);
  2221. consume(_INTCONST);
  2222. l:=1;
  2223. p1:=cordconstnode.create(l,sinttype,true);
  2224. end
  2225. else
  2226. begin
  2227. consume(_INTCONST);
  2228. p1:=crealconstnode.create(d,pbestrealtype^);
  2229. end;
  2230. end
  2231. else
  2232. { the necessary range checking has already been done by val }
  2233. tordconstnode(p1).rangecheck:=false;
  2234. end;
  2235. _REALNUMBER :
  2236. begin
  2237. val(pattern,d,code);
  2238. if code<>0 then
  2239. begin
  2240. Message(parser_e_error_in_real);
  2241. d:=1.0;
  2242. end;
  2243. consume(_REALNUMBER);
  2244. {$ifdef FPC_REAL2REAL_FIXED}
  2245. if current_settings.fputype=fpu_none then
  2246. Message(parser_e_unsupported_real);
  2247. if (current_settings.minfpconstprec=s32real) and
  2248. (d = single(d)) then
  2249. p1:=crealconstnode.create(d,s32floattype)
  2250. else if (current_settings.minfpconstprec=s64real) and
  2251. (d = double(d)) then
  2252. p1:=crealconstnode.create(d,s64floattype)
  2253. else
  2254. {$endif FPC_REAL2REAL_FIXED}
  2255. p1:=crealconstnode.create(d,pbestrealtype^);
  2256. {$ifdef FPC_HAS_STR_CURRENCY}
  2257. val(pattern,cur,code);
  2258. if code=0 then
  2259. trealconstnode(p1).value_currency:=cur;
  2260. {$endif FPC_HAS_STR_CURRENCY}
  2261. end;
  2262. _STRING :
  2263. begin
  2264. string_dec(hdef,true);
  2265. { STRING can be also a type cast }
  2266. if try_to_consume(_LKLAMMER) then
  2267. begin
  2268. p1:=comp_expr(true);
  2269. consume(_RKLAMMER);
  2270. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2271. { handle postfix operators here e.g. string(a)[10] }
  2272. again:=true;
  2273. postfixoperators(p1,again);
  2274. end
  2275. else
  2276. p1:=ctypenode.create(hdef);
  2277. end;
  2278. _FILE :
  2279. begin
  2280. hdef:=cfiletype;
  2281. consume(_FILE);
  2282. { FILE can be also a type cast }
  2283. if try_to_consume(_LKLAMMER) then
  2284. begin
  2285. p1:=comp_expr(true);
  2286. consume(_RKLAMMER);
  2287. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2288. { handle postfix operators here e.g. string(a)[10] }
  2289. again:=true;
  2290. postfixoperators(p1,again);
  2291. end
  2292. else
  2293. begin
  2294. p1:=ctypenode.create(hdef);
  2295. end;
  2296. end;
  2297. _CSTRING :
  2298. begin
  2299. p1:=cstringconstnode.createstr(pattern);
  2300. consume(_CSTRING);
  2301. end;
  2302. _CCHAR :
  2303. begin
  2304. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2305. consume(_CCHAR);
  2306. end;
  2307. _CWSTRING:
  2308. begin
  2309. p1:=cstringconstnode.createwstr(patternw);
  2310. consume(_CWSTRING);
  2311. end;
  2312. _CWCHAR:
  2313. begin
  2314. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2315. consume(_CWCHAR);
  2316. end;
  2317. _KLAMMERAFFE :
  2318. begin
  2319. consume(_KLAMMERAFFE);
  2320. got_addrn:=true;
  2321. { support both @<x> and @(<x>) }
  2322. if try_to_consume(_LKLAMMER) then
  2323. begin
  2324. p1:=factor(true);
  2325. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2326. begin
  2327. again:=true;
  2328. postfixoperators(p1,again);
  2329. end
  2330. else
  2331. consume(_RKLAMMER);
  2332. end
  2333. else
  2334. p1:=factor(true);
  2335. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2336. begin
  2337. again:=true;
  2338. postfixoperators(p1,again);
  2339. end;
  2340. got_addrn:=false;
  2341. p1:=caddrnode.create(p1);
  2342. p1.fileinfo:=filepos;
  2343. if cs_typed_addresses in current_settings.localswitches then
  2344. include(p1.flags,nf_typedaddr);
  2345. { Store the procvar that we are expecting, the
  2346. addrn will use the information to find the correct
  2347. procdef or it will return an error }
  2348. if assigned(getprocvardef) and
  2349. (taddrnode(p1).left.nodetype = loadn) then
  2350. taddrnode(p1).getprocvardef:=getprocvardef;
  2351. end;
  2352. _LKLAMMER :
  2353. begin
  2354. consume(_LKLAMMER);
  2355. p1:=comp_expr(true);
  2356. consume(_RKLAMMER);
  2357. { it's not a good solution }
  2358. { but (a+b)^ makes some problems }
  2359. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2360. begin
  2361. again:=true;
  2362. postfixoperators(p1,again);
  2363. end;
  2364. end;
  2365. _LECKKLAMMER :
  2366. begin
  2367. consume(_LECKKLAMMER);
  2368. p1:=factor_read_set;
  2369. consume(_RECKKLAMMER);
  2370. end;
  2371. _PLUS :
  2372. begin
  2373. consume(_PLUS);
  2374. p1:=factor(false);
  2375. { we must generate a new node to do 0+<p1> otherwise the + will
  2376. not be checked }
  2377. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2378. end;
  2379. _MINUS :
  2380. begin
  2381. consume(_MINUS);
  2382. if (token = _INTCONST) then
  2383. begin
  2384. { ugly hack, but necessary to be able to parse }
  2385. { -9223372036854775808 as int64 (JM) }
  2386. pattern := '-'+pattern;
  2387. p1:=sub_expr(oppower,false);
  2388. { -1 ** 4 should be - (1 ** 4) and not
  2389. (-1) ** 4
  2390. This was the reason of tw0869.pp test failure PM }
  2391. if p1.nodetype=starstarn then
  2392. begin
  2393. if tbinarynode(p1).left.nodetype=ordconstn then
  2394. begin
  2395. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2396. p1:=cunaryminusnode.create(p1);
  2397. end
  2398. else if tbinarynode(p1).left.nodetype=realconstn then
  2399. begin
  2400. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2401. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2402. p1:=cunaryminusnode.create(p1);
  2403. end
  2404. else
  2405. internalerror(20021029);
  2406. end;
  2407. end
  2408. else
  2409. begin
  2410. p1:=sub_expr(oppower,false);
  2411. p1:=cunaryminusnode.create(p1);
  2412. end;
  2413. end;
  2414. _OP_NOT :
  2415. begin
  2416. consume(_OP_NOT);
  2417. p1:=factor(false);
  2418. p1:=cnotnode.create(p1);
  2419. end;
  2420. _TRUE :
  2421. begin
  2422. consume(_TRUE);
  2423. p1:=cordconstnode.create(1,booltype,false);
  2424. end;
  2425. _FALSE :
  2426. begin
  2427. consume(_FALSE);
  2428. p1:=cordconstnode.create(0,booltype,false);
  2429. end;
  2430. _NIL :
  2431. begin
  2432. consume(_NIL);
  2433. p1:=cnilnode.create;
  2434. { It's really ugly code nil^, but delphi allows it }
  2435. if token in [_CARET] then
  2436. begin
  2437. again:=true;
  2438. postfixoperators(p1,again);
  2439. end;
  2440. end;
  2441. else
  2442. begin
  2443. Message(parser_e_illegal_expression);
  2444. p1:=cerrornode.create;
  2445. { recover }
  2446. consume(token);
  2447. end;
  2448. end;
  2449. end;
  2450. { generate error node if no node is created }
  2451. if not assigned(p1) then
  2452. begin
  2453. {$ifdef EXTDEBUG}
  2454. Comment(V_Warning,'factor: p1=nil');
  2455. {$endif}
  2456. p1:=cerrornode.create;
  2457. updatefpos:=true;
  2458. end;
  2459. { get the resultdef for the node }
  2460. if (not assigned(p1.resultdef)) then
  2461. begin
  2462. do_typecheckpass_changed(p1,nodechanged);
  2463. updatefpos:=updatefpos or nodechanged;
  2464. end;
  2465. if assigned(p1) and
  2466. updatefpos then
  2467. p1.fileinfo:=filepos;
  2468. factor:=p1;
  2469. end;
  2470. {$maxfpuregisters default}
  2471. {****************************************************************************
  2472. Sub_Expr
  2473. ****************************************************************************}
  2474. const
  2475. { Warning these stay be ordered !! }
  2476. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2477. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2478. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2479. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2480. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2481. [_STARSTAR] );
  2482. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2483. {Reads a subexpression while the operators are of the current precedence
  2484. level, or any higher level. Replaces the old term, simpl_expr and
  2485. simpl2_expr.}
  2486. var
  2487. p1,p2 : tnode;
  2488. oldt : Ttoken;
  2489. filepos : tfileposinfo;
  2490. begin
  2491. if pred_level=highest_precedence then
  2492. p1:=factor(false)
  2493. else
  2494. p1:=sub_expr(succ(pred_level),true);
  2495. repeat
  2496. if (token in operator_levels[pred_level]) and
  2497. ((token<>_EQUAL) or accept_equal) then
  2498. begin
  2499. oldt:=token;
  2500. filepos:=current_tokenpos;
  2501. consume(token);
  2502. if pred_level=highest_precedence then
  2503. p2:=factor(false)
  2504. else
  2505. p2:=sub_expr(succ(pred_level),true);
  2506. case oldt of
  2507. _PLUS :
  2508. p1:=caddnode.create(addn,p1,p2);
  2509. _MINUS :
  2510. p1:=caddnode.create(subn,p1,p2);
  2511. _STAR :
  2512. p1:=caddnode.create(muln,p1,p2);
  2513. _SLASH :
  2514. p1:=caddnode.create(slashn,p1,p2);
  2515. _EQUAL :
  2516. p1:=caddnode.create(equaln,p1,p2);
  2517. _GT :
  2518. p1:=caddnode.create(gtn,p1,p2);
  2519. _LT :
  2520. p1:=caddnode.create(ltn,p1,p2);
  2521. _GTE :
  2522. p1:=caddnode.create(gten,p1,p2);
  2523. _LTE :
  2524. p1:=caddnode.create(lten,p1,p2);
  2525. _SYMDIF :
  2526. p1:=caddnode.create(symdifn,p1,p2);
  2527. _STARSTAR :
  2528. p1:=caddnode.create(starstarn,p1,p2);
  2529. _OP_AS :
  2530. p1:=casnode.create(p1,p2);
  2531. _OP_IN :
  2532. p1:=cinnode.create(p1,p2);
  2533. _OP_IS :
  2534. p1:=cisnode.create(p1,p2);
  2535. _OP_OR,
  2536. _PIPE {macpas only} :
  2537. begin
  2538. p1:=caddnode.create(orn,p1,p2);
  2539. if (oldt = _PIPE) then
  2540. include(p1.flags,nf_short_bool);
  2541. end;
  2542. _OP_AND,
  2543. _AMPERSAND {macpas only} :
  2544. begin
  2545. p1:=caddnode.create(andn,p1,p2);
  2546. if (oldt = _AMPERSAND) then
  2547. include(p1.flags,nf_short_bool);
  2548. end;
  2549. _OP_DIV :
  2550. p1:=cmoddivnode.create(divn,p1,p2);
  2551. _OP_NOT :
  2552. p1:=cnotnode.create(p1);
  2553. _OP_MOD :
  2554. p1:=cmoddivnode.create(modn,p1,p2);
  2555. _OP_SHL :
  2556. p1:=cshlshrnode.create(shln,p1,p2);
  2557. _OP_SHR :
  2558. p1:=cshlshrnode.create(shrn,p1,p2);
  2559. _OP_XOR :
  2560. p1:=caddnode.create(xorn,p1,p2);
  2561. _ASSIGNMENT :
  2562. p1:=cassignmentnode.create(p1,p2);
  2563. _UNEQUAL :
  2564. p1:=caddnode.create(unequaln,p1,p2);
  2565. end;
  2566. p1.fileinfo:=filepos;
  2567. end
  2568. else
  2569. break;
  2570. until false;
  2571. sub_expr:=p1;
  2572. end;
  2573. function comp_expr(accept_equal : boolean):tnode;
  2574. var
  2575. oldafterassignment : boolean;
  2576. p1 : tnode;
  2577. begin
  2578. oldafterassignment:=afterassignment;
  2579. afterassignment:=true;
  2580. p1:=sub_expr(opcompare,accept_equal);
  2581. { get the resultdef for this expression }
  2582. if not assigned(p1.resultdef) then
  2583. do_typecheckpass(p1);
  2584. afterassignment:=oldafterassignment;
  2585. comp_expr:=p1;
  2586. end;
  2587. function expr : tnode;
  2588. var
  2589. p1,p2 : tnode;
  2590. filepos : tfileposinfo;
  2591. oldafterassignment,
  2592. updatefpos : boolean;
  2593. begin
  2594. oldafterassignment:=afterassignment;
  2595. p1:=sub_expr(opcompare,true);
  2596. { get the resultdef for this expression }
  2597. if not assigned(p1.resultdef) then
  2598. do_typecheckpass(p1);
  2599. filepos:=current_tokenpos;
  2600. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2601. afterassignment:=true;
  2602. updatefpos:=true;
  2603. case token of
  2604. _POINTPOINT :
  2605. begin
  2606. consume(_POINTPOINT);
  2607. p2:=sub_expr(opcompare,true);
  2608. p1:=crangenode.create(p1,p2);
  2609. end;
  2610. _ASSIGNMENT :
  2611. begin
  2612. consume(_ASSIGNMENT);
  2613. if (p1.resultdef.typ=procvardef) then
  2614. getprocvardef:=tprocvardef(p1.resultdef);
  2615. p2:=sub_expr(opcompare,true);
  2616. if assigned(getprocvardef) then
  2617. handle_procvar(getprocvardef,p2);
  2618. getprocvardef:=nil;
  2619. p1:=cassignmentnode.create(p1,p2);
  2620. end;
  2621. _PLUSASN :
  2622. begin
  2623. consume(_PLUSASN);
  2624. p2:=sub_expr(opcompare,true);
  2625. p1:=gen_c_style_operator(addn,p1,p2);
  2626. end;
  2627. _MINUSASN :
  2628. begin
  2629. consume(_MINUSASN);
  2630. p2:=sub_expr(opcompare,true);
  2631. p1:=gen_c_style_operator(subn,p1,p2);
  2632. end;
  2633. _STARASN :
  2634. begin
  2635. consume(_STARASN );
  2636. p2:=sub_expr(opcompare,true);
  2637. p1:=gen_c_style_operator(muln,p1,p2);
  2638. end;
  2639. _SLASHASN :
  2640. begin
  2641. consume(_SLASHASN );
  2642. p2:=sub_expr(opcompare,true);
  2643. p1:=gen_c_style_operator(slashn,p1,p2);
  2644. end;
  2645. else
  2646. updatefpos:=false;
  2647. end;
  2648. { get the resultdef for this expression }
  2649. if not assigned(p1.resultdef) then
  2650. do_typecheckpass(p1);
  2651. afterassignment:=oldafterassignment;
  2652. if updatefpos then
  2653. p1.fileinfo:=filepos;
  2654. expr:=p1;
  2655. end;
  2656. function get_intconst:TConstExprInt;
  2657. {Reads an expression, tries to evalute it and check if it is an integer
  2658. constant. Then the constant is returned.}
  2659. var
  2660. p:tnode;
  2661. begin
  2662. result:=0;
  2663. p:=comp_expr(true);
  2664. if not codegenerror then
  2665. begin
  2666. if (p.nodetype<>ordconstn) or
  2667. not(is_integer(p.resultdef)) then
  2668. Message(parser_e_illegal_expression)
  2669. else
  2670. result:=tordconstnode(p).value;
  2671. end;
  2672. p.free;
  2673. end;
  2674. function get_stringconst:string;
  2675. {Reads an expression, tries to evaluate it and checks if it is a string
  2676. constant. Then the constant is returned.}
  2677. var
  2678. p:tnode;
  2679. begin
  2680. get_stringconst:='';
  2681. p:=comp_expr(true);
  2682. if p.nodetype<>stringconstn then
  2683. begin
  2684. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2685. get_stringconst:=char(int64(tordconstnode(p).value))
  2686. else
  2687. Message(parser_e_illegal_expression);
  2688. end
  2689. else
  2690. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2691. p.free;
  2692. end;
  2693. end.