pdecsub.pas 129 KB

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