pdecsub.pas 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  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. { check that class operators have either return type of structure or }
  1351. { at least one argument of that type }
  1352. if (po_classmethod in pd.procoptions) and
  1353. (pd.returndef <> pd.struct) then
  1354. begin
  1355. found:=false;
  1356. for i := 0 to pd.parast.SymList.Count - 1 do
  1357. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1358. begin
  1359. found:=true;
  1360. break;
  1361. end;
  1362. if not found then
  1363. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName);
  1364. end;
  1365. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1366. ((pd.returndef.typ<>orddef) or
  1367. (torddef(pd.returndef).ordtype<>pasbool)) then
  1368. Message(parser_e_comparative_operator_return_boolean);
  1369. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1370. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1371. message(parser_e_no_such_assignment)
  1372. else if not isoperatoracceptable(pd,optoken) then
  1373. Message(parser_e_overload_impossible);
  1374. end;
  1375. end
  1376. else
  1377. begin
  1378. { recover }
  1379. try_to_consume(_ID);
  1380. consume(_COLON);
  1381. consume_all_until(_SEMICOLON);
  1382. end;
  1383. end;
  1384. end;
  1385. { file types can't be function results }
  1386. if assigned(pd) and
  1387. (pd.returndef.typ=filedef) then
  1388. message(parser_e_illegal_function_result);
  1389. { support procedure proc stdcall export; }
  1390. if not(check_proc_directive(false)) then
  1391. begin
  1392. if (token=_COLON) then
  1393. begin
  1394. message(parser_e_field_not_allowed_here);
  1395. consume_all_until(_SEMICOLON);
  1396. end;
  1397. consume(_SEMICOLON);
  1398. end;
  1399. result:=pd;
  1400. if locationstr<>'' then
  1401. begin
  1402. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1403. { I guess this needs a new message... (KB) }
  1404. message(parser_e_illegal_explicit_paraloc);
  1405. end;
  1406. end;
  1407. {****************************************************************************
  1408. Procedure directive handlers
  1409. ****************************************************************************}
  1410. procedure pd_far(pd:tabstractprocdef);
  1411. begin
  1412. Message1(parser_w_proc_directive_ignored,'FAR');
  1413. end;
  1414. procedure pd_near(pd:tabstractprocdef);
  1415. begin
  1416. Message1(parser_w_proc_directive_ignored,'NEAR');
  1417. end;
  1418. procedure pd_export(pd:tabstractprocdef);
  1419. begin
  1420. if pd.typ<>procdef then
  1421. internalerror(200304264);
  1422. if assigned(tprocdef(pd).struct) then
  1423. Message(parser_e_methods_dont_be_export);
  1424. if pd.parast.symtablelevel>normal_function_level then
  1425. Message(parser_e_dont_nest_export);
  1426. end;
  1427. procedure pd_forward(pd:tabstractprocdef);
  1428. begin
  1429. if pd.typ<>procdef then
  1430. internalerror(200304265);
  1431. tprocdef(pd).forwarddef:=true;
  1432. end;
  1433. procedure pd_alias(pd:tabstractprocdef);
  1434. begin
  1435. if pd.typ<>procdef then
  1436. internalerror(200304266);
  1437. consume(_COLON);
  1438. tprocdef(pd).aliasnames.insert(get_stringconst);
  1439. include(pd.procoptions,po_has_public_name);
  1440. end;
  1441. procedure pd_public(pd:tabstractprocdef);
  1442. begin
  1443. if pd.typ<>procdef then
  1444. internalerror(200304266);
  1445. if try_to_consume(_NAME) then
  1446. begin
  1447. tprocdef(pd).aliasnames.insert(get_stringconst);
  1448. include(pd.procoptions,po_has_public_name);
  1449. end;
  1450. end;
  1451. procedure pd_asmname(pd:tabstractprocdef);
  1452. begin
  1453. if pd.typ<>procdef then
  1454. internalerror(200304267);
  1455. if token=_CCHAR then
  1456. begin
  1457. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1458. consume(_CCHAR)
  1459. end
  1460. else
  1461. begin
  1462. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1463. consume(_CSTRING);
  1464. end;
  1465. { we don't need anything else }
  1466. tprocdef(pd).forwarddef:=false;
  1467. end;
  1468. procedure pd_internconst(pd:tabstractprocdef);
  1469. var v:Tconstexprint;
  1470. begin
  1471. if pd.typ<>procdef then
  1472. internalerror(200304268);
  1473. consume(_COLON);
  1474. v:=get_intconst;
  1475. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1476. message(parser_e_range_check_error)
  1477. else
  1478. Tprocdef(pd).extnumber:=longint(v.svalue);
  1479. end;
  1480. procedure pd_internproc(pd:tabstractprocdef);
  1481. var v:Tconstexprint;
  1482. begin
  1483. if pd.typ<>procdef then
  1484. internalerror(200304268);
  1485. consume(_COLON);
  1486. v:=get_intconst;
  1487. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1488. message(parser_e_range_check_error)
  1489. else
  1490. Tprocdef(pd).extnumber:=longint(v.svalue);
  1491. { the proc is defined }
  1492. tprocdef(pd).forwarddef:=false;
  1493. end;
  1494. procedure pd_interrupt(pd:tabstractprocdef);
  1495. begin
  1496. if pd.parast.symtablelevel>normal_function_level then
  1497. Message(parser_e_dont_nest_interrupt);
  1498. end;
  1499. procedure pd_abstract(pd:tabstractprocdef);
  1500. begin
  1501. if pd.typ<>procdef then
  1502. internalerror(200304269);
  1503. if assigned(tprocdef(pd).struct) and
  1504. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1505. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1506. else
  1507. if (po_virtualmethod in pd.procoptions) then
  1508. include(pd.procoptions,po_abstractmethod)
  1509. else
  1510. Message(parser_e_only_virtual_methods_abstract);
  1511. { the method is defined }
  1512. tprocdef(pd).forwarddef:=false;
  1513. end;
  1514. procedure pd_final(pd:tabstractprocdef);
  1515. begin
  1516. if pd.typ<>procdef then
  1517. internalerror(200910170);
  1518. if (po_virtualmethod in pd.procoptions) then
  1519. include(pd.procoptions,po_finalmethod)
  1520. else
  1521. Message(parser_e_only_virtual_methods_final);
  1522. end;
  1523. procedure pd_enumerator(pd:tabstractprocdef);
  1524. begin
  1525. if pd.typ<>procdef then
  1526. internalerror(200910250);
  1527. if (token = _ID) then
  1528. begin
  1529. if pattern='MOVENEXT' then
  1530. begin
  1531. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1532. message(parser_e_only_one_enumerator_movenext);
  1533. pd.calcparas;
  1534. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1535. (pd.minparacount = 0) then
  1536. begin
  1537. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1538. include(pd.procoptions,po_enumerator_movenext);
  1539. end
  1540. else
  1541. Message(parser_e_enumerator_movenext_is_not_valid)
  1542. end
  1543. else
  1544. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1545. consume(token);
  1546. end
  1547. else
  1548. Message(parser_e_enumerator_identifier_required);
  1549. end;
  1550. procedure pd_virtual(pd:tabstractprocdef);
  1551. {$ifdef WITHDMT}
  1552. var
  1553. pt : tnode;
  1554. {$endif WITHDMT}
  1555. begin
  1556. if pd.typ<>procdef then
  1557. internalerror(2003042610);
  1558. if (pd.proctypeoption=potype_constructor) and
  1559. is_object(tprocdef(pd).struct) then
  1560. Message(parser_e_constructor_cannot_be_not_virtual);
  1561. {$ifdef WITHDMT}
  1562. if is_object(tprocdef(pd).struct) and
  1563. (token<>_SEMICOLON) then
  1564. begin
  1565. { any type of parameter is allowed here! }
  1566. pt:=comp_expr(true);
  1567. if is_constintnode(pt) then
  1568. begin
  1569. include(pd.procoptions,po_msgint);
  1570. pd.messageinf.i:=pt.value;
  1571. end
  1572. else
  1573. Message(parser_e_ill_msg_expr);
  1574. disposetree(pt);
  1575. end;
  1576. {$endif WITHDMT}
  1577. end;
  1578. procedure pd_dispid(pd:tabstractprocdef);
  1579. var pt:Tnode;
  1580. begin
  1581. if pd.typ<>procdef then
  1582. internalerror(200604301);
  1583. pt:=comp_expr(true,false);
  1584. if is_constintnode(pt) then
  1585. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1586. message(parser_e_range_check_error)
  1587. else
  1588. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1589. else
  1590. message(parser_e_dispid_must_be_ord_const);
  1591. pt.free;
  1592. end;
  1593. procedure pd_static(pd:tabstractprocdef);
  1594. begin
  1595. if pd.typ=procdef then
  1596. include(tprocdef(pd).procsym.symoptions,sp_static);
  1597. include(pd.procoptions,po_staticmethod);
  1598. end;
  1599. procedure pd_override(pd:tabstractprocdef);
  1600. begin
  1601. if pd.typ<>procdef then
  1602. internalerror(2003042611);
  1603. if not(is_class_or_interface_or_objc(tprocdef(pd).struct)) then
  1604. Message(parser_e_no_object_override)
  1605. else if is_objccategory(tprocdef(pd).struct) then
  1606. Message(parser_e_no_category_override)
  1607. else if not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1608. not is_cppclass(tprocdef(pd).struct) and
  1609. (po_external in pd.procoptions) then
  1610. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1611. end;
  1612. procedure pd_overload(pd:tabstractprocdef);
  1613. begin
  1614. if pd.typ<>procdef then
  1615. internalerror(2003042612);
  1616. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1617. end;
  1618. procedure pd_message(pd:tabstractprocdef);
  1619. var
  1620. pt : tnode;
  1621. paracnt : longint;
  1622. begin
  1623. if pd.typ<>procdef then
  1624. internalerror(2003042613);
  1625. if not is_class(tprocdef(pd).struct) and
  1626. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1627. Message(parser_e_msg_only_for_classes);
  1628. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1629. Message(parser_e_multiple_messages);
  1630. { check parameter type }
  1631. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1632. begin
  1633. if po_external in pd.procoptions then
  1634. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1635. paracnt:=0;
  1636. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1637. if paracnt<>1 then
  1638. Message(parser_e_ill_msg_param);
  1639. end;
  1640. pt:=comp_expr(true,false);
  1641. { message is 1-character long }
  1642. if is_constcharnode(pt) then
  1643. begin
  1644. include(pd.procoptions,po_msgstr);
  1645. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1646. end
  1647. else if pt.nodetype=stringconstn then
  1648. begin
  1649. include(pd.procoptions,po_msgstr);
  1650. if (tstringconstnode(pt).len>255) then
  1651. Message(parser_e_message_string_too_long);
  1652. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1653. end
  1654. else
  1655. if is_constintnode(pt) and
  1656. is_class(tprocdef(pd).struct) then
  1657. begin
  1658. include(pd.procoptions,po_msgint);
  1659. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1660. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1661. message(parser_e_range_check_error)
  1662. else
  1663. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1664. end
  1665. else
  1666. Message(parser_e_ill_msg_expr);
  1667. { check whether the selector name is valid in case of Objective-C }
  1668. if (po_msgstr in pd.procoptions) and
  1669. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1670. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1671. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1672. pt.free;
  1673. end;
  1674. procedure pd_reintroduce(pd:tabstractprocdef);
  1675. begin
  1676. if pd.typ<>procdef then
  1677. internalerror(200401211);
  1678. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1679. not(is_objccategory(tprocdef(pd).struct)) then
  1680. Message(parser_e_no_object_reintroduce);
  1681. end;
  1682. procedure pd_syscall(pd:tabstractprocdef);
  1683. {$if defined(powerpc) or defined(m68k)}
  1684. var
  1685. vs : tparavarsym;
  1686. sym : tsym;
  1687. symtable : TSymtable;
  1688. v: Tconstexprint;
  1689. {$endif defined(powerpc) or defined(m68k)}
  1690. begin
  1691. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1692. internalerror(2003042614);
  1693. tprocdef(pd).forwarddef:=false;
  1694. {$ifdef m68k}
  1695. if target_info.system in [system_m68k_amiga] then
  1696. begin
  1697. include(pd.procoptions,po_syscall_legacy);
  1698. if consume_sym(sym,symtable) then
  1699. begin
  1700. if (sym.typ=staticvarsym) and
  1701. (
  1702. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1703. is_32bitint(tabstractvarsym(sym).vardef)
  1704. ) then
  1705. begin
  1706. tprocdef(pd).libsym:=sym;
  1707. if po_syscall_legacy in tprocdef(pd).procoptions then
  1708. begin
  1709. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1710. paramanager.parseparaloc(vs,'A6');
  1711. pd.parast.insert(vs);
  1712. end
  1713. end
  1714. else
  1715. Message(parser_e_32bitint_or_pointer_variable_expected);
  1716. end;
  1717. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1718. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1719. v:=get_intconst;
  1720. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1721. message(parser_e_range_check_error)
  1722. else
  1723. Tprocdef(pd).extnumber:=v.uvalue;
  1724. end;
  1725. {$endif m68k}
  1726. {$ifdef powerpc}
  1727. if target_info.system = system_powerpc_amiga then
  1728. begin
  1729. include(pd.procoptions,po_syscall_basesysv);
  1730. if consume_sym(sym,symtable) then
  1731. begin
  1732. if (sym.typ=staticvarsym) and
  1733. (
  1734. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1735. is_32bitint(tabstractvarsym(sym).vardef)
  1736. ) then
  1737. begin
  1738. tprocdef(pd).libsym:=sym;
  1739. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1740. pd.parast.insert(vs);
  1741. end
  1742. else
  1743. Message(parser_e_32bitint_or_pointer_variable_expected);
  1744. end;
  1745. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1746. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1747. v:=get_intconst;
  1748. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1749. message(parser_e_range_check_error)
  1750. else
  1751. Tprocdef(pd).extnumber:=v.uvalue;
  1752. end else
  1753. if target_info.system = system_powerpc_morphos then
  1754. begin
  1755. if idtoken=_LEGACY then
  1756. begin
  1757. consume(_LEGACY);
  1758. include(pd.procoptions,po_syscall_legacy);
  1759. end
  1760. else if idtoken=_SYSV then
  1761. begin
  1762. consume(_SYSV);
  1763. include(pd.procoptions,po_syscall_sysv);
  1764. end
  1765. else if idtoken=_BASESYSV then
  1766. begin
  1767. consume(_BASESYSV);
  1768. include(pd.procoptions,po_syscall_basesysv);
  1769. end
  1770. else if idtoken=_SYSVBASE then
  1771. begin
  1772. consume(_SYSVBASE);
  1773. include(pd.procoptions,po_syscall_sysvbase);
  1774. end
  1775. else if idtoken=_R12BASE then
  1776. begin
  1777. consume(_R12BASE);
  1778. include(pd.procoptions,po_syscall_r12base);
  1779. end
  1780. else
  1781. if syscall_convention='LEGACY' then
  1782. include(pd.procoptions,po_syscall_legacy)
  1783. else if syscall_convention='SYSV' then
  1784. include(pd.procoptions,po_syscall_sysv)
  1785. else if syscall_convention='BASESYSV' then
  1786. include(pd.procoptions,po_syscall_basesysv)
  1787. else if syscall_convention='SYSVBASE' then
  1788. include(pd.procoptions,po_syscall_sysvbase)
  1789. else if syscall_convention='R12BASE' then
  1790. include(pd.procoptions,po_syscall_r12base)
  1791. else
  1792. internalerror(2005010404);
  1793. if consume_sym(sym,symtable) then
  1794. begin
  1795. if (sym.typ=staticvarsym) and
  1796. (
  1797. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1798. is_32bitint(tabstractvarsym(sym).vardef)
  1799. ) then
  1800. begin
  1801. tprocdef(pd).libsym:=sym;
  1802. if po_syscall_legacy in tprocdef(pd).procoptions then
  1803. begin
  1804. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1805. paramanager.parseparaloc(vs,'A6');
  1806. pd.parast.insert(vs);
  1807. end
  1808. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1809. begin
  1810. { Nothing to be done for sysv here for now, but this might change }
  1811. end
  1812. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1813. begin
  1814. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1815. pd.parast.insert(vs);
  1816. end
  1817. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1818. begin
  1819. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1820. pd.parast.insert(vs);
  1821. end
  1822. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1823. begin
  1824. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1825. paramanager.parseparaloc(vs,'R12');
  1826. pd.parast.insert(vs);
  1827. end
  1828. else
  1829. internalerror(2005010501);
  1830. end
  1831. else
  1832. Message(parser_e_32bitint_or_pointer_variable_expected);
  1833. end;
  1834. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1835. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1836. v:=get_intconst;
  1837. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1838. message(parser_e_range_check_error)
  1839. else
  1840. Tprocdef(pd).extnumber:=v.uvalue;
  1841. end;
  1842. {$endif powerpc}
  1843. end;
  1844. procedure pd_external(pd:tabstractprocdef);
  1845. {
  1846. If import_dll=nil the procedure is assumed to be in another
  1847. object file. In that object file it should have the name to
  1848. which import_name is pointing to. Otherwise, the procedure is
  1849. assumed to be in the DLL to which import_dll is pointing to. In
  1850. that case either import_nr<>0 or import_name<>nil is true, so
  1851. the procedure is either imported by number or by name. (DM)
  1852. }
  1853. var
  1854. hs : string;
  1855. v:Tconstexprint;
  1856. begin
  1857. if pd.typ<>procdef then
  1858. internalerror(2003042615);
  1859. with tprocdef(pd) do
  1860. begin
  1861. forwarddef:=false;
  1862. { forbid local external procedures }
  1863. if parast.symtablelevel>normal_function_level then
  1864. Message(parser_e_no_local_proc_external);
  1865. { If the procedure should be imported from a DLL, a constant string follows.
  1866. This isn't really correct, an contant string expression follows
  1867. so we check if an semicolon follows, else a string constant have to
  1868. follow (FK) }
  1869. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1870. begin
  1871. { Always add library prefix and suffix to create an uniform name }
  1872. hs:=get_stringconst;
  1873. if ExtractFileExt(hs)='' then
  1874. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1875. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1876. hs:=target_info.sharedlibprefix+hs;
  1877. import_dll:=stringdup(hs);
  1878. include(procoptions,po_has_importdll);
  1879. if (idtoken=_NAME) then
  1880. begin
  1881. consume(_NAME);
  1882. import_name:=stringdup(get_stringconst);
  1883. include(procoptions,po_has_importname);
  1884. if import_name^='' then
  1885. message(parser_e_empty_import_name);
  1886. end;
  1887. if (idtoken=_INDEX) then
  1888. begin
  1889. {After the word index follows the index number in the DLL.}
  1890. consume(_INDEX);
  1891. v:=get_intconst;
  1892. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1893. message(parser_e_range_check_error)
  1894. else
  1895. import_nr:=longint(v.svalue);
  1896. end;
  1897. { default is to used the realname of the procedure }
  1898. if (import_nr=0) and not assigned(import_name) then
  1899. begin
  1900. import_name:=stringdup(procsym.realname);
  1901. include(procoptions,po_has_importname);
  1902. end;
  1903. end
  1904. else
  1905. begin
  1906. if (idtoken=_NAME) then
  1907. begin
  1908. consume(_NAME);
  1909. import_name:=stringdup(get_stringconst);
  1910. include(procoptions,po_has_importname);
  1911. if import_name^='' then
  1912. message(parser_e_empty_import_name);
  1913. end;
  1914. end;
  1915. end;
  1916. end;
  1917. procedure pd_weakexternal(pd:tabstractprocdef);
  1918. begin
  1919. if not(target_info.system in systems_weak_linking) then
  1920. message(parser_e_weak_external_not_supported)
  1921. else
  1922. pd_external(pd);
  1923. end;
  1924. type
  1925. pd_handler=procedure(pd:tabstractprocdef);
  1926. proc_dir_rec=record
  1927. idtok : ttoken;
  1928. pd_flags : tpdflags;
  1929. handler : pd_handler;
  1930. pocall : tproccalloption;
  1931. pooption : tprocoptions;
  1932. mutexclpocall : tproccalloptions;
  1933. mutexclpotype : tproctypeoptions;
  1934. mutexclpo : tprocoptions;
  1935. end;
  1936. const
  1937. {Should contain the number of procedure directives we support.}
  1938. num_proc_directives=42;
  1939. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1940. (
  1941. (
  1942. idtok:_ABSTRACT;
  1943. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1944. handler : @pd_abstract;
  1945. pocall : pocall_none;
  1946. pooption : [po_abstractmethod];
  1947. mutexclpocall : [pocall_internproc];
  1948. mutexclpotype : [];
  1949. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1950. ),(
  1951. idtok:_ALIAS;
  1952. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1953. handler : @pd_alias;
  1954. pocall : pocall_none;
  1955. pooption : [];
  1956. mutexclpocall : [];
  1957. mutexclpotype : [];
  1958. mutexclpo : [po_external,po_inline]
  1959. ),(
  1960. idtok:_ASMNAME;
  1961. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1962. handler : @pd_asmname;
  1963. pocall : pocall_cdecl;
  1964. pooption : [po_external];
  1965. mutexclpocall : [pocall_internproc];
  1966. mutexclpotype : [];
  1967. mutexclpo : [po_external,po_inline]
  1968. ),(
  1969. idtok:_ASSEMBLER;
  1970. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1971. handler : nil;
  1972. pocall : pocall_none;
  1973. pooption : [po_assembler];
  1974. mutexclpocall : [];
  1975. mutexclpotype : [];
  1976. mutexclpo : [po_external]
  1977. ),(
  1978. idtok:_C; {same as cdecl for mode mac}
  1979. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1980. handler : nil;
  1981. pocall : pocall_cdecl;
  1982. pooption : [];
  1983. mutexclpocall : [];
  1984. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1985. mutexclpo : [po_assembler,po_external]
  1986. ),(
  1987. idtok:_CDECL;
  1988. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1989. handler : nil;
  1990. pocall : pocall_cdecl;
  1991. pooption : [];
  1992. mutexclpocall : [];
  1993. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1994. mutexclpo : [po_assembler,po_external]
  1995. ),(
  1996. idtok:_DISPID;
  1997. pd_flags : [pd_dispinterface];
  1998. handler : @pd_dispid;
  1999. pocall : pocall_none;
  2000. pooption : [po_dispid];
  2001. mutexclpocall : [pocall_internproc];
  2002. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2003. mutexclpo : [po_interrupt,po_external,po_inline]
  2004. ),(
  2005. idtok:_DYNAMIC;
  2006. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2007. handler : @pd_virtual;
  2008. pocall : pocall_none;
  2009. pooption : [po_virtualmethod];
  2010. mutexclpocall : [pocall_internproc];
  2011. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2012. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2013. ),(
  2014. idtok:_EXPORT;
  2015. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord];
  2016. handler : @pd_export;
  2017. pocall : pocall_none;
  2018. pooption : [po_exports,po_global];
  2019. mutexclpocall : [pocall_internproc];
  2020. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2021. mutexclpo : [po_external,po_interrupt,po_inline]
  2022. ),(
  2023. idtok:_EXTERNAL;
  2024. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord];
  2025. handler : @pd_external;
  2026. pocall : pocall_none;
  2027. pooption : [po_external];
  2028. mutexclpocall : [pocall_internproc,pocall_syscall];
  2029. { allowed for external cpp classes }
  2030. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2031. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2032. ),(
  2033. idtok:_FAR;
  2034. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord];
  2035. handler : @pd_far;
  2036. pocall : pocall_none;
  2037. pooption : [];
  2038. mutexclpocall : [pocall_internproc];
  2039. mutexclpotype : [];
  2040. mutexclpo : [po_inline]
  2041. ),(
  2042. idtok:_FAR16;
  2043. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord];
  2044. handler : nil;
  2045. pocall : pocall_far16;
  2046. pooption : [];
  2047. mutexclpocall : [];
  2048. mutexclpotype : [];
  2049. mutexclpo : [po_external]
  2050. ),(
  2051. idtok:_FINAL;
  2052. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2053. handler : @pd_final;
  2054. pocall : pocall_none;
  2055. pooption : [po_finalmethod];
  2056. mutexclpocall : [pocall_internproc];
  2057. mutexclpotype : [];
  2058. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2059. ),(
  2060. idtok:_FORWARD;
  2061. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord];
  2062. handler : @pd_forward;
  2063. pocall : pocall_none;
  2064. pooption : [];
  2065. mutexclpocall : [pocall_internproc];
  2066. mutexclpotype : [];
  2067. mutexclpo : [po_external,po_inline]
  2068. ),(
  2069. idtok:_OLDFPCCALL;
  2070. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2071. handler : nil;
  2072. pocall : pocall_oldfpccall;
  2073. pooption : [];
  2074. mutexclpocall : [];
  2075. mutexclpotype : [];
  2076. mutexclpo : []
  2077. ),(
  2078. idtok:_INLINE;
  2079. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2080. handler : nil;
  2081. pocall : pocall_none;
  2082. pooption : [po_inline];
  2083. mutexclpocall : [];
  2084. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2085. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  2086. ),(
  2087. idtok:_INTERNCONST;
  2088. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2089. handler : @pd_internconst;
  2090. pocall : pocall_none;
  2091. pooption : [po_internconst];
  2092. mutexclpocall : [];
  2093. mutexclpotype : [potype_operator];
  2094. mutexclpo : []
  2095. ),(
  2096. idtok:_INTERNPROC;
  2097. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord];
  2098. handler : @pd_internproc;
  2099. pocall : pocall_internproc;
  2100. pooption : [];
  2101. mutexclpocall : [];
  2102. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2103. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2104. ),(
  2105. idtok:_INTERRUPT;
  2106. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2107. handler : @pd_interrupt;
  2108. pocall : pocall_oldfpccall;
  2109. pooption : [po_interrupt];
  2110. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  2111. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2112. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2113. mutexclpo : [po_external,po_inline]
  2114. ),(
  2115. idtok:_IOCHECK;
  2116. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2117. handler : nil;
  2118. pocall : pocall_none;
  2119. pooption : [po_iocheck];
  2120. mutexclpocall : [pocall_internproc];
  2121. mutexclpotype : [];
  2122. mutexclpo : [po_external]
  2123. ),(
  2124. idtok:_LOCAL;
  2125. pd_flags : [pd_implemen,pd_body];
  2126. handler : nil;
  2127. pocall : pocall_none;
  2128. pooption : [po_kylixlocal];
  2129. mutexclpocall : [pocall_internproc,pocall_far16];
  2130. mutexclpotype : [];
  2131. mutexclpo : [po_external,po_exports]
  2132. ),(
  2133. idtok:_MESSAGE;
  2134. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2135. handler : @pd_message;
  2136. pocall : pocall_none;
  2137. pooption : []; { can be po_msgstr or po_msgint }
  2138. mutexclpocall : [pocall_internproc];
  2139. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2140. mutexclpo : [po_interrupt,po_inline]
  2141. ),(
  2142. idtok:_MWPASCAL;
  2143. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2144. handler : nil;
  2145. pocall : pocall_mwpascal;
  2146. pooption : [];
  2147. mutexclpocall : [];
  2148. mutexclpotype : [];
  2149. mutexclpo : []
  2150. ),(
  2151. idtok:_NEAR;
  2152. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord];
  2153. handler : @pd_near;
  2154. pocall : pocall_none;
  2155. pooption : [];
  2156. mutexclpocall : [pocall_internproc];
  2157. mutexclpotype : [];
  2158. mutexclpo : []
  2159. ),(
  2160. idtok:_NOSTACKFRAME;
  2161. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2162. handler : nil;
  2163. pocall : pocall_none;
  2164. pooption : [po_nostackframe];
  2165. mutexclpocall : [pocall_internproc];
  2166. mutexclpotype : [];
  2167. mutexclpo : []
  2168. ),(
  2169. idtok:_OVERLOAD;
  2170. pd_flags : [pd_implemen,pd_interface,pd_body];
  2171. handler : @pd_overload;
  2172. pocall : pocall_none;
  2173. pooption : [po_overload];
  2174. mutexclpocall : [pocall_internproc];
  2175. mutexclpotype : [];
  2176. mutexclpo : []
  2177. ),(
  2178. idtok:_OVERRIDE;
  2179. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2180. handler : @pd_override;
  2181. pocall : pocall_none;
  2182. pooption : [po_overridingmethod,po_virtualmethod];
  2183. mutexclpocall : [pocall_internproc];
  2184. mutexclpotype : [];
  2185. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2186. ),(
  2187. idtok:_PASCAL;
  2188. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2189. handler : nil;
  2190. pocall : pocall_pascal;
  2191. pooption : [];
  2192. mutexclpocall : [];
  2193. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2194. mutexclpo : [po_external]
  2195. ),(
  2196. idtok:_PUBLIC;
  2197. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2198. handler : @pd_public;
  2199. pocall : pocall_none;
  2200. pooption : [po_public,po_global];
  2201. mutexclpocall : [pocall_internproc];
  2202. mutexclpotype : [];
  2203. mutexclpo : [po_external,po_inline]
  2204. ),(
  2205. idtok:_REGISTER;
  2206. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2207. handler : nil;
  2208. pocall : pocall_register;
  2209. pooption : [];
  2210. mutexclpocall : [];
  2211. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2212. mutexclpo : [po_external]
  2213. ),(
  2214. idtok:_REINTRODUCE;
  2215. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2216. handler : @pd_reintroduce;
  2217. pocall : pocall_none;
  2218. pooption : [po_reintroduce];
  2219. mutexclpocall : [pocall_internproc];
  2220. mutexclpotype : [];
  2221. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2222. ),(
  2223. idtok:_SAFECALL;
  2224. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2225. handler : nil;
  2226. pocall : pocall_safecall;
  2227. pooption : [];
  2228. mutexclpocall : [];
  2229. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2230. mutexclpo : [po_external]
  2231. ),(
  2232. idtok:_SOFTFLOAT;
  2233. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2234. handler : nil;
  2235. pocall : pocall_softfloat;
  2236. pooption : [];
  2237. mutexclpocall : [];
  2238. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2239. { it's available with po_external because the libgcc floating point routines on the arm
  2240. uses this calling convention }
  2241. mutexclpo : []
  2242. ),(
  2243. idtok:_STATIC;
  2244. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_notobjintf];
  2245. handler : @pd_static;
  2246. pocall : pocall_none;
  2247. pooption : [po_staticmethod];
  2248. mutexclpocall : [pocall_internproc];
  2249. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2250. mutexclpo : [po_external,po_interrupt,po_exports]
  2251. ),(
  2252. idtok:_STDCALL;
  2253. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2254. handler : nil;
  2255. pocall : pocall_stdcall;
  2256. pooption : [];
  2257. mutexclpocall : [];
  2258. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2259. mutexclpo : [po_external]
  2260. ),(
  2261. idtok:_SYSCALL;
  2262. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2263. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2264. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2265. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2266. handler : @pd_syscall;
  2267. pocall : pocall_syscall;
  2268. pooption : [];
  2269. mutexclpocall : [];
  2270. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2271. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2272. ),(
  2273. idtok:_VIRTUAL;
  2274. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2275. handler : @pd_virtual;
  2276. pocall : pocall_none;
  2277. pooption : [po_virtualmethod];
  2278. mutexclpocall : [pocall_internproc];
  2279. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2280. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2281. ),(
  2282. idtok:_CPPDECL;
  2283. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2284. handler : nil;
  2285. pocall : pocall_cppdecl;
  2286. pooption : [];
  2287. mutexclpocall : [];
  2288. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2289. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2290. ),(
  2291. idtok:_VARARGS;
  2292. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2293. handler : nil;
  2294. pocall : pocall_none;
  2295. pooption : [po_varargs];
  2296. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2297. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2298. mutexclpotype : [];
  2299. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2300. ),(
  2301. idtok:_COMPILERPROC;
  2302. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2303. handler : nil;
  2304. pocall : pocall_none;
  2305. pooption : [po_compilerproc];
  2306. mutexclpocall : [];
  2307. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2308. mutexclpo : [po_interrupt]
  2309. ),(
  2310. idtok:_WEAKEXTERNAL;
  2311. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord];
  2312. handler : @pd_weakexternal;
  2313. pocall : pocall_none;
  2314. { mark it both external and weak external, so we don't have to
  2315. adapt all code for external symbols to also check for weak external
  2316. }
  2317. pooption : [po_external,po_weakexternal];
  2318. mutexclpocall : [pocall_internproc,pocall_syscall];
  2319. { allowed for external cpp classes }
  2320. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2321. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2322. ),(
  2323. idtok:_ENUMERATOR;
  2324. pd_flags : [pd_interface,pd_object];
  2325. handler : @pd_enumerator;
  2326. pocall : pocall_none;
  2327. pooption : [];
  2328. mutexclpocall : [pocall_internproc];
  2329. mutexclpotype : [];
  2330. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2331. )
  2332. );
  2333. function check_proc_directive(isprocvar:boolean):boolean;
  2334. var
  2335. i : longint;
  2336. begin
  2337. result:=false;
  2338. for i:=1 to num_proc_directives do
  2339. if proc_direcdata[i].idtok=idtoken then
  2340. begin
  2341. if ((not isprocvar) or
  2342. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2343. { don't eat a public directive in classes }
  2344. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2345. result:=true;
  2346. exit;
  2347. end;
  2348. end;
  2349. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2350. {
  2351. Parse the procedure directive, returns true if a correct directive is found
  2352. }
  2353. var
  2354. p : longint;
  2355. found : boolean;
  2356. name : TIDString;
  2357. begin
  2358. parse_proc_direc:=false;
  2359. name:=tokeninfo^[idtoken].str;
  2360. found:=false;
  2361. { Hint directive? Then exit immediatly }
  2362. if (m_hintdirective in current_settings.modeswitches) then
  2363. begin
  2364. case idtoken of
  2365. _LIBRARY,
  2366. _PLATFORM,
  2367. _UNIMPLEMENTED,
  2368. _EXPERIMENTAL,
  2369. _DEPRECATED :
  2370. exit;
  2371. end;
  2372. end;
  2373. { C directive is MacPas only, because it breaks too much existing code
  2374. on other platforms (PFV) }
  2375. if (idtoken=_C) and
  2376. not(m_mac in current_settings.modeswitches) then
  2377. exit;
  2378. { retrieve data for directive if found }
  2379. for p:=1 to num_proc_directives do
  2380. if proc_direcdata[p].idtok=idtoken then
  2381. begin
  2382. found:=true;
  2383. break;
  2384. end;
  2385. { Check if the procedure directive is known }
  2386. if not found then
  2387. begin
  2388. { parsing a procvar type the name can be any
  2389. next variable !! }
  2390. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2391. not(idtoken=_PROPERTY) then
  2392. Message1(parser_w_unknown_proc_directive_ignored,name);
  2393. exit;
  2394. end;
  2395. { check if method and directive not for object, like public.
  2396. This needs to be checked also for procvars }
  2397. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2398. (symtablestack.top.symtabletype=ObjectSymtable) and
  2399. { directive allowed for cpp classes? }
  2400. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  2401. exit;
  2402. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2403. (symtablestack.top.symtabletype=recordsymtable) then
  2404. exit;
  2405. { Conflicts between directives ? }
  2406. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2407. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2408. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2409. begin
  2410. Message1(parser_e_proc_dir_conflict,name);
  2411. exit;
  2412. end;
  2413. { set calling convention }
  2414. if proc_direcdata[p].pocall<>pocall_none then
  2415. begin
  2416. if (po_hascallingconvention in pd.procoptions) then
  2417. begin
  2418. Message2(parser_w_proc_overriding_calling,
  2419. proccalloptionStr[pd.proccalloption],
  2420. proccalloptionStr[proc_direcdata[p].pocall]);
  2421. end;
  2422. { check if the target processor supports this calling convention }
  2423. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2424. begin
  2425. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2426. { recover }
  2427. proc_direcdata[p].pocall:=pocall_stdcall;
  2428. end;
  2429. pd.proccalloption:=proc_direcdata[p].pocall;
  2430. include(pd.procoptions,po_hascallingconvention);
  2431. end;
  2432. if pd.typ=procdef then
  2433. begin
  2434. { Check if the directive is only for objects }
  2435. if (pd_object in proc_direcdata[p].pd_flags) and
  2436. not assigned(tprocdef(pd).struct) then
  2437. exit;
  2438. { Check if the directive is only for records }
  2439. if (pd_record in proc_direcdata[p].pd_flags) and
  2440. not assigned(tprocdef(pd).struct) then
  2441. exit;
  2442. { check if method and directive not for interface }
  2443. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2444. is_interface(tprocdef(pd).struct) then
  2445. exit;
  2446. { check if method and directive not for interface }
  2447. if is_dispinterface(tprocdef(pd).struct) and
  2448. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2449. exit;
  2450. { check if method and directive not for objcclass }
  2451. if is_objcclass(tprocdef(pd).struct) and
  2452. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2453. exit;
  2454. { check if method and directive not for objcprotocol }
  2455. if is_objcprotocol(tprocdef(pd).struct) and
  2456. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2457. exit;
  2458. end;
  2459. { consume directive, and turn flag on }
  2460. consume(token);
  2461. parse_proc_direc:=true;
  2462. { Check the pd_flags if the directive should be allowed }
  2463. if (pd_interface in pdflags) and
  2464. not(pd_interface in proc_direcdata[p].pd_flags) then
  2465. begin
  2466. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2467. exit;
  2468. end;
  2469. if (pd_implemen in pdflags) and
  2470. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2471. begin
  2472. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2473. exit;
  2474. end;
  2475. if (pd_procvar in pdflags) and
  2476. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2477. begin
  2478. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2479. exit;
  2480. end;
  2481. { Return the new pd_flags }
  2482. if not(pd_body in proc_direcdata[p].pd_flags) then
  2483. exclude(pdflags,pd_body);
  2484. { Add the correct flag }
  2485. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2486. { Call the handler }
  2487. if pointer(proc_direcdata[p].handler)<>nil then
  2488. proc_direcdata[p].handler(pd);
  2489. end;
  2490. function proc_get_importname(pd:tprocdef):string;
  2491. function maybe_cprefix(const s:string):string;
  2492. begin
  2493. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2494. result:=s
  2495. else
  2496. result:=target_info.Cprefix+s;
  2497. end;
  2498. begin
  2499. result:='';
  2500. if not(po_external in pd.procoptions) then
  2501. internalerror(200412151);
  2502. { external name or number is specified }
  2503. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2504. begin
  2505. if assigned(pd.import_dll) then
  2506. begin
  2507. { If we are not using direct dll linking under win32 then imports
  2508. need to use the normal name since two functions can refer to the
  2509. same DLL function. This is also needed for compatability
  2510. with Delphi and TP7 }
  2511. (*
  2512. case target_info.system of
  2513. system_i386_emx,
  2514. system_i386_os2 :
  2515. begin
  2516. { keep normal mangledname }
  2517. if not (Assigned (PD.Import_Name)) then
  2518. Result := PD.MangledName;
  2519. end;
  2520. else
  2521. *)
  2522. if assigned(pd.import_name) then
  2523. begin
  2524. if target_info.system in (systems_all_windows + systems_nativent +
  2525. [system_i386_emx, system_i386_os2]) then
  2526. { cprefix is not used in DLL imports under Windows or OS/2 }
  2527. result:=pd.import_name^
  2528. else
  2529. result:=maybe_cprefix(pd.import_name^);
  2530. end
  2531. else
  2532. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2533. end
  2534. else
  2535. result:=maybe_cprefix(pd.import_name^);
  2536. end
  2537. else
  2538. begin
  2539. { Default names when importing variables }
  2540. case pd.proccalloption of
  2541. pocall_cdecl :
  2542. begin
  2543. if assigned(pd.struct) then
  2544. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2545. else
  2546. result:=target_info.Cprefix+pd.procsym.realname;
  2547. end;
  2548. pocall_cppdecl :
  2549. begin
  2550. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2551. end;
  2552. else
  2553. begin
  2554. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2555. { but according to MacPas mode description
  2556. Cprefix should still be used PM }
  2557. if (m_mac in current_settings.modeswitches) then
  2558. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2559. end;
  2560. end;
  2561. end;
  2562. end;
  2563. procedure proc_set_mangledname(pd:tprocdef);
  2564. var
  2565. s : string;
  2566. begin
  2567. { When the mangledname is already set we aren't allowed to change
  2568. it because it can already be used somewhere (PFV) }
  2569. if not(po_has_mangledname in pd.procoptions) then
  2570. begin
  2571. if (po_external in pd.procoptions) then
  2572. begin
  2573. { External Procedures are only allowed to change the mangledname
  2574. in their first declaration }
  2575. if (pd.forwarddef or (not pd.hasforward)) then
  2576. begin
  2577. s:=proc_get_importname(pd);
  2578. if s<>'' then
  2579. begin
  2580. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2581. { This allows to import VC++ mangled names from DLLs. }
  2582. { Do not perform replacement, if external symbol is not imported from DLL. }
  2583. if (target_info.system in systems_all_windows) and (pd.import_dll<>nil) then
  2584. begin
  2585. Replace(s,'?','__q$$');
  2586. {$ifdef arm}
  2587. { @ symbol is not allowed in ARM assembler only }
  2588. Replace(s,'@','__a$$');
  2589. {$endif arm}
  2590. end;
  2591. pd.setmangledname(s);
  2592. end;
  2593. end;
  2594. end
  2595. else
  2596. { Normal procedures }
  2597. begin
  2598. if (po_compilerproc in pd.procoptions) then
  2599. begin
  2600. pd.setmangledname(lower(pd.procsym.name));
  2601. end;
  2602. end;
  2603. end;
  2604. { Public/exported alias names }
  2605. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2606. not(po_has_public_name in pd.procoptions) then
  2607. begin
  2608. case pd.proccalloption of
  2609. pocall_cdecl :
  2610. begin
  2611. if assigned(pd.struct) then
  2612. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2613. else
  2614. begin
  2615. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2616. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2617. (po_exports in pd.procoptions) then
  2618. pd.aliasnames.insert(pd.procsym.realname)
  2619. else
  2620. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2621. end;
  2622. end;
  2623. pocall_cppdecl :
  2624. begin
  2625. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2626. end;
  2627. end;
  2628. { prevent adding the alias a second time }
  2629. include(pd.procoptions,po_has_public_name);
  2630. end;
  2631. end;
  2632. procedure handle_calling_convention(pd:tabstractprocdef);
  2633. begin
  2634. { set the default calling convention if none provided }
  2635. if (pd.typ=procdef) and
  2636. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2637. is_cppclass(tprocdef(pd).struct)) then
  2638. begin
  2639. { none of the explicit calling conventions should be allowed }
  2640. if (po_hascallingconvention in pd.procoptions) then
  2641. internalerror(2009032501);
  2642. if is_cppclass(tprocdef(pd).struct) then
  2643. pd.proccalloption:=pocall_cppdecl
  2644. else
  2645. pd.proccalloption:=pocall_cdecl;
  2646. end
  2647. else if not(po_hascallingconvention in pd.procoptions) then
  2648. pd.proccalloption:=current_settings.defproccall
  2649. else
  2650. begin
  2651. if pd.proccalloption=pocall_none then
  2652. internalerror(200309081);
  2653. end;
  2654. { handle proccall specific settings }
  2655. case pd.proccalloption of
  2656. pocall_cdecl,
  2657. pocall_cppdecl :
  2658. begin
  2659. { check C cdecl para types }
  2660. check_c_para(pd);
  2661. end;
  2662. pocall_far16 :
  2663. begin
  2664. { Temporary stub, must be rewritten to support OS/2 far16 }
  2665. Message1(parser_w_proc_directive_ignored,'FAR16');
  2666. end;
  2667. end;
  2668. { Inlining is enabled and supported? }
  2669. if (po_inline in pd.procoptions) and
  2670. not(cs_do_inline in current_settings.localswitches) then
  2671. begin
  2672. { Give an error if inline is not supported by the compiler mode,
  2673. otherwise only give a warning that this procedure will not be inlined }
  2674. if not(m_default_inline in current_settings.modeswitches) then
  2675. Message(parser_e_proc_inline_not_supported)
  2676. else
  2677. Message(parser_w_inlining_disabled);
  2678. exclude(pd.procoptions,po_inline);
  2679. end;
  2680. { For varargs directive also cdecl and external must be defined }
  2681. if (po_varargs in pd.procoptions) then
  2682. begin
  2683. { check first for external in the interface, if available there
  2684. then the cdecl must also be there since there is no implementation
  2685. available to contain it }
  2686. if parse_only then
  2687. begin
  2688. { if external is available, then cdecl must also be available,
  2689. procvars don't need external }
  2690. if not((po_external in pd.procoptions) or
  2691. (pd.typ=procvardef) or
  2692. { for objcclasses this is checked later, because the entire
  2693. class may be external. }
  2694. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2695. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2696. Message(parser_e_varargs_need_cdecl_and_external);
  2697. end
  2698. else
  2699. begin
  2700. { both must be defined now }
  2701. if not((po_external in pd.procoptions) or
  2702. (pd.typ=procvardef)) or
  2703. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2704. Message(parser_e_varargs_need_cdecl_and_external);
  2705. end;
  2706. end;
  2707. { insert hidden high parameters }
  2708. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2709. { insert hidden self parameter }
  2710. insert_self_and_vmt_para(pd);
  2711. { insert funcret parameter if required }
  2712. insert_funcret_para(pd);
  2713. { Make var parameters regable, this must be done after the calling
  2714. convention is set. }
  2715. { this must be done before parentfp is insert, because getting all cases
  2716. where parentfp must be in a memory location isn't catched properly so
  2717. we put parentfp never in a register }
  2718. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2719. { insert parentfp parameter if required }
  2720. insert_parentfp_para(pd);
  2721. { Calculate parameter tlist }
  2722. pd.calcparas;
  2723. end;
  2724. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2725. {
  2726. Parse the procedure directives. It does not matter if procedure directives
  2727. are written using ;procdir; or ['procdir'] syntax.
  2728. }
  2729. var
  2730. res : boolean;
  2731. begin
  2732. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2733. begin
  2734. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2735. include(pd.procoptions,po_public);
  2736. include(pd.procoptions,po_has_public_name);
  2737. include(pd.procoptions,po_global);
  2738. end;
  2739. { methods from external class definitions are all external themselves }
  2740. if (pd.typ=procdef) and
  2741. assigned(tprocdef(pd).struct) and
  2742. (tprocdef(pd).struct.typ=objectdef) and
  2743. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2744. tprocdef(pd).make_external;
  2745. { Class constructors and destructor are static class methods in real. }
  2746. { There are many places in the compiler where either class or static }
  2747. { method flag changes the behavior. It is simplier to add them to }
  2748. { the class constructors/destructors options than to fix all the }
  2749. { occurencies. (Paul) }
  2750. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2751. begin
  2752. include(pd.procoptions,po_classmethod);
  2753. include(pd.procoptions,po_staticmethod);
  2754. end;
  2755. while token in [_ID,_LECKKLAMMER] do
  2756. begin
  2757. if try_to_consume(_LECKKLAMMER) then
  2758. begin
  2759. repeat
  2760. parse_proc_direc(pd,pdflags);
  2761. until not try_to_consume(_COMMA);
  2762. consume(_RECKKLAMMER);
  2763. { we always expect at least '[];' }
  2764. res:=true;
  2765. end
  2766. else
  2767. begin
  2768. res:=parse_proc_direc(pd,pdflags);
  2769. end;
  2770. { A procedure directive normally followed by a semicolon, but in
  2771. a const section or reading a type we should stop when _EQ is found,
  2772. because a constant/default value follows }
  2773. if res then
  2774. begin
  2775. if (block_type=bt_const_type) and
  2776. (token=_EQ) then
  2777. break;
  2778. { support procedure proc;stdcall export; }
  2779. if not(check_proc_directive((pd.typ=procvardef))) then
  2780. begin
  2781. { support "record p : procedure stdcall end;" and
  2782. "var p : procedure stdcall = nil;" }
  2783. if (pd_procvar in pdflags) and
  2784. (token in [_END,_RKLAMMER,_EQ]) then
  2785. break
  2786. else
  2787. begin
  2788. if (token=_COLON) then
  2789. begin
  2790. Message(parser_e_field_not_allowed_here);
  2791. consume_all_until(_SEMICOLON);
  2792. end;
  2793. consume(_SEMICOLON)
  2794. end;
  2795. end;
  2796. end
  2797. else
  2798. break;
  2799. end;
  2800. end;
  2801. procedure parse_var_proc_directives(sym:tsym);
  2802. var
  2803. pdflags : tpdflags;
  2804. pd : tabstractprocdef;
  2805. begin
  2806. pdflags:=[pd_procvar];
  2807. pd:=nil;
  2808. case sym.typ of
  2809. fieldvarsym,
  2810. staticvarsym,
  2811. localvarsym,
  2812. paravarsym :
  2813. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2814. typesym :
  2815. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2816. else
  2817. internalerror(2003042617);
  2818. end;
  2819. if pd.typ<>procvardef then
  2820. internalerror(2003042618);
  2821. { names should never be used anyway }
  2822. parse_proc_directives(pd,pdflags);
  2823. end;
  2824. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2825. var
  2826. pdflags : tpdflags;
  2827. begin
  2828. pdflags:=[pd_object];
  2829. parse_proc_directives(pd,pdflags);
  2830. end;
  2831. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2832. var
  2833. pdflags : tpdflags;
  2834. begin
  2835. pdflags:=[pd_record];
  2836. parse_proc_directives(pd,pdflags);
  2837. end;
  2838. function proc_add_definition(var currpd:tprocdef):boolean;
  2839. {
  2840. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2841. forwarddef is found and reused it returns true
  2842. }
  2843. var
  2844. fwpd : tprocdef;
  2845. currparasym,
  2846. fwparasym : tsym;
  2847. currparacnt,
  2848. fwparacnt,
  2849. curridx,
  2850. fwidx,
  2851. i : longint;
  2852. po_comp : tprocoptions;
  2853. paracompopt: tcompare_paras_options;
  2854. forwardfound : boolean;
  2855. symentry: TSymEntry;
  2856. begin
  2857. forwardfound:=false;
  2858. { check overloaded functions if the same function already exists }
  2859. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2860. begin
  2861. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2862. { Skip overloaded definitions that are declared in other units }
  2863. if fwpd.procsym<>currpd.procsym then
  2864. continue;
  2865. { check the parameters, for delphi/tp it is possible to
  2866. leave the parameters away in the implementation (forwarddef=false).
  2867. But for an overload declared function this is not allowed }
  2868. if { check if empty implementation arguments match is allowed }
  2869. (
  2870. not(m_repeat_forward in current_settings.modeswitches) and
  2871. not(currpd.forwarddef) and
  2872. is_bareprocdef(currpd) and
  2873. not(po_overload in fwpd.procoptions)
  2874. ) or
  2875. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2876. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2877. (
  2878. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2879. (fwpd.returndef=currpd.returndef)
  2880. ) then
  2881. begin
  2882. { Check if we've found the forwarddef, if found then
  2883. we need to update the forward def with the current
  2884. implementation settings }
  2885. if fwpd.forwarddef then
  2886. begin
  2887. forwardfound:=true;
  2888. if not(m_repeat_forward in current_settings.modeswitches) and
  2889. (fwpd.proccalloption<>currpd.proccalloption) then
  2890. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2891. else
  2892. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2893. { Check calling convention }
  2894. if (fwpd.proccalloption<>currpd.proccalloption) then
  2895. begin
  2896. { In delphi it is possible to specify the calling
  2897. convention in the interface or implementation if
  2898. there was no convention specified in the other
  2899. part }
  2900. if (m_delphi in current_settings.modeswitches) then
  2901. begin
  2902. if not(po_hascallingconvention in currpd.procoptions) then
  2903. currpd.proccalloption:=fwpd.proccalloption
  2904. else
  2905. if not(po_hascallingconvention in fwpd.procoptions) then
  2906. fwpd.proccalloption:=currpd.proccalloption
  2907. else
  2908. begin
  2909. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2910. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2911. { restore interface settings }
  2912. currpd.proccalloption:=fwpd.proccalloption;
  2913. end;
  2914. end
  2915. else
  2916. begin
  2917. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2918. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2919. { restore interface settings }
  2920. currpd.proccalloption:=fwpd.proccalloption;
  2921. end;
  2922. end;
  2923. { Check static }
  2924. if (po_staticmethod in fwpd.procoptions) then
  2925. begin
  2926. if not (po_staticmethod in currpd.procoptions) then
  2927. begin
  2928. include(currpd.procoptions, po_staticmethod);
  2929. if (po_classmethod in currpd.procoptions) then
  2930. begin
  2931. { remove self from the hidden paras }
  2932. symentry:=currpd.parast.Find('self');
  2933. if symentry<>nil then
  2934. begin
  2935. currpd.parast.Delete(symentry);
  2936. currpd.calcparas;
  2937. end;
  2938. end;
  2939. end;
  2940. end;
  2941. { Check if the procedure type and return type are correct,
  2942. also the parameters must match also with the type }
  2943. if ((m_repeat_forward in current_settings.modeswitches) or
  2944. not is_bareprocdef(currpd)) and
  2945. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2946. (fwpd.returndef<>currpd.returndef)) then
  2947. begin
  2948. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2949. fwpd.fullprocname(false));
  2950. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2951. break;
  2952. end;
  2953. { Check if both are declared forward }
  2954. if fwpd.forwarddef and currpd.forwarddef then
  2955. begin
  2956. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2957. currpd.fullprocname(false));
  2958. end;
  2959. { internconst or internproc only need to be defined once }
  2960. if (fwpd.proccalloption=pocall_internproc) then
  2961. currpd.proccalloption:=fwpd.proccalloption
  2962. else
  2963. if (currpd.proccalloption=pocall_internproc) then
  2964. fwpd.proccalloption:=currpd.proccalloption;
  2965. { Check procedure options, Delphi requires that class is
  2966. repeated in the implementation for class methods }
  2967. if (m_fpc in current_settings.modeswitches) then
  2968. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2969. else
  2970. po_comp:=[po_classmethod,po_methodpointer];
  2971. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2972. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2973. begin
  2974. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2975. fwpd.fullprocname(false));
  2976. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2977. { This error is non-fatal, we can recover }
  2978. end;
  2979. { Forward declaration is external? }
  2980. if (po_external in fwpd.procoptions) then
  2981. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2982. { Check parameters }
  2983. if (m_repeat_forward in current_settings.modeswitches) or
  2984. (currpd.minparacount>0) then
  2985. begin
  2986. { If mangled names are equal then they have the same amount of arguments }
  2987. { We can check the names of the arguments }
  2988. { both symtables are in the same order from left to right }
  2989. curridx:=0;
  2990. fwidx:=0;
  2991. currparacnt:=currpd.parast.SymList.Count;
  2992. fwparacnt:=fwpd.parast.SymList.Count;
  2993. repeat
  2994. { skip default parameter constsyms }
  2995. while (curridx<currparacnt) and
  2996. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2997. inc(curridx);
  2998. while (fwidx<fwparacnt) and
  2999. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3000. inc(fwidx);
  3001. { stop when one of the two lists is at the end }
  3002. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3003. break;
  3004. { compare names of parameters, ignore implictly
  3005. renamed parameters }
  3006. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3007. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3008. if not(sp_implicitrename in currparasym.symoptions) and
  3009. not(sp_implicitrename in fwparasym.symoptions) then
  3010. begin
  3011. if (currparasym.name<>fwparasym.name) then
  3012. begin
  3013. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3014. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3015. break;
  3016. end;
  3017. end;
  3018. { next parameter }
  3019. inc(curridx);
  3020. inc(fwidx);
  3021. until false;
  3022. end;
  3023. { Everything is checked, now we can update the forward declaration
  3024. with the new data from the implementation }
  3025. fwpd.forwarddef:=currpd.forwarddef;
  3026. fwpd.hasforward:=true;
  3027. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3028. { marked as local but exported from unit? }
  3029. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3030. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3031. if fwpd.extnumber=$ffff then
  3032. fwpd.extnumber:=currpd.extnumber;
  3033. while not currpd.aliasnames.empty do
  3034. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3035. { update fileinfo so position references the implementation,
  3036. also update funcretsym if it is already generated }
  3037. fwpd.fileinfo:=currpd.fileinfo;
  3038. if assigned(fwpd.funcretsym) then
  3039. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3040. if assigned(currpd.deprecatedmsg) then
  3041. begin
  3042. stringdispose(fwpd.deprecatedmsg);
  3043. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3044. end;
  3045. { import names }
  3046. if assigned(currpd.import_dll) then
  3047. begin
  3048. stringdispose(fwpd.import_dll);
  3049. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3050. end;
  3051. if assigned(currpd.import_name) then
  3052. begin
  3053. stringdispose(fwpd.import_name);
  3054. fwpd.import_name:=stringdup(currpd.import_name^);
  3055. end;
  3056. fwpd.import_nr:=currpd.import_nr;
  3057. { for compilerproc defines we need to rename and update the
  3058. symbolname to lowercase }
  3059. if (po_compilerproc in fwpd.procoptions) then
  3060. begin
  3061. { rename to lowercase so users can't access it }
  3062. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  3063. { the mangeled name is already changed by the pd_compilerproc }
  3064. { handler. It must be done immediately because if we have a }
  3065. { call to a compilerproc before it's implementation is }
  3066. { encountered, it must already use the new mangled name (JM) }
  3067. end;
  3068. { Release current procdef }
  3069. currpd.owner.deletedef(currpd);
  3070. currpd:=fwpd;
  3071. end
  3072. else
  3073. begin
  3074. { abstract methods aren't forward defined, but this }
  3075. { needs another error message }
  3076. if (po_abstractmethod in fwpd.procoptions) then
  3077. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3078. else
  3079. begin
  3080. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3081. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3082. end;
  3083. end;
  3084. { we found one proc with the same arguments, there are no others
  3085. so we can stop }
  3086. break;
  3087. end;
  3088. { check for allowing overload directive }
  3089. if not(m_fpc in current_settings.modeswitches) then
  3090. begin
  3091. { overload directive turns on overloading }
  3092. if ((po_overload in currpd.procoptions) or
  3093. (po_overload in fwpd.procoptions)) then
  3094. begin
  3095. { check if all procs have overloading, but not if the proc is a method or
  3096. already declared forward, then the check is already done }
  3097. if not(fwpd.hasforward or
  3098. assigned(currpd.struct) or
  3099. (currpd.forwarddef<>fwpd.forwarddef) or
  3100. ((po_overload in currpd.procoptions) and
  3101. (po_overload in fwpd.procoptions))) then
  3102. begin
  3103. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3104. break;
  3105. end;
  3106. end
  3107. else
  3108. begin
  3109. if not(fwpd.forwarddef) then
  3110. begin
  3111. if (m_tp7 in current_settings.modeswitches) then
  3112. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3113. else
  3114. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3115. break;
  3116. end;
  3117. end;
  3118. end; { equal arguments }
  3119. end;
  3120. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3121. list }
  3122. if not forwardfound then
  3123. begin
  3124. { can happen in Delphi mode }
  3125. if (currpd.proctypeoption = potype_function) and
  3126. is_void(currpd.returndef) then
  3127. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3128. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3129. end;
  3130. proc_add_definition:=forwardfound;
  3131. end;
  3132. end.