| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | // |
| 5 | // file: method.cpp |
| 6 | // |
| 7 | |
| 8 | // |
| 9 | #include "ilasmpch.h" |
| 10 | #include "assembler.h" |
| 11 | |
| 12 | Method::Method(Assembler *pAssembler, Class *pClass, __in __nullterminated char *pszName, BinStr* pbsSig, DWORD Attr) |
| 13 | { |
| 14 | |
| 15 | // default values |
| 16 | m_pClass = pClass; |
| 17 | m_MaxStack = 8; |
| 18 | m_Flags = 0; |
| 19 | m_LocalsSig = 0; |
| 20 | m_dwNumExceptions = 0; |
| 21 | m_dwNumEndfilters = 0; |
| 22 | m_firstArgName = NULL; |
| 23 | m_firstVarName = NULL; |
| 24 | m_pMethodSig = NULL; |
| 25 | m_wImplAttr = miIL; //default, if native or optil are not specified |
| 26 | m_wVTEntry = 0; |
| 27 | m_wVTSlot = 0; |
| 28 | m_pAssembler = pAssembler; |
| 29 | m_pCurrScope = &m_MainScope; |
| 30 | m_pRetMarshal = NULL; |
| 31 | m_pRetValue = NULL; |
| 32 | m_szExportAlias = NULL; |
| 33 | m_dwExportOrdinal = 0xFFFFFFFF; |
| 34 | m_ulLines[0]=m_ulLines[1]=0; |
| 35 | m_ulColumns[0]=m_ulColumns[0]=0; |
| 36 | m_pbsBody = NULL; |
| 37 | m_fNewBody = TRUE; |
| 38 | m_fNew = TRUE; |
| 39 | |
| 40 | // move the PInvoke descriptor (if any) from Assembler |
| 41 | // (Assembler gets the descriptor BEFORE it calls new Method) |
| 42 | m_pPInvoke = pAssembler->m_pPInvoke; |
| 43 | pAssembler->m_pPInvoke = NULL; |
| 44 | |
| 45 | _ASSERTE(pszName); |
| 46 | if (!pszName) return; |
| 47 | |
| 48 | m_szName = pszName; |
| 49 | m_dwName = (DWORD)strlen(pszName); |
| 50 | |
| 51 | m_ExceptionList = new COR_ILMETHOD_SECT_EH_CLAUSE_FAT[MAX_EXCEPTIONS]; |
| 52 | m_EndfilterOffsetList = new DWORD[MAX_EXCEPTIONS]; |
| 53 | if((m_ExceptionList==NULL)||(m_EndfilterOffsetList==NULL)) |
| 54 | { |
| 55 | fprintf(stderr,"\nOutOfMemory!\n" ); |
| 56 | return; |
| 57 | } |
| 58 | m_dwMaxNumExceptions = MAX_EXCEPTIONS; |
| 59 | m_dwMaxNumEndfilters = MAX_EXCEPTIONS; |
| 60 | |
| 61 | m_Attr = Attr; |
| 62 | if((!strcmp(pszName,COR_CCTOR_METHOD_NAME))||(!strcmp(pszName,COR_CTOR_METHOD_NAME))) |
| 63 | m_Attr |= mdSpecialName; |
| 64 | m_fEntryPoint = FALSE; |
| 65 | m_fGlobalMethod = FALSE; |
| 66 | |
| 67 | if(pbsSig) |
| 68 | { |
| 69 | m_dwMethodCSig = pbsSig->length(); |
| 70 | m_pMethodSig = (COR_SIGNATURE*)(pbsSig->ptr()); |
| 71 | m_pbsMethodSig = pbsSig; |
| 72 | } |
| 73 | |
| 74 | m_firstArgName = pAssembler->getArgNameList(); |
| 75 | if(pClass == NULL) pClass = pAssembler->m_pModuleClass; // fake "class" <Module> |
| 76 | pClass->m_MethodList.PUSH(this); |
| 77 | pClass->m_fNewMembers = TRUE; |
| 78 | |
| 79 | |
| 80 | m_pPermissions = NULL; |
| 81 | m_pPermissionSets = NULL; |
| 82 | |
| 83 | m_TyPars = NULL; |
| 84 | m_NumTyPars = 0; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | // lexical scope handling |
| 89 | void Method::OpenScope() |
| 90 | { |
| 91 | Scope* psc = new Scope; |
| 92 | if(psc) |
| 93 | { |
| 94 | psc->dwStart = m_pAssembler->m_CurPC; |
| 95 | psc->pSuperScope = m_pCurrScope; |
| 96 | m_pCurrScope->SubScope.PUSH(psc); |
| 97 | m_pCurrScope = psc; |
| 98 | } |
| 99 | } |
| 100 | void Method::CloseScope() |
| 101 | { |
| 102 | VarDescr* pVD; |
| 103 | ARG_NAME_LIST* pAN; |
| 104 | for(pAN=m_pCurrScope->pLocals; pAN; pAN = pAN->pNext) |
| 105 | { |
| 106 | if((pVD = m_Locals.PEEK(pAN->dwAttr))) pVD->bInScope = FALSE; |
| 107 | } |
| 108 | m_pCurrScope->dwEnd = m_pAssembler->m_CurPC; |
| 109 | m_pCurrScope = m_pCurrScope->pSuperScope; |
| 110 | } |
| 111 | |
| 112 | Label *Method::FindLabel(LPCUTF8 pszName) |
| 113 | { |
| 114 | Label lSearch(pszName,0), *pL; |
| 115 | lSearch.m_szName = pszName; |
| 116 | //pL = m_lstLabel.FIND(&lSearch); |
| 117 | pL = m_pAssembler->m_lstLabel.FIND(&lSearch); |
| 118 | lSearch.m_szName = NULL; |
| 119 | return pL; |
| 120 | //return m_lstLabel.FIND(pszName); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | Label *Method::FindLabel(DWORD PC) |
| 125 | { |
| 126 | Label *pSearch; |
| 127 | |
| 128 | //for (int i = 0; (pSearch = m_lstLabel.PEEK(i)); i++) |
| 129 | for (int i = 0; (pSearch = m_pAssembler->m_lstLabel.PEEK(i)); i++) |
| 130 | { |
| 131 | if (pSearch->m_PC == PC) |
| 132 | return pSearch; |
| 133 | } |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | |