pdecsub.pas 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used in implementation section }
  26. pd_interface, { directive can be used in interface section }
  27. pd_object, { directive can be used with object declaration }
  28. pd_record, { directive can be used with record declaration }
  29. pd_procvar, { directive can be used with procvar declaration }
  30. pd_notobject, { directive can not be used with object declaration }
  31. pd_notrecord, { directive can not be used with record declaration }
  32. pd_notobjintf, { directive can not be used with interface declaration }
  33. pd_notprocvar, { directive can not be used with procvar declaration }
  34. pd_dispinterface,{ directive can be used with dispinterface methods }
  35. pd_cppobject, { directive can be used with cppclass }
  36. pd_objcclass, { directive can be used with objcclass }
  37. pd_objcprot, { directive can be used with objcprotocol }
  38. pd_nothelper, { directive can not be used with record/class helper declaration }
  39. pd_javaclass, { directive can be used with Java class }
  40. pd_intfjava { directive can be used with Java interface }
  41. );
  42. tpdflags=set of tpdflag;
  43. // flags of handle_calling_convention routine
  44. thccflag=(
  45. hcc_check, // perform checks and outup errors if found
  46. hcc_insert_hidden_paras // insert hidden parameters
  47. );
  48. thccflags=set of thccflag;
  49. const
  50. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  51. function check_proc_directive(isprocvar:boolean):boolean;
  52. function proc_add_definition(var currpd:tprocdef):boolean;
  53. function proc_get_importname(pd:tprocdef):string;
  54. procedure proc_set_mangledname(pd:tprocdef);
  55. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  56. procedure parse_parameter_dec(pd:tabstractprocdef);
  57. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  58. procedure parse_var_proc_directives(sym:tsym);
  59. procedure parse_object_proc_directives(pd:tabstractprocdef);
  60. procedure parse_record_proc_directives(pd:tabstractprocdef);
  61. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  62. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  63. { parse a record method declaration (not a (class) constructor/destructor) }
  64. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  65. procedure insert_record_hidden_paras(astruct: trecorddef);
  66. { helper functions - they insert nested objects hierarcy to the symtablestack
  67. with object hierarchy
  68. }
  69. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  70. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  71. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  72. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  73. implementation
  74. uses
  75. SysUtils,
  76. { common }
  77. cutils,cclasses,
  78. { global }
  79. globtype,globals,verbose,constexp,
  80. systems,fpccrc,
  81. cpuinfo,
  82. { symtable }
  83. symbase,symtable,symcreat,defutil,defcmp,paramgr,cpupara,
  84. { pass 1 }
  85. fmodule,node,htypechk,
  86. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  87. objcutil,
  88. { parser }
  89. scanner,
  90. pbase,pexpr,ptype,pdecl,pparautl
  91. {$ifdef jvm}
  92. ,pjvm
  93. {$endif}
  94. ;
  95. const
  96. { Please leave this here, this module should NOT use
  97. these variables.
  98. Declaring it as string here results in an error when compiling (PFV) }
  99. current_procinfo = 'error';
  100. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  101. var
  102. _class,hp : tobjectdef;
  103. begin
  104. if obj.typ=recorddef then
  105. begin
  106. symtablestack.push(obj.symtable);
  107. result:=1;
  108. exit;
  109. end;
  110. result:=0;
  111. { insert class hierarchy in the reverse order }
  112. hp:=nil;
  113. repeat
  114. _class:=tobjectdef(obj);
  115. while _class.childof<>hp do
  116. _class:=_class.childof;
  117. hp:=_class;
  118. symtablestack.push(_class.symtable);
  119. inc(result);
  120. until hp=obj;
  121. end;
  122. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  123. begin
  124. result:=0;
  125. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  126. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  127. inc(result,push_child_hierarcy(obj));
  128. end;
  129. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  130. var
  131. _class : tobjectdef;
  132. begin
  133. if obj.typ=recorddef then
  134. begin
  135. symtablestack.pop(obj.symtable);
  136. result:=1;
  137. exit;
  138. end;
  139. result:=0;
  140. _class:=tobjectdef(obj);
  141. while assigned(_class) do
  142. begin
  143. symtablestack.pop(_class.symtable);
  144. _class:=_class.childof;
  145. inc(result);
  146. end;
  147. end;
  148. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  149. begin
  150. result:=pop_child_hierarchy(obj);
  151. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  152. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  153. end;
  154. procedure check_msg_para(p:TObject;arg:pointer);
  155. begin
  156. if (tsym(p).typ<>paravarsym) then
  157. exit;
  158. with tparavarsym(p) do
  159. begin
  160. { Count parameters }
  161. if (paranr>=10) then
  162. inc(plongint(arg)^);
  163. { First parameter must be var }
  164. if (paranr=10) and
  165. (varspez<>vs_var) then
  166. MessagePos(fileinfo,parser_e_ill_msg_param);
  167. end;
  168. end;
  169. procedure set_addr_param_regable(p:TObject;arg:pointer);
  170. begin
  171. if (tsym(p).typ<>paravarsym) then
  172. exit;
  173. with tparavarsym(p) do
  174. begin
  175. if (not needs_finalization) and
  176. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  177. varregable:=vr_intreg;
  178. end;
  179. end;
  180. procedure parse_parameter_dec(pd:tabstractprocdef);
  181. {
  182. handle_procvar needs the same changes
  183. }
  184. type
  185. tppv = (pv_none,pv_proc,pv_func);
  186. var
  187. sc : TFPObjectList;
  188. hdef : tdef;
  189. arrayelementdef : tdef;
  190. vs : tparavarsym;
  191. i : longint;
  192. srsym : tsym;
  193. pv : tprocvardef;
  194. varspez : Tvarspez;
  195. defaultvalue : tconstsym;
  196. defaultrequired : boolean;
  197. old_block_type : tblock_type;
  198. currparast : tparasymtable;
  199. parseprocvar : tppv;
  200. locationstr : string;
  201. paranr : integer;
  202. dummytype : ttypesym;
  203. explicit_paraloc,
  204. need_array,
  205. is_univ: boolean;
  206. begin
  207. old_block_type:=block_type;
  208. explicit_paraloc:=false;
  209. consume(_LKLAMMER);
  210. { Delphi/Kylix supports nonsense like }
  211. { procedure p(); }
  212. if try_to_consume(_RKLAMMER) and
  213. not(m_tp7 in current_settings.modeswitches) then
  214. exit;
  215. { parsing a proc or procvar ? }
  216. currparast:=tparasymtable(pd.parast);
  217. { reset }
  218. sc:=TFPObjectList.create(false);
  219. defaultrequired:=false;
  220. paranr:=0;
  221. block_type:=bt_var;
  222. is_univ:=false;
  223. repeat
  224. parseprocvar:=pv_none;
  225. if try_to_consume(_VAR) then
  226. varspez:=vs_var
  227. else
  228. if try_to_consume(_CONST) then
  229. varspez:=vs_const
  230. else
  231. if (m_out in current_settings.modeswitches) and
  232. try_to_consume(_OUT) then
  233. varspez:=vs_out
  234. else
  235. if try_to_consume(_CONSTREF) then
  236. varspez:=vs_constref
  237. else
  238. if (m_mac in current_settings.modeswitches) and
  239. try_to_consume(_POINTPOINTPOINT) then
  240. begin
  241. include(pd.procoptions,po_varargs);
  242. break;
  243. end
  244. else
  245. if (m_nested_procvars in current_settings.modeswitches) and
  246. try_to_consume(_PROCEDURE) then
  247. begin
  248. parseprocvar:=pv_proc;
  249. varspez:=vs_const;
  250. end
  251. else
  252. if (m_nested_procvars in current_settings.modeswitches) and
  253. try_to_consume(_FUNCTION) then
  254. begin
  255. parseprocvar:=pv_func;
  256. varspez:=vs_const;
  257. end
  258. else
  259. varspez:=vs_value;
  260. defaultvalue:=nil;
  261. hdef:=nil;
  262. { read identifiers and insert with error type }
  263. sc.clear;
  264. repeat
  265. inc(paranr);
  266. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  267. currparast.insert(vs);
  268. if assigned(vs.owner) then
  269. sc.add(vs)
  270. else
  271. vs.free;
  272. consume(_ID);
  273. until not try_to_consume(_COMMA);
  274. locationstr:='';
  275. { macpas anonymous procvar }
  276. if parseprocvar<>pv_none then
  277. begin
  278. { inline procvar definitions are always nested procvars }
  279. pv:=tprocvardef.create(normal_function_level+1);
  280. if token=_LKLAMMER then
  281. parse_parameter_dec(pv);
  282. if parseprocvar=pv_func then
  283. begin
  284. block_type:=bt_var_type;
  285. consume(_COLON);
  286. single_type(pv.returndef,[]);
  287. block_type:=bt_var;
  288. end;
  289. hdef:=pv;
  290. { possible proc directives }
  291. if check_proc_directive(true) then
  292. begin
  293. dummytype:=ttypesym.create('unnamed',hdef);
  294. parse_var_proc_directives(tsym(dummytype));
  295. dummytype.typedef:=nil;
  296. hdef.typesym:=nil;
  297. dummytype.free;
  298. end;
  299. { Add implicit hidden parameters and function result }
  300. handle_calling_convention(pv);
  301. {$ifdef jvm}
  302. { anonymous -> no name }
  303. jvm_create_procvar_class('',pv);
  304. {$endif}
  305. end
  306. else
  307. { read type declaration, force reading for value paras }
  308. if (token=_COLON) or (varspez=vs_value) then
  309. begin
  310. consume(_COLON);
  311. { check for an open array }
  312. need_array:=false;
  313. { bitpacked open array are not yet supported }
  314. if (token=_PACKED) and
  315. not(cs_bitpacking in current_settings.localswitches) then
  316. begin
  317. consume(_PACKED);
  318. need_array:=true;
  319. end;
  320. if (token=_ARRAY) or
  321. need_array then
  322. begin
  323. consume(_ARRAY);
  324. consume(_OF);
  325. { define range and type of range }
  326. hdef:=tarraydef.create(0,-1,s32inttype);
  327. { array of const ? }
  328. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  329. begin
  330. consume(_CONST);
  331. srsym:=search_system_type('TVARREC');
  332. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  333. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  334. end
  335. else
  336. begin
  337. { define field type }
  338. single_type(arrayelementdef,[]);
  339. tarraydef(hdef).elementdef:=arrayelementdef;
  340. end;
  341. end
  342. else
  343. begin
  344. if (m_mac in current_settings.modeswitches) then
  345. is_univ:=try_to_consume(_UNIV);
  346. if try_to_consume(_TYPE) then
  347. hdef:=ctypedformaltype
  348. else
  349. begin
  350. block_type:=bt_var_type;
  351. single_type(hdef,[stoAllowSpecialization]);
  352. block_type:=bt_var;
  353. end;
  354. { open string ? }
  355. if is_shortstring(hdef) then
  356. begin
  357. case varspez of
  358. vs_var,vs_out,vs_constref:
  359. begin
  360. { not 100% Delphi-compatible: type xstr=string[255] cannot
  361. become an openstring there, while here it can }
  362. if (cs_openstring in current_settings.moduleswitches) and
  363. (tstringdef(hdef).len=255) then
  364. hdef:=openshortstringtype
  365. end;
  366. vs_value:
  367. begin
  368. { value "openstring" parameters don't make sense (the
  369. original string can never be modified, so there's no
  370. use in passing its original length), so change these
  371. into regular shortstring parameters (seems to be what
  372. Delphi also does) }
  373. if is_open_string(hdef) then
  374. hdef:=cshortstringtype;
  375. end;
  376. end;
  377. end;
  378. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  379. begin
  380. if (idtoken=_LOCATION) then
  381. begin
  382. consume(_LOCATION);
  383. locationstr:=cstringpattern;
  384. consume(_CSTRING);
  385. end
  386. else
  387. begin
  388. if explicit_paraloc then
  389. Message(parser_e_paraloc_all_paras);
  390. locationstr:='';
  391. end;
  392. end
  393. else
  394. locationstr:='';
  395. { default parameter }
  396. if (m_default_para in current_settings.modeswitches) then
  397. begin
  398. if try_to_consume(_EQ) then
  399. begin
  400. vs:=tparavarsym(sc[0]);
  401. if sc.count>1 then
  402. Message(parser_e_default_value_only_one_para);
  403. { prefix 'def' to the parameter name }
  404. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  405. if assigned(defaultvalue) then
  406. begin
  407. include(defaultvalue.symoptions,sp_internal);
  408. pd.parast.insert(defaultvalue);
  409. end;
  410. defaultrequired:=true;
  411. end
  412. else
  413. begin
  414. if defaultrequired then
  415. Message1(parser_e_default_value_expected_for_para,vs.name);
  416. end;
  417. end;
  418. end;
  419. end
  420. else
  421. hdef:=cformaltype;
  422. { File types are only allowed for var and out parameters }
  423. if (hdef.typ=filedef) and
  424. not(varspez in [vs_out,vs_var]) then
  425. CGMessage(cg_e_file_must_call_by_reference);
  426. { Dispinterfaces are restricted to using only automatable types }
  427. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  428. not is_automatable(hdef) then
  429. Message1(type_e_not_automatable,hdef.typename);
  430. { univ cannot be used with types whose size is not known at compile
  431. time }
  432. if is_univ and
  433. not is_valid_univ_para_type(hdef) then
  434. Message1(parser_e_invalid_univ_para,hdef.typename);
  435. for i:=0 to sc.count-1 do
  436. begin
  437. vs:=tparavarsym(sc[i]);
  438. vs.univpara:=is_univ;
  439. { update varsym }
  440. vs.vardef:=hdef;
  441. vs.defaultconstsym:=defaultvalue;
  442. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  443. begin
  444. if locationstr<>'' then
  445. begin
  446. if sc.count>1 then
  447. Message(parser_e_paraloc_only_one_para);
  448. if (paranr>1) and not(explicit_paraloc) then
  449. Message(parser_e_paraloc_all_paras);
  450. explicit_paraloc:=true;
  451. include(vs.varoptions,vo_has_explicit_paraloc);
  452. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  453. message(parser_e_illegal_explicit_paraloc);
  454. end
  455. else
  456. if explicit_paraloc then
  457. Message(parser_e_paraloc_all_paras);
  458. end;
  459. end;
  460. until not try_to_consume(_SEMICOLON);
  461. if explicit_paraloc then
  462. begin
  463. pd.has_paraloc_info:=callerside;
  464. include(pd.procoptions,po_explicitparaloc);
  465. end;
  466. { remove parasymtable from stack }
  467. sc.free;
  468. { reset object options }
  469. block_type:=old_block_type;
  470. consume(_RKLAMMER);
  471. end;
  472. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  473. var
  474. hs : string;
  475. orgsp,sp : TIDString;
  476. srsym : tsym;
  477. checkstack : psymtablestackitem;
  478. procstartfilepos : tfileposinfo;
  479. searchagain : boolean;
  480. st,
  481. genericst: TSymtable;
  482. aprocsym : tprocsym;
  483. popclass : integer;
  484. ImplIntf : TImplementedInterface;
  485. old_parse_generic : boolean;
  486. old_current_structdef: tabstractrecorddef;
  487. old_current_genericdef,
  488. old_current_specializedef: tstoreddef;
  489. lasttoken,lastidtoken: ttoken;
  490. procedure parse_operator_name;
  491. begin
  492. if (lasttoken in [first_overloaded..last_overloaded]) then
  493. begin
  494. optoken:=token;
  495. end
  496. else
  497. begin
  498. case lasttoken of
  499. _CARET:
  500. Message1(parser_e_overload_operator_failed,'**');
  501. _ID:
  502. case lastidtoken of
  503. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  504. _EXPLICIT:optoken:=_OP_EXPLICIT;
  505. _INC:optoken:=_OP_INC;
  506. _DEC:optoken:=_OP_DEC;
  507. else
  508. if (m_delphi in current_settings.modeswitches) then
  509. case lastidtoken of
  510. _IMPLICIT:optoken:=_ASSIGNMENT;
  511. _NEGATIVE:optoken:=_MINUS;
  512. _POSITIVE:optoken:=_PLUS;
  513. _LOGICALNOT:optoken:=_OP_NOT;
  514. _IN:optoken:=_OP_IN;
  515. _EQUAL:optoken:=_EQ;
  516. _NOTEQUAL:optoken:=_NE;
  517. _GREATERTHAN:optoken:=_GT;
  518. _GREATERTHANOREQUAL:optoken:=_GTE;
  519. _LESSTHAN:optoken:=_LT;
  520. _LESSTHANOREQUAL:optoken:=_LTE;
  521. _ADD:optoken:=_PLUS;
  522. _SUBTRACT:optoken:=_MINUS;
  523. _MULTIPLY:optoken:=_STAR;
  524. _DIVIDE:optoken:=_SLASH;
  525. _INTDIVIDE:optoken:=_OP_DIV;
  526. _MODULUS:optoken:=_OP_MOD;
  527. _LEFTSHIFT:optoken:=_OP_SHL;
  528. _RIGHTSHIFT:optoken:=_OP_SHR;
  529. _LOGICALAND:optoken:=_OP_AND;
  530. _LOGICALOR:optoken:=_OP_OR;
  531. _LOGICALXOR:optoken:=_OP_XOR;
  532. _BITWISEAND:optoken:=_OP_AND;
  533. _BITWISEOR:optoken:=_OP_OR;
  534. _BITWISEXOR:optoken:=_OP_XOR;
  535. else
  536. Message1(parser_e_overload_operator_failed,'');
  537. end
  538. else
  539. Message1(parser_e_overload_operator_failed,'');
  540. end
  541. else
  542. Message1(parser_e_overload_operator_failed,'');
  543. end;
  544. end;
  545. sp:=overloaded_names[optoken];
  546. orgsp:=sp;
  547. end;
  548. procedure consume_proc_name;
  549. begin
  550. lasttoken:=token;
  551. lastidtoken:=idtoken;
  552. if potype=potype_operator then
  553. optoken:=NOTOKEN;
  554. if (potype=potype_operator) and (token<>_ID) then
  555. begin
  556. parse_operator_name;
  557. consume(token);
  558. end
  559. else
  560. begin
  561. sp:=pattern;
  562. orgsp:=orgpattern;
  563. consume(_ID);
  564. end;
  565. end;
  566. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  567. var
  568. storepos:tfileposinfo;
  569. srsymtable:TSymtable;
  570. begin
  571. storepos:=current_tokenpos;
  572. current_tokenpos:=procstartfilepos;
  573. searchsym(sp,result,srsymtable);
  574. if not assigned(result) then
  575. begin
  576. if gen_error then
  577. identifier_not_found(orgsp);
  578. result:=generrorsym;
  579. end;
  580. current_tokenpos:=storepos;
  581. end;
  582. function consume_generic_type_parameter:boolean;
  583. var
  584. idx : integer;
  585. genparalistdecl : TFPHashList;
  586. genname : tidstring;
  587. s : shortstring;
  588. begin
  589. result:=not assigned(astruct)and(m_delphi in current_settings.modeswitches);
  590. if result then
  591. begin
  592. { parse all parameters first so we can check whether we have
  593. the correct generic def available }
  594. genparalistdecl:=TFPHashList.Create;
  595. if try_to_consume(_LT) then
  596. begin
  597. { start with 1, so Find can return Nil (= 0) }
  598. idx:=1;
  599. repeat
  600. if token=_ID then
  601. begin
  602. genparalistdecl.Add(pattern, Pointer(PtrInt(idx)));
  603. consume(_ID);
  604. inc(idx);
  605. end
  606. else
  607. begin
  608. message2(scan_f_syn_expected,arraytokeninfo[_ID].str,arraytokeninfo[token].str);
  609. if token<>_COMMA then
  610. consume(token);
  611. end;
  612. until not try_to_consume(_COMMA);
  613. if not try_to_consume(_GT) then
  614. consume(_RSHARPBRACKET);
  615. end
  616. else
  617. begin
  618. { no generic }
  619. srsym:=nil;
  620. exit;
  621. end;
  622. s:='';
  623. str(genparalistdecl.count,s);
  624. genname:=sp+'$'+s;
  625. genparalistdecl.free;
  626. srsym:=search_object_name(genname,false);
  627. if not assigned(srsym) then
  628. begin
  629. { TODO : print a nicer typename that contains the parsed
  630. generic types }
  631. Message1(type_e_generic_declaration_does_not_match,genname);
  632. srsym:=nil;
  633. exit;
  634. end;
  635. end;
  636. end;
  637. begin
  638. { Save the position where this procedure really starts }
  639. procstartfilepos:=current_tokenpos;
  640. old_parse_generic:=parse_generic;
  641. result:=false;
  642. pd:=nil;
  643. aprocsym:=nil;
  644. consume_proc_name;
  645. { examine interface map: function/procedure iname.functionname=locfuncname }
  646. if assigned(astruct) and
  647. (astruct.typ=objectdef) and
  648. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  649. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  650. try_to_consume(_POINT) then
  651. begin
  652. srsym:=search_object_name(sp,true);
  653. { qualifier is interface? }
  654. ImplIntf:=nil;
  655. if (srsym.typ=typesym) and
  656. (ttypesym(srsym).typedef.typ=objectdef) then
  657. ImplIntf:=tobjectdef(astruct).find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  658. if ImplIntf=nil then
  659. Message(parser_e_interface_id_expected);
  660. { must be a directly implemented interface }
  661. if Assigned(ImplIntf.ImplementsGetter) then
  662. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  663. consume(_ID);
  664. { Create unique name <interface>.<method> }
  665. hs:=sp+'.'+pattern;
  666. consume(_EQ);
  667. if assigned(ImplIntf) and
  668. (token=_ID) then
  669. ImplIntf.AddMapping(hs,pattern);
  670. consume(_ID);
  671. result:=true;
  672. exit;
  673. end;
  674. { method ? }
  675. srsym:=nil;
  676. if (consume_generic_type_parameter or not assigned(astruct)) and
  677. (symtablestack.top.symtablelevel=main_program_level) and
  678. try_to_consume(_POINT) then
  679. begin
  680. repeat
  681. searchagain:=false;
  682. if not assigned(astruct) and not assigned(srsym) then
  683. srsym:=search_object_name(sp,true);
  684. { consume proc name }
  685. procstartfilepos:=current_tokenpos;
  686. consume_proc_name;
  687. { qualifier is class name ? }
  688. if (srsym.typ=typesym) and
  689. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  690. begin
  691. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  692. if (token<>_POINT) then
  693. if (potype in [potype_class_constructor,potype_class_destructor]) then
  694. sp:=lower(sp)
  695. else
  696. if (potype=potype_operator)and(optoken=NOTOKEN) then
  697. parse_operator_name;
  698. srsym:=tsym(astruct.symtable.Find(sp));
  699. if assigned(srsym) then
  700. begin
  701. if srsym.typ=procsym then
  702. aprocsym:=tprocsym(srsym)
  703. else
  704. if (srsym.typ=typesym) and
  705. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  706. begin
  707. searchagain:=true;
  708. consume(_POINT);
  709. end
  710. else
  711. begin
  712. { we use a different error message for tp7 so it looks more compatible }
  713. if (m_fpc in current_settings.modeswitches) then
  714. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  715. else
  716. Message(parser_e_methode_id_expected);
  717. { rename the name to an unique name to avoid an
  718. error when inserting the symbol in the symtable }
  719. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  720. end;
  721. end
  722. else
  723. begin
  724. Message(parser_e_methode_id_expected);
  725. { recover by making it a normal procedure instead of method }
  726. astruct:=nil;
  727. end;
  728. end
  729. else
  730. Message(parser_e_class_id_expected);
  731. until not searchagain;
  732. end
  733. else
  734. begin
  735. { check for constructor/destructor/class operators which are not allowed here }
  736. if (not parse_only) and
  737. ((potype in [potype_constructor,potype_destructor,
  738. potype_class_constructor,potype_class_destructor]) or
  739. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  740. Message(parser_e_only_methods_allowed);
  741. repeat
  742. searchagain:=false;
  743. current_tokenpos:=procstartfilepos;
  744. if (potype=potype_operator)and(optoken=NOTOKEN) then
  745. parse_operator_name;
  746. srsym:=tsym(symtablestack.top.Find(sp));
  747. { Also look in the globalsymtable if we didn't found
  748. the symbol in the localsymtable }
  749. if not assigned(srsym) and
  750. not(parse_only) and
  751. (symtablestack.top=current_module.localsymtable) and
  752. assigned(current_module.globalsymtable) then
  753. srsym:=tsym(current_module.globalsymtable.Find(sp));
  754. { Check if overloaded is a procsym }
  755. if assigned(srsym) then
  756. begin
  757. if srsym.typ=procsym then
  758. aprocsym:=tprocsym(srsym)
  759. else
  760. begin
  761. { when the other symbol is a unit symbol then hide the unit
  762. symbol, this is not supported in tp7 }
  763. if not(m_tp7 in current_settings.modeswitches) and
  764. (srsym.typ=unitsym) then
  765. begin
  766. HideSym(srsym);
  767. searchagain:=true;
  768. end
  769. else
  770. begin
  771. { we use a different error message for tp7 so it looks more compatible }
  772. if (m_fpc in current_settings.modeswitches) then
  773. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  774. else
  775. Message1(sym_e_duplicate_id,srsym.realname);
  776. { rename the name to an unique name to avoid an
  777. error when inserting the symbol in the symtable }
  778. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  779. end;
  780. end;
  781. end;
  782. until not searchagain;
  783. end;
  784. { test again if assigned, it can be reset to recover }
  785. if not assigned(aprocsym) then
  786. begin
  787. { create a new procsym and set the real filepos }
  788. current_tokenpos:=procstartfilepos;
  789. { for operator we have only one procsym for each overloaded
  790. operation }
  791. if (potype=potype_operator) then
  792. begin
  793. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  794. if aprocsym=nil then
  795. aprocsym:=tprocsym.create('$'+sp);
  796. end
  797. else
  798. if (potype in [potype_class_constructor,potype_class_destructor]) then
  799. aprocsym:=tprocsym.create('$'+lower(sp))
  800. else
  801. aprocsym:=tprocsym.create(orgsp);
  802. symtablestack.top.insert(aprocsym);
  803. end;
  804. { to get the correct symtablelevel we must ignore ObjectSymtables }
  805. st:=nil;
  806. checkstack:=symtablestack.stack;
  807. while assigned(checkstack) do
  808. begin
  809. st:=checkstack^.symtable;
  810. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  811. break;
  812. checkstack:=checkstack^.next;
  813. end;
  814. pd:=tprocdef.create(st.symtablelevel+1);
  815. pd.struct:=astruct;
  816. pd.procsym:=aprocsym;
  817. pd.proctypeoption:=potype;
  818. { methods inherit df_generic or df_specialization from the objectdef }
  819. if assigned(pd.struct) and
  820. (pd.parast.symtablelevel=normal_function_level) then
  821. begin
  822. if (df_generic in pd.struct.defoptions) then
  823. begin
  824. include(pd.defoptions,df_generic);
  825. parse_generic:=true;
  826. end;
  827. if (df_specialization in pd.struct.defoptions) then
  828. begin
  829. include(pd.defoptions,df_specialization);
  830. { Find corresponding genericdef, we need it later to
  831. replay the tokens to generate the body }
  832. if not assigned(pd.struct.genericdef) then
  833. internalerror(200512113);
  834. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  835. if not assigned(genericst) then
  836. internalerror(200512114);
  837. { We are parsing the same objectdef, the def index numbers
  838. are the same }
  839. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  840. if not assigned(pd.genericdef) or
  841. (pd.genericdef.typ<>procdef) then
  842. internalerror(200512115);
  843. end;
  844. end;
  845. { methods need to be exported }
  846. if assigned(astruct) and
  847. (
  848. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  849. (symtablestack.top.symtablelevel=main_program_level)
  850. ) then
  851. include(pd.procoptions,po_global);
  852. { symbol options that need to be kept per procdef }
  853. pd.fileinfo:=procstartfilepos;
  854. pd.visibility:=symtablestack.top.currentvisibility;
  855. if symtablestack.top.currentlyoptional then
  856. include(pd.procoptions,po_optional);
  857. { parse parameters }
  858. if token=_LKLAMMER then
  859. begin
  860. { Add ObjectSymtable to be able to find nested type definitions }
  861. popclass:=0;
  862. if assigned(pd.struct) and
  863. (pd.parast.symtablelevel>=normal_function_level) and
  864. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  865. begin
  866. popclass:=push_nested_hierarchy(pd.struct);
  867. old_current_structdef:=current_structdef;
  868. old_current_genericdef:=current_genericdef;
  869. old_current_specializedef:=current_specializedef;
  870. current_structdef:=pd.struct;
  871. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  872. current_genericdef:=current_structdef;
  873. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  874. current_specializedef:=current_structdef;
  875. end;
  876. { Add parameter symtable }
  877. if pd.parast.symtabletype<>staticsymtable then
  878. symtablestack.push(pd.parast);
  879. parse_parameter_dec(pd);
  880. if pd.parast.symtabletype<>staticsymtable then
  881. symtablestack.pop(pd.parast);
  882. if popclass>0 then
  883. begin
  884. current_structdef:=old_current_structdef;
  885. current_genericdef:=old_current_genericdef;
  886. current_specializedef:=old_current_specializedef;
  887. dec(popclass,pop_nested_hierarchy(pd.struct));
  888. if popclass<>0 then
  889. internalerror(201011260); // 11 nov 2010 index 0
  890. end;
  891. end;
  892. parse_generic:=old_parse_generic;
  893. result:=true;
  894. end;
  895. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  896. var
  897. pd: tprocdef;
  898. locationstr: string;
  899. i: integer;
  900. found: boolean;
  901. old_block_type: tblock_type;
  902. procedure read_returndef(pd: tprocdef);
  903. var
  904. popclass: integer;
  905. old_parse_generic: boolean;
  906. old_current_structdef: tabstractrecorddef;
  907. old_current_genericdef,
  908. old_current_specializedef: tstoreddef;
  909. begin
  910. old_parse_generic:=parse_generic;
  911. { Add ObjectSymtable to be able to find generic type definitions }
  912. popclass:=0;
  913. if assigned(pd.struct) and
  914. (pd.parast.symtablelevel>=normal_function_level) and
  915. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  916. begin
  917. popclass:=push_nested_hierarchy(pd.struct);
  918. parse_generic:=(df_generic in pd.struct.defoptions);
  919. old_current_structdef:=current_structdef;
  920. old_current_genericdef:=current_genericdef;
  921. old_current_specializedef:=current_specializedef;
  922. current_structdef:=pd.struct;
  923. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  924. current_genericdef:=current_structdef;
  925. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  926. current_specializedef:=current_structdef;
  927. end;
  928. single_type(pd.returndef,[stoAllowSpecialization]);
  929. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  930. Message1(type_e_not_automatable,pd.returndef.typename);
  931. if popclass>0 then
  932. begin
  933. current_structdef:=old_current_structdef;
  934. current_genericdef:=old_current_genericdef;
  935. current_specializedef:=old_current_specializedef;
  936. dec(popclass,pop_nested_hierarchy(pd.struct));
  937. if popclass<>0 then
  938. internalerror(201012020);
  939. end;
  940. parse_generic:=old_parse_generic;
  941. end;
  942. begin
  943. locationstr:='';
  944. pd:=nil;
  945. case token of
  946. _FUNCTION :
  947. begin
  948. consume(_FUNCTION);
  949. if parse_proc_head(astruct,potype_function,pd) then
  950. begin
  951. { pd=nil when it is a interface mapping }
  952. if assigned(pd) then
  953. begin
  954. if try_to_consume(_COLON) then
  955. begin
  956. read_returndef(pd);
  957. if (target_info.system in [system_m68k_amiga]) then
  958. begin
  959. if (idtoken=_LOCATION) then
  960. begin
  961. if po_explicitparaloc in pd.procoptions then
  962. begin
  963. consume(_LOCATION);
  964. locationstr:=cstringpattern;
  965. consume(_CSTRING);
  966. end
  967. else
  968. { I guess this needs a new message... (KB) }
  969. Message(parser_e_paraloc_all_paras);
  970. end
  971. else
  972. begin
  973. if po_explicitparaloc in pd.procoptions then
  974. { assign default locationstr, if none specified }
  975. { and we've arguments with explicit paraloc }
  976. locationstr:='D0';
  977. end;
  978. end;
  979. end
  980. else
  981. begin
  982. if (
  983. parse_only and
  984. not(is_interface(pd.struct))
  985. ) or
  986. (m_repeat_forward in current_settings.modeswitches) then
  987. begin
  988. consume(_COLON);
  989. consume_all_until(_SEMICOLON);
  990. end;
  991. end;
  992. if isclassmethod then
  993. include(pd.procoptions,po_classmethod);
  994. end;
  995. end
  996. else
  997. begin
  998. { recover }
  999. consume(_COLON);
  1000. consume_all_until(_SEMICOLON);
  1001. end;
  1002. end;
  1003. _PROCEDURE :
  1004. begin
  1005. consume(_PROCEDURE);
  1006. if parse_proc_head(astruct,potype_procedure,pd) then
  1007. begin
  1008. { pd=nil when it is an interface mapping }
  1009. if assigned(pd) then
  1010. begin
  1011. pd.returndef:=voidtype;
  1012. if isclassmethod then
  1013. include(pd.procoptions,po_classmethod);
  1014. end;
  1015. end;
  1016. end;
  1017. _CONSTRUCTOR :
  1018. begin
  1019. consume(_CONSTRUCTOR);
  1020. if isclassmethod then
  1021. parse_proc_head(astruct,potype_class_constructor,pd)
  1022. else
  1023. parse_proc_head(astruct,potype_constructor,pd);
  1024. if not isclassmethod and
  1025. assigned(pd) and
  1026. assigned(pd.struct) then
  1027. begin
  1028. { Set return type, class constructors return the
  1029. created instance, object constructors return boolean }
  1030. if is_class(pd.struct) or
  1031. is_record(pd.struct) or
  1032. is_javaclass(pd.struct) then
  1033. pd.returndef:=pd.struct
  1034. else
  1035. if is_objectpascal_helper(pd.struct) then
  1036. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1037. else
  1038. {$ifdef CPU64bitaddr}
  1039. pd.returndef:=bool64type;
  1040. {$else CPU64bitaddr}
  1041. pd.returndef:=bool32type;
  1042. {$endif CPU64bitaddr}
  1043. end
  1044. else
  1045. pd.returndef:=voidtype;
  1046. end;
  1047. _DESTRUCTOR :
  1048. begin
  1049. consume(_DESTRUCTOR);
  1050. if isclassmethod then
  1051. parse_proc_head(astruct,potype_class_destructor,pd)
  1052. else
  1053. parse_proc_head(astruct,potype_destructor,pd);
  1054. if assigned(pd) then
  1055. pd.returndef:=voidtype;
  1056. end;
  1057. else
  1058. if (token=_OPERATOR) or
  1059. (isclassmethod and (idtoken=_OPERATOR)) then
  1060. begin
  1061. { we need to set the block type to bt_body, so that operator names
  1062. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1063. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1064. old_block_type:=block_type;
  1065. block_type:=bt_body;
  1066. consume(_OPERATOR);
  1067. parse_proc_head(astruct,potype_operator,pd);
  1068. block_type:=old_block_type;
  1069. if assigned(pd) then
  1070. begin
  1071. { operators always need to be searched in all units }
  1072. include(pd.procoptions,po_overload);
  1073. if pd.parast.symtablelevel>normal_function_level then
  1074. Message(parser_e_no_local_operator);
  1075. if isclassmethod then
  1076. include(pd.procoptions,po_classmethod);
  1077. if token<>_ID then
  1078. begin
  1079. if not(m_result in current_settings.modeswitches) then
  1080. consume(_ID);
  1081. end
  1082. else
  1083. begin
  1084. pd.resultname:=stringdup(orgpattern);
  1085. consume(_ID);
  1086. end;
  1087. if not try_to_consume(_COLON) then
  1088. begin
  1089. consume(_COLON);
  1090. pd.returndef:=generrordef;
  1091. consume_all_until(_SEMICOLON);
  1092. end
  1093. else
  1094. begin
  1095. read_returndef(pd);
  1096. { check that class operators have either return type of structure or }
  1097. { at least one argument of that type }
  1098. if (po_classmethod in pd.procoptions) and
  1099. (pd.returndef <> pd.struct) then
  1100. begin
  1101. found:=false;
  1102. for i := 0 to pd.parast.SymList.Count - 1 do
  1103. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1104. begin
  1105. found:=true;
  1106. break;
  1107. end;
  1108. if not found then
  1109. if assigned(pd.struct) then
  1110. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1111. else
  1112. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1113. end;
  1114. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1115. ((pd.returndef.typ<>orddef) or
  1116. (torddef(pd.returndef).ordtype<>pasbool8)) then
  1117. Message(parser_e_comparative_operator_return_boolean);
  1118. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1119. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1120. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1121. message(parser_e_no_such_assignment)
  1122. else if not isoperatoracceptable(pd,optoken) then
  1123. Message(parser_e_overload_impossible);
  1124. end;
  1125. end
  1126. else
  1127. begin
  1128. { recover }
  1129. try_to_consume(_ID);
  1130. consume(_COLON);
  1131. consume_all_until(_SEMICOLON);
  1132. end;
  1133. end;
  1134. end;
  1135. { file types can't be function results }
  1136. if assigned(pd) and
  1137. (pd.returndef.typ=filedef) then
  1138. message(parser_e_illegal_function_result);
  1139. { support procedure proc stdcall export; }
  1140. if not(check_proc_directive(false)) then
  1141. begin
  1142. if (token=_COLON) then
  1143. begin
  1144. message(parser_e_field_not_allowed_here);
  1145. consume_all_until(_SEMICOLON);
  1146. end;
  1147. consume(_SEMICOLON);
  1148. end;
  1149. result:=pd;
  1150. if locationstr<>'' then
  1151. begin
  1152. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1153. { I guess this needs a new message... (KB) }
  1154. message(parser_e_illegal_explicit_paraloc);
  1155. end;
  1156. end;
  1157. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  1158. var
  1159. oldparse_only: boolean;
  1160. begin
  1161. oldparse_only:=parse_only;
  1162. parse_only:=true;
  1163. result:=parse_proc_dec(is_classdef,astruct);
  1164. { this is for error recovery as well as forward }
  1165. { interface mappings, i.e. mapping to a method }
  1166. { which isn't declared yet }
  1167. if assigned(result) then
  1168. begin
  1169. parse_record_proc_directives(result);
  1170. { since records have no inheritance, don't allow non-static
  1171. class methods. Selphi does the same. }
  1172. if (result.proctypeoption<>potype_operator) and
  1173. is_classdef and
  1174. not (po_staticmethod in result.procoptions) then
  1175. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1176. // we can't add hidden params here because record is not yet defined
  1177. // and therefore record size which has influence on paramter passing rules may change too
  1178. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1179. handle_calling_convention(result,[hcc_check]);
  1180. { add definition to procsym }
  1181. proc_add_definition(result);
  1182. end;
  1183. maybe_parse_hint_directives(result);
  1184. parse_only:=oldparse_only;
  1185. end;
  1186. procedure insert_record_hidden_paras(astruct: trecorddef);
  1187. var
  1188. pd: tdef;
  1189. i: longint;
  1190. oldpos : tfileposinfo;
  1191. oldparse_only: boolean;
  1192. begin
  1193. // handle calling conventions of record methods
  1194. oldpos:=current_filepos;
  1195. oldparse_only:=parse_only;
  1196. parse_only:=true;
  1197. { don't keep track of procdefs in a separate list, because the
  1198. compiler may add additional procdefs (e.g. property wrappers for
  1199. the jvm backend) }
  1200. for i := 0 to astruct.symtable.deflist.count - 1 do
  1201. begin
  1202. pd:=tdef(astruct.symtable.deflist[i]);
  1203. if pd.typ<>procdef then
  1204. continue;
  1205. current_filepos:=tprocdef(pd).fileinfo;
  1206. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1207. end;
  1208. parse_only:=oldparse_only;
  1209. current_filepos:=oldpos;
  1210. end;
  1211. {****************************************************************************
  1212. Procedure directive handlers
  1213. ****************************************************************************}
  1214. procedure pd_far(pd:tabstractprocdef);
  1215. begin
  1216. Message1(parser_w_proc_directive_ignored,'FAR');
  1217. end;
  1218. procedure pd_near(pd:tabstractprocdef);
  1219. begin
  1220. Message1(parser_w_proc_directive_ignored,'NEAR');
  1221. end;
  1222. procedure pd_export(pd:tabstractprocdef);
  1223. begin
  1224. if pd.typ<>procdef then
  1225. internalerror(200304264);
  1226. if assigned(tprocdef(pd).struct) then
  1227. Message(parser_e_methods_dont_be_export);
  1228. if pd.parast.symtablelevel>normal_function_level then
  1229. Message(parser_e_dont_nest_export);
  1230. end;
  1231. procedure pd_forward(pd:tabstractprocdef);
  1232. begin
  1233. if pd.typ<>procdef then
  1234. internalerror(200304265);
  1235. tprocdef(pd).forwarddef:=true;
  1236. end;
  1237. procedure pd_alias(pd:tabstractprocdef);
  1238. begin
  1239. if pd.typ<>procdef then
  1240. internalerror(200304266);
  1241. consume(_COLON);
  1242. tprocdef(pd).aliasnames.insert(get_stringconst);
  1243. include(pd.procoptions,po_has_public_name);
  1244. end;
  1245. procedure pd_public(pd:tabstractprocdef);
  1246. begin
  1247. if pd.typ<>procdef then
  1248. internalerror(200304266);
  1249. if try_to_consume(_NAME) then
  1250. begin
  1251. tprocdef(pd).aliasnames.insert(get_stringconst);
  1252. include(pd.procoptions,po_has_public_name);
  1253. end;
  1254. end;
  1255. procedure pd_asmname(pd:tabstractprocdef);
  1256. begin
  1257. if pd.typ<>procdef then
  1258. internalerror(200304267);
  1259. if token=_CCHAR then
  1260. begin
  1261. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1262. consume(_CCHAR)
  1263. end
  1264. else
  1265. begin
  1266. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1267. consume(_CSTRING);
  1268. end;
  1269. { we don't need anything else }
  1270. tprocdef(pd).forwarddef:=false;
  1271. end;
  1272. procedure pd_internconst(pd:tabstractprocdef);
  1273. var v:Tconstexprint;
  1274. begin
  1275. if pd.typ<>procdef then
  1276. internalerror(200304268);
  1277. consume(_COLON);
  1278. v:=get_intconst;
  1279. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1280. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1281. else
  1282. Tprocdef(pd).extnumber:=longint(v.svalue);
  1283. end;
  1284. procedure pd_internproc(pd:tabstractprocdef);
  1285. var v:Tconstexprint;
  1286. begin
  1287. if pd.typ<>procdef then
  1288. internalerror(200304268);
  1289. consume(_COLON);
  1290. v:=get_intconst;
  1291. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1292. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1293. else
  1294. Tprocdef(pd).extnumber:=longint(v.svalue);
  1295. { the proc is defined }
  1296. tprocdef(pd).forwarddef:=false;
  1297. end;
  1298. procedure pd_interrupt(pd:tabstractprocdef);
  1299. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1300. var v: Tconstexprint;
  1301. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1302. begin
  1303. if pd.parast.symtablelevel>normal_function_level then
  1304. Message(parser_e_dont_nest_interrupt);
  1305. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1306. if target_info.system in systems_interrupt_table then
  1307. begin
  1308. if token<>_SEMICOLON then
  1309. begin
  1310. pd.proccalloption:=pocall_interrupt;
  1311. v:=get_intconst;
  1312. Tprocdef(pd).interruptvector:=v.uvalue;
  1313. end;
  1314. end;
  1315. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1316. end;
  1317. procedure pd_abstract(pd:tabstractprocdef);
  1318. begin
  1319. if pd.typ<>procdef then
  1320. internalerror(200304269);
  1321. if is_objectpascal_helper(tprocdef(pd).struct) then
  1322. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1323. if assigned(tprocdef(pd).struct) and
  1324. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1325. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1326. else if (po_virtualmethod in pd.procoptions) then
  1327. begin
  1328. include(pd.procoptions,po_abstractmethod);
  1329. { one more abstract method }
  1330. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1331. end
  1332. else
  1333. Message(parser_e_only_virtual_methods_abstract);
  1334. { the method is defined }
  1335. tprocdef(pd).forwarddef:=false;
  1336. end;
  1337. procedure pd_final(pd:tabstractprocdef);
  1338. begin
  1339. if pd.typ<>procdef then
  1340. internalerror(200910170);
  1341. if is_objectpascal_helper(tprocdef(pd).struct) and
  1342. (m_objfpc in current_settings.modeswitches) then
  1343. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1344. if (po_virtualmethod in pd.procoptions) or
  1345. (is_javaclass(tprocdef(pd).struct) and
  1346. (po_classmethod in pd.procoptions)) then
  1347. include(pd.procoptions,po_finalmethod)
  1348. else
  1349. Message(parser_e_only_virtual_methods_final);
  1350. end;
  1351. procedure pd_enumerator(pd:tabstractprocdef);
  1352. begin
  1353. if pd.typ<>procdef then
  1354. internalerror(200910250);
  1355. if (token = _ID) then
  1356. begin
  1357. if pattern='MOVENEXT' then
  1358. begin
  1359. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1360. message(parser_e_only_one_enumerator_movenext);
  1361. pd.calcparas;
  1362. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1363. (pd.minparacount = 0) then
  1364. begin
  1365. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1366. include(pd.procoptions,po_enumerator_movenext);
  1367. end
  1368. else
  1369. Message(parser_e_enumerator_movenext_is_not_valid)
  1370. end
  1371. else
  1372. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1373. consume(token);
  1374. end
  1375. else
  1376. Message(parser_e_enumerator_identifier_required);
  1377. end;
  1378. procedure pd_virtual(pd:tabstractprocdef);
  1379. {$ifdef WITHDMT}
  1380. var
  1381. pt : tnode;
  1382. {$endif WITHDMT}
  1383. begin
  1384. if (not assigned(pd.owner.defowner) or
  1385. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1386. (po_external in pd.procoptions) then
  1387. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1388. if pd.typ<>procdef then
  1389. internalerror(2003042610);
  1390. if (pd.proctypeoption=potype_constructor) and
  1391. is_object(tprocdef(pd).struct) then
  1392. Message(parser_e_constructor_cannot_be_not_virtual);
  1393. if is_objectpascal_helper(tprocdef(pd).struct) and
  1394. (m_objfpc in current_settings.modeswitches) then
  1395. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1396. {$ifdef WITHDMT}
  1397. if is_object(tprocdef(pd).struct) and
  1398. (token<>_SEMICOLON) then
  1399. begin
  1400. { any type of parameter is allowed here! }
  1401. pt:=comp_expr(true);
  1402. if is_constintnode(pt) then
  1403. begin
  1404. include(pd.procoptions,po_msgint);
  1405. pd.messageinf.i:=pt.value;
  1406. end
  1407. else
  1408. Message(parser_e_ill_msg_expr);
  1409. disposetree(pt);
  1410. end;
  1411. {$endif WITHDMT}
  1412. end;
  1413. procedure pd_dispid(pd:tabstractprocdef);
  1414. var pt:Tnode;
  1415. begin
  1416. if pd.typ<>procdef then
  1417. internalerror(200604301);
  1418. pt:=comp_expr(true,false);
  1419. if is_constintnode(pt) then
  1420. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1421. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1422. else
  1423. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1424. else
  1425. message(parser_e_dispid_must_be_ord_const);
  1426. pt.free;
  1427. end;
  1428. procedure pd_static(pd:tabstractprocdef);
  1429. begin
  1430. if pd.typ=procdef then
  1431. include(tprocdef(pd).procsym.symoptions,sp_static);
  1432. include(pd.procoptions,po_staticmethod);
  1433. end;
  1434. procedure pd_override(pd:tabstractprocdef);
  1435. begin
  1436. if pd.typ<>procdef then
  1437. internalerror(2003042611);
  1438. if is_objectpascal_helper(tprocdef(pd).struct) then
  1439. begin
  1440. if m_objfpc in current_settings.modeswitches then
  1441. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1442. end
  1443. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1444. Message(parser_e_no_object_override)
  1445. else if is_objccategory(tprocdef(pd).struct) then
  1446. Message(parser_e_no_category_override)
  1447. else if (po_external in pd.procoptions) and
  1448. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1449. not is_cppclass(tprocdef(pd).struct) and
  1450. not is_java_class_or_interface(tprocdef(pd).struct) then
  1451. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1452. end;
  1453. procedure pd_overload(pd:tabstractprocdef);
  1454. begin
  1455. if pd.typ<>procdef then
  1456. internalerror(2003042612);
  1457. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1458. end;
  1459. procedure pd_message(pd:tabstractprocdef);
  1460. var
  1461. pt : tnode;
  1462. paracnt : longint;
  1463. begin
  1464. if pd.typ<>procdef then
  1465. internalerror(2003042613);
  1466. if is_objectpascal_helper(tprocdef(pd).struct) then
  1467. begin
  1468. if m_objfpc in current_settings.modeswitches then
  1469. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1470. end
  1471. else
  1472. if not is_class(tprocdef(pd).struct) and
  1473. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1474. Message(parser_e_msg_only_for_classes);
  1475. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1476. Message(parser_e_multiple_messages);
  1477. { check parameter type }
  1478. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1479. begin
  1480. if po_external in pd.procoptions then
  1481. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1482. paracnt:=0;
  1483. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1484. if paracnt<>1 then
  1485. Message(parser_e_ill_msg_param);
  1486. end;
  1487. pt:=comp_expr(true,false);
  1488. { message is 1-character long }
  1489. if is_constcharnode(pt) then
  1490. begin
  1491. include(pd.procoptions,po_msgstr);
  1492. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1493. end
  1494. else if pt.nodetype=stringconstn then
  1495. begin
  1496. include(pd.procoptions,po_msgstr);
  1497. if (tstringconstnode(pt).len>255) then
  1498. Message(parser_e_message_string_too_long);
  1499. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1500. end
  1501. else
  1502. if is_constintnode(pt) and
  1503. (is_class(tprocdef(pd).struct) or
  1504. is_objectpascal_helper(tprocdef(pd).struct)) then
  1505. begin
  1506. include(pd.procoptions,po_msgint);
  1507. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1508. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1509. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1510. else
  1511. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1512. end
  1513. else
  1514. Message(parser_e_ill_msg_expr);
  1515. { check whether the selector name is valid in case of Objective-C }
  1516. if (po_msgstr in pd.procoptions) and
  1517. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1518. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1519. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1520. pt.free;
  1521. end;
  1522. procedure pd_reintroduce(pd:tabstractprocdef);
  1523. begin
  1524. if pd.typ<>procdef then
  1525. internalerror(200401211);
  1526. if is_objectpascal_helper(tprocdef(pd).struct) then
  1527. begin
  1528. if m_objfpc in current_settings.modeswitches then
  1529. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1530. end
  1531. else
  1532. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1533. not(is_objccategory(tprocdef(pd).struct)) and
  1534. not(is_javaclass(tprocdef(pd).struct)) then
  1535. Message(parser_e_no_object_reintroduce);
  1536. end;
  1537. procedure pd_syscall(pd:tabstractprocdef);
  1538. {$if defined(powerpc) or defined(m68k)}
  1539. var
  1540. vs : tparavarsym;
  1541. sym : tsym;
  1542. symtable : TSymtable;
  1543. v: Tconstexprint;
  1544. {$endif defined(powerpc) or defined(m68k)}
  1545. begin
  1546. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1547. internalerror(2003042614);
  1548. tprocdef(pd).forwarddef:=false;
  1549. {$ifdef m68k}
  1550. if target_info.system in [system_m68k_amiga] then
  1551. begin
  1552. include(pd.procoptions,po_syscall_legacy);
  1553. if consume_sym(sym,symtable) then
  1554. begin
  1555. if (sym.typ=staticvarsym) and
  1556. (
  1557. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1558. is_32bitint(tabstractvarsym(sym).vardef)
  1559. ) then
  1560. begin
  1561. tprocdef(pd).libsym:=sym;
  1562. if po_syscall_legacy in tprocdef(pd).procoptions then
  1563. begin
  1564. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1565. paramanager.parseparaloc(vs,'A6');
  1566. pd.parast.insert(vs);
  1567. end
  1568. end
  1569. else
  1570. Message(parser_e_32bitint_or_pointer_variable_expected);
  1571. end;
  1572. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1573. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1574. v:=get_intconst;
  1575. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1576. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  1577. else
  1578. Tprocdef(pd).extnumber:=v.uvalue;
  1579. end;
  1580. {$endif m68k}
  1581. {$ifdef powerpc}
  1582. if target_info.system = system_powerpc_amiga then
  1583. begin
  1584. include(pd.procoptions,po_syscall_basesysv);
  1585. if consume_sym(sym,symtable) then
  1586. begin
  1587. if (sym.typ=staticvarsym) and
  1588. (
  1589. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1590. is_32bitint(tabstractvarsym(sym).vardef)
  1591. ) then
  1592. begin
  1593. tprocdef(pd).libsym:=sym;
  1594. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1595. pd.parast.insert(vs);
  1596. end
  1597. else
  1598. Message(parser_e_32bitint_or_pointer_variable_expected);
  1599. end;
  1600. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1601. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1602. v:=get_intconst;
  1603. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1604. message(parser_e_range_check_error)
  1605. else
  1606. Tprocdef(pd).extnumber:=v.uvalue;
  1607. end else
  1608. if target_info.system = system_powerpc_morphos then
  1609. begin
  1610. if idtoken=_LEGACY then
  1611. begin
  1612. consume(_LEGACY);
  1613. include(pd.procoptions,po_syscall_legacy);
  1614. end
  1615. else if idtoken=_SYSV then
  1616. begin
  1617. consume(_SYSV);
  1618. include(pd.procoptions,po_syscall_sysv);
  1619. end
  1620. else if idtoken=_BASESYSV then
  1621. begin
  1622. consume(_BASESYSV);
  1623. include(pd.procoptions,po_syscall_basesysv);
  1624. end
  1625. else if idtoken=_SYSVBASE then
  1626. begin
  1627. consume(_SYSVBASE);
  1628. include(pd.procoptions,po_syscall_sysvbase);
  1629. end
  1630. else if idtoken=_R12BASE then
  1631. begin
  1632. consume(_R12BASE);
  1633. include(pd.procoptions,po_syscall_r12base);
  1634. end
  1635. else
  1636. if syscall_convention='LEGACY' then
  1637. include(pd.procoptions,po_syscall_legacy)
  1638. else if syscall_convention='SYSV' then
  1639. include(pd.procoptions,po_syscall_sysv)
  1640. else if syscall_convention='BASESYSV' then
  1641. include(pd.procoptions,po_syscall_basesysv)
  1642. else if syscall_convention='SYSVBASE' then
  1643. include(pd.procoptions,po_syscall_sysvbase)
  1644. else if syscall_convention='R12BASE' then
  1645. include(pd.procoptions,po_syscall_r12base)
  1646. else
  1647. internalerror(2005010404);
  1648. if consume_sym(sym,symtable) then
  1649. begin
  1650. if (sym.typ=staticvarsym) and
  1651. (
  1652. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1653. is_32bitint(tabstractvarsym(sym).vardef)
  1654. ) then
  1655. begin
  1656. tprocdef(pd).libsym:=sym;
  1657. if po_syscall_legacy in tprocdef(pd).procoptions then
  1658. begin
  1659. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1660. paramanager.parseparaloc(vs,'A6');
  1661. pd.parast.insert(vs);
  1662. end
  1663. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1664. begin
  1665. { Nothing to be done for sysv here for now, but this might change }
  1666. end
  1667. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1668. begin
  1669. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1670. pd.parast.insert(vs);
  1671. end
  1672. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1673. begin
  1674. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1675. pd.parast.insert(vs);
  1676. end
  1677. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1678. begin
  1679. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1680. paramanager.parseparaloc(vs,'R12');
  1681. pd.parast.insert(vs);
  1682. end
  1683. else
  1684. internalerror(2005010501);
  1685. end
  1686. else
  1687. Message(parser_e_32bitint_or_pointer_variable_expected);
  1688. end;
  1689. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1690. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1691. v:=get_intconst;
  1692. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1693. message(parser_e_range_check_error)
  1694. else
  1695. Tprocdef(pd).extnumber:=v.uvalue;
  1696. end;
  1697. {$endif powerpc}
  1698. end;
  1699. procedure pd_external(pd:tabstractprocdef);
  1700. {
  1701. If import_dll=nil the procedure is assumed to be in another
  1702. object file. In that object file it should have the name to
  1703. which import_name is pointing to. Otherwise, the procedure is
  1704. assumed to be in the DLL to which import_dll is pointing to. In
  1705. that case either import_nr<>0 or import_name<>nil is true, so
  1706. the procedure is either imported by number or by name. (DM)
  1707. }
  1708. var
  1709. hs : string;
  1710. v:Tconstexprint;
  1711. is_java_external: boolean;
  1712. begin
  1713. if pd.typ<>procdef then
  1714. internalerror(2003042615);
  1715. { Allow specifying a separate external name for methods in external Java
  1716. because its identifier naming constraints are laxer than FPC's
  1717. (e.g., case sensitive).
  1718. Limitation: only allows specifying the symbol name and not the package name,
  1719. and only for external classes/interfaces }
  1720. is_java_external:=
  1721. (pd.typ=procdef) and
  1722. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1723. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1724. with tprocdef(pd) do
  1725. begin
  1726. forwarddef:=false;
  1727. { forbid local external procedures }
  1728. if parast.symtablelevel>normal_function_level then
  1729. Message(parser_e_no_local_proc_external);
  1730. { If the procedure should be imported from a DLL, a constant string follows.
  1731. This isn't really correct, an contant string expression follows
  1732. so we check if an semicolon follows, else a string constant have to
  1733. follow (FK) }
  1734. if not is_java_external and
  1735. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1736. begin
  1737. { Always add library prefix and suffix to create an uniform name }
  1738. hs:=get_stringconst;
  1739. if ExtractFileExt(hs)='' then
  1740. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1741. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1742. hs:=target_info.sharedlibprefix+hs;
  1743. { the JVM expects java/lang/Object rather than java.lang.Object }
  1744. if target_info.system in systems_jvm then
  1745. Replace(hs,'.','/');
  1746. import_dll:=stringdup(hs);
  1747. include(procoptions,po_has_importdll);
  1748. if (idtoken=_NAME) then
  1749. begin
  1750. consume(_NAME);
  1751. import_name:=stringdup(get_stringconst);
  1752. include(procoptions,po_has_importname);
  1753. if import_name^='' then
  1754. message(parser_e_empty_import_name);
  1755. end;
  1756. if (idtoken=_INDEX) then
  1757. begin
  1758. {After the word index follows the index number in the DLL.}
  1759. consume(_INDEX);
  1760. v:=get_intconst;
  1761. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1762. message(parser_e_range_check_error)
  1763. else
  1764. import_nr:=longint(v.svalue);
  1765. end;
  1766. { default is to used the realname of the procedure }
  1767. if (import_nr=0) and not assigned(import_name) then
  1768. begin
  1769. import_name:=stringdup(procsym.realname);
  1770. include(procoptions,po_has_importname);
  1771. end;
  1772. end
  1773. else
  1774. begin
  1775. if (idtoken=_NAME) or
  1776. is_java_external then
  1777. begin
  1778. consume(_NAME);
  1779. import_name:=stringdup(get_stringconst);
  1780. include(procoptions,po_has_importname);
  1781. if import_name^='' then
  1782. message(parser_e_empty_import_name);
  1783. end;
  1784. end;
  1785. end;
  1786. end;
  1787. procedure pd_weakexternal(pd:tabstractprocdef);
  1788. begin
  1789. if not(target_info.system in systems_weak_linking) then
  1790. message(parser_e_weak_external_not_supported)
  1791. else
  1792. pd_external(pd);
  1793. end;
  1794. type
  1795. pd_handler=procedure(pd:tabstractprocdef);
  1796. proc_dir_rec=record
  1797. idtok : ttoken;
  1798. pd_flags : tpdflags;
  1799. handler : pd_handler;
  1800. pocall : tproccalloption;
  1801. pooption : tprocoptions;
  1802. mutexclpocall : tproccalloptions;
  1803. mutexclpotype : tproctypeoptions;
  1804. mutexclpo : tprocoptions;
  1805. end;
  1806. const
  1807. {Should contain the number of procedure directives we support.}
  1808. num_proc_directives=43;
  1809. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1810. (
  1811. (
  1812. idtok:_ABSTRACT;
  1813. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1814. handler : @pd_abstract;
  1815. pocall : pocall_none;
  1816. pooption : [po_abstractmethod];
  1817. mutexclpocall : [pocall_internproc];
  1818. mutexclpotype : [];
  1819. mutexclpo : [po_exports,po_interrupt,po_inline]
  1820. ),(
  1821. idtok:_ALIAS;
  1822. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1823. handler : @pd_alias;
  1824. pocall : pocall_none;
  1825. pooption : [];
  1826. mutexclpocall : [];
  1827. mutexclpotype : [];
  1828. mutexclpo : [po_external,po_inline]
  1829. ),(
  1830. idtok:_ASMNAME;
  1831. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1832. handler : @pd_asmname;
  1833. pocall : pocall_cdecl;
  1834. pooption : [po_external];
  1835. mutexclpocall : [pocall_internproc];
  1836. mutexclpotype : [];
  1837. mutexclpo : [po_external,po_inline]
  1838. ),(
  1839. idtok:_ASSEMBLER;
  1840. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1841. handler : nil;
  1842. pocall : pocall_none;
  1843. pooption : [po_assembler];
  1844. mutexclpocall : [];
  1845. mutexclpotype : [];
  1846. mutexclpo : [po_external]
  1847. ),(
  1848. idtok:_C; {same as cdecl for mode mac}
  1849. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1850. handler : nil;
  1851. pocall : pocall_cdecl;
  1852. pooption : [];
  1853. mutexclpocall : [];
  1854. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1855. mutexclpo : [po_assembler,po_external]
  1856. ),(
  1857. idtok:_CDECL;
  1858. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1859. handler : nil;
  1860. pocall : pocall_cdecl;
  1861. pooption : [];
  1862. mutexclpocall : [];
  1863. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1864. mutexclpo : [po_assembler,po_external]
  1865. ),(
  1866. idtok:_DISPID;
  1867. pd_flags : [pd_dispinterface];
  1868. handler : @pd_dispid;
  1869. pocall : pocall_none;
  1870. pooption : [po_dispid];
  1871. mutexclpocall : [pocall_internproc];
  1872. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1873. mutexclpo : [po_interrupt,po_external,po_inline]
  1874. ),(
  1875. idtok:_DYNAMIC;
  1876. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1877. handler : @pd_virtual;
  1878. pocall : pocall_none;
  1879. pooption : [po_virtualmethod];
  1880. mutexclpocall : [pocall_internproc];
  1881. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1882. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1883. ),(
  1884. idtok:_EXPORT;
  1885. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  1886. handler : @pd_export;
  1887. pocall : pocall_none;
  1888. pooption : [po_exports,po_global];
  1889. mutexclpocall : [pocall_internproc];
  1890. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1891. mutexclpo : [po_external,po_interrupt,po_inline]
  1892. ),(
  1893. idtok:_EXTERNAL;
  1894. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  1895. handler : @pd_external;
  1896. pocall : pocall_none;
  1897. pooption : [po_external];
  1898. mutexclpocall : [pocall_syscall];
  1899. { allowed for external cpp classes }
  1900. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1901. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1902. ),(
  1903. idtok:_FAR;
  1904. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1905. handler : @pd_far;
  1906. pocall : pocall_none;
  1907. pooption : [];
  1908. mutexclpocall : [pocall_internproc];
  1909. mutexclpotype : [];
  1910. mutexclpo : [po_inline]
  1911. ),(
  1912. idtok:_FAR16;
  1913. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  1914. handler : nil;
  1915. pocall : pocall_far16;
  1916. pooption : [];
  1917. mutexclpocall : [];
  1918. mutexclpotype : [];
  1919. mutexclpo : [po_external]
  1920. ),(
  1921. idtok:_FINAL;
  1922. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1923. handler : @pd_final;
  1924. pocall : pocall_none;
  1925. pooption : [po_finalmethod];
  1926. mutexclpocall : [pocall_internproc];
  1927. mutexclpotype : [];
  1928. mutexclpo : [po_exports,po_interrupt,po_inline]
  1929. ),(
  1930. idtok:_FORWARD;
  1931. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1932. handler : @pd_forward;
  1933. pocall : pocall_none;
  1934. pooption : [];
  1935. mutexclpocall : [pocall_internproc];
  1936. mutexclpotype : [];
  1937. mutexclpo : [po_external,po_inline]
  1938. ),(
  1939. idtok:_OLDFPCCALL;
  1940. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1941. handler : nil;
  1942. pocall : pocall_oldfpccall;
  1943. pooption : [];
  1944. mutexclpocall : [];
  1945. mutexclpotype : [];
  1946. mutexclpo : []
  1947. ),(
  1948. idtok:_INLINE;
  1949. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1950. handler : nil;
  1951. pocall : pocall_none;
  1952. pooption : [po_inline];
  1953. mutexclpocall : [pocall_safecall];
  1954. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1955. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  1956. ),(
  1957. idtok:_INTERNCONST;
  1958. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1959. handler : @pd_internconst;
  1960. pocall : pocall_none;
  1961. pooption : [po_internconst];
  1962. mutexclpocall : [];
  1963. mutexclpotype : [potype_operator];
  1964. mutexclpo : []
  1965. ),(
  1966. idtok:_INTERNPROC;
  1967. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1968. handler : @pd_internproc;
  1969. pocall : pocall_internproc;
  1970. pooption : [];
  1971. mutexclpocall : [];
  1972. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1973. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1974. ),(
  1975. idtok:_INTERRUPT;
  1976. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1977. handler : @pd_interrupt;
  1978. pocall : pocall_oldfpccall;
  1979. pooption : [po_interrupt];
  1980. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1981. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1982. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1983. mutexclpo : [po_external,po_inline]
  1984. ),(
  1985. idtok:_IOCHECK;
  1986. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1987. handler : nil;
  1988. pocall : pocall_none;
  1989. pooption : [po_iocheck];
  1990. mutexclpocall : [pocall_internproc];
  1991. mutexclpotype : [];
  1992. mutexclpo : [po_external]
  1993. ),(
  1994. idtok:_LOCAL;
  1995. pd_flags : [pd_implemen,pd_body];
  1996. handler : nil;
  1997. pocall : pocall_none;
  1998. pooption : [po_kylixlocal];
  1999. mutexclpocall : [pocall_internproc,pocall_far16];
  2000. mutexclpotype : [];
  2001. mutexclpo : [po_external,po_exports]
  2002. ),(
  2003. idtok:_MESSAGE;
  2004. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2005. handler : @pd_message;
  2006. pocall : pocall_none;
  2007. pooption : []; { can be po_msgstr or po_msgint }
  2008. mutexclpocall : [pocall_internproc];
  2009. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2010. mutexclpo : [po_interrupt,po_inline]
  2011. ),(
  2012. idtok:_MWPASCAL;
  2013. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2014. handler : nil;
  2015. pocall : pocall_mwpascal;
  2016. pooption : [];
  2017. mutexclpocall : [];
  2018. mutexclpotype : [];
  2019. mutexclpo : []
  2020. ),(
  2021. idtok:_NEAR;
  2022. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2023. handler : @pd_near;
  2024. pocall : pocall_none;
  2025. pooption : [];
  2026. mutexclpocall : [pocall_internproc];
  2027. mutexclpotype : [];
  2028. mutexclpo : []
  2029. ),(
  2030. idtok:_NOSTACKFRAME;
  2031. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2032. handler : nil;
  2033. pocall : pocall_none;
  2034. pooption : [po_nostackframe];
  2035. mutexclpocall : [pocall_internproc];
  2036. mutexclpotype : [];
  2037. mutexclpo : []
  2038. ),(
  2039. idtok:_OVERLOAD;
  2040. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava];
  2041. handler : @pd_overload;
  2042. pocall : pocall_none;
  2043. pooption : [po_overload];
  2044. mutexclpocall : [pocall_internproc];
  2045. mutexclpotype : [];
  2046. mutexclpo : []
  2047. ),(
  2048. idtok:_OVERRIDE;
  2049. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2050. handler : @pd_override;
  2051. pocall : pocall_none;
  2052. pooption : [po_overridingmethod,po_virtualmethod];
  2053. mutexclpocall : [pocall_internproc];
  2054. mutexclpotype : [];
  2055. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2056. ),(
  2057. idtok:_PASCAL;
  2058. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2059. handler : nil;
  2060. pocall : pocall_pascal;
  2061. pooption : [];
  2062. mutexclpocall : [];
  2063. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2064. mutexclpo : [po_external]
  2065. ),(
  2066. idtok:_PUBLIC;
  2067. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2068. handler : @pd_public;
  2069. pocall : pocall_none;
  2070. pooption : [po_public,po_global];
  2071. mutexclpocall : [pocall_internproc];
  2072. mutexclpotype : [];
  2073. mutexclpo : [po_external,po_inline]
  2074. ),(
  2075. idtok:_REGISTER;
  2076. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2077. handler : nil;
  2078. pocall : pocall_register;
  2079. pooption : [];
  2080. mutexclpocall : [];
  2081. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2082. mutexclpo : [po_external]
  2083. ),(
  2084. idtok:_REINTRODUCE;
  2085. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2086. handler : @pd_reintroduce;
  2087. pocall : pocall_none;
  2088. pooption : [po_reintroduce];
  2089. mutexclpocall : [pocall_internproc];
  2090. mutexclpotype : [];
  2091. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2092. ),(
  2093. idtok:_SAFECALL;
  2094. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2095. handler : nil;
  2096. pocall : pocall_safecall;
  2097. pooption : [];
  2098. mutexclpocall : [];
  2099. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2100. mutexclpo : [po_external]
  2101. ),(
  2102. idtok:_SOFTFLOAT;
  2103. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2104. handler : nil;
  2105. pocall : pocall_softfloat;
  2106. pooption : [];
  2107. mutexclpocall : [];
  2108. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2109. { it's available with po_external because the libgcc floating point routines on the arm
  2110. uses this calling convention }
  2111. mutexclpo : []
  2112. ),(
  2113. idtok:_STATIC;
  2114. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2115. handler : @pd_static;
  2116. pocall : pocall_none;
  2117. pooption : [po_staticmethod];
  2118. mutexclpocall : [pocall_internproc];
  2119. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2120. mutexclpo : [po_interrupt,po_exports]
  2121. ),(
  2122. idtok:_STDCALL;
  2123. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2124. handler : nil;
  2125. pocall : pocall_stdcall;
  2126. pooption : [];
  2127. mutexclpocall : [];
  2128. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2129. mutexclpo : [po_external]
  2130. ),(
  2131. idtok:_SYSCALL;
  2132. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2133. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2134. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2135. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2136. handler : @pd_syscall;
  2137. pocall : pocall_syscall;
  2138. pooption : [];
  2139. mutexclpocall : [];
  2140. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2141. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2142. ),(
  2143. idtok:_VIRTUAL;
  2144. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2145. handler : @pd_virtual;
  2146. pocall : pocall_none;
  2147. pooption : [po_virtualmethod];
  2148. mutexclpocall : [pocall_internproc];
  2149. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2150. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2151. ),(
  2152. idtok:_CPPDECL;
  2153. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2154. handler : nil;
  2155. pocall : pocall_cppdecl;
  2156. pooption : [];
  2157. mutexclpocall : [];
  2158. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2159. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2160. ),(
  2161. idtok:_VARARGS;
  2162. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2163. handler : nil;
  2164. pocall : pocall_none;
  2165. pooption : [po_varargs];
  2166. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2167. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2168. mutexclpotype : [];
  2169. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2170. ),(
  2171. idtok:_COMPILERPROC;
  2172. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2173. handler : nil;
  2174. pocall : pocall_none;
  2175. pooption : [po_compilerproc];
  2176. mutexclpocall : [];
  2177. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2178. mutexclpo : [po_interrupt]
  2179. ),(
  2180. idtok:_WEAKEXTERNAL;
  2181. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2182. handler : @pd_weakexternal;
  2183. pocall : pocall_none;
  2184. { mark it both external and weak external, so we don't have to
  2185. adapt all code for external symbols to also check for weak external
  2186. }
  2187. pooption : [po_external,po_weakexternal];
  2188. mutexclpocall : [pocall_internproc,pocall_syscall];
  2189. { allowed for external cpp classes }
  2190. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2191. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2192. ),(
  2193. idtok:_ENUMERATOR;
  2194. pd_flags : [pd_interface,pd_object,pd_record];
  2195. handler : @pd_enumerator;
  2196. pocall : pocall_none;
  2197. pooption : [];
  2198. mutexclpocall : [pocall_internproc];
  2199. mutexclpotype : [];
  2200. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2201. ),(
  2202. idtok:_RTLPROC;
  2203. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2204. handler : nil;
  2205. pocall : pocall_none;
  2206. pooption : [po_rtlproc];
  2207. mutexclpocall : [];
  2208. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2209. mutexclpo : [po_interrupt]
  2210. )
  2211. );
  2212. function check_proc_directive(isprocvar:boolean):boolean;
  2213. var
  2214. i : longint;
  2215. begin
  2216. result:=false;
  2217. for i:=1 to num_proc_directives do
  2218. if proc_direcdata[i].idtok=idtoken then
  2219. begin
  2220. if ((not isprocvar) or
  2221. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2222. { don't eat a public directive in classes }
  2223. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2224. result:=true;
  2225. exit;
  2226. end;
  2227. end;
  2228. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2229. {
  2230. Parse the procedure directive, returns true if a correct directive is found
  2231. }
  2232. var
  2233. p : longint;
  2234. found : boolean;
  2235. name : TIDString;
  2236. begin
  2237. parse_proc_direc:=false;
  2238. name:=tokeninfo^[idtoken].str;
  2239. found:=false;
  2240. { Hint directive? Then exit immediatly }
  2241. if (m_hintdirective in current_settings.modeswitches) then
  2242. begin
  2243. case idtoken of
  2244. _LIBRARY,
  2245. _PLATFORM,
  2246. _UNIMPLEMENTED,
  2247. _EXPERIMENTAL,
  2248. _DEPRECATED :
  2249. exit;
  2250. end;
  2251. end;
  2252. { C directive is MacPas only, because it breaks too much existing code
  2253. on other platforms (PFV) }
  2254. if (idtoken=_C) and
  2255. not(m_mac in current_settings.modeswitches) then
  2256. exit;
  2257. { retrieve data for directive if found }
  2258. for p:=1 to num_proc_directives do
  2259. if proc_direcdata[p].idtok=idtoken then
  2260. begin
  2261. found:=true;
  2262. break;
  2263. end;
  2264. { Check if the procedure directive is known }
  2265. if not found then
  2266. begin
  2267. { parsing a procvar type the name can be any
  2268. next variable !! }
  2269. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2270. not(idtoken=_PROPERTY) then
  2271. Message1(parser_w_unknown_proc_directive_ignored,name);
  2272. exit;
  2273. end;
  2274. { check if method and directive not for object, like public.
  2275. This needs to be checked also for procvars }
  2276. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2277. (symtablestack.top.symtabletype=ObjectSymtable) and
  2278. { directive allowed for cpp classes? }
  2279. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2280. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2281. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2282. exit;
  2283. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2284. (symtablestack.top.symtabletype=recordsymtable) then
  2285. exit;
  2286. { check if method and directive not for java class }
  2287. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2288. is_javaclass(tdef(symtablestack.top.defowner)) then
  2289. exit;
  2290. { check if method and directive not for java interface }
  2291. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2292. is_javainterface(tdef(symtablestack.top.defowner)) then
  2293. exit;
  2294. { Conflicts between directives ? }
  2295. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2296. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2297. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2298. begin
  2299. Message1(parser_e_proc_dir_conflict,name);
  2300. exit;
  2301. end;
  2302. { set calling convention }
  2303. if proc_direcdata[p].pocall<>pocall_none then
  2304. begin
  2305. if (po_hascallingconvention in pd.procoptions) then
  2306. begin
  2307. Message2(parser_w_proc_overriding_calling,
  2308. proccalloptionStr[pd.proccalloption],
  2309. proccalloptionStr[proc_direcdata[p].pocall]);
  2310. end;
  2311. { check if the target processor supports this calling convention }
  2312. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2313. begin
  2314. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2315. { recover }
  2316. proc_direcdata[p].pocall:=pocall_stdcall;
  2317. end;
  2318. pd.proccalloption:=proc_direcdata[p].pocall;
  2319. include(pd.procoptions,po_hascallingconvention);
  2320. end;
  2321. if pd.typ=procdef then
  2322. begin
  2323. { Check if the directive is only for objects }
  2324. if (pd_object in proc_direcdata[p].pd_flags) and
  2325. not assigned(tprocdef(pd).struct) then
  2326. exit;
  2327. { Check if the directive is only for records }
  2328. if (pd_record in proc_direcdata[p].pd_flags) and
  2329. not assigned(tprocdef(pd).struct) then
  2330. exit;
  2331. { check if method and directive not for interface }
  2332. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2333. is_interface(tprocdef(pd).struct) then
  2334. exit;
  2335. { check if method and directive not for interface }
  2336. if is_dispinterface(tprocdef(pd).struct) and
  2337. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2338. exit;
  2339. { check if method and directive not for objcclass }
  2340. if is_objcclass(tprocdef(pd).struct) and
  2341. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2342. exit;
  2343. { check if method and directive not for objcprotocol }
  2344. if is_objcprotocol(tprocdef(pd).struct) and
  2345. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2346. exit;
  2347. { check if method and directive not for record/class helper }
  2348. if is_objectpascal_helper(tprocdef(pd).struct) and
  2349. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2350. end;
  2351. { consume directive, and turn flag on }
  2352. consume(token);
  2353. parse_proc_direc:=true;
  2354. { Check the pd_flags if the directive should be allowed }
  2355. if (pd_interface in pdflags) and
  2356. not(pd_interface in proc_direcdata[p].pd_flags) then
  2357. begin
  2358. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2359. exit;
  2360. end;
  2361. if (pd_implemen in pdflags) and
  2362. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2363. begin
  2364. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2365. exit;
  2366. end;
  2367. if (pd_procvar in pdflags) and
  2368. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2369. begin
  2370. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2371. exit;
  2372. end;
  2373. { Return the new pd_flags }
  2374. if not(pd_body in proc_direcdata[p].pd_flags) then
  2375. exclude(pdflags,pd_body);
  2376. { Add the correct flag }
  2377. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2378. { Call the handler }
  2379. if pointer(proc_direcdata[p].handler)<>nil then
  2380. proc_direcdata[p].handler(pd);
  2381. end;
  2382. function proc_get_importname(pd:tprocdef):string;
  2383. var
  2384. dllname, importname : string;
  2385. begin
  2386. result:='';
  2387. if not(po_external in pd.procoptions) then
  2388. internalerror(200412151);
  2389. { external name or number is specified }
  2390. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2391. begin
  2392. if assigned(pd.import_dll) then
  2393. dllname:=pd.import_dll^
  2394. else
  2395. dllname:='';
  2396. if assigned(pd.import_name) then
  2397. importname:=pd.import_name^
  2398. else
  2399. importname:='';
  2400. proc_get_importname:=make_dllmangledname(dllname,
  2401. importname,pd.import_nr,pd.proccalloption);
  2402. end
  2403. else
  2404. begin
  2405. { Default names when importing variables }
  2406. case pd.proccalloption of
  2407. pocall_cdecl :
  2408. begin
  2409. if assigned(pd.struct) then
  2410. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2411. else
  2412. result:=target_info.Cprefix+pd.procsym.realname;
  2413. end;
  2414. pocall_cppdecl :
  2415. begin
  2416. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2417. end;
  2418. else
  2419. begin
  2420. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2421. { but according to MacPas mode description
  2422. Cprefix should still be used PM }
  2423. if (m_mac in current_settings.modeswitches) then
  2424. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2425. end;
  2426. end;
  2427. end;
  2428. end;
  2429. procedure proc_set_mangledname(pd:tprocdef);
  2430. var
  2431. s : string;
  2432. begin
  2433. { When the mangledname is already set we aren't allowed to change
  2434. it because it can already be used somewhere (PFV) }
  2435. if not(po_has_mangledname in pd.procoptions) then
  2436. begin
  2437. if (po_external in pd.procoptions) then
  2438. begin
  2439. { External Procedures are only allowed to change the mangledname
  2440. in their first declaration }
  2441. if (pd.forwarddef or (not pd.hasforward)) then
  2442. begin
  2443. s:=proc_get_importname(pd);
  2444. if s<>'' then
  2445. begin
  2446. pd.setmangledname(s);
  2447. end;
  2448. end;
  2449. end
  2450. else
  2451. { Normal procedures }
  2452. begin
  2453. if (po_compilerproc in pd.procoptions) then
  2454. begin
  2455. pd.setmangledname(lower(pd.procsym.name));
  2456. end;
  2457. end;
  2458. end;
  2459. { Public/exported alias names }
  2460. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2461. not(po_has_public_name in pd.procoptions) then
  2462. begin
  2463. case pd.proccalloption of
  2464. pocall_cdecl :
  2465. begin
  2466. if assigned(pd.struct) then
  2467. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2468. else
  2469. begin
  2470. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2471. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2472. (po_exports in pd.procoptions) then
  2473. pd.aliasnames.insert(pd.procsym.realname)
  2474. else
  2475. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2476. end;
  2477. end;
  2478. pocall_cppdecl :
  2479. begin
  2480. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2481. end;
  2482. end;
  2483. { prevent adding the alias a second time }
  2484. include(pd.procoptions,po_has_public_name);
  2485. end;
  2486. end;
  2487. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2488. begin
  2489. if hcc_check in flags then
  2490. begin
  2491. { set the default calling convention if none provided }
  2492. if (pd.typ=procdef) and
  2493. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2494. is_cppclass(tprocdef(pd).struct)) then
  2495. begin
  2496. { none of the explicit calling conventions should be allowed }
  2497. if (po_hascallingconvention in pd.procoptions) then
  2498. internalerror(2009032501);
  2499. if is_cppclass(tprocdef(pd).struct) then
  2500. pd.proccalloption:=pocall_cppdecl
  2501. else
  2502. pd.proccalloption:=pocall_cdecl;
  2503. end
  2504. else if not(po_hascallingconvention in pd.procoptions) then
  2505. pd.proccalloption:=current_settings.defproccall
  2506. else
  2507. begin
  2508. if pd.proccalloption=pocall_none then
  2509. internalerror(200309081);
  2510. end;
  2511. { handle proccall specific settings }
  2512. case pd.proccalloption of
  2513. pocall_cdecl,
  2514. pocall_cppdecl :
  2515. begin
  2516. { check C cdecl para types }
  2517. check_c_para(pd);
  2518. end;
  2519. pocall_far16 :
  2520. begin
  2521. { Temporary stub, must be rewritten to support OS/2 far16 }
  2522. Message1(parser_w_proc_directive_ignored,'FAR16');
  2523. end;
  2524. end;
  2525. { Inlining is enabled and supported? }
  2526. if (po_inline in pd.procoptions) and
  2527. not(cs_do_inline in current_settings.localswitches) then
  2528. begin
  2529. { Give an error if inline is not supported by the compiler mode,
  2530. otherwise only give a hint that this procedure will not be inlined }
  2531. if not(m_default_inline in current_settings.modeswitches) then
  2532. Message(parser_e_proc_inline_not_supported)
  2533. else
  2534. Message(parser_h_inlining_disabled);
  2535. exclude(pd.procoptions,po_inline);
  2536. end;
  2537. { For varargs directive also cdecl and external must be defined }
  2538. if (po_varargs in pd.procoptions) then
  2539. begin
  2540. { check first for external in the interface, if available there
  2541. then the cdecl must also be there since there is no implementation
  2542. available to contain it }
  2543. if parse_only then
  2544. begin
  2545. { if external is available, then cdecl must also be available,
  2546. procvars don't need external }
  2547. if not((po_external in pd.procoptions) or
  2548. (pd.typ=procvardef) or
  2549. { for objcclasses this is checked later, because the entire
  2550. class may be external. }
  2551. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2552. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2553. Message(parser_e_varargs_need_cdecl_and_external);
  2554. end
  2555. else
  2556. begin
  2557. { both must be defined now }
  2558. if not((po_external in pd.procoptions) or
  2559. (pd.typ=procvardef)) or
  2560. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2561. Message(parser_e_varargs_need_cdecl_and_external);
  2562. end;
  2563. end;
  2564. end;
  2565. if hcc_insert_hidden_paras in flags then
  2566. begin
  2567. { insert hidden high parameters }
  2568. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2569. { insert hidden self parameter }
  2570. insert_self_and_vmt_para(pd);
  2571. { insert funcret parameter if required }
  2572. insert_funcret_para(pd);
  2573. { Make var parameters regable, this must be done after the calling
  2574. convention is set. }
  2575. { this must be done before parentfp is insert, because getting all cases
  2576. where parentfp must be in a memory location isn't catched properly so
  2577. we put parentfp never in a register }
  2578. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2579. { insert parentfp parameter if required }
  2580. insert_parentfp_para(pd);
  2581. end;
  2582. { Calculate parameter tlist }
  2583. pd.calcparas;
  2584. end;
  2585. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2586. {
  2587. Parse the procedure directives. It does not matter if procedure directives
  2588. are written using ;procdir; or ['procdir'] syntax.
  2589. }
  2590. var
  2591. res : boolean;
  2592. begin
  2593. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2594. begin
  2595. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2596. include(pd.procoptions,po_public);
  2597. include(pd.procoptions,po_has_public_name);
  2598. include(pd.procoptions,po_global);
  2599. end;
  2600. { methods from external class definitions are all external themselves }
  2601. if (pd.typ=procdef) and
  2602. assigned(tprocdef(pd).struct) and
  2603. (tprocdef(pd).struct.typ=objectdef) and
  2604. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2605. tprocdef(pd).make_external;
  2606. { Class constructors and destructor are static class methods in real. }
  2607. { There are many places in the compiler where either class or static }
  2608. { method flag changes the behavior. It is simplier to add them to }
  2609. { the class constructors/destructors options than to fix all the }
  2610. { occurencies. (Paul) }
  2611. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2612. begin
  2613. include(pd.procoptions,po_classmethod);
  2614. include(pd.procoptions,po_staticmethod);
  2615. end;
  2616. while token in [_ID,_LECKKLAMMER] do
  2617. begin
  2618. if try_to_consume(_LECKKLAMMER) then
  2619. begin
  2620. repeat
  2621. parse_proc_direc(pd,pdflags);
  2622. until not try_to_consume(_COMMA);
  2623. consume(_RECKKLAMMER);
  2624. { we always expect at least '[];' }
  2625. res:=true;
  2626. end
  2627. else
  2628. begin
  2629. res:=parse_proc_direc(pd,pdflags);
  2630. end;
  2631. { A procedure directive normally followed by a semicolon, but in
  2632. a const section or reading a type we should stop when _EQ is found,
  2633. because a constant/default value follows }
  2634. if res then
  2635. begin
  2636. if (block_type=bt_const_type) and
  2637. (token=_EQ) then
  2638. break;
  2639. { support procedure proc;stdcall export; }
  2640. if not(check_proc_directive((pd.typ=procvardef))) then
  2641. begin
  2642. { support "record p : procedure stdcall end;" and
  2643. "var p : procedure stdcall = nil;" }
  2644. if (pd_procvar in pdflags) and
  2645. (token in [_END,_RKLAMMER,_EQ]) then
  2646. break
  2647. else
  2648. begin
  2649. if (token=_COLON) then
  2650. begin
  2651. Message(parser_e_field_not_allowed_here);
  2652. consume_all_until(_SEMICOLON);
  2653. end;
  2654. consume(_SEMICOLON)
  2655. end;
  2656. end;
  2657. end
  2658. else
  2659. break;
  2660. end;
  2661. end;
  2662. procedure parse_var_proc_directives(sym:tsym);
  2663. var
  2664. pdflags : tpdflags;
  2665. pd : tabstractprocdef;
  2666. begin
  2667. pdflags:=[pd_procvar];
  2668. pd:=nil;
  2669. case sym.typ of
  2670. fieldvarsym,
  2671. staticvarsym,
  2672. localvarsym,
  2673. paravarsym :
  2674. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2675. typesym :
  2676. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2677. else
  2678. internalerror(2003042617);
  2679. end;
  2680. if pd.typ<>procvardef then
  2681. internalerror(2003042618);
  2682. { names should never be used anyway }
  2683. parse_proc_directives(pd,pdflags);
  2684. end;
  2685. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2686. var
  2687. pdflags : tpdflags;
  2688. begin
  2689. pdflags:=[pd_object];
  2690. parse_proc_directives(pd,pdflags);
  2691. end;
  2692. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2693. var
  2694. pdflags : tpdflags;
  2695. begin
  2696. pdflags:=[pd_record];
  2697. parse_proc_directives(pd,pdflags);
  2698. end;
  2699. function proc_add_definition(var currpd:tprocdef):boolean;
  2700. {
  2701. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2702. forwarddef is found and reused it returns true
  2703. }
  2704. var
  2705. fwpd : tprocdef;
  2706. currparasym,
  2707. fwparasym : tsym;
  2708. currparacnt,
  2709. fwparacnt,
  2710. curridx,
  2711. fwidx,
  2712. i : longint;
  2713. po_comp : tprocoptions;
  2714. paracompopt: tcompare_paras_options;
  2715. forwardfound : boolean;
  2716. symentry: TSymEntry;
  2717. begin
  2718. forwardfound:=false;
  2719. { check overloaded functions if the same function already exists }
  2720. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2721. begin
  2722. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2723. { can happen for internally generated routines }
  2724. if (fwpd=currpd) then
  2725. begin
  2726. result:=true;
  2727. exit;
  2728. end;
  2729. { Skip overloaded definitions that are declared in other units }
  2730. if fwpd.procsym<>currpd.procsym then
  2731. continue;
  2732. { check the parameters, for delphi/tp it is possible to
  2733. leave the parameters away in the implementation (forwarddef=false).
  2734. But for an overload declared function this is not allowed }
  2735. if { check if empty implementation arguments match is allowed }
  2736. (
  2737. not(m_repeat_forward in current_settings.modeswitches) and
  2738. not(currpd.forwarddef) and
  2739. is_bareprocdef(currpd) and
  2740. not(po_overload in fwpd.procoptions)
  2741. ) or
  2742. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2743. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  2744. don't check default values here, because routines that are the same except for their default
  2745. values should be reported as mismatches (since you can't overload based on different default
  2746. parameter values) }
  2747. (
  2748. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2749. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2750. ) then
  2751. begin
  2752. { Check if we've found the forwarddef, if found then
  2753. we need to update the forward def with the current
  2754. implementation settings }
  2755. if fwpd.forwarddef then
  2756. begin
  2757. forwardfound:=true;
  2758. if not(m_repeat_forward in current_settings.modeswitches) and
  2759. (fwpd.proccalloption<>currpd.proccalloption) then
  2760. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2761. else
  2762. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2763. { Check calling convention }
  2764. if (fwpd.proccalloption<>currpd.proccalloption) then
  2765. begin
  2766. { In delphi it is possible to specify the calling
  2767. convention in the interface or implementation if
  2768. there was no convention specified in the other
  2769. part }
  2770. if (m_delphi in current_settings.modeswitches) then
  2771. begin
  2772. if not(po_hascallingconvention in currpd.procoptions) then
  2773. currpd.proccalloption:=fwpd.proccalloption
  2774. else
  2775. if not(po_hascallingconvention in fwpd.procoptions) then
  2776. fwpd.proccalloption:=currpd.proccalloption
  2777. else
  2778. begin
  2779. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2780. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2781. { restore interface settings }
  2782. currpd.proccalloption:=fwpd.proccalloption;
  2783. end;
  2784. end
  2785. else
  2786. begin
  2787. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2788. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2789. { restore interface settings }
  2790. currpd.proccalloption:=fwpd.proccalloption;
  2791. end;
  2792. end;
  2793. { Check static }
  2794. if (po_staticmethod in fwpd.procoptions) then
  2795. begin
  2796. if not (po_staticmethod in currpd.procoptions) then
  2797. begin
  2798. include(currpd.procoptions, po_staticmethod);
  2799. if (po_classmethod in currpd.procoptions) then
  2800. begin
  2801. { remove self from the hidden paras }
  2802. symentry:=currpd.parast.Find('self');
  2803. if symentry<>nil then
  2804. begin
  2805. currpd.parast.Delete(symentry);
  2806. currpd.calcparas;
  2807. end;
  2808. end;
  2809. end;
  2810. end;
  2811. { Check if the procedure type and return type are correct,
  2812. also the parameters must match also with the type and that
  2813. if the implementation has default parameters, the interface
  2814. also has them and that if they both have them, that they
  2815. have the same value }
  2816. if ((m_repeat_forward in current_settings.modeswitches) or
  2817. not is_bareprocdef(currpd)) and
  2818. ((compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  2819. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2820. begin
  2821. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2822. fwpd.fullprocname(false));
  2823. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2824. break;
  2825. end;
  2826. { Check if both are declared forward }
  2827. if fwpd.forwarddef and currpd.forwarddef then
  2828. begin
  2829. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2830. currpd.fullprocname(false));
  2831. end;
  2832. { internconst or internproc only need to be defined once }
  2833. if (fwpd.proccalloption=pocall_internproc) then
  2834. currpd.proccalloption:=fwpd.proccalloption
  2835. else
  2836. if (currpd.proccalloption=pocall_internproc) then
  2837. fwpd.proccalloption:=currpd.proccalloption;
  2838. { Check procedure options, Delphi requires that class is
  2839. repeated in the implementation for class methods }
  2840. if (m_fpc in current_settings.modeswitches) then
  2841. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2842. else
  2843. po_comp:=[po_classmethod,po_methodpointer];
  2844. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2845. (fwpd.proctypeoption <> currpd.proctypeoption) or
  2846. { if the implementation version has an "overload" modifier,
  2847. the interface version must also have it (otherwise we can
  2848. get annoying crashes due to interface crc changes) }
  2849. (not(po_overload in fwpd.procoptions) and
  2850. (po_overload in currpd.procoptions)) then
  2851. begin
  2852. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2853. fwpd.fullprocname(false));
  2854. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2855. { This error is non-fatal, we can recover }
  2856. end;
  2857. { Forward declaration is external? }
  2858. if (po_external in fwpd.procoptions) then
  2859. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2860. { Check parameters }
  2861. if (m_repeat_forward in current_settings.modeswitches) or
  2862. (currpd.minparacount>0) then
  2863. begin
  2864. { If mangled names are equal then they have the same amount of arguments }
  2865. { We can check the names of the arguments }
  2866. { both symtables are in the same order from left to right }
  2867. curridx:=0;
  2868. fwidx:=0;
  2869. currparacnt:=currpd.parast.SymList.Count;
  2870. fwparacnt:=fwpd.parast.SymList.Count;
  2871. repeat
  2872. { skip default parameter constsyms }
  2873. while (curridx<currparacnt) and
  2874. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2875. inc(curridx);
  2876. while (fwidx<fwparacnt) and
  2877. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2878. inc(fwidx);
  2879. { stop when one of the two lists is at the end }
  2880. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2881. break;
  2882. { compare names of parameters, ignore implictly
  2883. renamed parameters }
  2884. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2885. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2886. if not(sp_implicitrename in currparasym.symoptions) and
  2887. not(sp_implicitrename in fwparasym.symoptions) then
  2888. begin
  2889. if (currparasym.name<>fwparasym.name) then
  2890. begin
  2891. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2892. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2893. break;
  2894. end;
  2895. end;
  2896. { next parameter }
  2897. inc(curridx);
  2898. inc(fwidx);
  2899. until false;
  2900. end;
  2901. { Everything is checked, now we can update the forward declaration
  2902. with the new data from the implementation }
  2903. fwpd.forwarddef:=currpd.forwarddef;
  2904. fwpd.hasforward:=true;
  2905. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2906. { marked as local but exported from unit? }
  2907. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2908. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2909. if fwpd.extnumber=$ffff then
  2910. fwpd.extnumber:=currpd.extnumber;
  2911. while not currpd.aliasnames.empty do
  2912. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2913. { update fileinfo so position references the implementation,
  2914. also update funcretsym if it is already generated }
  2915. fwpd.fileinfo:=currpd.fileinfo;
  2916. if assigned(fwpd.funcretsym) then
  2917. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2918. if assigned(currpd.deprecatedmsg) then
  2919. begin
  2920. stringdispose(fwpd.deprecatedmsg);
  2921. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2922. end;
  2923. { import names }
  2924. if assigned(currpd.import_dll) then
  2925. begin
  2926. stringdispose(fwpd.import_dll);
  2927. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2928. end;
  2929. if assigned(currpd.import_name) then
  2930. begin
  2931. stringdispose(fwpd.import_name);
  2932. fwpd.import_name:=stringdup(currpd.import_name^);
  2933. end;
  2934. fwpd.import_nr:=currpd.import_nr;
  2935. { for compilerproc defines we need to rename and update the
  2936. symbolname to lowercase }
  2937. if (po_compilerproc in fwpd.procoptions) then
  2938. begin
  2939. { rename to lowercase so users can't access it }
  2940. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2941. { the mangeled name is already changed by the pd_compilerproc }
  2942. { handler. It must be done immediately because if we have a }
  2943. { call to a compilerproc before it's implementation is }
  2944. { encountered, it must already use the new mangled name (JM) }
  2945. end;
  2946. { Release current procdef }
  2947. currpd.owner.deletedef(currpd);
  2948. currpd:=fwpd;
  2949. end
  2950. else
  2951. begin
  2952. { abstract methods aren't forward defined, but this }
  2953. { needs another error message }
  2954. if (po_abstractmethod in fwpd.procoptions) then
  2955. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2956. else
  2957. begin
  2958. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2959. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2960. end;
  2961. end;
  2962. { we found one proc with the same arguments, there are no others
  2963. so we can stop }
  2964. break;
  2965. end;
  2966. { check for allowing overload directive }
  2967. if not(m_fpc in current_settings.modeswitches) then
  2968. begin
  2969. { overload directive turns on overloading }
  2970. if ((po_overload in currpd.procoptions) or
  2971. (po_overload in fwpd.procoptions)) then
  2972. begin
  2973. { check if all procs have overloading, but not if the proc is a method or
  2974. already declared forward, then the check is already done }
  2975. if not(fwpd.hasforward or
  2976. assigned(currpd.struct) or
  2977. (currpd.forwarddef<>fwpd.forwarddef) or
  2978. ((po_overload in currpd.procoptions) and
  2979. (po_overload in fwpd.procoptions))) then
  2980. begin
  2981. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2982. break;
  2983. end
  2984. end
  2985. else
  2986. begin
  2987. if not(fwpd.forwarddef) then
  2988. begin
  2989. if (m_tp7 in current_settings.modeswitches) then
  2990. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2991. else
  2992. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2993. break;
  2994. end;
  2995. end;
  2996. end; { equal arguments }
  2997. end;
  2998. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2999. list }
  3000. if not forwardfound then
  3001. begin
  3002. { can happen in Delphi mode }
  3003. if (currpd.proctypeoption = potype_function) and
  3004. is_void(currpd.returndef) then
  3005. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3006. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3007. end;
  3008. proc_add_definition:=forwardfound;
  3009. end;
  3010. end.