pdecsub.pas 146 KB

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