pdecsub.pas 126 KB

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