pdecsub.pas 145 KB

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