pdecsub.pas 123 KB

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