pdecsub.pas 121 KB

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