pdecsub.pas 145 KB

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