pdecsub.pas 127 KB

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