pdecsub.pas 121 KB

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