pexpr.pas 109 KB

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