pexpr.pas 107 KB

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