pdecsub.pas 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. // flags of handle_calling_convention routine
  49. thccflag=(
  50. hcc_check, // perform checks and outup errors if found
  51. hcc_insert_hidden_paras // insert hidden parameters
  52. );
  53. thccflags=set of thccflag;
  54. const
  55. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  56. function check_proc_directive(isprocvar:boolean):boolean;
  57. function proc_add_definition(var currpd:tprocdef):boolean;
  58. function proc_get_importname(pd:tprocdef):string;
  59. procedure proc_set_mangledname(pd:tprocdef);
  60. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  61. procedure parse_parameter_dec(pd:tabstractprocdef);
  62. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  63. procedure parse_var_proc_directives(sym:tsym);
  64. procedure parse_object_proc_directives(pd:tabstractprocdef);
  65. procedure parse_record_proc_directives(pd:tabstractprocdef);
  66. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  67. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  68. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean);
  69. { parse a record method declaration (not a (class) constructor/destructor) }
  70. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  71. procedure insert_record_hidden_paras(astruct: trecorddef);
  72. { helper functions - they insert nested objects hierarchy to the symtablestack
  73. with object hierarchy
  74. }
  75. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  76. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  77. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  78. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  79. implementation
  80. uses
  81. SysUtils,
  82. { common }
  83. cutils,
  84. { global }
  85. globtype,globals,verbose,constexp,
  86. systems,
  87. cpuinfo,
  88. { symtable }
  89. symbase,symcpu,symtable,defutil,defcmp,
  90. { parameter handling }
  91. paramgr,cpupara,
  92. { pass 1 }
  93. fmodule,node,htypechk,ncon,ppu,nld,
  94. objcutil,
  95. { parser }
  96. scanner,
  97. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  98. {$ifdef jvm}
  99. ,pjvm
  100. {$endif}
  101. ;
  102. const
  103. { Please leave this here, this module should NOT use
  104. these variables.
  105. Declaring it as string here results in an error when compiling (PFV) }
  106. current_procinfo = 'error';
  107. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  108. var
  109. _class,hp : tobjectdef;
  110. begin
  111. if obj.typ=recorddef then
  112. begin
  113. symtablestack.push(obj.symtable);
  114. result:=1;
  115. exit;
  116. end;
  117. result:=0;
  118. { insert class hierarchy in the reverse order }
  119. hp:=nil;
  120. repeat
  121. _class:=tobjectdef(obj);
  122. while _class.childof<>hp do
  123. _class:=_class.childof;
  124. hp:=_class;
  125. symtablestack.push(_class.symtable);
  126. inc(result);
  127. until hp=obj;
  128. end;
  129. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  130. begin
  131. result:=0;
  132. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  133. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  134. inc(result,push_child_hierarchy(obj));
  135. end;
  136. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  137. var
  138. _class : tobjectdef;
  139. begin
  140. if obj.typ=recorddef then
  141. begin
  142. symtablestack.pop(obj.symtable);
  143. result:=1;
  144. exit;
  145. end;
  146. result:=0;
  147. _class:=tobjectdef(obj);
  148. while assigned(_class) do
  149. begin
  150. symtablestack.pop(_class.symtable);
  151. _class:=_class.childof;
  152. inc(result);
  153. end;
  154. end;
  155. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  156. begin
  157. result:=pop_child_hierarchy(obj);
  158. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  159. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  160. end;
  161. procedure check_msg_para(p:TObject;arg:pointer);
  162. begin
  163. if (tsym(p).typ<>paravarsym) then
  164. exit;
  165. with tparavarsym(p) do
  166. begin
  167. { Count parameters }
  168. if (paranr>=10) then
  169. inc(plongint(arg)^);
  170. { First parameter must be var }
  171. if (paranr=10) and
  172. (varspez<>vs_var) then
  173. MessagePos(fileinfo,parser_e_ill_msg_param);
  174. end;
  175. end;
  176. procedure set_addr_param_regable(p:TObject;arg:pointer);
  177. begin
  178. if (tsym(p).typ<>paravarsym) then
  179. exit;
  180. with tparavarsym(p) do
  181. begin
  182. if (not needs_finalization) and
  183. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  184. varregable:=vr_addr;
  185. end;
  186. end;
  187. procedure parse_parameter_dec(pd:tabstractprocdef);
  188. {
  189. handle_procvar needs the same changes
  190. }
  191. type
  192. tppv = (pv_none,pv_proc,pv_func);
  193. var
  194. sc : TFPObjectList;
  195. hdef : tdef;
  196. arrayelementdef : tdef;
  197. vs : tparavarsym;
  198. i : longint;
  199. srsym : tsym;
  200. pv : tprocvardef;
  201. varspez : Tvarspez;
  202. defaultvalue : tconstsym;
  203. defaultrequired : boolean;
  204. old_block_type : tblock_type;
  205. currparast : tparasymtable;
  206. parseprocvar : tppv;
  207. locationstr : string;
  208. paranr : integer;
  209. dummytype : ttypesym;
  210. explicit_paraloc,
  211. need_array,
  212. is_univ: boolean;
  213. stoptions : TSingleTypeOptions;
  214. procedure handle_default_para_value;
  215. var
  216. convpd : tprocdef;
  217. doconv : tconverttype;
  218. nodetype : tnodetype;
  219. bt : tblock_type;
  220. begin
  221. { only allowed for types that can be represented by a
  222. constant expression }
  223. if try_to_consume(_EQ) then
  224. begin
  225. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  226. is_object(hdef) or
  227. ((hdef.typ=arraydef) and
  228. not is_dynamic_array(hdef)) then
  229. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  230. vs:=tparavarsym(sc[0]);
  231. if sc.count>1 then
  232. Message(parser_e_default_value_only_one_para);
  233. if not(vs.varspez in [vs_value,vs_const]) then
  234. Message(parser_e_default_value_val_const);
  235. bt:=block_type;
  236. block_type:=bt_const;
  237. { prefix 'def' to the parameter name }
  238. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  239. block_type:=bt;
  240. if assigned(defaultvalue) then
  241. begin
  242. include(defaultvalue.symoptions,sp_internal);
  243. pd.parast.insert(defaultvalue);
  244. { check whether the default value is of the correct
  245. type }
  246. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  247. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  248. end;
  249. defaultrequired:=true;
  250. end
  251. else
  252. begin
  253. if defaultrequired then
  254. Message1(parser_e_default_value_expected_for_para,vs.name);
  255. end;
  256. end;
  257. begin
  258. old_block_type:=block_type;
  259. explicit_paraloc:=false;
  260. consume(_LKLAMMER);
  261. { Delphi/Kylix supports nonsense like }
  262. { procedure p(); }
  263. if try_to_consume(_RKLAMMER) and
  264. not(m_tp7 in current_settings.modeswitches) then
  265. exit;
  266. { parsing a proc or procvar ? }
  267. currparast:=tparasymtable(pd.parast);
  268. { reset }
  269. sc:=TFPObjectList.create(false);
  270. defaultrequired:=false;
  271. paranr:=0;
  272. block_type:=bt_var;
  273. is_univ:=false;
  274. repeat
  275. parseprocvar:=pv_none;
  276. if try_to_consume(_VAR) then
  277. varspez:=vs_var
  278. else
  279. if try_to_consume(_CONST) then
  280. varspez:=vs_const
  281. else
  282. if (m_out in current_settings.modeswitches) and
  283. try_to_consume(_OUT) then
  284. varspez:=vs_out
  285. else
  286. if try_to_consume(_CONSTREF) then
  287. varspez:=vs_constref
  288. else
  289. if (m_mac in current_settings.modeswitches) and
  290. try_to_consume(_POINTPOINTPOINT) then
  291. begin
  292. include(pd.procoptions,po_varargs);
  293. break;
  294. end
  295. else
  296. if (m_nested_procvars in current_settings.modeswitches) and
  297. try_to_consume(_PROCEDURE) then
  298. begin
  299. parseprocvar:=pv_proc;
  300. varspez:=vs_const;
  301. end
  302. else
  303. if (m_nested_procvars in current_settings.modeswitches) and
  304. try_to_consume(_FUNCTION) then
  305. begin
  306. parseprocvar:=pv_func;
  307. varspez:=vs_const;
  308. end
  309. else
  310. varspez:=vs_value;
  311. defaultvalue:=nil;
  312. hdef:=nil;
  313. { read identifiers and insert with error type }
  314. sc.clear;
  315. repeat
  316. inc(paranr);
  317. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  318. currparast.insert(vs);
  319. if assigned(vs.owner) then
  320. sc.add(vs)
  321. else
  322. vs.free;
  323. consume(_ID);
  324. until not try_to_consume(_COMMA);
  325. locationstr:='';
  326. { macpas anonymous procvar }
  327. if parseprocvar<>pv_none then
  328. begin
  329. { inline procvar definitions are always nested procvars }
  330. pv:=cprocvardef.create(normal_function_level+1);
  331. if token=_LKLAMMER then
  332. parse_parameter_dec(pv);
  333. if parseprocvar=pv_func then
  334. begin
  335. block_type:=bt_var_type;
  336. consume(_COLON);
  337. single_type(pv.returndef,[]);
  338. block_type:=bt_var;
  339. end;
  340. hdef:=pv;
  341. { possible proc directives }
  342. if check_proc_directive(true) then
  343. begin
  344. dummytype:=ctypesym.create('unnamed',hdef,true);
  345. parse_var_proc_directives(tsym(dummytype));
  346. dummytype.typedef:=nil;
  347. hdef.typesym:=nil;
  348. dummytype.free;
  349. end;
  350. { Add implicit hidden parameters and function result }
  351. handle_calling_convention(pv);
  352. {$ifdef jvm}
  353. { anonymous -> no name }
  354. jvm_create_procvar_class('',pv);
  355. {$endif}
  356. end
  357. else
  358. { read type declaration, force reading for value paras }
  359. if (token=_COLON) or (varspez=vs_value) then
  360. begin
  361. consume(_COLON);
  362. { check for an open array }
  363. need_array:=false;
  364. { bitpacked open array are not yet supported }
  365. if (token=_PACKED) and
  366. not(cs_bitpacking in current_settings.localswitches) then
  367. begin
  368. consume(_PACKED);
  369. need_array:=true;
  370. end;
  371. if (token=_ARRAY) or
  372. need_array then
  373. begin
  374. consume(_ARRAY);
  375. consume(_OF);
  376. { define range and type of range }
  377. hdef:=carraydef.create(0,-1,ptrsinttype);
  378. { array of const ? }
  379. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  380. begin
  381. consume(_CONST);
  382. srsym:=search_system_type('TVARREC');
  383. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  384. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  385. end
  386. else
  387. begin
  388. { define field type }
  389. if m_delphi in current_settings.modeswitches then
  390. stoptions:=[stoAllowSpecialization]
  391. else
  392. stoptions:=[];
  393. single_type(arrayelementdef,stoptions);
  394. tarraydef(hdef).elementdef:=arrayelementdef;
  395. end;
  396. end
  397. else
  398. begin
  399. if (m_mac in current_settings.modeswitches) then
  400. is_univ:=try_to_consume(_UNIV);
  401. if try_to_consume(_TYPE) then
  402. hdef:=ctypedformaltype
  403. else
  404. begin
  405. block_type:=bt_var_type;
  406. single_type(hdef,[stoAllowSpecialization]);
  407. block_type:=bt_var;
  408. end;
  409. { open string ? }
  410. if is_shortstring(hdef) then
  411. begin
  412. case varspez of
  413. vs_var,vs_out:
  414. begin
  415. { not 100% Delphi-compatible: type xstr=string[255] cannot
  416. become an openstring there, while here it can }
  417. if (cs_openstring in current_settings.moduleswitches) and
  418. (tstringdef(hdef).len=255) then
  419. hdef:=openshortstringtype
  420. end;
  421. vs_value:
  422. begin
  423. { value "openstring" parameters don't make sense (the
  424. original string can never be modified, so there's no
  425. use in passing its original length), so change these
  426. into regular shortstring parameters (seems to be what
  427. Delphi also does) }
  428. if is_open_string(hdef) then
  429. hdef:=cshortstringtype;
  430. end;
  431. end;
  432. end;
  433. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  434. begin
  435. if (idtoken=_LOCATION) then
  436. begin
  437. consume(_LOCATION);
  438. locationstr:=cstringpattern;
  439. consume(_CSTRING);
  440. end
  441. else
  442. begin
  443. if explicit_paraloc then
  444. Message(parser_e_paraloc_all_paras);
  445. locationstr:='';
  446. end;
  447. end
  448. else
  449. locationstr:='';
  450. { default parameter }
  451. if (m_default_para in current_settings.modeswitches) then
  452. handle_default_para_value;
  453. end;
  454. end
  455. else
  456. hdef:=cformaltype;
  457. { File types are only allowed for var and out parameters }
  458. if (hdef.typ=filedef) and
  459. not(varspez in [vs_out,vs_var]) then
  460. CGMessage(cg_e_file_must_call_by_reference);
  461. { Dispinterfaces are restricted to using only automatable types }
  462. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  463. not is_automatable(hdef) then
  464. Message1(type_e_not_automatable,hdef.typename);
  465. { univ cannot be used with types whose size is not known at compile
  466. time }
  467. if is_univ and
  468. not is_valid_univ_para_type(hdef) then
  469. Message1(parser_e_invalid_univ_para,hdef.typename);
  470. for i:=0 to sc.count-1 do
  471. begin
  472. vs:=tparavarsym(sc[i]);
  473. vs.univpara:=is_univ;
  474. { update varsym }
  475. vs.vardef:=hdef;
  476. vs.defaultconstsym:=defaultvalue;
  477. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  478. begin
  479. if locationstr<>'' then
  480. begin
  481. if sc.count>1 then
  482. Message(parser_e_paraloc_only_one_para);
  483. if (paranr>1) and not(explicit_paraloc) then
  484. Message(parser_e_paraloc_all_paras);
  485. explicit_paraloc:=true;
  486. include(vs.varoptions,vo_has_explicit_paraloc);
  487. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  488. message(parser_e_illegal_explicit_paraloc);
  489. end
  490. else
  491. if explicit_paraloc then
  492. Message(parser_e_paraloc_all_paras);
  493. end;
  494. end;
  495. until not try_to_consume(_SEMICOLON);
  496. if explicit_paraloc then
  497. begin
  498. pd.has_paraloc_info:=callerside;
  499. include(pd.procoptions,po_explicitparaloc);
  500. end;
  501. { remove parasymtable from stack }
  502. sc.free;
  503. { reset object options }
  504. block_type:=old_block_type;
  505. consume(_RKLAMMER);
  506. end;
  507. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  508. var
  509. hs : string;
  510. orgsp,sp,orgspnongen,spnongen : TIDString;
  511. dummysym,srsym : tsym;
  512. checkstack : psymtablestackitem;
  513. oldfilepos,
  514. classstartfilepos,
  515. procstartfilepos : tfileposinfo;
  516. i,
  517. index : longint;
  518. hadspecialize,
  519. firstpart,
  520. freegenericparams,
  521. found,
  522. searchagain : boolean;
  523. st,
  524. genericst: TSymtable;
  525. aprocsym : tprocsym;
  526. popclass : integer;
  527. ImplIntf : TImplementedInterface;
  528. old_parse_generic : boolean;
  529. old_current_structdef: tabstractrecorddef;
  530. old_current_genericdef,
  531. old_current_specializedef: tstoreddef;
  532. lasttoken,lastidtoken: ttoken;
  533. genericparams : tfphashobjectlist;
  534. procedure parse_operator_name;
  535. begin
  536. if (lasttoken in [first_overloaded..last_overloaded]) then
  537. begin
  538. optoken:=token;
  539. end
  540. else
  541. begin
  542. case lasttoken of
  543. _CARET:
  544. Message1(parser_e_overload_operator_failed,'**');
  545. _ID:
  546. case lastidtoken of
  547. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  548. _EXPLICIT:optoken:=_OP_EXPLICIT;
  549. _INC:optoken:=_OP_INC;
  550. _DEC:optoken:=_OP_DEC;
  551. 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_far(pd:tabstractprocdef);
  1544. begin
  1545. pd.declared_far;
  1546. end;
  1547. procedure pd_near(pd:tabstractprocdef);
  1548. begin
  1549. pd.declared_near;
  1550. end;
  1551. procedure pd_export(pd:tabstractprocdef);
  1552. begin
  1553. if pd.typ<>procdef then
  1554. internalerror(200304264);
  1555. if assigned(tprocdef(pd).struct) then
  1556. Message(parser_e_methods_dont_be_export);
  1557. if pd.parast.symtablelevel>normal_function_level then
  1558. Message(parser_e_dont_nest_export);
  1559. end;
  1560. procedure pd_forward(pd:tabstractprocdef);
  1561. begin
  1562. if pd.typ<>procdef then
  1563. internalerror(200304265);
  1564. tprocdef(pd).forwarddef:=true;
  1565. end;
  1566. procedure pd_alias(pd:tabstractprocdef);
  1567. begin
  1568. if pd.typ<>procdef then
  1569. internalerror(200304266);
  1570. consume(_COLON);
  1571. tprocdef(pd).aliasnames.insert(get_stringconst);
  1572. include(pd.procoptions,po_has_public_name);
  1573. end;
  1574. procedure pd_public(pd:tabstractprocdef);
  1575. begin
  1576. if pd.typ<>procdef then
  1577. internalerror(200304266);
  1578. if try_to_consume(_NAME) then
  1579. begin
  1580. tprocdef(pd).aliasnames.insert(get_stringconst);
  1581. include(pd.procoptions,po_has_public_name);
  1582. end;
  1583. end;
  1584. procedure pd_asmname(pd:tabstractprocdef);
  1585. begin
  1586. if pd.typ<>procdef then
  1587. internalerror(200304267);
  1588. if token=_CCHAR then
  1589. begin
  1590. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1591. consume(_CCHAR)
  1592. end
  1593. else
  1594. begin
  1595. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1596. consume(_CSTRING);
  1597. end;
  1598. { we don't need anything else }
  1599. tprocdef(pd).forwarddef:=false;
  1600. end;
  1601. procedure pd_internconst(pd:tabstractprocdef);
  1602. var v:Tconstexprint;
  1603. begin
  1604. if pd.typ<>procdef then
  1605. internalerror(200304268);
  1606. consume(_COLON);
  1607. v:=get_intconst;
  1608. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1609. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1610. else
  1611. Tprocdef(pd).extnumber:=longint(v.svalue);
  1612. end;
  1613. procedure pd_internproc(pd:tabstractprocdef);
  1614. var v:Tconstexprint;
  1615. begin
  1616. if pd.typ<>procdef then
  1617. internalerror(200304268);
  1618. consume(_COLON);
  1619. v:=get_intconst;
  1620. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1621. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1622. else
  1623. Tprocdef(pd).extnumber:=longint(v.svalue);
  1624. { the proc is defined }
  1625. tprocdef(pd).forwarddef:=false;
  1626. end;
  1627. procedure pd_interrupt(pd:tabstractprocdef);
  1628. begin
  1629. if pd.parast.symtablelevel>normal_function_level then
  1630. Message(parser_e_dont_nest_interrupt);
  1631. end;
  1632. procedure pd_abstract(pd:tabstractprocdef);
  1633. begin
  1634. if pd.typ<>procdef then
  1635. internalerror(200304269);
  1636. if is_objectpascal_helper(tprocdef(pd).struct) then
  1637. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1638. if assigned(tprocdef(pd).struct) and
  1639. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1640. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1641. else if (po_virtualmethod in pd.procoptions) then
  1642. begin
  1643. include(pd.procoptions,po_abstractmethod);
  1644. { one more abstract method }
  1645. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1646. end
  1647. else
  1648. Message(parser_e_only_virtual_methods_abstract);
  1649. { the method is defined }
  1650. tprocdef(pd).forwarddef:=false;
  1651. end;
  1652. procedure pd_final(pd:tabstractprocdef);
  1653. begin
  1654. if pd.typ<>procdef then
  1655. internalerror(200910170);
  1656. if is_objectpascal_helper(tprocdef(pd).struct) and
  1657. (m_objfpc in current_settings.modeswitches) then
  1658. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1659. if (po_virtualmethod in pd.procoptions) or
  1660. (is_javaclass(tprocdef(pd).struct) and
  1661. (po_classmethod in pd.procoptions)) then
  1662. include(pd.procoptions,po_finalmethod)
  1663. else
  1664. Message(parser_e_only_virtual_methods_final);
  1665. end;
  1666. procedure pd_enumerator(pd:tabstractprocdef);
  1667. begin
  1668. if pd.typ<>procdef then
  1669. internalerror(200910250);
  1670. if (token = _ID) then
  1671. begin
  1672. if pattern='MOVENEXT' then
  1673. begin
  1674. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1675. message(parser_e_only_one_enumerator_movenext);
  1676. pd.calcparas;
  1677. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1678. (pd.minparacount = 0) then
  1679. begin
  1680. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1681. include(pd.procoptions,po_enumerator_movenext);
  1682. end
  1683. else
  1684. Message(parser_e_enumerator_movenext_is_not_valid)
  1685. end
  1686. else
  1687. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1688. consume(token);
  1689. end
  1690. else
  1691. Message(parser_e_enumerator_identifier_required);
  1692. end;
  1693. procedure pd_virtual(pd:tabstractprocdef);
  1694. {$ifdef WITHDMT}
  1695. var
  1696. pt : tnode;
  1697. {$endif WITHDMT}
  1698. begin
  1699. if (not assigned(pd.owner.defowner) or
  1700. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1701. (po_external in pd.procoptions) then
  1702. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1703. if pd.typ<>procdef then
  1704. internalerror(2003042610);
  1705. if (pd.proctypeoption=potype_constructor) and
  1706. is_object(tprocdef(pd).struct) then
  1707. Message(parser_e_constructor_cannot_be_not_virtual);
  1708. if pd.is_generic then
  1709. message(parser_e_genfuncs_cannot_be_virtual);
  1710. if is_objectpascal_helper(tprocdef(pd).struct) and
  1711. (m_objfpc in current_settings.modeswitches) then
  1712. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1713. {$ifdef WITHDMT}
  1714. if is_object(tprocdef(pd).struct) and
  1715. (token<>_SEMICOLON) then
  1716. begin
  1717. { any type of parameter is allowed here! }
  1718. pt:=comp_expr(true);
  1719. if is_constintnode(pt) then
  1720. begin
  1721. include(pd.procoptions,po_msgint);
  1722. pd.messageinf.i:=pt.value;
  1723. end
  1724. else
  1725. Message(parser_e_ill_msg_expr);
  1726. disposetree(pt);
  1727. end;
  1728. {$endif WITHDMT}
  1729. end;
  1730. procedure pd_dispid(pd:tabstractprocdef);
  1731. var pt:Tnode;
  1732. begin
  1733. if pd.typ<>procdef then
  1734. internalerror(200604301);
  1735. pt:=comp_expr([ef_accept_equal]);
  1736. if is_constintnode(pt) then
  1737. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1738. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1739. else
  1740. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1741. else
  1742. message(parser_e_dispid_must_be_ord_const);
  1743. pt.free;
  1744. end;
  1745. procedure pd_static(pd:tabstractprocdef);
  1746. begin
  1747. if pd.typ<>procdef then
  1748. internalerror(2013032001);
  1749. if not assigned(tprocdef(pd).struct) then
  1750. internalerror(2013032002);
  1751. include(tprocdef(pd).procsym.symoptions,sp_static);
  1752. { "static" is not allowed for operators or normal methods (except in objects) }
  1753. if (pd.proctypeoption=potype_operator) or
  1754. (
  1755. not (po_classmethod in pd.procoptions) and
  1756. not is_object(tprocdef(pd).struct)
  1757. )
  1758. then
  1759. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1760. include(pd.procoptions,po_staticmethod);
  1761. end;
  1762. procedure pd_override(pd:tabstractprocdef);
  1763. begin
  1764. if pd.typ<>procdef then
  1765. internalerror(2003042611);
  1766. if is_objectpascal_helper(tprocdef(pd).struct) then
  1767. begin
  1768. if m_objfpc in current_settings.modeswitches then
  1769. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1770. end
  1771. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1772. Message(parser_e_no_object_override)
  1773. else if is_objccategory(tprocdef(pd).struct) then
  1774. Message(parser_e_no_category_override)
  1775. else if (po_external in pd.procoptions) and
  1776. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1777. not is_cppclass(tprocdef(pd).struct) and
  1778. not is_java_class_or_interface(tprocdef(pd).struct) then
  1779. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1780. end;
  1781. procedure pd_overload(pd:tabstractprocdef);
  1782. begin
  1783. if pd.typ<>procdef then
  1784. internalerror(2003042612);
  1785. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1786. end;
  1787. procedure pd_message(pd:tabstractprocdef);
  1788. var
  1789. pt : tnode;
  1790. paracnt : longint;
  1791. begin
  1792. if pd.typ<>procdef then
  1793. internalerror(2003042613);
  1794. if is_objectpascal_helper(tprocdef(pd).struct) then
  1795. begin
  1796. if m_objfpc in current_settings.modeswitches then
  1797. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1798. end
  1799. else
  1800. if not is_class(tprocdef(pd).struct) and
  1801. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1802. Message(parser_e_msg_only_for_classes);
  1803. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1804. Message(parser_e_multiple_messages);
  1805. { check parameter type }
  1806. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1807. begin
  1808. if po_external in pd.procoptions then
  1809. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1810. paracnt:=0;
  1811. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1812. if paracnt<>1 then
  1813. Message(parser_e_ill_msg_param);
  1814. end;
  1815. pt:=comp_expr([ef_accept_equal]);
  1816. { message is 1-character long }
  1817. if is_constcharnode(pt) then
  1818. begin
  1819. include(pd.procoptions,po_msgstr);
  1820. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1821. end
  1822. else if pt.nodetype=stringconstn then
  1823. begin
  1824. include(pd.procoptions,po_msgstr);
  1825. if (tstringconstnode(pt).len>255) then
  1826. Message(parser_e_message_string_too_long);
  1827. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1828. end
  1829. else
  1830. if is_constintnode(pt) and
  1831. (is_class(tprocdef(pd).struct) or
  1832. is_objectpascal_helper(tprocdef(pd).struct)) then
  1833. begin
  1834. include(pd.procoptions,po_msgint);
  1835. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1836. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1837. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1838. else
  1839. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1840. end
  1841. else
  1842. Message(parser_e_ill_msg_expr);
  1843. { check whether the selector name is valid in case of Objective-C }
  1844. if (po_msgstr in pd.procoptions) and
  1845. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1846. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1847. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1848. pt.free;
  1849. end;
  1850. procedure pd_reintroduce(pd:tabstractprocdef);
  1851. begin
  1852. if pd.typ<>procdef then
  1853. internalerror(200401211);
  1854. if is_objectpascal_helper(tprocdef(pd).struct) then
  1855. begin
  1856. if m_objfpc in current_settings.modeswitches then
  1857. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1858. end
  1859. else
  1860. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1861. not(is_objccategory(tprocdef(pd).struct)) and
  1862. not(is_javaclass(tprocdef(pd).struct)) then
  1863. Message(parser_e_no_object_reintroduce);
  1864. end;
  1865. procedure pd_syscall(pd:tabstractprocdef);
  1866. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64)}
  1867. var
  1868. vs : tparavarsym;
  1869. sym : tsym;
  1870. symtable : TSymtable;
  1871. v: Tconstexprint;
  1872. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64)}
  1873. begin
  1874. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1875. internalerror(2003042614);
  1876. tprocdef(pd).forwarddef:=false;
  1877. {$ifdef m68k}
  1878. if target_info.system in [system_m68k_amiga] then
  1879. begin
  1880. include(pd.procoptions,po_syscall_legacy);
  1881. if consume_sym(sym,symtable) then
  1882. begin
  1883. if (sym.typ=staticvarsym) and
  1884. (
  1885. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1886. is_32bitint(tabstractvarsym(sym).vardef)
  1887. ) then
  1888. begin
  1889. include(pd.procoptions,po_syscall_has_libsym);
  1890. tcpuprocdef(pd).libsym:=sym;
  1891. if po_syscall_legacy in tprocdef(pd).procoptions then
  1892. begin
  1893. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1894. paramanager.parseparaloc(vs,'A6');
  1895. pd.parast.insert(vs);
  1896. end
  1897. end
  1898. else
  1899. Message(parser_e_32bitint_or_pointer_variable_expected);
  1900. end;
  1901. paramanager.create_funcretloc_info(pd,calleeside);
  1902. paramanager.create_funcretloc_info(pd,callerside);
  1903. v:=get_intconst;
  1904. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1905. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  1906. else
  1907. Tprocdef(pd).extnumber:=v.uvalue;
  1908. end;
  1909. {$endif m68k}
  1910. {$ifdef powerpc}
  1911. if target_info.system = system_powerpc_amiga then
  1912. begin
  1913. include(pd.procoptions,po_syscall_basesysv);
  1914. if consume_sym(sym,symtable) then
  1915. begin
  1916. if (sym.typ=staticvarsym) and
  1917. (
  1918. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1919. is_32bitint(tabstractvarsym(sym).vardef)
  1920. ) then
  1921. begin
  1922. include(pd.procoptions,po_syscall_has_libsym);
  1923. tcpuprocdef(pd).libsym:=sym;
  1924. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1925. pd.parast.insert(vs);
  1926. end
  1927. else
  1928. Message(parser_e_32bitint_or_pointer_variable_expected);
  1929. end;
  1930. paramanager.create_funcretloc_info(pd,calleeside);
  1931. paramanager.create_funcretloc_info(pd,callerside);
  1932. v:=get_intconst;
  1933. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1934. message(parser_e_range_check_error)
  1935. else
  1936. Tprocdef(pd).extnumber:=v.uvalue;
  1937. end else
  1938. if target_info.system = system_powerpc_morphos then
  1939. begin
  1940. if idtoken=_LEGACY then
  1941. begin
  1942. consume(_LEGACY);
  1943. include(pd.procoptions,po_syscall_legacy);
  1944. end
  1945. else if idtoken=_SYSV then
  1946. begin
  1947. consume(_SYSV);
  1948. include(pd.procoptions,po_syscall_sysv);
  1949. end
  1950. else if idtoken=_BASESYSV then
  1951. begin
  1952. consume(_BASESYSV);
  1953. include(pd.procoptions,po_syscall_basesysv);
  1954. end
  1955. else if idtoken=_SYSVBASE then
  1956. begin
  1957. consume(_SYSVBASE);
  1958. include(pd.procoptions,po_syscall_sysvbase);
  1959. end
  1960. else if idtoken=_R12BASE then
  1961. begin
  1962. consume(_R12BASE);
  1963. include(pd.procoptions,po_syscall_r12base);
  1964. end
  1965. else
  1966. if syscall_convention='LEGACY' then
  1967. include(pd.procoptions,po_syscall_legacy)
  1968. else if syscall_convention='SYSV' then
  1969. include(pd.procoptions,po_syscall_sysv)
  1970. else if syscall_convention='BASESYSV' then
  1971. include(pd.procoptions,po_syscall_basesysv)
  1972. else if syscall_convention='SYSVBASE' then
  1973. include(pd.procoptions,po_syscall_sysvbase)
  1974. else if syscall_convention='R12BASE' then
  1975. include(pd.procoptions,po_syscall_r12base)
  1976. else
  1977. internalerror(2005010404);
  1978. if consume_sym(sym,symtable) then
  1979. begin
  1980. if (sym.typ=staticvarsym) and
  1981. (
  1982. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1983. is_32bitint(tabstractvarsym(sym).vardef)
  1984. ) then
  1985. begin
  1986. include(pd.procoptions,po_syscall_has_libsym);
  1987. tcpuprocdef(pd).libsym:=sym;
  1988. if po_syscall_legacy in tprocdef(pd).procoptions then
  1989. begin
  1990. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1991. paramanager.parseparaloc(vs,'A6');
  1992. pd.parast.insert(vs);
  1993. end
  1994. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1995. begin
  1996. { Nothing to be done for sysv here for now, but this might change }
  1997. end
  1998. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1999. begin
  2000. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2001. pd.parast.insert(vs);
  2002. end
  2003. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  2004. begin
  2005. vs:=cparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2006. pd.parast.insert(vs);
  2007. end
  2008. else if po_syscall_r12base in tprocdef(pd).procoptions then
  2009. begin
  2010. vs:=cparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  2011. paramanager.parseparaloc(vs,'R12');
  2012. pd.parast.insert(vs);
  2013. end
  2014. else
  2015. internalerror(2005010501);
  2016. end
  2017. else
  2018. Message(parser_e_32bitint_or_pointer_variable_expected);
  2019. end;
  2020. paramanager.create_funcretloc_info(pd,calleeside);
  2021. paramanager.create_funcretloc_info(pd,callerside);
  2022. v:=get_intconst;
  2023. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2024. message(parser_e_range_check_error)
  2025. else
  2026. Tprocdef(pd).extnumber:=v.uvalue;
  2027. end;
  2028. {$endif powerpc}
  2029. {$if defined(i386) or defined(x86_64)}
  2030. if target_info.system in [system_i386_aros,system_x86_64_aros] then
  2031. begin
  2032. include(pd.procoptions,po_syscall_sysvbase);
  2033. if consume_sym(sym,symtable) then
  2034. begin
  2035. if (sym.typ=staticvarsym) and
  2036. (
  2037. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  2038. is_32bitint(tabstractvarsym(sym).vardef)
  2039. ) then
  2040. begin
  2041. include(pd.procoptions,po_syscall_has_libsym);
  2042. tcpuprocdef(pd).libsym:=sym;
  2043. vs:=cparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  2044. pd.parast.insert(vs);
  2045. end
  2046. else
  2047. Message(parser_e_32bitint_or_pointer_variable_expected);
  2048. end;
  2049. paramanager.create_funcretloc_info(pd,calleeside);
  2050. paramanager.create_funcretloc_info(pd,callerside);
  2051. v:=get_intconst;
  2052. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2053. message(parser_e_range_check_error)
  2054. else
  2055. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint);
  2056. end;
  2057. {$endif}
  2058. end;
  2059. procedure pd_external(pd:tabstractprocdef);
  2060. {
  2061. If import_dll=nil the procedure is assumed to be in another
  2062. object file. In that object file it should have the name to
  2063. which import_name is pointing to. Otherwise, the procedure is
  2064. assumed to be in the DLL to which import_dll is pointing to. In
  2065. that case either import_nr<>0 or import_name<>nil is true, so
  2066. the procedure is either imported by number or by name. (DM)
  2067. }
  2068. var
  2069. hs : string;
  2070. v:Tconstexprint;
  2071. is_java_external: boolean;
  2072. begin
  2073. if pd.typ<>procdef then
  2074. internalerror(2003042615);
  2075. { Allow specifying a separate external name for methods in external Java
  2076. because its identifier naming constraints are laxer than FPC's
  2077. (e.g., case sensitive).
  2078. Limitation: only allows specifying the symbol name and not the package name,
  2079. and only for external classes/interfaces }
  2080. is_java_external:=
  2081. (pd.typ=procdef) and
  2082. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2083. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2084. with tprocdef(pd) do
  2085. begin
  2086. forwarddef:=false;
  2087. { forbid local external procedures }
  2088. if parast.symtablelevel>normal_function_level then
  2089. Message(parser_e_no_local_proc_external);
  2090. { If the procedure should be imported from a DLL, a constant string follows.
  2091. This isn't really correct, an contant string expression follows
  2092. so we check if an semicolon follows, else a string constant have to
  2093. follow (FK) }
  2094. if not is_java_external and
  2095. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2096. begin
  2097. { Always add library prefix and suffix to create an uniform name }
  2098. hs:=get_stringconst;
  2099. if ExtractFileExt(hs)='' then
  2100. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2101. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2102. hs:=target_info.sharedlibprefix+hs;
  2103. { the JVM expects java/lang/Object rather than java.lang.Object }
  2104. if target_info.system in systems_jvm then
  2105. Replace(hs,'.','/');
  2106. import_dll:=stringdup(hs);
  2107. include(procoptions,po_has_importdll);
  2108. if (idtoken=_NAME) then
  2109. begin
  2110. consume(_NAME);
  2111. import_name:=stringdup(get_stringconst);
  2112. include(procoptions,po_has_importname);
  2113. if import_name^='' then
  2114. message(parser_e_empty_import_name);
  2115. end;
  2116. if (idtoken=_INDEX) then
  2117. begin
  2118. {After the word index follows the index number in the DLL.}
  2119. consume(_INDEX);
  2120. v:=get_intconst;
  2121. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2122. message(parser_e_range_check_error)
  2123. else
  2124. import_nr:=longint(v.svalue);
  2125. end;
  2126. { default is to used the realname of the procedure }
  2127. if (import_nr=0) and not assigned(import_name) then
  2128. begin
  2129. import_name:=stringdup(procsym.realname);
  2130. include(procoptions,po_has_importname);
  2131. end;
  2132. end
  2133. else
  2134. begin
  2135. if (idtoken=_NAME) or
  2136. is_java_external then
  2137. begin
  2138. consume(_NAME);
  2139. import_name:=stringdup(get_stringconst);
  2140. include(procoptions,po_has_importname);
  2141. if import_name^='' then
  2142. message(parser_e_empty_import_name);
  2143. end;
  2144. end;
  2145. end;
  2146. end;
  2147. procedure pd_weakexternal(pd:tabstractprocdef);
  2148. begin
  2149. if not(target_info.system in systems_weak_linking) then
  2150. message(parser_e_weak_external_not_supported)
  2151. else
  2152. pd_external(pd);
  2153. end;
  2154. procedure pd_winapi(pd:tabstractprocdef);
  2155. begin
  2156. if not(target_info.system in systems_wince) then
  2157. pd.proccalloption:=pocall_cdecl
  2158. else
  2159. pd.proccalloption:=pocall_stdcall;
  2160. end;
  2161. procedure pd_hardfloat(pd:tabstractprocdef);
  2162. begin
  2163. if
  2164. {$if defined(arm)}
  2165. (current_settings.fputype=fpu_soft) or
  2166. {$endif defined(arm)}
  2167. (cs_fp_emulation in current_settings.moduleswitches) then
  2168. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2169. end;
  2170. type
  2171. pd_handler=procedure(pd:tabstractprocdef);
  2172. proc_dir_rec=record
  2173. idtok : ttoken;
  2174. pd_flags : tpdflags;
  2175. handler : pd_handler;
  2176. pocall : tproccalloption;
  2177. pooption : tprocoptions;
  2178. mutexclpocall : tproccalloptions;
  2179. mutexclpotype : tproctypeoptions;
  2180. mutexclpo : tprocoptions;
  2181. end;
  2182. const
  2183. {Should contain the number of procedure directives we support.}
  2184. num_proc_directives=46;
  2185. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2186. (
  2187. (
  2188. idtok:_ABSTRACT;
  2189. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2190. handler : @pd_abstract;
  2191. pocall : pocall_none;
  2192. pooption : [po_abstractmethod];
  2193. mutexclpocall : [pocall_internproc];
  2194. mutexclpotype : [];
  2195. mutexclpo : [po_exports,po_interrupt,po_inline]
  2196. ),(
  2197. idtok:_ALIAS;
  2198. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2199. handler : @pd_alias;
  2200. pocall : pocall_none;
  2201. pooption : [];
  2202. mutexclpocall : [];
  2203. mutexclpotype : [];
  2204. mutexclpo : [po_external,po_inline]
  2205. ),(
  2206. idtok:_ASMNAME;
  2207. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2208. handler : @pd_asmname;
  2209. pocall : pocall_cdecl;
  2210. pooption : [po_external];
  2211. mutexclpocall : [pocall_internproc];
  2212. mutexclpotype : [];
  2213. mutexclpo : [po_external,po_inline]
  2214. ),(
  2215. idtok:_ASSEMBLER;
  2216. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2217. handler : nil;
  2218. pocall : pocall_none;
  2219. pooption : [po_assembler];
  2220. mutexclpocall : [];
  2221. mutexclpotype : [];
  2222. mutexclpo : [po_external]
  2223. ),(
  2224. idtok:_C; {same as cdecl for mode mac}
  2225. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2226. handler : nil;
  2227. pocall : pocall_cdecl;
  2228. pooption : [];
  2229. mutexclpocall : [];
  2230. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2231. mutexclpo : [po_assembler,po_external]
  2232. ),(
  2233. idtok:_CDECL;
  2234. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2235. handler : nil;
  2236. pocall : pocall_cdecl;
  2237. pooption : [];
  2238. mutexclpocall : [];
  2239. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2240. mutexclpo : [po_assembler,po_external]
  2241. ),(
  2242. idtok:_DISPID;
  2243. pd_flags : [pd_dispinterface];
  2244. handler : @pd_dispid;
  2245. pocall : pocall_none;
  2246. pooption : [po_dispid];
  2247. mutexclpocall : [pocall_internproc];
  2248. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2249. mutexclpo : [po_interrupt,po_external,po_inline]
  2250. ),(
  2251. idtok:_DYNAMIC;
  2252. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2253. handler : @pd_virtual;
  2254. pocall : pocall_none;
  2255. pooption : [po_virtualmethod];
  2256. mutexclpocall : [pocall_internproc];
  2257. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2258. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2259. ),(
  2260. idtok:_EXPORT;
  2261. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2262. handler : @pd_export;
  2263. pocall : pocall_none;
  2264. pooption : [po_exports,po_global];
  2265. mutexclpocall : [pocall_internproc];
  2266. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2267. mutexclpo : [po_external,po_interrupt,po_inline]
  2268. ),(
  2269. idtok:_EXTERNAL;
  2270. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2271. handler : @pd_external;
  2272. pocall : pocall_none;
  2273. pooption : [po_external];
  2274. mutexclpocall : [pocall_syscall];
  2275. { allowed for external cpp classes }
  2276. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2277. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2278. ),(
  2279. idtok:_FAR;
  2280. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2281. handler : @pd_far;
  2282. pocall : pocall_none;
  2283. pooption : [];
  2284. mutexclpocall : [pocall_internproc];
  2285. mutexclpotype : [];
  2286. mutexclpo : [po_inline]
  2287. ),(
  2288. idtok:_FAR16;
  2289. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2290. handler : nil;
  2291. pocall : pocall_far16;
  2292. pooption : [];
  2293. mutexclpocall : [];
  2294. mutexclpotype : [];
  2295. mutexclpo : [po_external]
  2296. ),(
  2297. idtok:_FINAL;
  2298. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2299. handler : @pd_final;
  2300. pocall : pocall_none;
  2301. pooption : [po_finalmethod];
  2302. mutexclpocall : [pocall_internproc];
  2303. mutexclpotype : [];
  2304. mutexclpo : [po_exports,po_interrupt,po_inline]
  2305. ),(
  2306. idtok:_FORWARD;
  2307. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2308. handler : @pd_forward;
  2309. pocall : pocall_none;
  2310. pooption : [];
  2311. mutexclpocall : [pocall_internproc];
  2312. mutexclpotype : [];
  2313. mutexclpo : [po_external,po_inline]
  2314. ),(
  2315. idtok:_OLDFPCCALL;
  2316. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2317. handler : nil;
  2318. pocall : pocall_oldfpccall;
  2319. pooption : [];
  2320. mutexclpocall : [];
  2321. mutexclpotype : [];
  2322. mutexclpo : []
  2323. ),(
  2324. idtok:_INLINE;
  2325. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2326. handler : nil;
  2327. pocall : pocall_none;
  2328. pooption : [po_inline];
  2329. mutexclpocall : [pocall_safecall];
  2330. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2331. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2332. ),(
  2333. idtok:_INTERNCONST;
  2334. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2335. handler : @pd_internconst;
  2336. pocall : pocall_none;
  2337. pooption : [po_internconst];
  2338. mutexclpocall : [];
  2339. mutexclpotype : [potype_operator];
  2340. mutexclpo : []
  2341. ),(
  2342. idtok:_INTERNPROC;
  2343. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2344. handler : @pd_internproc;
  2345. pocall : pocall_internproc;
  2346. pooption : [];
  2347. mutexclpocall : [];
  2348. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2349. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2350. ),(
  2351. idtok:_INTERRUPT;
  2352. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2353. handler : @pd_interrupt;
  2354. pocall : pocall_oldfpccall;
  2355. pooption : [po_interrupt];
  2356. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2357. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2358. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2359. mutexclpo : [po_external,po_inline,po_exports]
  2360. ),(
  2361. idtok:_IOCHECK;
  2362. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2363. handler : nil;
  2364. pocall : pocall_none;
  2365. pooption : [po_iocheck];
  2366. mutexclpocall : [pocall_internproc];
  2367. mutexclpotype : [];
  2368. mutexclpo : [po_external]
  2369. ),(
  2370. idtok:_LOCAL;
  2371. pd_flags : [pd_implemen,pd_body];
  2372. handler : nil;
  2373. pocall : pocall_none;
  2374. pooption : [po_kylixlocal];
  2375. mutexclpocall : [pocall_internproc,pocall_far16];
  2376. mutexclpotype : [];
  2377. mutexclpo : [po_external,po_exports]
  2378. ),(
  2379. idtok:_MESSAGE;
  2380. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2381. handler : @pd_message;
  2382. pocall : pocall_none;
  2383. pooption : []; { can be po_msgstr or po_msgint }
  2384. mutexclpocall : [pocall_internproc];
  2385. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2386. mutexclpo : [po_interrupt,po_inline]
  2387. ),(
  2388. idtok:_MWPASCAL;
  2389. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2390. handler : nil;
  2391. pocall : pocall_mwpascal;
  2392. pooption : [];
  2393. mutexclpocall : [];
  2394. mutexclpotype : [];
  2395. mutexclpo : []
  2396. ),(
  2397. idtok:_NEAR;
  2398. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2399. handler : @pd_near;
  2400. pocall : pocall_none;
  2401. pooption : [];
  2402. mutexclpocall : [pocall_internproc];
  2403. mutexclpotype : [];
  2404. mutexclpo : []
  2405. ),(
  2406. idtok:_NORETURN;
  2407. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2408. handler : nil;
  2409. pocall : pocall_none;
  2410. pooption : [po_noreturn];
  2411. mutexclpocall : [];
  2412. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2413. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2414. ),(
  2415. idtok:_NOSTACKFRAME;
  2416. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2417. handler : nil;
  2418. pocall : pocall_none;
  2419. pooption : [po_nostackframe];
  2420. mutexclpocall : [pocall_internproc];
  2421. mutexclpotype : [];
  2422. mutexclpo : []
  2423. ),(
  2424. idtok:_OVERLOAD;
  2425. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2426. handler : @pd_overload;
  2427. pocall : pocall_none;
  2428. pooption : [po_overload];
  2429. mutexclpocall : [pocall_internproc];
  2430. mutexclpotype : [];
  2431. mutexclpo : []
  2432. ),(
  2433. idtok:_OVERRIDE;
  2434. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2435. handler : @pd_override;
  2436. pocall : pocall_none;
  2437. pooption : [po_overridingmethod,po_virtualmethod];
  2438. mutexclpocall : [pocall_internproc];
  2439. mutexclpotype : [];
  2440. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2441. ),(
  2442. idtok:_PASCAL;
  2443. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2444. handler : nil;
  2445. pocall : pocall_pascal;
  2446. pooption : [];
  2447. mutexclpocall : [];
  2448. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2449. mutexclpo : [po_external]
  2450. ),(
  2451. idtok:_PUBLIC;
  2452. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2453. handler : @pd_public;
  2454. pocall : pocall_none;
  2455. pooption : [po_public,po_global];
  2456. mutexclpocall : [pocall_internproc];
  2457. mutexclpotype : [];
  2458. mutexclpo : [po_external,po_inline]
  2459. ),(
  2460. idtok:_REGISTER;
  2461. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2462. handler : nil;
  2463. pocall : pocall_register;
  2464. pooption : [];
  2465. mutexclpocall : [];
  2466. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2467. mutexclpo : [po_external]
  2468. ),(
  2469. idtok:_REINTRODUCE;
  2470. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2471. handler : @pd_reintroduce;
  2472. pocall : pocall_none;
  2473. pooption : [po_reintroduce];
  2474. mutexclpocall : [pocall_internproc];
  2475. mutexclpotype : [];
  2476. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2477. ),(
  2478. idtok:_SAFECALL;
  2479. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2480. handler : nil;
  2481. pocall : pocall_safecall;
  2482. pooption : [];
  2483. mutexclpocall : [];
  2484. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2485. mutexclpo : [po_external]
  2486. ),(
  2487. idtok:_SOFTFLOAT;
  2488. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2489. handler : nil;
  2490. pocall : pocall_softfloat;
  2491. pooption : [];
  2492. mutexclpocall : [];
  2493. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2494. { it's available with po_external because the libgcc floating point routines on the arm
  2495. uses this calling convention }
  2496. mutexclpo : []
  2497. ),(
  2498. idtok:_STATIC;
  2499. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2500. handler : @pd_static;
  2501. pocall : pocall_none;
  2502. pooption : [po_staticmethod];
  2503. mutexclpocall : [pocall_internproc];
  2504. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2505. mutexclpo : [po_interrupt,po_exports]
  2506. ),(
  2507. idtok:_STDCALL;
  2508. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2509. handler : nil;
  2510. pocall : pocall_stdcall;
  2511. pooption : [];
  2512. mutexclpocall : [];
  2513. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2514. mutexclpo : [po_external]
  2515. ),(
  2516. idtok:_SYSCALL;
  2517. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2518. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2519. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2520. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2521. handler : @pd_syscall;
  2522. pocall : pocall_syscall;
  2523. pooption : [];
  2524. mutexclpocall : [];
  2525. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2526. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2527. ),(
  2528. idtok:_VIRTUAL;
  2529. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2530. handler : @pd_virtual;
  2531. pocall : pocall_none;
  2532. pooption : [po_virtualmethod];
  2533. mutexclpocall : [pocall_internproc];
  2534. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2535. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2536. ),(
  2537. idtok:_CPPDECL;
  2538. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2539. handler : nil;
  2540. pocall : pocall_cppdecl;
  2541. pooption : [];
  2542. mutexclpocall : [];
  2543. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2544. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2545. ),(
  2546. idtok:_VARARGS;
  2547. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2548. handler : nil;
  2549. pocall : pocall_none;
  2550. pooption : [po_varargs];
  2551. mutexclpocall : [pocall_internproc,pocall_register,
  2552. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2553. mutexclpotype : [];
  2554. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2555. ),(
  2556. idtok:_COMPILERPROC;
  2557. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2558. handler : nil;
  2559. pocall : pocall_none;
  2560. pooption : [po_compilerproc];
  2561. mutexclpocall : [];
  2562. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2563. mutexclpo : [po_interrupt]
  2564. ),(
  2565. idtok:_WEAKEXTERNAL;
  2566. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2567. handler : @pd_weakexternal;
  2568. pocall : pocall_none;
  2569. { mark it both external and weak external, so we don't have to
  2570. adapt all code for external symbols to also check for weak external
  2571. }
  2572. pooption : [po_external,po_weakexternal];
  2573. mutexclpocall : [pocall_internproc,pocall_syscall];
  2574. { allowed for external cpp classes }
  2575. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2576. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2577. ),(
  2578. idtok:_WINAPI;
  2579. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2580. handler : @pd_winapi;
  2581. pocall : pocall_none;
  2582. pooption : [];
  2583. mutexclpocall : [pocall_stdcall,pocall_cdecl];
  2584. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2585. mutexclpo : [po_external]
  2586. ),(
  2587. idtok:_ENUMERATOR;
  2588. pd_flags : [pd_interface,pd_object,pd_record];
  2589. handler : @pd_enumerator;
  2590. pocall : pocall_none;
  2591. pooption : [];
  2592. mutexclpocall : [pocall_internproc];
  2593. mutexclpotype : [];
  2594. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2595. ),(
  2596. idtok:_RTLPROC;
  2597. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2598. handler : nil;
  2599. pocall : pocall_none;
  2600. pooption : [po_rtlproc];
  2601. mutexclpocall : [];
  2602. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2603. mutexclpo : [po_interrupt]
  2604. ),(
  2605. idtok:_HARDFLOAT;
  2606. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2607. handler : @pd_hardfloat;
  2608. pocall : pocall_hardfloat;
  2609. pooption : [];
  2610. mutexclpocall : [];
  2611. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2612. { it's available with po_external because the libgcc floating point routines on the arm
  2613. uses this calling convention }
  2614. mutexclpo : []
  2615. )
  2616. );
  2617. function check_proc_directive(isprocvar:boolean):boolean;
  2618. var
  2619. i : longint;
  2620. begin
  2621. result:=false;
  2622. for i:=1 to num_proc_directives do
  2623. if proc_direcdata[i].idtok=idtoken then
  2624. begin
  2625. if ((not isprocvar) or
  2626. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2627. { don't eat a public directive in classes }
  2628. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2629. result:=true;
  2630. exit;
  2631. end;
  2632. end;
  2633. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2634. {
  2635. Parse the procedure directive, returns true if a correct directive is found
  2636. }
  2637. var
  2638. p : longint;
  2639. found : boolean;
  2640. name : TIDString;
  2641. begin
  2642. parse_proc_direc:=false;
  2643. name:=tokeninfo^[idtoken].str;
  2644. found:=false;
  2645. { Hint directive? Then exit immediatly }
  2646. if (m_hintdirective in current_settings.modeswitches) then
  2647. begin
  2648. case idtoken of
  2649. _LIBRARY,
  2650. _PLATFORM,
  2651. _UNIMPLEMENTED,
  2652. _EXPERIMENTAL,
  2653. _DEPRECATED :
  2654. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2655. begin
  2656. maybe_parse_hint_directives(tprocdef(pd));
  2657. { could the new token still be a directive? }
  2658. if token<>_ID then
  2659. exit;
  2660. name:=tokeninfo^[idtoken].str;
  2661. end
  2662. else
  2663. exit;
  2664. end;
  2665. end;
  2666. { C directive is MacPas only, because it breaks too much existing code
  2667. on other platforms (PFV) }
  2668. if (idtoken=_C) and
  2669. not(m_mac in current_settings.modeswitches) then
  2670. exit;
  2671. { retrieve data for directive if found }
  2672. for p:=1 to num_proc_directives do
  2673. if proc_direcdata[p].idtok=idtoken then
  2674. begin
  2675. found:=true;
  2676. break;
  2677. end;
  2678. { Check if the procedure directive is known }
  2679. if not found then
  2680. begin
  2681. { parsing a procvar type the name can be any
  2682. next variable !! }
  2683. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2684. not(idtoken=_PROPERTY) then
  2685. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2686. exit;
  2687. end;
  2688. { check if method and directive not for object, like public.
  2689. This needs to be checked also for procvars }
  2690. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2691. (symtablestack.top.symtabletype=ObjectSymtable) and
  2692. { directive allowed for cpp classes? }
  2693. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2694. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2695. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2696. exit;
  2697. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2698. (symtablestack.top.symtabletype=recordsymtable) then
  2699. exit;
  2700. { check if method and directive not for java class }
  2701. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2702. is_javaclass(tdef(symtablestack.top.defowner)) then
  2703. exit;
  2704. { check if method and directive not for java interface }
  2705. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2706. is_javainterface(tdef(symtablestack.top.defowner)) then
  2707. exit;
  2708. { Conflicts between directives ? }
  2709. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2710. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2711. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2712. begin
  2713. Message1(parser_e_proc_dir_conflict,name);
  2714. exit;
  2715. end;
  2716. { set calling convention }
  2717. if proc_direcdata[p].pocall<>pocall_none then
  2718. begin
  2719. if (po_hascallingconvention in pd.procoptions) then
  2720. begin
  2721. Message2(parser_w_proc_overriding_calling,
  2722. proccalloptionStr[pd.proccalloption],
  2723. proccalloptionStr[proc_direcdata[p].pocall]);
  2724. end;
  2725. { check if the target processor supports this calling convention }
  2726. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2727. begin
  2728. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2729. { recover }
  2730. proc_direcdata[p].pocall:=pocall_stdcall;
  2731. end;
  2732. pd.proccalloption:=proc_direcdata[p].pocall;
  2733. include(pd.procoptions,po_hascallingconvention);
  2734. end;
  2735. if pd.typ=procdef then
  2736. begin
  2737. { Check if the directive is only for objects }
  2738. if (pd_object in proc_direcdata[p].pd_flags) and
  2739. not assigned(tprocdef(pd).struct) then
  2740. exit;
  2741. { Check if the directive is only for records }
  2742. if (pd_record in proc_direcdata[p].pd_flags) and
  2743. not assigned(tprocdef(pd).struct) then
  2744. exit;
  2745. { check if method and directive not for interface }
  2746. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2747. is_interface(tprocdef(pd).struct) then
  2748. exit;
  2749. { check if method and directive not for interface }
  2750. if is_dispinterface(tprocdef(pd).struct) and
  2751. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2752. exit;
  2753. { check if method and directive not for objcclass }
  2754. if is_objcclass(tprocdef(pd).struct) and
  2755. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2756. exit;
  2757. { check if method and directive not for objcprotocol }
  2758. if is_objcprotocol(tprocdef(pd).struct) and
  2759. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2760. exit;
  2761. { check if method and directive not for record/class helper }
  2762. if is_objectpascal_helper(tprocdef(pd).struct) and
  2763. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2764. end;
  2765. { consume directive, and turn flag on }
  2766. consume(token);
  2767. parse_proc_direc:=true;
  2768. { Check the pd_flags if the directive should be allowed }
  2769. if (pd_interface in pdflags) and
  2770. not(pd_interface in proc_direcdata[p].pd_flags) then
  2771. begin
  2772. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2773. exit;
  2774. end;
  2775. if (pd_implemen in pdflags) and
  2776. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2777. begin
  2778. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2779. exit;
  2780. end;
  2781. if (pd_procvar in pdflags) and
  2782. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2783. begin
  2784. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2785. exit;
  2786. end;
  2787. { Return the new pd_flags }
  2788. if not(pd_body in proc_direcdata[p].pd_flags) then
  2789. exclude(pdflags,pd_body);
  2790. { Add the correct flag }
  2791. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2792. { Call the handler }
  2793. if pointer(proc_direcdata[p].handler)<>nil then
  2794. proc_direcdata[p].handler(pd);
  2795. end;
  2796. function proc_get_importname(pd:tprocdef):string;
  2797. var
  2798. dllname, importname : string;
  2799. begin
  2800. result:='';
  2801. if not(po_external in pd.procoptions) then
  2802. internalerror(200412151);
  2803. { external name or number is specified }
  2804. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2805. begin
  2806. if assigned(pd.import_dll) then
  2807. dllname:=pd.import_dll^
  2808. else
  2809. dllname:='';
  2810. if assigned(pd.import_name) then
  2811. importname:=pd.import_name^
  2812. else
  2813. importname:='';
  2814. proc_get_importname:=make_dllmangledname(dllname,
  2815. importname,pd.import_nr,pd.proccalloption);
  2816. end
  2817. else
  2818. begin
  2819. { Default names when importing variables }
  2820. case pd.proccalloption of
  2821. pocall_cdecl :
  2822. begin
  2823. if assigned(pd.struct) then
  2824. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2825. else
  2826. result:=target_info.Cprefix+pd.procsym.realname;
  2827. end;
  2828. pocall_cppdecl :
  2829. begin
  2830. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2831. end;
  2832. else
  2833. begin
  2834. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2835. { but according to MacPas mode description
  2836. Cprefix should still be used PM }
  2837. if (m_mac in current_settings.modeswitches) then
  2838. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  2839. else
  2840. result:=pd.procsym.realname;
  2841. end;
  2842. end;
  2843. end;
  2844. end;
  2845. procedure compilerproc_set_symbol_name(pd: tprocdef);
  2846. begin
  2847. pd.procsym.realname:='$'+lower(pd.procsym.name);
  2848. end;
  2849. procedure proc_set_mangledname(pd:tprocdef);
  2850. var
  2851. s : string;
  2852. begin
  2853. { When the mangledname is already set we aren't allowed to change
  2854. it because it can already be used somewhere (PFV) }
  2855. if not(po_has_mangledname in pd.procoptions) then
  2856. begin
  2857. if (po_external in pd.procoptions) then
  2858. begin
  2859. { External Procedures are only allowed to change the mangledname
  2860. in their first declaration }
  2861. if (pd.forwarddef or (not pd.hasforward)) then
  2862. begin
  2863. s:=proc_get_importname(pd);
  2864. if s<>'' then
  2865. begin
  2866. pd.setmangledname(s);
  2867. end;
  2868. { since this is an external declaration, there won't be an
  2869. implementation that needs to match the original symbol
  2870. again -> immediately convert here }
  2871. if po_compilerproc in pd.procoptions then
  2872. compilerproc_set_symbol_name(pd);
  2873. end
  2874. end
  2875. else
  2876. { Normal procedures }
  2877. begin
  2878. if (po_compilerproc in pd.procoptions) then
  2879. begin
  2880. pd.setmangledname(lower(pd.procsym.name));
  2881. end;
  2882. end;
  2883. end;
  2884. { Public/exported alias names }
  2885. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2886. not(po_has_public_name in pd.procoptions) then
  2887. begin
  2888. case pd.proccalloption of
  2889. pocall_cdecl :
  2890. begin
  2891. if assigned(pd.struct) then
  2892. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2893. else
  2894. begin
  2895. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2896. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2897. (po_exports in pd.procoptions) then
  2898. pd.aliasnames.insert(pd.procsym.realname)
  2899. else
  2900. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2901. end;
  2902. end;
  2903. pocall_cppdecl :
  2904. begin
  2905. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2906. end;
  2907. end;
  2908. { prevent adding the alias a second time }
  2909. include(pd.procoptions,po_has_public_name);
  2910. end;
  2911. end;
  2912. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2913. begin
  2914. if hcc_check in flags then
  2915. begin
  2916. { set the default calling convention if none provided }
  2917. if (pd.typ=procdef) and
  2918. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2919. is_cppclass(tprocdef(pd).struct)) then
  2920. begin
  2921. { none of the explicit calling conventions should be allowed }
  2922. if (po_hascallingconvention in pd.procoptions) then
  2923. internalerror(2009032501);
  2924. if is_cppclass(tprocdef(pd).struct) then
  2925. pd.proccalloption:=pocall_cppdecl
  2926. else
  2927. pd.proccalloption:=pocall_cdecl;
  2928. end
  2929. else if not(po_hascallingconvention in pd.procoptions) then
  2930. pd.proccalloption:=current_settings.defproccall
  2931. else
  2932. begin
  2933. if pd.proccalloption=pocall_none then
  2934. internalerror(200309081);
  2935. end;
  2936. { handle proccall specific settings }
  2937. case pd.proccalloption of
  2938. pocall_cdecl,
  2939. pocall_cppdecl :
  2940. begin
  2941. { check C cdecl para types }
  2942. check_c_para(pd);
  2943. end;
  2944. pocall_far16 :
  2945. begin
  2946. { Temporary stub, must be rewritten to support OS/2 far16 }
  2947. Message1(parser_w_proc_directive_ignored,'FAR16');
  2948. end;
  2949. end;
  2950. { Inlining is enabled and supported? }
  2951. if (po_inline in pd.procoptions) and
  2952. not(cs_do_inline in current_settings.localswitches) then
  2953. begin
  2954. { Give an error if inline is not supported by the compiler mode,
  2955. otherwise only give a hint that this procedure will not be inlined }
  2956. if not(m_default_inline in current_settings.modeswitches) then
  2957. Message(parser_e_proc_inline_not_supported)
  2958. else
  2959. Message(parser_h_inlining_disabled);
  2960. exclude(pd.procoptions,po_inline);
  2961. end;
  2962. { For varargs directive also cdecl and external must be defined }
  2963. if (po_varargs in pd.procoptions) then
  2964. begin
  2965. { check first for external in the interface, if available there
  2966. then the cdecl must also be there since there is no implementation
  2967. available to contain it }
  2968. if parse_only then
  2969. begin
  2970. { if external is available, then cdecl must also be available,
  2971. procvars don't need external }
  2972. if not((po_external in pd.procoptions) or
  2973. (pd.typ=procvardef) or
  2974. { for objcclasses this is checked later, because the entire
  2975. class may be external. }
  2976. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2977. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2978. Message(parser_e_varargs_need_cdecl_and_external);
  2979. end
  2980. else
  2981. begin
  2982. { both must be defined now }
  2983. if not((po_external in pd.procoptions) or
  2984. (pd.typ=procvardef)) or
  2985. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2986. Message(parser_e_varargs_need_cdecl_and_external);
  2987. end;
  2988. end;
  2989. end;
  2990. if hcc_insert_hidden_paras in flags then
  2991. begin
  2992. { insert hidden high parameters }
  2993. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2994. { insert hidden self parameter }
  2995. insert_self_and_vmt_para(pd);
  2996. { insert funcret parameter if required }
  2997. insert_funcret_para(pd);
  2998. { Make var parameters regable, this must be done after the calling
  2999. convention is set. }
  3000. { this must be done before parentfp is insert, because getting all cases
  3001. where parentfp must be in a memory location isn't catched properly so
  3002. we put parentfp never in a register }
  3003. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  3004. { insert parentfp parameter if required }
  3005. insert_parentfp_para(pd);
  3006. end;
  3007. { Calculate parameter tlist }
  3008. pd.calcparas;
  3009. end;
  3010. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  3011. {
  3012. Parse the procedure directives. It does not matter if procedure directives
  3013. are written using ;procdir; or ['procdir'] syntax.
  3014. }
  3015. var
  3016. res : boolean;
  3017. begin
  3018. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  3019. begin
  3020. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3021. include(pd.procoptions,po_public);
  3022. include(pd.procoptions,po_has_public_name);
  3023. include(pd.procoptions,po_global);
  3024. end;
  3025. { methods from external class definitions are all external themselves }
  3026. if (pd.typ=procdef) and
  3027. assigned(tprocdef(pd).struct) and
  3028. (tprocdef(pd).struct.typ=objectdef) and
  3029. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3030. tprocdef(pd).make_external;
  3031. { Class constructors and destructor are static class methods in real. }
  3032. { There are many places in the compiler where either class or static }
  3033. { method flag changes the behavior. It is simplier to add them to }
  3034. { the class constructors/destructors options than to fix all the }
  3035. { occurencies. (Paul) }
  3036. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3037. begin
  3038. include(pd.procoptions,po_classmethod);
  3039. include(pd.procoptions,po_staticmethod);
  3040. end;
  3041. while token in [_ID,_LECKKLAMMER] do
  3042. begin
  3043. if try_to_consume(_LECKKLAMMER) then
  3044. begin
  3045. repeat
  3046. parse_proc_direc(pd,pdflags);
  3047. until not try_to_consume(_COMMA);
  3048. consume(_RECKKLAMMER);
  3049. { we always expect at least '[];' }
  3050. res:=true;
  3051. end
  3052. else
  3053. begin
  3054. res:=parse_proc_direc(pd,pdflags);
  3055. end;
  3056. { A procedure directive normally followed by a semicolon, but in
  3057. a const section or reading a type we should stop when _EQ is found,
  3058. because a constant/default value follows }
  3059. if res then
  3060. begin
  3061. if (block_type=bt_const_type) and
  3062. (token=_EQ) then
  3063. break;
  3064. { support procedure proc;stdcall export; }
  3065. if not(check_proc_directive((pd.typ=procvardef))) then
  3066. begin
  3067. { support "record p : procedure stdcall end;" and
  3068. "var p : procedure stdcall = nil;" }
  3069. if (pd_procvar in pdflags) and
  3070. (token in [_END,_RKLAMMER,_EQ]) then
  3071. break
  3072. else
  3073. begin
  3074. if (token=_COLON) then
  3075. begin
  3076. Message(parser_e_field_not_allowed_here);
  3077. consume_all_until(_SEMICOLON);
  3078. end;
  3079. consume(_SEMICOLON)
  3080. end;
  3081. end;
  3082. end
  3083. else
  3084. break;
  3085. end;
  3086. { nostackframe requires assembler, but assembler
  3087. may be specified in the implementation part only,
  3088. and in not required if the function is first forward declared
  3089. if it is a procdef that has forwardef set to true
  3090. we postpone the possible error message to the real implementation
  3091. parse_only does not need to be considered as po_nostackframe
  3092. is an implementation only directive }
  3093. if (po_nostackframe in pd.procoptions) and
  3094. not (po_assembler in pd.procoptions) and
  3095. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3096. message(parser_e_nostackframe_without_assembler);
  3097. end;
  3098. procedure parse_var_proc_directives(sym:tsym);
  3099. var
  3100. pdflags : tpdflags;
  3101. pd : tabstractprocdef;
  3102. begin
  3103. pdflags:=[pd_procvar];
  3104. pd:=nil;
  3105. case sym.typ of
  3106. fieldvarsym,
  3107. staticvarsym,
  3108. localvarsym,
  3109. paravarsym :
  3110. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  3111. typesym :
  3112. pd:=tabstractprocdef(ttypesym(sym).typedef);
  3113. else
  3114. internalerror(2003042617);
  3115. end;
  3116. if pd.typ<>procvardef then
  3117. internalerror(2003042618);
  3118. { names should never be used anyway }
  3119. parse_proc_directives(pd,pdflags);
  3120. end;
  3121. procedure parse_object_proc_directives(pd:tabstractprocdef);
  3122. var
  3123. pdflags : tpdflags;
  3124. begin
  3125. pdflags:=[pd_object];
  3126. parse_proc_directives(pd,pdflags);
  3127. end;
  3128. procedure parse_record_proc_directives(pd:tabstractprocdef);
  3129. var
  3130. pdflags : tpdflags;
  3131. begin
  3132. pdflags:=[pd_record];
  3133. parse_proc_directives(pd,pdflags);
  3134. end;
  3135. function proc_add_definition(var currpd:tprocdef):boolean;
  3136. function equal_generic_procdefs(fwpd,currpd:tprocdef):boolean;
  3137. var
  3138. i : longint;
  3139. fwtype,
  3140. currtype : ttypesym;
  3141. foundretdef : boolean;
  3142. begin
  3143. result:=false;
  3144. if fwpd.genericparas.count<>currpd.genericparas.count then
  3145. exit;
  3146. { comparing generic declarations is a bit more cumbersome as the
  3147. defs of the generic parameter types are not equal, especially if the
  3148. declaration contains constraints; essentially we have two cases:
  3149. - proc declared in interface of unit (or in class/record/object)
  3150. and defined in implementation; here the fwpd might contain
  3151. constraints while currpd must only contain undefineddefs
  3152. - forward declaration in implementation; this case is not supported
  3153. right now }
  3154. foundretdef:=false;
  3155. for i:=0 to fwpd.genericparas.count-1 do
  3156. begin
  3157. fwtype:=ttypesym(fwpd.genericparas[i]);
  3158. currtype:=ttypesym(currpd.genericparas[i]);
  3159. { if the type in the currpd isn't a pure undefineddef, then we can
  3160. stop right there }
  3161. if (currtype.typedef.typ<>undefineddef) or (df_genconstraint in currtype.typedef.defoptions) then
  3162. exit;
  3163. if not foundretdef then
  3164. begin
  3165. { if the returndef is the same as this parameter's def then this
  3166. needs to be the case for both procdefs }
  3167. foundretdef:=fwpd.returndef=fwtype.typedef;
  3168. if foundretdef xor (currpd.returndef=currtype.typedef) then
  3169. exit;
  3170. end;
  3171. end;
  3172. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  3173. exit;
  3174. if not foundretdef then
  3175. { the returndef isn't a type parameter, so compare as usual }
  3176. result:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact
  3177. else
  3178. result:=true;
  3179. end;
  3180. {
  3181. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  3182. forwarddef is found and reused it returns true
  3183. }
  3184. var
  3185. fwpd : tprocdef;
  3186. currparasym,
  3187. fwparasym : tsym;
  3188. currparacnt,
  3189. fwparacnt,
  3190. curridx,
  3191. fwidx,
  3192. i : longint;
  3193. po_comp : tprocoptions;
  3194. paracompopt: tcompare_paras_options;
  3195. forwardfound : boolean;
  3196. symentry: TSymEntry;
  3197. begin
  3198. forwardfound:=false;
  3199. { check overloaded functions if the same function already exists }
  3200. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  3201. begin
  3202. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  3203. { can happen for internally generated routines }
  3204. if (fwpd=currpd) then
  3205. begin
  3206. result:=true;
  3207. exit;
  3208. end;
  3209. { Skip overloaded definitions that are declared in other units }
  3210. if fwpd.procsym<>currpd.procsym then
  3211. continue;
  3212. { check the parameters, for delphi/tp it is possible to
  3213. leave the parameters away in the implementation (forwarddef=false).
  3214. But for an overload declared function this is not allowed }
  3215. if { check if empty implementation arguments match is allowed }
  3216. (
  3217. not(m_repeat_forward in current_settings.modeswitches) and
  3218. not(currpd.forwarddef) and
  3219. is_bareprocdef(currpd) and
  3220. not(po_overload in fwpd.procoptions)
  3221. ) or
  3222. (
  3223. fwpd.is_generic and
  3224. currpd.is_generic and
  3225. equal_generic_procdefs(fwpd,currpd)
  3226. ) or
  3227. { check arguments, we need to check only the user visible parameters. The hidden parameters
  3228. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  3229. don't check default values here, because routines that are the same except for their default
  3230. values should be reported as mismatches (since you can't overload based on different default
  3231. parameter values) }
  3232. (
  3233. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  3234. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  3235. ) then
  3236. begin
  3237. { Check if we've found the forwarddef, if found then
  3238. we need to update the forward def with the current
  3239. implementation settings }
  3240. if fwpd.forwarddef then
  3241. begin
  3242. forwardfound:=true;
  3243. if not(m_repeat_forward in current_settings.modeswitches) and
  3244. (fwpd.proccalloption<>currpd.proccalloption) then
  3245. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  3246. else
  3247. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  3248. { Check calling convention }
  3249. if (fwpd.proccalloption<>currpd.proccalloption) then
  3250. begin
  3251. { In delphi it is possible to specify the calling
  3252. convention in the interface or implementation if
  3253. there was no convention specified in the other
  3254. part }
  3255. if (m_delphi in current_settings.modeswitches) then
  3256. begin
  3257. if not(po_hascallingconvention in currpd.procoptions) then
  3258. currpd.proccalloption:=fwpd.proccalloption
  3259. else
  3260. if not(po_hascallingconvention in fwpd.procoptions) then
  3261. fwpd.proccalloption:=currpd.proccalloption
  3262. else
  3263. begin
  3264. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3265. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3266. { restore interface settings }
  3267. currpd.proccalloption:=fwpd.proccalloption;
  3268. end;
  3269. end
  3270. else
  3271. begin
  3272. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3273. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3274. { restore interface settings }
  3275. currpd.proccalloption:=fwpd.proccalloption;
  3276. end;
  3277. end;
  3278. { Check static }
  3279. if (po_staticmethod in fwpd.procoptions) then
  3280. begin
  3281. if not (po_staticmethod in currpd.procoptions) then
  3282. begin
  3283. include(currpd.procoptions, po_staticmethod);
  3284. if (po_classmethod in currpd.procoptions) then
  3285. begin
  3286. { remove self from the hidden paras }
  3287. symentry:=currpd.parast.Find('self');
  3288. if symentry<>nil then
  3289. begin
  3290. currpd.parast.Delete(symentry);
  3291. currpd.calcparas;
  3292. end;
  3293. end;
  3294. end;
  3295. end;
  3296. { Check if the procedure type and return type are correct,
  3297. also the parameters must match also with the type and that
  3298. if the implementation has default parameters, the interface
  3299. also has them and that if they both have them, that they
  3300. have the same value }
  3301. if ((m_repeat_forward in current_settings.modeswitches) or
  3302. not is_bareprocdef(currpd)) and
  3303. (
  3304. (
  3305. fwpd.is_generic and
  3306. currpd.is_generic and
  3307. not equal_generic_procdefs(fwpd,currpd)
  3308. ) or
  3309. (
  3310. (
  3311. not fwpd.is_generic or
  3312. not currpd.is_generic
  3313. ) and
  3314. (
  3315. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  3316. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  3317. )
  3318. )
  3319. ) then
  3320. begin
  3321. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3322. fwpd.fullprocname(false));
  3323. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3324. break;
  3325. end;
  3326. { Check if both are declared forward }
  3327. if fwpd.forwarddef and currpd.forwarddef then
  3328. begin
  3329. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  3330. currpd.fullprocname(false));
  3331. end;
  3332. { internconst or internproc only need to be defined once }
  3333. if (fwpd.proccalloption=pocall_internproc) then
  3334. currpd.proccalloption:=fwpd.proccalloption
  3335. else
  3336. if (currpd.proccalloption=pocall_internproc) then
  3337. fwpd.proccalloption:=currpd.proccalloption;
  3338. { Check procedure options, Delphi requires that class is
  3339. repeated in the implementation for class methods }
  3340. if (m_fpc in current_settings.modeswitches) then
  3341. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  3342. else
  3343. po_comp:=[po_classmethod,po_methodpointer];
  3344. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  3345. (fwpd.proctypeoption <> currpd.proctypeoption) or
  3346. { if the implementation version has an "overload" modifier,
  3347. the interface version must also have it (otherwise we can
  3348. get annoying crashes due to interface crc changes) }
  3349. (not(po_overload in fwpd.procoptions) and
  3350. (po_overload in currpd.procoptions)) then
  3351. begin
  3352. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3353. fwpd.fullprocname(false));
  3354. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3355. { This error is non-fatal, we can recover }
  3356. end;
  3357. { Forward declaration is external? }
  3358. if (po_external in fwpd.procoptions) then
  3359. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3360. { Check parameters }
  3361. if (m_repeat_forward in current_settings.modeswitches) or
  3362. (currpd.minparacount>0) then
  3363. begin
  3364. { If mangled names are equal then they have the same amount of arguments }
  3365. { We can check the names of the arguments }
  3366. { both symtables are in the same order from left to right }
  3367. curridx:=0;
  3368. fwidx:=0;
  3369. currparacnt:=currpd.parast.SymList.Count;
  3370. fwparacnt:=fwpd.parast.SymList.Count;
  3371. repeat
  3372. { skip default parameter constsyms }
  3373. while (curridx<currparacnt) and
  3374. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3375. inc(curridx);
  3376. while (fwidx<fwparacnt) and
  3377. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3378. inc(fwidx);
  3379. { stop when one of the two lists is at the end }
  3380. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3381. break;
  3382. { compare names of parameters, ignore implictly
  3383. renamed parameters }
  3384. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3385. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3386. if not(sp_implicitrename in currparasym.symoptions) and
  3387. not(sp_implicitrename in fwparasym.symoptions) then
  3388. begin
  3389. if (currparasym.name<>fwparasym.name) then
  3390. begin
  3391. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3392. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3393. break;
  3394. end;
  3395. end;
  3396. { next parameter }
  3397. inc(curridx);
  3398. inc(fwidx);
  3399. until false;
  3400. end;
  3401. { Everything is checked, now we can update the forward declaration
  3402. with the new data from the implementation }
  3403. fwpd.forwarddef:=currpd.forwarddef;
  3404. fwpd.hasforward:=true;
  3405. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3406. { marked as local but exported from unit? }
  3407. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3408. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3409. if fwpd.extnumber=$ffff then
  3410. fwpd.extnumber:=currpd.extnumber;
  3411. while not currpd.aliasnames.empty do
  3412. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3413. { update fileinfo so position references the implementation,
  3414. also update funcretsym if it is already generated }
  3415. fwpd.fileinfo:=currpd.fileinfo;
  3416. if assigned(fwpd.funcretsym) then
  3417. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3418. if assigned(currpd.deprecatedmsg) then
  3419. begin
  3420. stringdispose(fwpd.deprecatedmsg);
  3421. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3422. end;
  3423. { import names }
  3424. if assigned(currpd.import_dll) then
  3425. begin
  3426. stringdispose(fwpd.import_dll);
  3427. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3428. end;
  3429. if assigned(currpd.import_name) then
  3430. begin
  3431. stringdispose(fwpd.import_name);
  3432. fwpd.import_name:=stringdup(currpd.import_name^);
  3433. end;
  3434. fwpd.import_nr:=currpd.import_nr;
  3435. { for compilerproc defines we need to rename and update the
  3436. symbolname to lowercase so users can' access it (can't do
  3437. it immediately, because then the implementation symbol
  3438. won't be matched) }
  3439. if po_compilerproc in fwpd.procoptions then
  3440. compilerproc_set_symbol_name(fwpd);
  3441. { Release current procdef }
  3442. currpd.owner.deletedef(currpd);
  3443. currpd:=fwpd;
  3444. end
  3445. else
  3446. begin
  3447. { abstract methods aren't forward defined, but this }
  3448. { needs another error message }
  3449. if (po_abstractmethod in fwpd.procoptions) then
  3450. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3451. else
  3452. begin
  3453. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3454. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3455. end;
  3456. end;
  3457. { we found one proc with the same arguments, there are no others
  3458. so we can stop }
  3459. break;
  3460. end;
  3461. { check for allowing overload directive }
  3462. if not(m_fpc in current_settings.modeswitches) then
  3463. begin
  3464. { overload directive turns on overloading }
  3465. if ((po_overload in currpd.procoptions) or
  3466. (po_overload in fwpd.procoptions)) then
  3467. begin
  3468. { check if all procs have overloading, but not if the proc is a method or
  3469. already declared forward, then the check is already done }
  3470. if not(fwpd.hasforward or
  3471. assigned(currpd.struct) or
  3472. (currpd.forwarddef<>fwpd.forwarddef) or
  3473. ((po_overload in currpd.procoptions) and
  3474. (po_overload in fwpd.procoptions))) then
  3475. begin
  3476. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3477. break;
  3478. end
  3479. end
  3480. else
  3481. begin
  3482. if not(fwpd.forwarddef) then
  3483. begin
  3484. if (m_tp7 in current_settings.modeswitches) then
  3485. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3486. else
  3487. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3488. break;
  3489. end;
  3490. end;
  3491. end; { equal arguments }
  3492. end;
  3493. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3494. list }
  3495. if not forwardfound then
  3496. begin
  3497. { can happen in Delphi mode }
  3498. if (currpd.proctypeoption = potype_function) and
  3499. is_void(currpd.returndef) then
  3500. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3501. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3502. end;
  3503. proc_add_definition:=forwardfound;
  3504. end;
  3505. end.