pdecsub.pas 129 KB

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